@genesislcap/mock-server 15.3.2-canary.16 → 15.7.3-alpha-277c463.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 CHANGED
@@ -97,6 +97,19 @@ CI/local dev. Leave it unset to require an explicit `EVENT_LOGIN_AUTH` POST
97
97
  (validated against `config.auth.users`, same as the WS flow) before
98
98
  `event-login-details` will succeed on a later reload.
99
99
 
100
+ WS data traffic (`DATA_LOGON`, `REQ_*`, `META_REQUEST`, `JSON_SCHEMA_REQUEST`,
101
+ commit events) requires authentication, like the real server: either the
102
+ connection logged in over WS, or the message carries a `SESSION_AUTH_TOKEN`
103
+ this server issued over **either** transport — foundation-comms stamps the
104
+ stored token onto every outgoing message, which is exactly how a client that
105
+ logged in over HTTP (including via `httpAutoLogin`) authenticates its WS
106
+ traffic. The WS token-refresh login (`EVENT_LOGIN_AUTH` with only
107
+ `DETAILS.REFRESH_AUTH_TOKEN`, no credentials — foundation-comms'
108
+ `createRefreshTokenMessage`) is honored the same way. Unauthenticated data
109
+ messages get a `NOT_LOGGED_ON` error shaped like the target handler's own
110
+ error replies. Set `config.auth.requireAuth: false` to switch the gate off
111
+ and accept everything.
112
+
100
113
  ## Protocol coverage
101
114
 
102
115
  | MESSAGE_TYPE | Handled |
@@ -109,7 +122,7 @@ CI/local dev. Leave it unset to require an explicit `EVENT_LOGIN_AUTH` POST
109
122
  | `META_REQUEST` | ✅ auto-derived from seed data |
110
123
  | `JSON_SCHEMA_REQUEST` | ✅ auto-derived per event — see below |
111
124
  | `RESOURCES_REQUEST` | ✅ |
112
- | `REQ_<NAME>` / `REP_<NAME>` | ✅ |
125
+ | `REQ_<NAME>` / `REP_<NAME>` | ✅ incl. keyed `REQUEST` field lookups (`'*'` = any) and `CRITERIA_MATCH` |
113
126
  | `EVENT_<ENTITY>_<VERB>` commit events | ✅ via `config.eventHandlers` |
114
127
 
115
128
  ## Entity-management (`@genesislcap/foundation-entity-management`) support
@@ -224,14 +237,32 @@ extracted from for how each was actually found:
224
237
  directly.
225
238
  4. **Datasource `init()` needs `TYPE: 'DATASERVER'` plus a non-empty `FIELD`
226
239
  array** or the client silently reports "no fields returned" and gives up.
227
- `deriveFields()` (`src/db/metadata.ts`) infers this from the seed rows'
228
- own JS types the first time a resource is queried — only hand-author
229
- metadata for cases inference gets wrong.
240
+ `deriveFields()` (`src/db/metadata.ts`) infers this from the rows' own JS
241
+ types — falling back to the registration-time seed rows when every live
242
+ row has been deleted, so a runtime delete can't collapse a resource's
243
+ schema to zero fields. Numeric inference scans every row (a column is
244
+ `DOUBLE` if any value is fractional), but a column whose values are *all*
245
+ whole numbers still infers as `LONG` — if it's semantically a `DOUBLE`
246
+ (e.g. a NOTIONAL that happens to be round in the seed data), say so via
247
+ the `fieldTypes` override on the query/request-reply.
230
248
  5. **`CRITERIA_MATCH` needs more than `==`.** `compileCriteria()`
231
- (`src/db/criteria.ts`) supports `== != > >= < <= AND OR NOT` and
232
- parentheses via a small typed grammar (not `eval`/`new Function` — a
233
- malformed criteria string can only fail to parse, never execute
234
- arbitrary JS).
249
+ (`src/db/criteria.ts`) supports `== != > >= < <= AND OR NOT` (also `&&`/`||`)
250
+ and parentheses via a small typed grammar (not `eval`/`new Function` — a
251
+ malformed criteria string can only fail to parse, never execute arbitrary
252
+ JS). It also understands the `Expr.<fn>(field, value)` function-call
253
+ criteria that grid date/text filters generate — mirroring
254
+ `foundation-criteria`'s `serialisers.ts` and `foundation-ui`'s date-range
255
+ filter: `contains`, `containsIgnoreCase`, `dateIsToday`, `dateIsEqual`,
256
+ `dateIsGreaterEqual`, `dateIsLessEqual`, `dateTimeIsEqual`,
257
+ `dateTimeIsGreaterEqual`, `dateTimeIsLessEqual`, `dateTimeIsAfter`,
258
+ `dateTimeIsBefore` (`dateIs*` compares by calendar day, `dateTimeIs*` by
259
+ full timestamp; date values are `'YYYYMMDD-HH:MM[:SS]'`, matching
260
+ `formatDate()` in `serialisers.ts`). Any other criteria this grammar still
261
+ doesn't cover fails open instead of crashing the handler: `compileCriteria()`
262
+ logs a warning and returns a match-all predicate, so `DATA_LOGON` still
263
+ responds (unfiltered) rather than leaving the client's grid stuck on
264
+ "Loading...". Add new `Expr.*` entries to `EXPR_FUNCTIONS` in `criteria.ts`
265
+ if you hit one that isn't covered yet.
235
266
  6. **Composite-key AMEND/DELETE matching needs a fallback beyond the PK.**
