@genesislcap/mock-server 15.7.3-alpha-277c463.0 → 15.7.3-alpha-e7d4aa5.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 +237 -52
- package/dist/db/criteria.d.ts +5 -0
- package/dist/db/criteria.js +304 -0
- package/dist/db/metadata.d.ts +3 -0
- package/dist/db/metadata.js +35 -0
- package/dist/db/store.d.ts +27 -0
- package/dist/db/store.js +226 -0
- package/dist/handlers/auth.d.ts +9 -0
- package/dist/handlers/auth.js +99 -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 +46 -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 +165 -0
- package/dist/handlers/jsonSchema.d.ts +5 -0
- package/dist/handlers/jsonSchema.js +77 -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 +19 -0
- package/dist/launch.js +59 -0
- package/dist/protocol/connection.d.ts +24 -0
- package/dist/protocol/connection.js +35 -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 +274 -0
- package/dist/types.d.ts +142 -0
- package/dist/types.js +4 -0
- package/package.json +16 -6
- package/src/db/criteria.ts +53 -21
- package/src/db/store.ts +100 -27
- package/src/handlers/commitEvent.ts +13 -5
- package/src/handlers/dataLogon.ts +11 -4
- package/src/handlers/heartbeat.ts +16 -3
- package/src/handlers/http.ts +27 -0
- package/src/handlers/jsonSchema.ts +3 -3
- 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 +95 -0
- package/src/protocol/connection.ts +3 -1
- package/src/protocol/messageTypes.ts +9 -0
- package/src/protocol/router.ts +19 -12
- package/src/protocol/rowUpdate.ts +5 -4
- package/src/server.ts +73 -8
- package/src/types.ts +132 -8
- 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 -289
- package/test/store.test.ts +0 -181
- package/tsconfig.json +0 -18
package/test/criteria.test.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { describe, it } from 'node:test';
|
|
3
|
-
import { compileCriteria, filterByCriteria } from '../src/db/criteria.ts';
|
|
4
|
-
|
|
5
|
-
const TRADE = {
|
|
6
|
-
TRADE_ID: 'TR001',
|
|
7
|
-
TRADE_STATUS: 'CONFIRMED',
|
|
8
|
-
SIDE: 'BUY',
|
|
9
|
-
QUANTITY: 150,
|
|
10
|
-
PRICE: 105.5,
|
|
11
|
-
ENABLED: true,
|
|
12
|
-
NOTES: null,
|
|
13
|
-
COUNTERPARTY_NAME: "O'Brien Securities",
|
|
14
|
-
TRADE_DATE: Date.UTC(2024, 5, 15, 10, 30, 0), // 2024-06-15T10:30Z as epoch millis
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
describe('compileCriteria comparisons', () => {
|
|
18
|
-
it('matches string equality with == and =', () => {
|
|
19
|
-
assert.equal(compileCriteria("TRADE_STATUS == 'CONFIRMED'")(TRADE), true);
|
|
20
|
-
assert.equal(compileCriteria("TRADE_STATUS = 'CONFIRMED'")(TRADE), true);
|
|
21
|
-
assert.equal(compileCriteria("TRADE_STATUS == 'PENDING'")(TRADE), false);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('matches inequality with != and <>', () => {
|
|
25
|
-
assert.equal(compileCriteria("SIDE != 'SELL'")(TRADE), true);
|
|
26
|
-
assert.equal(compileCriteria("SIDE <> 'SELL'")(TRADE), true);
|
|
27
|
-
assert.equal(compileCriteria("SIDE != 'BUY'")(TRADE), false);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('compares numbers with > >= < <=', () => {
|
|
31
|
-
assert.equal(compileCriteria('QUANTITY > 100')(TRADE), true);
|
|
32
|
-
assert.equal(compileCriteria('QUANTITY >= 150')(TRADE), true);
|
|
33
|
-
assert.equal(compileCriteria('QUANTITY < 150')(TRADE), false);
|
|
34
|
-
assert.equal(compileCriteria('QUANTITY <= 150')(TRADE), true);
|
|
35
|
-
assert.equal(compileCriteria('PRICE > 105.4')(TRADE), true);
|
|
36
|
-
assert.equal(compileCriteria('PRICE > -1')(TRADE), true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('matches boolean and null literals', () => {
|
|
40
|
-
assert.equal(compileCriteria('ENABLED == true')(TRADE), true);
|
|
41
|
-
assert.equal(compileCriteria('ENABLED == false')(TRADE), false);
|
|
42
|
-
assert.equal(compileCriteria('NOTES == null')(TRADE), true);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('handles double-quoted strings and escaped quotes', () => {
|
|
46
|
-
assert.equal(compileCriteria('SIDE == "BUY"')(TRADE), true);
|
|
47
|
-
assert.equal(compileCriteria("COUNTERPARTY_NAME == 'O\\'Brien Securities'")(TRADE), true);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('equality is strict — numeric field never equals a string literal', () => {
|
|
51
|
-
assert.equal(compileCriteria("QUANTITY == '150'")(TRADE), false);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('compileCriteria boolean operators', () => {
|
|
56
|
-
it('supports AND / OR / NOT keywords case-insensitively and && / ||', () => {
|
|
57
|
-
assert.equal(compileCriteria("SIDE == 'BUY' AND QUANTITY > 100")(TRADE), true);
|
|
58
|
-
assert.equal(compileCriteria("SIDE == 'BUY' and QUANTITY > 999")(TRADE), false);
|
|
59
|
-
assert.equal(compileCriteria("SIDE == 'SELL' OR QUANTITY > 100")(TRADE), true);
|
|
60
|
-
assert.equal(compileCriteria("SIDE == 'SELL' || QUANTITY > 100")(TRADE), true);
|
|
61
|
-
assert.equal(compileCriteria("SIDE == 'BUY' && QUANTITY > 100")(TRADE), true);
|
|
62
|
-
assert.equal(compileCriteria("NOT SIDE == 'SELL'")(TRADE), true);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('binds AND tighter than OR', () => {
|
|
66
|
-
// Parses as A OR (B AND C), which is true; ((A OR B) AND C) would be false.
|
|
67
|
-
const predicate = compileCriteria(
|
|
68
|
-
"SIDE == 'SELL' OR TRADE_STATUS == 'CONFIRMED' AND QUANTITY > 100",
|
|
69
|
-
);
|
|
70
|
-
assert.equal(predicate(TRADE), true);
|
|
71
|
-
const flipped = compileCriteria(
|
|
72
|
-
"(SIDE == 'SELL' OR TRADE_STATUS == 'CONFIRMED') AND QUANTITY > 999",
|
|
73
|
-
);
|
|
74
|
-
assert.equal(flipped(TRADE), false);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('honors parentheses and NOT of a group', () => {
|
|
78
|
-
assert.equal(compileCriteria("NOT (SIDE == 'SELL') OR PRICE >= 999")(TRADE), true);
|
|
79
|
-
assert.equal(compileCriteria("NOT (SIDE == 'BUY' AND QUANTITY > 100)")(TRADE), false);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
describe('compileCriteria Expr.* functions', () => {
|
|
84
|
-
it('contains / containsIgnoreCase', () => {
|
|
85
|
-
assert.equal(compileCriteria("Expr.contains(COUNTERPARTY_NAME, 'Brien')")(TRADE), true);
|
|
86
|
-
assert.equal(compileCriteria("Expr.contains(COUNTERPARTY_NAME, 'brien')")(TRADE), false);
|
|
87
|
-
assert.equal(
|
|
88
|
-
compileCriteria("Expr.containsIgnoreCase(COUNTERPARTY_NAME, 'brien')")(TRADE),
|
|
89
|
-
true,
|
|
90
|
-
);
|
|
91
|
-
assert.equal(compileCriteria("Expr.contains(QUANTITY, '15')")(TRADE), false); // non-string field
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('dateIs* compares by calendar day against YYYYMMDD-HH:MM[:SS] literals', () => {
|
|
95
|
-
assert.equal(compileCriteria("Expr.dateIsEqual(TRADE_DATE, '20240615-00:00')")(TRADE), true);
|
|
96
|
-
assert.equal(compileCriteria("Expr.dateIsEqual(TRADE_DATE, '20240616-00:00')")(TRADE), false);
|
|
97
|
-
assert.equal(
|
|
98
|
-
compileCriteria("Expr.dateIsGreaterEqual(TRADE_DATE, '20240615-23:59')")(TRADE),
|
|
99
|
-
true, // same calendar day, time-of-day ignored
|
|
100
|
-
);
|
|
101
|
-
assert.equal(
|
|
102
|
-
compileCriteria("Expr.dateIsLessEqual(TRADE_DATE, '20240614-00:00')")(TRADE),
|
|
103
|
-
false,
|
|
104
|
-
);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('dateTimeIs* compares full timestamps (After/Before alias >= / <=)', () => {
|
|
108
|
-
assert.equal(
|
|
109
|
-
compileCriteria("Expr.dateTimeIsEqual(TRADE_DATE, '20240615-10:30:00')")(TRADE),
|
|
110
|
-
true,
|
|
111
|
-
);
|
|
112
|
-
assert.equal(
|
|
113
|
-
compileCriteria("Expr.dateTimeIsGreaterEqual(TRADE_DATE, '20240615-10:30:01')")(TRADE),
|
|
114
|
-
false,
|
|
115
|
-
);
|
|
116
|
-
assert.equal(
|
|
117
|
-
compileCriteria("Expr.dateTimeIsAfter(TRADE_DATE, '20240615-10:00')")(TRADE),
|
|
118
|
-
true,
|
|
119
|
-
);
|
|
120
|
-
assert.equal(
|
|
121
|
-
compileCriteria("Expr.dateTimeIsBefore(TRADE_DATE, '20240615-11:00')")(TRADE),
|
|
122
|
-
true,
|
|
123
|
-
);
|
|
124
|
-
assert.equal(compileCriteria('Expr.dateIsToday(TRADE_DATE)')(TRADE), false);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it('date functions return false (not throw) for unparseable values', () => {
|
|
128
|
-
assert.equal(compileCriteria("Expr.dateIsEqual(SIDE, '20240615-00:00')")(TRADE), false);
|
|
129
|
-
assert.equal(compileCriteria("Expr.dateTimeIsEqual(NOTES, '20240615-00:00')")(TRADE), false);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
describe('compileCriteria fail-open behavior', () => {
|
|
134
|
-
it('matches everything for empty or missing criteria', () => {
|
|
135
|
-
assert.equal(compileCriteria(undefined)(TRADE), true);
|
|
136
|
-
assert.equal(compileCriteria(null)(TRADE), true);
|
|
137
|
-
assert.equal(compileCriteria(' ')(TRADE), true);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('fails open on untokenizable input', () => {
|
|
141
|
-
assert.equal(compileCriteria('£$%^ not a criteria')(TRADE), true);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('fails open on trailing tokens', () => {
|
|
145
|
-
assert.equal(compileCriteria("SIDE == 'SELL' QUANTITY")(TRADE), true);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('fails open on an unknown Expr function', () => {
|
|
149
|
-
assert.equal(compileCriteria("Expr.noSuchFn(SIDE, 'BUY')")(TRADE), true);
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('fails open on unbalanced parentheses', () => {
|
|
153
|
-
assert.equal(compileCriteria("(SIDE == 'SELL'")(TRADE), true);
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe('filterByCriteria', () => {
|
|
158
|
-
const rows = [
|
|
159
|
-
{ ID: 'A', STATUS: 'OPEN', QTY: 10 },
|
|
160
|
-
{ ID: 'B', STATUS: 'CLOSED', QTY: 20 },
|
|
161
|
-
{ ID: 'C', STATUS: 'OPEN', QTY: 30 },
|
|
162
|
-
];
|
|
163
|
-
|
|
164
|
-
it('filters rows by the compiled predicate', () => {
|
|
165
|
-
assert.deepEqual(
|
|
166
|
-
filterByCriteria(rows, "STATUS == 'OPEN' AND QTY > 15").map((r) => r.ID),
|
|
167
|
-
['C'],
|
|
168
|
-
);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('returns all rows when criteria is absent', () => {
|
|
172
|
-
assert.equal(filterByCriteria(rows, undefined).length, 3);
|
|
173
|
-
});
|
|
174
|
-
});
|
package/test/jsonSchema.test.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { describe, it } from 'node:test';
|
|
3
|
-
import { inferEventTable } from '../src/handlers/jsonSchema.ts';
|
|
4
|
-
|
|
5
|
-
const TABLES = ['TRADE', 'COUNTERPARTY', 'COUNTERPARTY_AUTH', 'POSITION'];
|
|
6
|
-
|
|
7
|
-
describe('inferEventTable', () => {
|
|
8
|
-
it('maps EVENT_<TABLE>_<VERB> to the table', () => {
|
|
9
|
-
assert.equal(inferEventTable('EVENT_TRADE_INSERT', TABLES), 'TRADE');
|
|
10
|
-
assert.equal(inferEventTable('EVENT_POSITION_DELETE', TABLES), 'POSITION');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('handles suffixed verb variants', () => {
|
|
14
|
-
assert.equal(
|
|
15
|
-
inferEventTable('EVENT_COUNTERPARTY_INSERT_WITH_APPROVAL', TABLES),
|
|
16
|
-
'COUNTERPARTY',
|
|
17
|
-
);
|
|
18
|
-
assert.equal(inferEventTable('EVENT_TRADE_MODIFY_PRICE', TABLES), 'TRADE');
|
|
19
|
-
assert.equal(inferEventTable('EVENT_TRADE_INSERT_FAIL', TABLES), 'TRADE');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('prefers the longest matching table name', () => {
|
|
23
|
-
assert.equal(inferEventTable('EVENT_COUNTERPARTY_AUTH_MODIFY', TABLES), 'COUNTERPARTY_AUTH');
|
|
24
|
-
assert.equal(inferEventTable('EVENT_COUNTERPARTY_MODIFY', TABLES), 'COUNTERPARTY');
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('matches an exact EVENT_<TABLE> with no verb', () => {
|
|
28
|
-
assert.equal(inferEventTable('EVENT_TRADE', TABLES), 'TRADE');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('returns undefined for non-EVENT names, unknown tables, and missing input', () => {
|
|
32
|
-
assert.equal(inferEventTable('REQ_TRADE', TABLES), undefined);
|
|
33
|
-
assert.equal(inferEventTable('EVENT_ORDER_INSERT', TABLES), undefined);
|
|
34
|
-
assert.equal(inferEventTable(undefined, TABLES), undefined);
|
|
35
|
-
// Prefix of a table name is not a match — TRADES is not TRADE.
|
|
36
|
-
assert.equal(inferEventTable('EVENT_TRADES_INSERT', TABLES), undefined);
|
|
37
|
-
});
|
|
38
|
-
});
|
package/test/metadata.test.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { describe, it } from 'node:test';
|
|
3
|
-
import { deriveFields } from '../src/db/metadata.ts';
|
|
4
|
-
|
|
5
|
-
describe('deriveFields', () => {
|
|
6
|
-
it('covers every key seen across rows, in first-seen order', () => {
|
|
7
|
-
const fields = deriveFields([
|
|
8
|
-
{ A: 1, B: 'x' },
|
|
9
|
-
{ B: 'y', C: true },
|
|
10
|
-
]);
|
|
11
|
-
assert.deepEqual(
|
|
12
|
-
fields.map((f) => f.NAME),
|
|
13
|
-
['A', 'B', 'C'],
|
|
14
|
-
);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('infers LONG only when every numeric value is whole', () => {
|
|
18
|
-
const [qty] = deriveFields([{ QTY: 100 }, { QTY: 250 }]);
|
|
19
|
-
assert.deepEqual(qty, { NAME: 'QTY', TYPE: 'LONG' });
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('infers DOUBLE when any row holds a fractional value, even if the first is whole', () => {
|
|
23
|
-
const [price] = deriveFields([{ PRICE: 100 }, { PRICE: 100.5 }, { PRICE: 99 }]);
|
|
24
|
-
assert.deepEqual(price, { NAME: 'PRICE', TYPE: 'DOUBLE' });
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('infers BOOLEAN and STRING from the first non-null value', () => {
|
|
28
|
-
const fields = deriveFields([
|
|
29
|
-
{ ENABLED: null, NAME: null },
|
|
30
|
-
{ ENABLED: true, NAME: 'x' },
|
|
31
|
-
]);
|
|
32
|
-
assert.deepEqual(fields, [
|
|
33
|
-
{ NAME: 'ENABLED', TYPE: 'BOOLEAN' },
|
|
34
|
-
{ NAME: 'NAME', TYPE: 'STRING' },
|
|
35
|
-
]);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('defaults to STRING for all-null fields and applies overrides', () => {
|
|
39
|
-
const fields = deriveFields([{ EMPTY: null, NOTIONAL: 105000 }], { NOTIONAL: 'DOUBLE' });
|
|
40
|
-
assert.deepEqual(fields, [
|
|
41
|
-
{ NAME: 'EMPTY', TYPE: 'STRING' },
|
|
42
|
-
{ NAME: 'NOTIONAL', TYPE: 'DOUBLE' },
|
|
43
|
-
]);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('returns an empty list for no rows', () => {
|
|
47
|
-
assert.deepEqual(deriveFields([]), []);
|
|
48
|
-
});
|
|
49
|
-
});
|
package/test/server.test.ts
DELETED
|
@@ -1,289 +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('rejects data traffic before authentication with NOT_LOGGED_ON', async () => {
|
|
191
|
-
const { client } = await setup();
|
|
192
|
-
const logon = await client.request({
|
|
193
|
-
MESSAGE_TYPE: 'DATA_LOGON',
|
|
194
|
-
DETAILS: { DATASOURCE_NAME: 'ALL_TRADES' },
|
|
195
|
-
});
|
|
196
|
-
assert.equal(logon.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
197
|
-
|
|
198
|
-
const rep = await client.request({ MESSAGE_TYPE: 'REQ_TRADE' });
|
|
199
|
-
assert.equal(rep.MESSAGE_TYPE, 'REP_TRADE');
|
|
200
|
-
assert.equal(rep.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
201
|
-
|
|
202
|
-
const nack = await client.request({ MESSAGE_TYPE: 'EVENT_TRADE_DELETE', DETAILS: {} });
|
|
203
|
-
assert.equal(nack.MESSAGE_TYPE, 'EVENT_TRADE_DELETE_NACK');
|
|
204
|
-
assert.equal(nack.ERROR?.[0]?.CODE, 'NOT_LOGGED_ON');
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('accepts everything unauthenticated when auth.requireAuth is false', async () => {
|
|
208
|
-
const { client } = await setup((config) => {
|
|
209
|
-
config.auth!.requireAuth = false;
|
|
210
|
-
});
|
|
211
|
-
const reply = await client.request({ MESSAGE_TYPE: 'REQ_TRADE' });
|
|
212
|
-
assert.equal(reply.REPLY.length, 2);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it('authenticates WS traffic with a token issued by the HTTP login transport', async () => {
|
|
216
|
-
const { client, port } = await setup();
|
|
217
|
-
const response = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`);
|
|
218
|
-
assert.equal(response.status, 200);
|
|
219
|
-
const ack = (await response.json()) as Row;
|
|
220
|
-
assert.equal(ack.MESSAGE_TYPE, 'EVENT_LOGIN_DETAILS_ACK');
|
|
221
|
-
|
|
222
|
-
const reply = await client.request({
|
|
223
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
224
|
-
SESSION_AUTH_TOKEN: ack.SESSION_AUTH_TOKEN,
|
|
225
|
-
});
|
|
226
|
-
assert.equal(reply.REPLY.length, 2);
|
|
227
|
-
|
|
228
|
-
// The WS token-refresh login (no credentials) also succeeds with it.
|
|
229
|
-
const refreshed = await client.request({
|
|
230
|
-
MESSAGE_TYPE: 'EVENT_LOGIN_AUTH',
|
|
231
|
-
SESSION_AUTH_TOKEN: ack.SESSION_AUTH_TOKEN,
|
|
232
|
-
DETAILS: { REFRESH_AUTH_TOKEN: ack.REFRESH_AUTH_TOKEN },
|
|
233
|
-
});
|
|
234
|
-
assert.equal(refreshed.MESSAGE_TYPE, 'EVENT_LOGIN_AUTH_ACK');
|
|
235
|
-
assert.equal(refreshed.USER_NAME, 'admin');
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it('filters REQ_* rows by top-level REQUEST fields, honoring the * wildcard', async () => {
|
|
239
|
-
const { client } = await setup();
|
|
240
|
-
await client.login();
|
|
241
|
-
|
|
242
|
-
const keyed = await client.request({
|
|
243
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
244
|
-
REQUEST: { TRADE_ID: 'TR002' },
|
|
245
|
-
});
|
|
246
|
-
assert.equal(keyed.REPLY.length, 1);
|
|
247
|
-
assert.equal(keyed.REPLY[0].TRADE_ID, 'TR002');
|
|
248
|
-
|
|
249
|
-
const wildcard = await client.request({
|
|
250
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
251
|
-
REQUEST: { TRADE_ID: '*' },
|
|
252
|
-
});
|
|
253
|
-
assert.equal(wildcard.REPLY.length, 2);
|
|
254
|
-
|
|
255
|
-
const none = await client.request({
|
|
256
|
-
MESSAGE_TYPE: 'REQ_TRADE',
|
|
257
|
-
REQUEST: { TRADE_ID: 'TR999' },
|
|
258
|
-
});
|
|
259
|
-
assert.deepEqual(none.REPLY, []);
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
it('reuses the HTTP session for a request that already carries its cookie', async () => {
|
|
263
|
-
const { port } = await setup();
|
|
264
|
-
const first = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`);
|
|
265
|
-
const cookie = first.headers.get('set-cookie')!;
|
|
266
|
-
assert.ok(cookie.includes('MOCK_SESSION_ID='));
|
|
267
|
-
|
|
268
|
-
const second = await fetch(`http://127.0.0.1:${port}/gwf/event-login-details`, {
|
|
269
|
-
headers: { cookie: cookie.split(';')[0] },
|
|
270
|
-
});
|
|
271
|
-
assert.equal(second.headers.get('set-cookie'), cookie);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it('stop() resolves promptly even with a live WS client attached (regression)', async () => {
|
|
275
|
-
const config = baseConfig();
|
|
276
|
-
const { server, port } = await startServer(config);
|
|
277
|
-
const client = await TestClient.connect(port);
|
|
278
|
-
cleanups.push(() => client.close());
|
|
279
|
-
|
|
280
|
-
// Before the fix, wss.close() left the client socket open and
|
|
281
|
-
// httpServer.close() waited on it forever.
|
|
282
|
-
await Promise.race([
|
|
283
|
-
server.stop(),
|
|
284
|
-
new Promise((_, reject) => {
|
|
285
|
-
setTimeout(() => reject(new Error('stop() did not resolve with a client attached')), 3000);
|
|
286
|
-
}),
|
|
287
|
-
]);
|
|
288
|
-
});
|
|
289
|
-
});
|