@genesislcap/mock-server 15.7.3-alpha-94532c4.0 → 15.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +246 -40
- package/dist/db/criteria.d.ts +5 -0
- package/dist/db/criteria.js +327 -0
- package/dist/db/metadata.d.ts +3 -0
- package/dist/db/metadata.js +35 -0
- package/dist/db/store.d.ts +28 -0
- package/dist/db/store.js +254 -0
- package/dist/handlers/auth.d.ts +10 -0
- package/dist/handlers/auth.js +121 -0
- package/dist/handlers/commitEvent.d.ts +8 -0
- package/dist/handlers/commitEvent.js +40 -0
- package/dist/handlers/dataLogon.d.ts +5 -0
- package/dist/handlers/dataLogon.js +56 -0
- package/dist/handlers/heartbeat.d.ts +3 -0
- package/dist/handlers/heartbeat.js +16 -0
- package/dist/handlers/http.d.ts +4 -0
- package/dist/handlers/http.js +179 -0
- package/dist/handlers/jsonSchema.d.ts +5 -0
- package/dist/handlers/jsonSchema.js +94 -0
- package/dist/handlers/meta.d.ts +4 -0
- package/dist/handlers/meta.js +42 -0
- package/dist/handlers/requestReply.d.ts +4 -0
- package/dist/handlers/requestReply.js +68 -0
- package/dist/handlers/resourceAuth.d.ts +5 -0
- package/dist/handlers/resourceAuth.js +27 -0
- package/dist/handlers/resources.d.ts +3 -0
- package/dist/handlers/resources.js +34 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/launch.d.ts +20 -0
- package/dist/launch.js +164 -0
- package/dist/protocol/connection.d.ts +25 -0
- package/dist/protocol/connection.js +36 -0
- package/dist/protocol/errors.d.ts +17 -0
- package/dist/protocol/errors.js +16 -0
- package/dist/protocol/messageTypes.d.ts +28 -0
- package/dist/protocol/messageTypes.js +42 -0
- package/dist/protocol/router.d.ts +6 -0
- package/dist/protocol/router.js +101 -0
- package/dist/protocol/rowUpdate.d.ts +4 -0
- package/dist/protocol/rowUpdate.js +17 -0
- package/dist/server.d.ts +23 -0
- package/dist/server.js +328 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.js +4 -0
- package/package.json +16 -7
- package/src/db/criteria.ts +75 -21
- package/src/db/store.ts +126 -29
- package/src/handlers/auth.ts +32 -6
- package/src/handlers/commitEvent.ts +4 -1
- package/src/handlers/dataLogon.ts +23 -7
- package/src/handlers/heartbeat.ts +16 -3
- package/src/handlers/http.ts +43 -2
- package/src/handlers/jsonSchema.ts +21 -5
- package/src/handlers/meta.ts +11 -3
- package/src/handlers/requestReply.ts +22 -8
- package/src/handlers/resourceAuth.ts +37 -0
- package/src/handlers/resources.ts +8 -1
- package/src/index.ts +9 -0
- package/src/launch.ts +220 -0
- package/src/protocol/connection.ts +4 -1
- package/src/protocol/router.ts +2 -2
- package/src/protocol/rowUpdate.ts +5 -4
- package/src/server.ts +150 -22
- package/src/types.ts +124 -3
- package/license.txt +0 -46
- package/test/criteria.test.ts +0 -174
- package/test/jsonSchema.test.ts +0 -38
- package/test/metadata.test.ts +0 -49
- package/test/server.test.ts +0 -308
- package/test/store.test.ts +0 -181
- package/tsconfig.json +0 -18
package/test/server.test.ts
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { afterEach, describe, it } from 'node:test';
|
|
3
|
-
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
-
import { WebSocket } from 'ws';
|
|
5
|
-
import { createMockServer, type MockServer } from '../src/server.ts';
|
|
6
|
-
import type { MockServerConfig, Row } from '../src/types.ts';
|
|
7
|
-
|
|
8
|
-
const WAIT_TIMEOUT_MS = 2000;
|
|
9
|
-
|
|
10
|
-
function baseConfig(): MockServerConfig {
|
|
11
|
-
return {
|
|
12
|
-
tables: {
|
|
13
|
-
TRADE: {
|
|
14
|
-
pkField: 'TRADE_ID',
|
|
15
|
-
sequencePrefix: 'TR',
|
|
16
|
-
rows: [
|
|
17
|
-
{ TRADE_ID: 'TR001', TRADE_STATUS: 'CONFIRMED', QUANTITY: 100, PRICE: 105.5 },
|
|
18
|
-
{ TRADE_ID: 'TR002', TRADE_STATUS: 'PENDING', QUANTITY: 200, PRICE: 98.75 },
|
|
19
|
-
],
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
queries: {
|
|
23
|
-
ALL_TRADES: { source: 'TRADE' },
|
|
24
|
-
},
|
|
25
|
-
requestReplies: {
|
|
26
|
-
TRADE: { source: 'TRADE' },
|
|
27
|
-
},
|
|
28
|
-
eventHandlers: {
|
|
29
|
-
EVENT_TRADE_DELETE: (details, { store, broadcastTableChange }) => {
|
|
30
|
-
store.deleteRow('TRADE', details.TRADE_ID);
|
|
31
|
-
broadcastTableChange('TRADE', 'DELETE', details.TRADE_ID);
|
|
32
|
-
return {};
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
auth: {
|
|
36
|
-
users: [{ username: 'admin', password: 'admin' }],
|
|
37
|
-
httpAutoLogin: 'admin',
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// A minimal promise-based WS test client: send() fires a message, next()
|
|
43
|
-
// awaits the next received message matching a predicate.
|
|
44
|
-
class TestClient {
|
|
45
|
-
private ws: WebSocket;
|
|
46
|
-
private queue: Row[] = [];
|
|
47
|
-
private waiters: Array<{
|
|
48
|
-
predicate: (message: Row) => boolean;
|
|
49
|
-
resolve: (message: Row) => void;
|
|
50
|
-
}> = [];
|
|
51
|
-
private refCounter = 0;
|
|
52
|
-
|
|
53
|
-
private constructor(ws: WebSocket) {
|
|
54
|
-
this.ws = ws;
|
|
55
|
-
ws.on('message', (data) => {
|
|
56
|
-
const message = JSON.parse(data.toString());
|
|
57
|
-
const index = this.waiters.findIndex((w) => w.predicate(message));
|
|
58
|
-
if (index >= 0) {
|
|
59
|
-
const [waiter] = this.waiters.splice(index, 1);
|
|
60
|
-
waiter.resolve(message);
|
|
61
|
-
} else {
|
|
62
|
-
this.queue.push(message);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
static async connect(port: number): Promise<TestClient> {
|
|
68
|
-
const ws = new WebSocket(`ws://127.0.0.1:${port}`);
|
|
69
|
-
await new Promise<void>((resolve, reject) => {
|
|
70
|
-
ws.once('open', resolve);
|
|
71
|
-
ws.once('error', reject);
|
|
72
|
-
});
|
|
73
|
-
return new TestClient(ws);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
nextRef(): string {
|
|
77
|
-
this.refCounter += 1;
|
|
78
|
-
return `ref-${this.refCounter}`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
send(message: Row): void {
|
|
82
|
-
this.ws.send(JSON.stringify(message));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
next(predicate: (message: Row) => boolean = () => true): Promise<Row> {
|
|
86
|
-
const queued = this.queue.findIndex((message) => predicate(message));
|
|
87
|
-
if (queued >= 0) return Promise.resolve(this.queue.splice(queued, 1)[0]);
|
|
88
|
-
return new Promise((resolve, reject) => {
|
|
89
|
-
const timer = setTimeout(
|
|
90
|
-
() => reject(new Error('Timed out waiting for a matching message')),
|
|
91
|
-
WAIT_TIMEOUT_MS,
|
|
92
|
-
);
|
|
93
|
-
this.waiters.push({
|
|
94
|
-
predicate,
|
|
95
|
-
resolve: (message) => {
|
|
96
|
-
clearTimeout(timer);
|
|
97
|
-
resolve(message);
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async request(message: Row): Promise<Row> {
|
|
104
|
-
const sourceRef = this.nextRef();
|
|
105
|
-
this.send({ ...message, SOURCE_REF: sourceRef });
|
|
106
|
-
return this.next((reply) => reply.SOURCE_REF === sourceRef);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async login(): Promise<Row> {
|
|
110
|
-
return this.request({
|
|
111
|
-
MESSAGE_TYPE: 'EVENT_LOGIN_AUTH',
|
|
112
|
-
DETAILS: { USER_NAME: 'admin', PASSWORD: 'admin' },
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
close(): void {
|
|
117
|
-
this.ws.terminate();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// The http server binds asynchronously; with an external server ws does not
|
|
122
|
-
// re-emit 'listening', so poll address() until the port is known.
|
|
123
|
-
async function waitForPort(wss: ReturnType<MockServer['start']>, attempt = 0): Promise<number> {
|
|
124
|
-
const address = wss.address() as { port: number } | null;
|
|
125
|
-
if (address?.port) return address.port;
|
|
126
|
-
if (attempt >= 200) throw new Error('Server did not start listening');
|
|
127
|
-
await delay(5);
|
|
128
|
-
return waitForPort(wss, attempt + 1);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async function startServer(
|
|
132
|
-
config: MockServerConfig,
|
|
133
|
-
): Promise<{ server: MockServer; port: number }> {
|
|
134
|
-
const server = createMockServer(config);
|
|
135
|
-
const wss = server.start(0);
|
|
136
|
-
const port = await waitForPort(wss);
|
|
137
|
-
return { server, port };
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
describe('mock server end to end', () => {
|
|
141
|
-
const cleanups: Array<() => Promise<void> | void> = [];
|
|
142
|
-
afterEach(async () => {
|
|
143
|
-
await Promise.all(cleanups.splice(0).map((cleanup) => cleanup()));
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
async function setup(mutate: (config: MockServerConfig) => void = () => {}) {
|
|
147
|
-
const config = baseConfig();
|
|
148
|
-
mutate(config);
|
|
149
|
-
const { server, port } = await startServer(config);
|
|
150
|
-
cleanups.push(() => server.stop());
|
|
151
|
-
const client = await TestClient.connect(port);
|
|
152
|
-
cleanups.push(() => client.close());
|
|
153
|
-
return { server, port, client, config };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
it('answers DATA_LOGON with a stamped snapshot after login', async () => {
|
|
157
|
-
const { client } = await setup();
|
|
158
|
-
await client.login();
|
|
159
|
-
const snapshot = await client.request({
|
|
160
|
-
MESSAGE_TYPE: 'DATA_LOGON',
|
|
161
|
-
DETAILS: { DATASOURCE_NAME: 'ALL_TRADES' },
|
|
162
|
-
});
|
|
163
|
-
assert.equal(snapshot.MESSAGE_TYPE, 'QUERY_UPDATE');
|
|
164
|
-
assert.equal(snapshot.ROWS_COUNT, 2);
|
|
165
|
-
assert.equal(snapshot.ROW[0].DETAILS.OPERATION, 'INSERT');
|
|
166
|
-
assert.equal(snapshot.ROW[0].DETAILS.ROW_REF, 'TR001');
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('delivers DELETEs to criteria-filtered subscriptions (regression)', async () => {
|
|
170
|
-
const { client } = await setup();
|
|
171
|
-
await client.login();
|
|
172
|
-
const sourceRef = client.nextRef();
|
|
173
|
-
client.send({
|
|
174
|
-
MESSAGE_TYPE: 'DATA_LOGON',
|
|
175
|
-
SOURCE_REF: sourceRef,
|
|
176
|
-
DETAILS: { DATASOURCE_NAME: 'ALL_TRADES', CRITERIA_MATCH: "TRADE_STATUS == 'CONFIRMED'" },
|
|
177
|
-
});
|
|
178
|
-
const snapshot = await client.next((m) => m.SOURCE_REF === sourceRef);
|
|
179
|
-
assert.equal(snapshot.ROWS_COUNT, 1); // criteria filters the snapshot
|
|
180
|
-
|
|
181
|
-
await client.request({ MESSAGE_TYPE: 'EVENT_TRADE_DELETE', DETAILS: { TRADE_ID: 'TR001' } });
|
|
182
|
-
// Before the fix, the PK-only DELETE stub failed the criteria predicate
|
|
183
|
-
// and this push never arrived.
|
|
184
|
-
const push = await client.next(
|
|
185
|
-
(m) => m.SOURCE_REF === sourceRef && m.ROW?.[0]?.DETAILS?.OPERATION === 'DELETE',
|
|
186
|
-
);
|
|
187
|
-
assert.equal(push.ROW[0].DETAILS.ROW_REF, 'TR001');
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it('acks commit events with the generic EVENT_ACK/EVENT_NACK pair', async () => {
|
|
191
|
-
const { client } = await setup();
|
|
192
|
-
await client.login();
|
|
193
|
-
|
|
194
|
-
// Real consumers (foundation-comms' kv.service, entity-management) match
|
|
195
|
-
// MESSAGE_TYPE 'EVENT_ACK'/'EVENT_NACK' exactly — never <EVENT>_ACK.
|
|
196
|
-
const ack = await client.request({
|
|
197
|
-
MESSAGE_TYPE: 'EVENT_TRADE_DELETE',
|
|
198
|
-
DETAILS: { TRADE_ID: 'TR001' },
|
|
199
|
-
});
|
|
200
|
-
assert.equal(ack.MESSAGE_TYPE, 'EVENT_ACK');
|
|
201
|
-
|
|
202
|
-
const unknown = await client.request({ MESSAGE_TYPE: 'EVENT_NO_SUCH_THING', DETAILS: {} });
|
|
203
|
-
assert.equal(unknown.MESSAGE_TYPE, 'EVENT_NACK');
|
|
204
|
-
assert.equal(unknown.ERROR?.[0]?.CODE, 'UNKNOWN_RESOURCE');
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('rejects data traffic before authentication when requireAuth is opted in', async () => {
|
|
208
|
-
const { client } = await setup((config) => {
|
|
209
|
-
config.auth!.requireAuth = true;
|
|
210
|
-
});
|
|
211
|
-
const logon = await client.request({
|
|
212
|
-
MESSAGE_TYPE: 'DATA_LOGON',
|
|
213
|
-
DETAILS: { DATASOURCE_NAME: 'ALL_TRADES' },
|
|
214
|
-
});
|
|
215
|
-
assert.equal(logon.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
216
|
-
|
|
217
|
-
const rep = await client.request({ MESSAGE_TYPE: 'REQ_TRADE' });
|
|
218
|
-
assert.equal(rep.MESSAGE_TYPE, 'REP_TRADE');
|
|
219
|
-
assert.equal(rep.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
220
|
-
|
|
221
|
-
const nack = await client.request({ MESSAGE_TYPE: 'EVENT_TRADE_DELETE', DETAILS: {} });
|
|
222
|
-
assert.equal(nack.MESSAGE_TYPE, 'EVENT_NACK');
|
|
223
|
-
assert.equal(nack.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
it('accepts unauthenticated traffic by default (HTTP-login clients send no WS token)', async () => {
|
|
227
|
-
const { client } = await setup();
|
|
228
|
-
const reply = await client.request({ MESSAGE_TYPE: 'REQ_TRADE' });
|
|
229
|
-
assert.equal(reply.REPLY.length, 2);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it('authenticates WS traffic with a token issued by the HTTP login transport', async () => {
|
|
233
|
-
const { client, port } = await setup((config) => {
|
|
234
|
-
config.auth!.requireAuth = true;
|
|
235
|
-
});
|
|
236
|
-
const response = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`);
|
|
237
|
-
assert.equal(response.status, 200);
|
|
238
|
-
const ack = (await response.json()) as Row;
|
|
239
|
-
assert.equal(ack.MESSAGE_TYPE, 'EVENT_LOGIN_DETAILS_ACK');
|
|
240
|
-
|
|
241
|
-
const reply = await client.request({
|
|
242
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
243
|
-
SESSION_AUTH_TOKEN: ack.SESSION_AUTH_TOKEN,
|
|
244
|
-
});
|
|
245
|
-
assert.equal(reply.REPLY.length, 2);
|
|
246
|
-
|
|
247
|
-
// The WS token-refresh login (no credentials) also succeeds with it.
|
|
248
|
-
const refreshed = await client.request({
|
|
249
|
-
MESSAGE_TYPE: 'EVENT_LOGIN_AUTH',
|
|
250
|
-
SESSION_AUTH_TOKEN: ack.SESSION_AUTH_TOKEN,
|
|
251
|
-
DETAILS: { REFRESH_AUTH_TOKEN: ack.REFRESH_AUTH_TOKEN },
|
|
252
|
-
});
|
|
253
|
-
assert.equal(refreshed.MESSAGE_TYPE, 'EVENT_LOGIN_AUTH_ACK');
|
|
254
|
-
assert.equal(refreshed.USER_NAME, 'admin');
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it('filters REQ_* rows by top-level REQUEST fields, honoring the * wildcard', async () => {
|
|
258
|
-
const { client } = await setup();
|
|
259
|
-
await client.login();
|
|
260
|
-
|
|
261
|
-
const keyed = await client.request({
|
|
262
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
263
|
-
REQUEST: { TRADE_ID: 'TR002' },
|
|
264
|
-
});
|
|
265
|
-
assert.equal(keyed.REPLY.length, 1);
|
|
266
|
-
assert.equal(keyed.REPLY[0].TRADE_ID, 'TR002');
|
|
267
|
-
|
|
268
|
-
const wildcard = await client.request({
|
|
269
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
270
|
-
REQUEST: { TRADE_ID: '*' },
|
|
271
|
-
});
|
|
272
|
-
assert.equal(wildcard.REPLY.length, 2);
|
|
273
|
-
|
|
274
|
-
const none = await client.request({
|
|
275
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
276
|
-
REQUEST: { TRADE_ID: 'TR999' },
|
|
277
|
-
});
|
|
278
|
-
assert.deepEqual(none.REPLY, []);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('reuses the HTTP session for a request that already carries its cookie', async () => {
|
|
282
|
-
const { port } = await setup();
|
|
283
|
-
const first = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`);
|
|
284
|
-
const cookie = first.headers.get('set-cookie')!;
|
|
285
|
-
assert.ok(cookie.includes('MOCK_SESSION_ID='));
|
|
286
|
-
|
|
287
|
-
const second = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`, {
|
|
288
|
-
headers: { cookie: cookie.split(';')[0] },
|
|
289
|
-
});
|
|
290
|
-
assert.equal(second.headers.get('set-cookie'), cookie);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it('stop() resolves promptly even with a live WS client attached (regression)', async () => {
|
|
294
|
-
const config = baseConfig();
|
|
295
|
-
const { server, port } = await startServer(config);
|
|
296
|
-
const client = await TestClient.connect(port);
|
|
297
|
-
cleanups.push(() => client.close());
|
|
298
|
-
|
|
299
|
-
// Before the fix, wss.close() left the client socket open and
|
|
300
|
-
// httpServer.close() waited on it forever.
|
|
301
|
-
await Promise.race([
|
|
302
|
-
server.stop(),
|
|
303
|
-
new Promise((_, reject) => {
|
|
304
|
-
setTimeout(() => reject(new Error('stop() did not resolve with a client attached')), 3000);
|
|
305
|
-
}),
|
|
306
|
-
]);
|
|
307
|
-
});
|
|
308
|
-
});
|
package/test/store.test.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { beforeEach, describe, it } from 'node:test';
|
|
3
|
-
import { Store } from '../src/db/store.ts';
|
|
4
|
-
|
|
5
|
-
function makeStore(): Store {
|
|
6
|
-
const store = new Store();
|
|
7
|
-
store.registerTable('TRADE', {
|
|
8
|
-
pkField: 'TRADE_ID',
|
|
9
|
-
sequencePrefix: 'TR',
|
|
10
|
-
rows: [
|
|
11
|
-
{ TRADE_ID: 'TR001', INSTRUMENT_ID: 'VOD', COUNTERPARTY_ID: 'CP001', PRICE: 105.5 },
|
|
12
|
-
{ TRADE_ID: 'TR007', INSTRUMENT_ID: 'MSFT', COUNTERPARTY_ID: 'CP002', PRICE: 98 },
|
|
13
|
-
],
|
|
14
|
-
});
|
|
15
|
-
store.registerTable('COUNTERPARTY', {
|
|
16
|
-
pkField: 'COUNTERPARTY_ID',
|
|
17
|
-
rows: [
|
|
18
|
-
{ COUNTERPARTY_ID: 'CP001', NAME: 'Acme' },
|
|
19
|
-
{ COUNTERPARTY_ID: 'CP002', NAME: 'Globex' },
|
|
20
|
-
],
|
|
21
|
-
});
|
|
22
|
-
store.registerView('TRADE_VIEW', {
|
|
23
|
-
base: 'TRADE',
|
|
24
|
-
pkField: 'TRADE_ID',
|
|
25
|
-
joins: [
|
|
26
|
-
{
|
|
27
|
-
table: 'COUNTERPARTY',
|
|
28
|
-
on: ['COUNTERPARTY_ID', 'COUNTERPARTY_ID'],
|
|
29
|
-
select: { COUNTERPARTY_NAME: 'NAME' },
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
derived: { LABEL: (row) => `${row.TRADE_ID}:${row.INSTRUMENT_ID}` },
|
|
33
|
-
});
|
|
34
|
-
return store;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
describe('Store sequences', () => {
|
|
38
|
-
let store: Store;
|
|
39
|
-
beforeEach(() => {
|
|
40
|
-
store = makeStore();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('seeds the counter past the highest existing pk for the prefix', () => {
|
|
44
|
-
const row = store.insertRow('TRADE', { INSTRUMENT_ID: 'AMZ' });
|
|
45
|
-
assert.equal(row.TRADE_ID, 'TR008');
|
|
46
|
-
assert.equal(store.insertRow('TRADE', {}).TRADE_ID, 'TR009');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('pads generated pks to the configured width', () => {
|
|
50
|
-
const store2 = new Store();
|
|
51
|
-
store2.registerTable('FILL', { pkField: 'FILL_ID', sequencePrefix: 'FL', sequenceWidth: 5 });
|
|
52
|
-
assert.equal(store2.insertRow('FILL', {}).FILL_ID, 'FL00001');
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('throws when inserting without a pk into a table with no sequence', () => {
|
|
56
|
-
assert.throws(() => store.insertRow('COUNTERPARTY', { NAME: 'No PK' }), /Primary key/);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('throws for an unknown table', () => {
|
|
60
|
-
assert.throws(() => store.insertRow('NOPE', {}), /Unknown table/);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe('Store.findRow fallback matching', () => {
|
|
65
|
-
let store: Store;
|
|
66
|
-
beforeEach(() => {
|
|
67
|
-
store = makeStore();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('matches by the pk field first', () => {
|
|
71
|
-
assert.equal(store.findRow('TRADE', { TRADE_ID: 'TR007' })?.INSTRUMENT_ID, 'MSFT');
|
|
72
|
-
assert.equal(store.findRow('TRADE', { TRADE_ID: 'missing' }), undefined);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('matches by any single field that uniquely identifies one row', () => {
|
|
76
|
-
assert.equal(store.findRow('TRADE', { INSTRUMENT_ID: 'VOD' })?.TRADE_ID, 'TR001');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('matches by the full combination when no single field is unique', () => {
|
|
80
|
-
store.insertRow('TRADE', { INSTRUMENT_ID: 'VOD', COUNTERPARTY_ID: 'CP002' });
|
|
81
|
-
const row = store.findRow('TRADE', { INSTRUMENT_ID: 'VOD', COUNTERPARTY_ID: 'CP002' });
|
|
82
|
-
assert.equal(row?.TRADE_ID, 'TR008');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('returns undefined when the combination matches nothing', () => {
|
|
86
|
-
assert.equal(
|
|
87
|
-
store.findRow('TRADE', { INSTRUMENT_ID: 'VOD', COUNTERPARTY_ID: 'CP999' }),
|
|
88
|
-
undefined,
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe('Store row mutation', () => {
|
|
94
|
-
let store: Store;
|
|
95
|
-
beforeEach(() => {
|
|
96
|
-
store = makeStore();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('updateRow merges the patch and returns the updated row', () => {
|
|
100
|
-
const updated = store.updateRow('TRADE', 'TR001', { PRICE: 999 });
|
|
101
|
-
assert.equal(updated?.PRICE, 999);
|
|
102
|
-
assert.equal(updated?.INSTRUMENT_ID, 'VOD');
|
|
103
|
-
assert.equal(store.getRow('TRADE', 'TR001')?.PRICE, 999);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('updateRow returns undefined for a missing pk', () => {
|
|
107
|
-
assert.equal(store.updateRow('TRADE', 'TR999', { PRICE: 1 }), undefined);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('deleteRow removes the row and reports whether it existed', () => {
|
|
111
|
-
assert.equal(store.deleteRow('TRADE', 'TR001'), true);
|
|
112
|
-
assert.equal(store.deleteRow('TRADE', 'TR001'), false);
|
|
113
|
-
assert.equal(store.getRow('TRADE', 'TR001'), undefined);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe('Store views', () => {
|
|
118
|
-
let store: Store;
|
|
119
|
-
beforeEach(() => {
|
|
120
|
-
store = makeStore();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('joins aliased fields and computes derived fields', () => {
|
|
124
|
-
const rows = store.getViewRows('TRADE_VIEW');
|
|
125
|
-
assert.equal(rows.length, 2);
|
|
126
|
-
assert.equal(rows[0].COUNTERPARTY_NAME, 'Acme');
|
|
127
|
-
assert.equal(rows[0].LABEL, 'TR001:VOD');
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('leaves joined fields undefined when the join has no match', () => {
|
|
131
|
-
store.insertRow('TRADE', { INSTRUMENT_ID: 'AMZ', COUNTERPARTY_ID: 'CP999' });
|
|
132
|
-
const row = store.getViewRows('TRADE_VIEW').find((r) => r.TRADE_ID === 'TR008');
|
|
133
|
-
assert.equal(row?.COUNTERPARTY_NAME, undefined);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('resolves pk fields for views and tables', () => {
|
|
137
|
-
assert.equal(store.getPkField('TRADE_VIEW'), 'TRADE_ID');
|
|
138
|
-
assert.equal(store.getPkField('COUNTERPARTY'), 'COUNTERPARTY_ID');
|
|
139
|
-
assert.equal(store.getPkField('NOPE'), undefined);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('throws for an unknown view', () => {
|
|
143
|
-
assert.throws(() => store.getViewRows('NOPE'), /Unknown view/);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
describe('Store.getMetadataRows seed fallback', () => {
|
|
148
|
-
let store: Store;
|
|
149
|
-
beforeEach(() => {
|
|
150
|
-
store = makeStore();
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it('returns live rows while any exist', () => {
|
|
154
|
-
store.updateRow('TRADE', 'TR001', { PRICE: 1 });
|
|
155
|
-
const rows = store.getMetadataRows('TRADE');
|
|
156
|
-
assert.equal(rows.find((r) => r.TRADE_ID === 'TR001')?.PRICE, 1);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('falls back to seed rows after every live row is deleted', () => {
|
|
160
|
-
store.deleteRow('TRADE', 'TR001');
|
|
161
|
-
store.deleteRow('TRADE', 'TR007');
|
|
162
|
-
assert.equal(store.getResourceRows('TRADE').length, 0);
|
|
163
|
-
const rows = store.getMetadataRows('TRADE');
|
|
164
|
-
assert.equal(rows.length, 2);
|
|
165
|
-
assert.deepEqual(Object.keys(rows[0]), [
|
|
166
|
-
'TRADE_ID',
|
|
167
|
-
'INSTRUMENT_ID',
|
|
168
|
-
'COUNTERPARTY_ID',
|
|
169
|
-
'PRICE',
|
|
170
|
-
]);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('re-joins views through seed data when the base table is emptied', () => {
|
|
174
|
-
store.deleteRow('TRADE', 'TR001');
|
|
175
|
-
store.deleteRow('TRADE', 'TR007');
|
|
176
|
-
const rows = store.getMetadataRows('TRADE_VIEW');
|
|
177
|
-
assert.equal(rows.length, 2);
|
|
178
|
-
assert.equal(rows[0].COUNTERPARTY_NAME, 'Acme');
|
|
179
|
-
assert.equal(rows[0].LABEL, 'TR001:VOD');
|
|
180
|
-
});
|
|
181
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"lib": ["ESNext"],
|
|
7
|
-
"strict": true,
|
|
8
|
-
"noUncheckedIndexedAccess": false,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"resolveJsonModule": true,
|
|
12
|
-
"noEmit": true,
|
|
13
|
-
"allowJs": false,
|
|
14
|
-
"allowImportingTsExtensions": true,
|
|
15
|
-
"verbatimModuleSyntax": false
|
|
16
|
-
},
|
|
17
|
-
"include": ["src/**/*.ts", "test/**/*.ts"]
|
|
18
|
-
}
|