236
267
  `Store.findRow()` (`src/db/store.ts`) tries the configured PK field, then
237
268
  any single field that uniquely identifies one row, then the full
@@ -240,15 +271,28 @@ extracted from for how each was actually found:
240
271
 
241
272
  ## Known, accepted limitation
242
273
 
243
- If a live update makes a row fall out of a subscription's `CRITERIA_MATCH`,
244
- the correct behavior is to push it as a `DELETE` so the client drops the now
245
- out-of-scope row. This engine does not do that automatically — `broadcast()`
246
- only checks the *new* row against the predicate, so a row that stops
247
- matching just stops being pushed rather than being explicitly removed from
248
- the client's local state. Fix this in your own `eventHandlers` if a project
249
- leans harder on live filtering than the showcase example does (e.g. re-check
250
- membership yourself and call `broadcastTableChange(table, 'DELETE', pk)`
251
- when a row exits the criteria).
274
+ If a live **update** (MODIFY) makes a row fall out of a subscription's
275
+ `CRITERIA_MATCH`, the correct behavior is to push it as a `DELETE` so the
276
+ client drops the now out-of-scope row. This engine does not do that
277
+ automatically — `broadcast()` only checks the *new* row against the
278
+ predicate, so a row that stops matching just stops being pushed rather than
279
+ being explicitly removed from the client's local state. Fix this in your own
280
+ `eventHandlers` if a project leans harder on live filtering than the
281
+ showcase example does (e.g. re-check membership yourself and call
282
+ `broadcastTableChange(table, 'DELETE', pk)` when a row exits the criteria).
283
+
284
+ Actual **deletes** are not affected: DELETE pushes bypass the criteria
285
+ predicate entirely (the pushed row is a PK-only stub that couldn't satisfy
286
+ one anyway), so every subscriber is told to drop the row — a client that
287
+ never held that `ROW_REF` ignores the message.
288
+
289
+ ## Tests
290
+
291
+ `npm run --workspace=@genesislcap/mock-server test` runs the unit tests for
292
+ the criteria grammar, store, metadata inference, and event-table inference
293
+ plus end-to-end WS/HTTP tests (auth gating, filtered-subscription DELETE
294
+ delivery, keyed `REQ_*` lookups, session reuse, clean shutdown) via
295
+ `node --test`, no build step.
252
296
 
253
297
  ## What's per-project vs. generic
254
298
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genesislcap/mock-server",
3
- "version": "15.3.2-canary.16",
3
+ "version": "15.7.3-alpha-277c463.0",
4
4
  "description": "Reusable in-memory Genesis WebSocket protocol mock server for client automation/CI, without a JVM or database.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -33,5 +33,5 @@
33
33
  "publishConfig": {
34
34
  "access": "public"
35
35
  },
36
- "gitHead": "c42f51609cc3891f49defc8a8e443e42976c5688"
36
+ "gitHead": "3659a1907a981b483ed3a3ad1163bc2ece4e08e7"
37
37
  }
@@ -1,18 +1,22 @@
1
1
  // Evaluator for Genesis CRITERIA_MATCH expressions, e.g.:
2
2
  // TRADE_STATUS == 'CONFIRMED' AND QUANTITY > 100
3
3
  // NOT (SIDE == 'SELL') OR PRICE >= 100.5
4
+ // Expr.dateTimeIsGreaterEqual(TRADE_DATE, '20240101-00:00:00') && COUNTERPARTY_ID == 'CPTY001'
4
5
  //
5
- // Supports: == = != <> > >= < <=, AND OR NOT, parentheses, string/number/boolean
6
- // literals, and bare field identifiers. Deliberately NOT eval()/new Function() —
7
- // this parses a small typed grammar so a malformed or hostile criteria string can
8
- // only fail to parse, never execute arbitrary JS.
6
+ // Supports: == = != <> > >= < <=, AND OR NOT (also && ||), parentheses,
7
+ // string/number/boolean literals, bare field identifiers, and the
8
+ // `Expr.<fn>(field, value)` function-call criteria that foundation-criteria's
9
+ // serialisers.ts emits for grid date/text filters (see EXPR_FUNCTIONS below).
10
+ // Deliberately NOT eval()/new Function() — this parses a small typed grammar
11
+ // so a malformed or hostile criteria string can only fail to parse, never
12
+ // execute arbitrary JS.
9
13
 
10
14
  import type { Row } from '../types.ts';
11
15
 
12
16
  export type Predicate = (record: Row) => boolean;
13
17
 
14
18
  const TOKEN_RE =
15
- /\s*(=>|<=|>=|!=|<>|==|=|<|>|\(|\)|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|-?\d+(?:\.\d+)?|[A-Za-z_][A-Za-z0-9_.]*)/g;
19
+ /\s*(&&|\|\||<=|>=|!=|<>|==|=|<|>|\(|\)|,|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|-?\d+(?:\.\d+)?|[A-Za-z_][A-Za-z0-9_.]*)/g;
16
20
 
17
21
  function tokenize(input: string): string[] {
18
22
  const tokens: string[] = [];
@@ -43,6 +47,100 @@ function parseLiteral(token: string): unknown {
43
47
 
44
48
  const COMPARATORS = new Set(['==', '=', '!=', '<>', '<', '<=', '>', '>=']);
45
49
 
50
+ // Genesis date/datetime values serialize as 'YYYYMMDD-HH:MM[:SS]' (see
51
+ // foundation-criteria's serialisers.ts formatDate); row values may already be
52
+ // epoch millis (number) or an ISO string. Returns null if unparseable.
53
+ function parseGenesisDateTime(value: unknown): number | null {
54
+ if (typeof value === 'number') return value;
55
+ if (typeof value !== 'string') return null;
56
+ const match = value.match(/^(\d{4})(\d{2})(\d{2})-(\d{2}):(\d{2})(?::(\d{2}))?$/);
57
+ if (match) {
58
+ const [, y, mo, d, h, mi, s] = match;
59
+ return Date.UTC(Number(y), Number(mo) - 1, Number(d), Number(h), Number(mi), Number(s ?? '0'));
60
+ }
61
+ const parsed = Date.parse(value);
62
+ return Number.isNaN(parsed) ? null : parsed;
63
+ }
64
+
65
+ const HOURS_PER_DAY = 24;
66
+ const MINUTES_PER_HOUR = 60;
67
+ const SECONDS_PER_MINUTE = 60;
68
+ const MS_PER_SECOND = 1000;
69
+ const MS_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE * MS_PER_SECOND;
70
+
71
+ function toEpochDay(value: unknown): number | null {
72
+ const epoch = parseGenesisDateTime(value);
73
+ return epoch === null ? null : Math.floor(epoch / MS_PER_DAY);
74
+ }
75
+
76
+ type ExprResolver = (token: string, record: Row) => unknown;
77
+
78
+ // Expr.<fn>(field, value) function-call criteria, as emitted by
79
+ // foundation-criteria's serialisers.ts (contains/containsIgnoreCase/dateIs*)
80
+ // and foundation-ui's date-range filter.ts (dateTimeIsAfter/dateTimeIsBefore,
81
+ // which operator-map.ts maps onto the same >=/<= semantics as
82
+ // dateTimeIsGreaterEqual/dateTimeIsLessEqual). `dateIs*` compares by calendar
83
+ // day (UTC), `dateTimeIs*` compares full timestamps.
84
+ const EXPR_FUNCTIONS: Record<
85
+ string,
86
+ (record: Row, args: string[], resolve: ExprResolver) => boolean
87
+ > = {
88
+ contains: (record, args, resolve) => {
89
+ const value = resolve(args[0], record);
90
+ const needle = resolve(args[1], record);
91
+ return typeof value === 'string' && typeof needle === 'string' && value.includes(needle);
92
+ },
93
+ containsIgnoreCase: (record, args, resolve) => {
94
+ const value = resolve(args[0], record);
95
+ const needle = resolve(args[1], record);
96
+ return (
97
+ typeof value === 'string' &&
98
+ typeof needle === 'string' &&
99
+ value.toLowerCase().includes(needle.toLowerCase())
100
+ );
101
+ },
102
+ dateIsToday: (record, args, resolve) => {
103
+ const day = toEpochDay(resolve(args[0], record));
104
+ return day !== null && day === toEpochDay(Date.now());
105
+ },
106
+ dateIsEqual: (record, args, resolve) => compareByDay(record, args, resolve, (a, b) => a === b),
107
+ dateIsGreaterEqual: (record, args, resolve) =>
108
+ compareByDay(record, args, resolve, (a, b) => a >= b),
109
+ dateIsLessEqual: (record, args, resolve) => compareByDay(record, args, resolve, (a, b) => a <= b),
110
+ dateTimeIsEqual: (record, args, resolve) =>
111
+ compareByTime(record, args, resolve, (a, b) => a === b),
112
+ dateTimeIsGreaterEqual: (record, args, resolve) =>
113
+ compareByTime(record, args, resolve, (a, b) => a >= b),
114
+ dateTimeIsLessEqual: (record, args, resolve) =>
115
+ compareByTime(record, args, resolve, (a, b) => a <= b),
116
+ dateTimeIsAfter: (record, args, resolve) =>
117
+ compareByTime(record, args, resolve, (a, b) => a >= b),
118
+ dateTimeIsBefore: (record, args, resolve) =>
119
+ compareByTime(record, args, resolve, (a, b) => a <= b),
120
+ };
121
+
122
+ function compareByDay(
123
+ record: Row,
124
+ args: string[],
125
+ resolve: ExprResolver,
126
+ cmp: (a: number, b: number) => boolean,
127
+ ): boolean {
128
+ const a = toEpochDay(resolve(args[0], record));
129
+ const b = toEpochDay(resolve(args[1], record));
130
+ return a !== null && b !== null && cmp(a, b);
131
+ }
132
+
133
+ function compareByTime(
134
+ record: Row,
135
+ args: string[],
136
+ resolve: ExprResolver,
137
+ cmp: (a: number, b: number) => boolean,
138
+ ): boolean {
139
+ const a = parseGenesisDateTime(resolve(args[0], record));
140
+ const b = parseGenesisDateTime(resolve(args[1], record));
141
+ return a !== null && b !== null && cmp(a, b);
142
+ }
143
+
46
144
  class Parser {
47
145
  private tokens: string[];
48
146
  private pos = 0;
@@ -71,7 +169,7 @@ class Parser {
71
169
 
72
170
  parseOr(): Predicate {
73
171
  let left = this.parseAnd();
74
- while (this.peek()?.toUpperCase() === 'OR') {
172
+ while (this.peek() === '||' || this.peek()?.toUpperCase() === 'OR') {
75
173
  this.next();
76
174
  const right = this.parseAnd();
77
175
  const prev = left;
@@ -82,7 +180,7 @@ class Parser {
82
180
 
83
181
  parseAnd(): Predicate {
84
182
  let left = this.parseNot();
85
- while (this.peek()?.toUpperCase() === 'AND') {
183
+ while (this.peek() === '&&' || this.peek()?.toUpperCase() === 'AND') {
86
184
  this.next();
87
185
  const right = this.parseNot();
88
186
  const prev = left;
@@ -112,6 +210,11 @@ class Parser {
112
210
  if (leftToken === undefined) {
113
211
  throw new Error('Expected operand on the left side of comparison');
114
212
  }
213
+
214
+ if (/^Expr\.[A-Za-z_][A-Za-z0-9_]*$/.test(leftToken) && this.peek() === '(') {
215
+ return this.parseFunctionCall(leftToken);
216
+ }
217
+
115
218
  const op = this.peek();
116
219
  if (!op || !COMPARATORS.has(op)) {
117
220
  throw new Error(`Expected comparator, got: ${op}`);
@@ -154,6 +257,30 @@ class Parser {
154
257
  if (literal !== undefined) return literal;
155
258
  return record[token];
156
259
  }
260
+
261
+ parseFunctionCall(name: string): Predicate {
262
+ this.next(); // consume '('
263
+ const args: string[] = [];
264
+ if (this.peek() !== ')') {
265
+ const arg = this.next();
266
+ if (arg === undefined) throw new Error(`Unterminated argument list in ${name}(...)`);
267
+ args.push(arg);
268
+ while (this.peek() === ',') {
269
+ this.next();
270
+ const nextArg = this.next();
271
+ if (nextArg === undefined) throw new Error(`Unterminated argument list in ${name}(...)`);
272
+ args.push(nextArg);
273
+ }
274
+ }
275
+ if (this.next() !== ')') throw new Error(`Missing closing parenthesis in ${name}(...)`);
276
+
277
+ const fnName = name.slice('Expr.'.length);
278
+ const handler = EXPR_FUNCTIONS[fnName];
279
+ if (!handler) throw new Error(`Unsupported criteria function: ${name}`);
280
+
281
+ const resolve: ExprResolver = (token, record) => this.resolveValue(token, record);
282
+ return (record: Row) => handler(record, args, resolve);
283
+ }
157
284
  }
158
285
 
159
286
  /** Compiles a CRITERIA_MATCH string into a predicate function(record) => boolean. */
@@ -161,13 +288,26 @@ export function compileCriteria(criteria?: string | null): Predicate {
161
288
  if (!criteria || typeof criteria !== 'string' || criteria.trim() === '') {
162
289
  return () => true;
163
290
  }
164
- const tokens = tokenize(criteria.trim());
165
- const parser = new Parser(tokens);
166
- const predicate = parser.parseExpression();
167
- if (parser.position !== tokens.length) {
168
- throw new Error(`Unexpected trailing tokens in criteria: ${criteria}`);
291
+ // Real Genesis clients can emit CRITERIA_MATCH syntax this grammar doesn't
292
+ // cover yet (e.g. function-call criteria like Expr.dateTimeIsGreaterEqual(...)
293
+ // from date-range filters). Failing open — matching everything and logging —
294
+ // keeps DATA_LOGON/broadcast working (unfiltered) instead of crashing the
295
+ // handler and leaving the client's grid stuck on "Loading...".
296
+ try {
297
+ const tokens = tokenize(criteria.trim());
298
+ const parser = new Parser(tokens);
299
+ const predicate = parser.parseExpression();
300
+ if (parser.position !== tokens.length) {
301
+ throw new Error(`Unexpected trailing tokens in criteria: ${criteria}`);
302
+ }
303
+ return predicate;
304
+ } catch (error) {
305
+ console.warn(
306
+ `[mock-server] Unsupported CRITERIA_MATCH syntax, ignoring filter: ${criteria}`,
307
+ error,
308
+ );
309
+ return () => true;
169
310
  }
170
- return predicate;
171
311
  }
172
312
 
173
313
  export function filterByCriteria(rows: Row[], criteria?: string | null): Row[] {
@@ -4,10 +4,19 @@
4
4
 
5
5
  import type { FieldMetadata, Row } from '../types.ts';
6
6
 
7
- function inferFieldType(value: unknown): string {
8
- if (typeof value === 'number') return Number.isInteger(value) ? 'LONG' : 'DOUBLE';
9
- if (typeof value === 'boolean') return 'BOOLEAN';
10
- if (typeof value === 'string') return 'STRING';
7
+ // Numeric fields scan EVERY row, not just the first non-null value: a column
8
+ // like PRICE is DOUBLE if any row holds a fractional value, even when row one
9
+ // happens to land on a whole number. A column whose values are all whole
10
+ // numbers still infers as LONG — if it's semantically a DOUBLE, say so via
11
+ // the fieldTypes override (see README).
12
+ function inferFieldType(name: string, rows: Row[], sample: unknown): string {
13
+ if (typeof sample === 'number') {
14
+ const anyFractional = rows.some(
15
+ (row) => typeof row[name] === 'number' && !Number.isInteger(row[name]),
16
+ );
17
+ return anyFractional ? 'DOUBLE' : 'LONG';
18
+ }
19
+ if (typeof sample === 'boolean') return 'BOOLEAN';
11
20
  return 'STRING';
12
21
  }
13
22
 
@@ -26,7 +35,8 @@ export function deriveFields(rows: Row[], overrides: Record<string, string> = {}
26
35
 
27
36
  return fieldNames.map((name) => {
28
37
  const sampleRow = rows.find((row) => row[name] !== undefined && row[name] !== null);
29
- const type = overrides[name] ?? (sampleRow ? inferFieldType(sampleRow[name]) : 'STRING');
38
+ const type =
39
+ overrides[name] ?? (sampleRow ? inferFieldType(name, rows, sampleRow[name]) : 'STRING');
30
40
  return { NAME: name, TYPE: type };
31
41
  });
32
42
  }
package/src/db/store.ts CHANGED
@@ -15,6 +15,10 @@ export class Store {
15
15
  private tables = new Map<string, Map<Pk, Row>>();
16
16
  private tableConfig = new Map<string, TableConfig>();
17
17
  private sequenceCounters = new Map<string, number>();
18
+ // Immutable snapshot of each table's rows as registered, used only for field
19
+ // metadata derivation (see getMetadataRows) so deleting every live row can't
20
+ // collapse a resource's schema to zero fields.
21
+ private seedRows = new Map<string, Row[]>();
18
22
  readonly views = new Map<string, ViewDef>();
19
23
 
20
24
  registerTable(
@@ -27,6 +31,10 @@ export class Store {
27
31
  }
28
32
  this.tables.set(tableName, table);
29
33
  this.tableConfig.set(tableName, { pkField, sequencePrefix, sequenceWidth });
34
+ this.seedRows.set(
35
+ tableName,
36
+ rows.map((row) => ({ ...row })),
37
+ );
30
38
 
31
39
  if (sequencePrefix) {
32
40
  let maxN = 0;
@@ -122,16 +130,16 @@ export class Store {
122
130
  getViewRows(viewName: string): Row[] {
123
131
  const view = this.views.get(viewName);
124
132
  if (!view) throw new Error(`Unknown view: ${viewName}`);
133
+ return this.materializeView(view, (tableName) => this.getAllRows(tableName));
134
+ }
125
135
 
126
- let rows = this.getAllRows(view.base).map((row) => ({ ...row }));
136
+ private materializeView(view: ViewDef, getRows: (tableName: string) => Row[]): Row[] {
137
+ let rows = getRows(view.base).map((row) => ({ ...row }));
127
138
 
128
139
  for (const join of view.joins ?? []) {
129
- const joinTable = this.tables.get(join.table);
130
140
  const joinIndex = new Map<unknown, Row>();
131
- if (joinTable) {
132
- for (const row of joinTable.values()) {
133
- joinIndex.set(row[join.on[1]], row);
134
- }
141
+ for (const row of getRows(join.table)) {
142
+ joinIndex.set(row[join.on[1]], row);
135
143
  }
136
144
  rows = rows.map((row) => {
137
145
  const match = joinIndex.get(row[join.on[0]]);
@@ -162,6 +170,20 @@ export class Store {
162
170
  return this.getAllRows(resourceName);
163
171
  }
164
172
 
173
+ // Rows to derive META_REQUEST/JSON_SCHEMA_REQUEST field metadata from:
174
+ // current rows when there are any, else the registration-time seed rows
175
+ // (re-joined through the view definition for views). Without this fallback,
176
+ // deleting a table's last row would make the next metadata reply say the
177
+ // resource has zero fields — which entity-management renders as a form with
178
+ // no inputs and a delete button that can't build a payload, silently.
179
+ getMetadataRows(resourceName: string): Row[] {
180
+ const live = this.getResourceRows(resourceName);
181
+ if (live.length > 0) return live;
182
+ const view = this.views.get(resourceName);
183
+ if (view) return this.materializeView(view, (tableName) => this.seedRows.get(tableName) ?? []);
184
+ return this.seedRows.get(resourceName) ?? [];
185
+ }
186
+
165
187
  // Row-identity field for a table or view, used to stamp DETAILS.ROW_REF on
166
188
  // QUERY_UPDATE rows (see handlers/dataLogon.ts and dataServerResultFilter.ts
167
189
  // client-side, which keys inserts/amends/deletes off this field).
@@ -5,6 +5,32 @@ import type { AuthUser, GenesisMessage, MockServerConfig, Row } from '../types.t
5
5
 
6
6
  const DEFAULT_HEARTBEAT_INTERVAL_SECS = 30;
7
7
 
8
+ // Every SESSION_AUTH_TOKEN/REFRESH_AUTH_TOKEN issued by this server -> the
9
+ // username it was issued to. Shared across the WS and HTTP transports so a
10
+ // token from an HTTP login authenticates later WS traffic (see router.ts) and
11
+ // foundation-comms' WS token-refresh (EVENT_LOGIN_AUTH with only
12
+ // DETAILS.REFRESH_AUTH_TOKEN, no credentials) can succeed.
13
+ export type TokenRegistry = Map<string, string>;
14
+
15
+ // Both transports mint tokens on every login/silent-login, so a long-lived
16
+ // dev/CI server would otherwise grow these maps one entry per page load.
17
+ // Maps iterate in insertion order, so dropping from the front evicts the
18
+ // oldest sessions/tokens first.
19
+ const MAX_REGISTRY_ENTRIES = 1000;
20
+ export function capRegistry(registry: Map<string, string>): void {
21
+ while (registry.size > MAX_REGISTRY_ENTRIES) {
22
+ const oldest = registry.keys().next().value;
23
+ if (oldest === undefined) return;
24
+ registry.delete(oldest);
25
+ }
26
+ }
27
+
28
+ export function registerAckTokens(ack: Row, username: string, tokens: TokenRegistry): void {
29
+ tokens.set(ack.SESSION_AUTH_TOKEN, username);
30
+ tokens.set(ack.REFRESH_AUTH_TOKEN, username);
31
+ capRegistry(tokens);
32
+ }
33
+
8
34
  // Shared by the WS EVENT_LOGIN_AUTH handler and the HTTP login handlers
9
35
  // (handlers/http.ts) — EVENT_LOGIN_DETAILS_ACK is byte-for-byte the same shape
10
36
  // as EVENT_LOGIN_AUTH_ACK, just under a different MESSAGE_TYPE (see
@@ -54,9 +80,23 @@ export function handleLogin(
54
80
  message: GenesisMessage,
55
81
  connection: Connection,
56
82
  config: MockServerConfig,
83
+ tokens: TokenRegistry,
57
84
  ): void {
58
85
  const details = message.DETAILS ?? {};
59
- const user = findUser(config, details.USER_NAME, details.PASSWORD);
86
+ // Two login shapes share EVENT_LOGIN_AUTH: credentials ({USER_NAME,
87
+ // PASSWORD}) and token refresh — foundation-comms' createRefreshTokenMessage
88
+ // sends only DETAILS.REFRESH_AUTH_TOKEN (plus the stored SESSION_AUTH_TOKEN
89
+ // at the top level), no username at all.
90
+ const tokenUsername =
91
+ details.USER_NAME === undefined
92
+ ? (tokens.get(details.REFRESH_AUTH_TOKEN) ??
93
+ tokens.get(details.SESSION_AUTH_TOKEN) ??
94
+ tokens.get(message.SESSION_AUTH_TOKEN))
95
+ : undefined;
96
+ const user =
97
+ details.USER_NAME !== undefined
98
+ ? findUser(config, details.USER_NAME, details.PASSWORD)
99
+ : findUser(config, tokenUsername);
60
100
 
61
101
  if (!user) {
62
102
  connection.reply(message, {
@@ -74,6 +114,7 @@ export function handleLogin(
74
114
  }
75
115
 
76
116
  const ack = buildLoginAckBody(user, config);
117
+ registerAckTokens(ack, user.username, tokens);
77
118
  connection.authenticated = true;
78
119
  connection.userName = user.username;
79
120
  connection.profile = user.profile ?? [];
@@ -2,7 +2,13 @@ import crypto from 'node:crypto';
2
2
  import type { IncomingMessage, ServerResponse } from 'node:http';
3
3
  import { MESSAGE_TYPE } from '../protocol/messageTypes.ts';
4
4
  import type { MockServerConfig } from '../types.ts';
5
- import { buildLoginAckBody, findUser } from './auth.ts';
5
+ import {
6
+ buildLoginAckBody,
7
+ capRegistry,
8
+ findUser,
9
+ registerAckTokens,
10
+ type TokenRegistry,
11
+ } from './auth.ts';
6
12
 
7
13
  const SESSION_COOKIE = 'MOCK_SESSION_ID';
8
14
 
@@ -58,7 +64,26 @@ async function readJsonBody(req: IncomingMessage): Promise<Record<string, any>>
58
64
  return raw ? JSON.parse(raw) : {};
59
65
  }
60
66
 
61
- export function createHttpRequestListener(config: MockServerConfig, sessions: Map<string, string>) {
67
+ // Reuse the request's own session when its cookie already maps to this user —
68
+ // otherwise every page load would mint (and leak) a fresh session entry on a
69
+ // long-lived dev/CI server.
70
+ function ensureSession(
71
+ sessions: Map<string, string>,
72
+ cookieSessionId: string | undefined,
73
+ username: string,
74
+ ): string {
75
+ if (cookieSessionId && sessions.get(cookieSessionId) === username) return cookieSessionId;
76
+ const sessionId = crypto.randomUUID();
77
+ sessions.set(sessionId, username);
78
+ capRegistry(sessions);
79
+ return sessionId;
80
+ }
81
+
82
+ export function createHttpRequestListener(
83
+ config: MockServerConfig,
84
+ sessions: Map<string, string>,
85
+ tokens: TokenRegistry = new Map(),
86
+ ) {
62
87
  return async function requestListener(req: IncomingMessage, res: ServerResponse): Promise<void> {
63
88
  const pathname = (req.url ?? '').split('?')[0];
64
89
 
@@ -95,10 +120,11 @@ export function createHttpRequestListener(config: MockServerConfig, sessions: Ma
95
120
  return;
96
121
  }
97
122
 
98
- const sessionId = crypto.randomUUID();
99
- sessions.set(sessionId, user.username);
123
+ const sessionId = ensureSession(sessions, cookies[SESSION_COOKIE], user.username);
100
124
  setSessionCookie(res, sessionId);
101
- sendJson(res, HTTP_STATUS.OK, buildLoginAckBody(user, config, 'EVENT_LOGIN_DETAILS_ACK'));
125
+ const ack = buildLoginAckBody(user, config, 'EVENT_LOGIN_DETAILS_ACK');
126
+ registerAckTokens(ack, user.username, tokens);
127
+ sendJson(res, HTTP_STATUS.OK, ack);
102
128
  return;
103
129
  }
104
130
 
@@ -130,10 +156,11 @@ export function createHttpRequestListener(config: MockServerConfig, sessions: Ma
130
156
  return;
131
157
  }
132
158
 
133
- const sessionId = crypto.randomUUID();
134
- sessions.set(sessionId, user.username);
159
+ const sessionId = ensureSession(sessions, cookies[SESSION_COOKIE], user.username);
135
160
  setSessionCookie(res, sessionId);
136
- sendJson(res, HTTP_STATUS.OK, buildLoginAckBody(user, config));
161
+ const ack = buildLoginAckBody(user, config);
162
+ registerAckTokens(ack, user.username, tokens);
163
+ sendJson(res, HTTP_STATUS.OK, ack);
137
164
  return;
138
165
  }
139
166
 
@@ -51,7 +51,11 @@ const DELETE_EVENT_RE = /_DELETE(_|$)/;
51
51
  // variants like INSERT_WITH_APPROVAL, DEMO_INSERT, MODIFY_PRICE, INSERT_FAIL)
52
52
  // without hand-maintaining a table per event. Override via
53
53
  // config.eventSchemas for names that don't follow this convention.
54
- function inferEventTable(eventName: string | undefined, tableNames: string[]): string | undefined {
54
+ // Exported for unit tests only.
55
+ export function inferEventTable(
56
+ eventName: string | undefined,
57
+ tableNames: string[],
58
+ ): string | undefined {
55
59
  if (!eventName?.startsWith('EVENT_')) return undefined;
56
60
  const rest = eventName.slice('EVENT_'.length);
57
61
  const candidates = tableNames.filter((name) => rest === name || rest.startsWith(`${name}_`));
@@ -79,7 +83,7 @@ export function handleJsonSchemaRequest(
79
83
 
80
84
  const pkOnly = override?.pkOnly ?? DELETE_EVENT_RE.test(eventName!);
81
85
  const pkField = store.getPkField(table);
82
- let fields = deriveFields(store.getResourceRows(table), override?.fieldTypes);
86
+ let fields = deriveFields(store.getMetadataRows(table), override?.fieldTypes);
83
87
  if (pkOnly) fields = fields.filter((field) => field.NAME === pkField);
84
88
 
85
89
  const schema = buildSchema(fields, pkOnly && pkField ? [pkField] : []);
@@ -37,7 +37,7 @@ export function handleMetaRequest(
37
37
  }
38
38
 
39
39
  const resolved = (query ?? requestReply)!;
40
- const rows = store.getResourceRows(resolved.source);
40
+ const rows = store.getMetadataRows(resolved.source);
41
41
  const fields = deriveFields(rows, resolved.fieldTypes);
42
42
 
43
43
  connection.reply(message, {
@@ -3,7 +3,20 @@ import type { Store } from '../db/store.ts';
3
3
  import type { Connection } from '../protocol/connection.ts';
4
4
  import { NackError } from '../protocol/errors.ts';
5
5
  import { ERROR_CODE } from '../protocol/messageTypes.ts';
6
- import type { GenesisMessage, MockServerConfig } from '../types.ts';
6
+ import type { GenesisMessage, MockServerConfig, Row } from '../types.ts';
7
+
8
+ // Keyed lookups: foundation-comms' createRequestMessage spreads RequestParams
9
+ // onto the message, so REQUEST is a *top-level* field ({FIELD: value} pairs
10
+ // naming the request server's index fields). A real request server only
11
+ // returns rows matching them; '*' (Genesis's wildcard), null/undefined, and
12
+ // '' mean "any". Arrays match any listed value.
13
+ function matchesRequestFields(row: Row, request: Row): boolean {
14
+ return Object.entries(request).every(([field, value]) => {
15
+ if (value === undefined || value === null || value === '*' || value === '') return true;
16
+ if (Array.isArray(value)) return value.length === 0 || value.includes(row[field]);
17
+ return row[field] === value;
18
+ });
19
+ }
7
20
 
8
21
  export function handleRequestReply(
9
22
  message: GenesisMessage,
@@ -29,6 +42,10 @@ export function handleRequestReply(
29
42
 
30
43
  try {
31
44
  let rows = store.getResourceRows(requestReply.source);
45
+ const request = message.REQUEST ?? message.DETAILS?.REQUEST;
46
+ if (request && typeof request === 'object') {
47
+ rows = rows.filter((row) => matchesRequestFields(row, request));
48
+ }
32
49
  const criteria = message.DETAILS?.CRITERIA_MATCH ?? message.CRITERIA_MATCH;
33
50
  if (criteria) rows = filterByCriteria(rows, criteria);
34
51
  if (requestReply.filter) rows = rows.filter(requestReply.filter);