@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.
Files changed (72) hide show
  1. package/README.md +246 -40
  2. package/dist/db/criteria.d.ts +5 -0
  3. package/dist/db/criteria.js +327 -0
  4. package/dist/db/metadata.d.ts +3 -0
  5. package/dist/db/metadata.js +35 -0
  6. package/dist/db/store.d.ts +28 -0
  7. package/dist/db/store.js +254 -0
  8. package/dist/handlers/auth.d.ts +10 -0
  9. package/dist/handlers/auth.js +121 -0
  10. package/dist/handlers/commitEvent.d.ts +8 -0
  11. package/dist/handlers/commitEvent.js +40 -0
  12. package/dist/handlers/dataLogon.d.ts +5 -0
  13. package/dist/handlers/dataLogon.js +56 -0
  14. package/dist/handlers/heartbeat.d.ts +3 -0
  15. package/dist/handlers/heartbeat.js +16 -0
  16. package/dist/handlers/http.d.ts +4 -0
  17. package/dist/handlers/http.js +179 -0
  18. package/dist/handlers/jsonSchema.d.ts +5 -0
  19. package/dist/handlers/jsonSchema.js +94 -0
  20. package/dist/handlers/meta.d.ts +4 -0
  21. package/dist/handlers/meta.js +42 -0
  22. package/dist/handlers/requestReply.d.ts +4 -0
  23. package/dist/handlers/requestReply.js +68 -0
  24. package/dist/handlers/resourceAuth.d.ts +5 -0
  25. package/dist/handlers/resourceAuth.js +27 -0
  26. package/dist/handlers/resources.d.ts +3 -0
  27. package/dist/handlers/resources.js +34 -0
  28. package/dist/index.d.ts +6 -0
  29. package/dist/index.js +5 -0
  30. package/dist/launch.d.ts +20 -0
  31. package/dist/launch.js +164 -0
  32. package/dist/protocol/connection.d.ts +25 -0
  33. package/dist/protocol/connection.js +36 -0
  34. package/dist/protocol/errors.d.ts +17 -0
  35. package/dist/protocol/errors.js +16 -0
  36. package/dist/protocol/messageTypes.d.ts +28 -0
  37. package/dist/protocol/messageTypes.js +42 -0
  38. package/dist/protocol/router.d.ts +6 -0
  39. package/dist/protocol/router.js +101 -0
  40. package/dist/protocol/rowUpdate.d.ts +4 -0
  41. package/dist/protocol/rowUpdate.js +17 -0
  42. package/dist/server.d.ts +23 -0
  43. package/dist/server.js +328 -0
  44. package/dist/types.d.ts +142 -0
  45. package/dist/types.js +4 -0
  46. package/package.json +16 -7
  47. package/src/db/criteria.ts +75 -21
  48. package/src/db/store.ts +126 -29
  49. package/src/handlers/auth.ts +32 -6
  50. package/src/handlers/commitEvent.ts +4 -1
  51. package/src/handlers/dataLogon.ts +23 -7
  52. package/src/handlers/heartbeat.ts +16 -3
  53. package/src/handlers/http.ts +43 -2
  54. package/src/handlers/jsonSchema.ts +21 -5
  55. package/src/handlers/meta.ts +11 -3
  56. package/src/handlers/requestReply.ts +22 -8
  57. package/src/handlers/resourceAuth.ts +37 -0
  58. package/src/handlers/resources.ts +8 -1
  59. package/src/index.ts +9 -0
  60. package/src/launch.ts +220 -0
  61. package/src/protocol/connection.ts +4 -1
  62. package/src/protocol/router.ts +2 -2
  63. package/src/protocol/rowUpdate.ts +5 -4
  64. package/src/server.ts +150 -22
  65. package/src/types.ts +124 -3
  66. package/license.txt +0 -46
  67. package/test/criteria.test.ts +0 -174
  68. package/test/jsonSchema.test.ts +0 -38
  69. package/test/metadata.test.ts +0 -49
  70. package/test/server.test.ts +0 -308
  71. package/test/store.test.ts +0 -181
  72. package/tsconfig.json +0 -18
package/src/types.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  // a real Genesis payload's shape is "whatever the project defines" (row data,
3
3
  // event DETAILS) — Record<string, any> there is the honest type, not a cop-out.
4
4
 
5
+ import type { IncomingMessage, ServerResponse } from 'node:http';
5
6
  import type { Store } from './db/store.ts';
6
7
 
7
8
  export type Row = Record<string, any>;
@@ -14,7 +15,12 @@ export interface GenesisMessage {
14
15
  }
15
16
 
16
17
  export interface TableDef {
17
- pkField: string;
18
+ // A single field, or several for composite-keyed tables (e.g. PRICE keyed
19
+ // by [RFQ_ID, SIDE]). Composite rows are identified by the fields' values
20
+ // joined with '|' (see Store.rowKey) — no synthetic key column needed, and
21
+ // nothing extra shows up in grids. Field values therefore must not contain
22
+ // '|' themselves. Sequence generation requires a single pkField.
23
+ pkField: string | string[];
18
24
  sequencePrefix?: string;
19
25
  sequenceWidth?: number;
20
26
  rows?: Row[];
@@ -24,24 +30,86 @@ export interface JoinDef {
24
30
  table: string;
25
31
  on: [string, string];
26
32
  select?: Record<string, string>;
33
+ // Extra join predicate beyond the `on` equality — receives the candidate
34
+ // join row and the base row, e.g. side-specific joins like
35
+ // (price, rfq) => price.SIDE === 'SALES'. When present, matching scans the
36
+ // join table per base row instead of using a prebuilt index (fine at mock
37
+ // scale).
38
+ where?: (joinRow: Row, baseRow: Row) => boolean;
39
+ }
40
+
41
+ // Keeps only base rows with NO match in `table` (e.g. USER_WITHOUT_DETAIL:
42
+ // users lacking a USER_DETAIL row). `where` narrows what counts as a match,
43
+ // same contract as JoinDef.where.
44
+ export interface AntiJoinDef {
45
+ table: string;
46
+ on: [string, string];
47
+ where?: (joinRow: Row, baseRow: Row) => boolean;
27
48
  }
28
49
 
29
50
  export interface ViewDef {
30
51
  base: string;
31
- pkField?: string;
52
+ pkField?: string | string[];
32
53
  joins?: JoinDef[];
54
+ antiJoins?: AntiJoinDef[];
33
55
  derived?: Record<string, (row: Row) => any>;
34
56
  }
35
57
 
58
+ // The authenticated identity a resource-level auth hook sees. userName is
59
+ // undefined for unauthenticated connections (possible when requireAuth is
60
+ // off, or pre-login).
61
+ export interface RequestingUser {
62
+ userName?: string;
63
+ profile: string[];
64
+ permission: string[];
65
+ }
66
+
67
+ // Per-resource permissioning, mirroring what dataserver auth maps and
68
+ // hideFields do on the real server: rowFilter drops rows the user must not
69
+ // see (applied to snapshots, live pushes, and REQ_ replies); hideFields
70
+ // strips columns from rows AND from META_REQUEST field metadata, so e.g.
71
+ // sales users never see the trader side of mirrored columns.
72
+ export interface ResourceAuthDef {
73
+ rowFilter?: (row: Row, user: RequestingUser) => boolean;
74
+ hideFields?: string[] | ((user: RequestingUser) => string[]);
75
+ }
76
+
36
77
  export interface QueryDef {
37
78
  source: string;
38
79
  fieldTypes?: Record<string, string>;
80
+ // Server-side where-clause equivalent, applied before any client
81
+ // CRITERIA_MATCH — to both the DATA_LOGON snapshot and live pushes. Use it
82
+ // for things the real query's filter/where clause does (e.g. keep template
83
+ // rows out of ALL_QTE_MAIN_ENTRIES, CANCELLED out of the blotter).
84
+ filter?: (row: Row) => boolean;
85
+ auth?: ResourceAuthDef;
86
+ }
87
+
88
+ // Details a request-reply resolver receives: the keyed REQUEST fields, the
89
+ // full message, the requesting user, and the live store.
90
+ export interface RequestReplyResolverCtx {
91
+ request: Row;
92
+ message: GenesisMessage;
93
+ user: RequestingUser;
94
+ store: Store;
39
95
  }
40
96
 
41
97
  export interface RequestReplyDef {
42
- source: string;
98
+ // Either serve rows from a table/view (`source`, optionally narrowed by
99
+ // `filter`/REQUEST fields/CRITERIA_MATCH), or compute the reply yourself
100
+ // (`resolver`) — for request servers that calculate rather than read (date
101
+ // calculations, holiday validate/roll, premium schedules, composite
102
+ // lookups). A resolver receives the REQUEST fields and returns the REPLY
103
+ // rows verbatim; throw NackError for a protocol-correct error reply. When
104
+ // both are set, resolver wins. With resolver only, set `metadataRows` (a
105
+ // representative sample reply) or `fieldTypes` so META_REQUEST can still
106
+ // derive REPLY_FIELD metadata.
107
+ source?: string;
108
+ resolver?: (ctx: RequestReplyResolverCtx) => Row[];
109
+ metadataRows?: Row[];
43
110
  fieldTypes?: Record<string, string>;
44
111
  filter?: (row: Row) => boolean;
112
+ auth?: ResourceAuthDef;
45
113
  }
46
114
 
47
115
  export interface EventSchemaOverride {
@@ -63,8 +131,25 @@ export interface AuthUser {
63
131
  user?: Row;
64
132
  }
65
133
 
134
+ // Entry served from GET gwf/sso/list, matching foundation-auth's
135
+ // IDENTITY_PROVIDER DTO (services/idp.ts): { ID, DESCRIPTION, TYPE }.
136
+ export interface SsoIdentityProvider {
137
+ ID: string;
138
+ DESCRIPTION?: string;
139
+ TYPE?: string;
140
+ [key: string]: any;
141
+ }
142
+
66
143
  export interface AuthConfig {
67
144
  users?: AuthUser[];
145
+ // Served from GET gwf/sso/list as { IDPS: [...] }. foundation-auth's login
146
+ // screen fetches this whenever sso.toggled is on, shows a red "Failed to
147
+ // fetch identity providers" AND hides the credentials form if the fetch
148
+ // fails, and rejects an empty list too ("Identity provider list empty") —
149
+ // so when unset, the mock serves one non-functional placeholder provider
150
+ // to keep the login screen rendering. Clicking it navigates to the real
151
+ // SSO login URL template, which the mock does not implement.
152
+ ssoIdentityProviders?: SsoIdentityProvider[];
68
153
  heartbeatIntervalSecs?: number;
69
154
  // A username to always ack gwf/event-login-details as (skips the login
70
155
  // screen entirely), or `true` to use the first configured user.
@@ -100,15 +185,51 @@ export type EventHandlerResult = { generated?: Row[] } | void;
100
185
 
101
186
  export type EventHandler = (details: Row, ctx: EventHandlerCtx) => EventHandlerResult;
102
187
 
188
+ // Extra HTTP route served by the engine's shared http.Server — for project
189
+ // endpoints the protocol doesn't cover (e.g. GET pbi/configs). Routes are
190
+ // tried FIRST, before the built-in login/SSO handlers, so they can also
191
+ // override those. A string path matches pathname.endsWith(path) (same
192
+ // convention as the built-ins, which tolerates any gateway prefix); a RegExp
193
+ // is tested against the full pathname.
194
+ export interface HttpRouteDef {
195
+ method?: string;
196
+ path: string | RegExp;
197
+ handler: (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
198
+ }
199
+
200
+ export interface OnMessageCtx {
201
+ connection: import('./protocol/connection.ts').Connection;
202
+ store: Store;
203
+ broadcast: BroadcastFn;
204
+ broadcastTableChange: BroadcastTableChangeFn;
205
+ }
206
+
207
+ // Inbound-message hook, called for every parsed WS message BEFORE routing.
208
+ // Return a message (possibly rewritten — e.g. normalizing a CRITERIA_MATCH
209
+ // dialect) to continue routing with it, `false` if the hook fully handled
210
+ // the message itself (reply via ctx.connection.reply), or nothing to route
211
+ // the original unchanged.
212
+ export type OnMessageHook = (
213
+ message: GenesisMessage,
214
+ ctx: OnMessageCtx,
215
+ ) => GenesisMessage | false | void;
216
+
103
217
  export interface MockServerConfig {
104
218
  tables?: Record<string, TableDef>;
105
219
  views?: Record<string, ViewDef>;
106
220
  queries?: Record<string, QueryDef>;
107
221
  requestReplies?: Record<string, RequestReplyDef>;
108
222
  eventHandlers?: Record<string, EventHandler>;
223
+ // Fallback for commit events with no registered handler — lets a project
224
+ // with many events adopt incrementally (e.g. `() => ({})` acks everything
225
+ // unhandled) instead of enumerating ack-only handlers. Unregistered events
226
+ // NACK with UNKNOWN_RESOURCE when this is unset.
227
+ defaultEventHandler?: EventHandler;
109
228
  eventSchemas?: Record<string, EventSchemaOverride>;
110
229
  auth?: AuthConfig;
111
230
  resources?: ResourceItem[];
231
+ httpRoutes?: HttpRouteDef[];
232
+ onMessage?: OnMessageHook;
112
233
  productName?: string;
113
234
  }
114
235
 
package/license.txt DELETED
@@ -1,46 +0,0 @@
1
- Developer License Agreement
2
-
3
- This Developer License Agreement (the “Agreement”) is entered into by and between Genesis, Global FinTech Inc.(“Genesis”) and you and/or the entity on whose behalf you are downloading the Software (as defined below) (“you”). This Agreement forms a legally binding contract between you and Genesis in relation to your use of the software libraries, reference implementations, and other related demonstration code (collectively, the “Genesis Software”). [a]
4
-
5
- To use the Software, you must first agree to this Agreement. You may not use the Software if you do not agree to be bound by the terms of this Agreement. Accordingly, you and Genesis acknowledge and agree as follows:
6
-
7
- 1. LIMITED LICENSE.
8
- Subject to your complete and ongoing compliance with all the terms and conditions set forth in this Agreement, including without limitation all license limitations and restrictions set forth herein, Genesis grants you the following limited, non-exclusive, non-transferable, non-sublicensable, revocable licenses to use, and (where applicable) authorize your employees to use, the Software internally solely in connection with building, developing and testing your own applications that interoperate with or incorporate the Software (“Applications”) for the purposes of evaluation.
9
-
10
- 2. RESTRICTIONS.
11
- A. By accessing or using the Software, you represent, warrant, and covenant that (a) you are a person or business entity engaged in the development of software applications, and (b) in the case of a business entity, you have the full power and authority to bind such entity to the terms of this Agreement.
12
- B. You acknowledge that the foregoing license does not include any right to:
13
- (i) redistribute, sell, lease, license, modify or otherwise create any derivative works of any portion of the Software, or;
14
- (ii) distribute, deploy, or otherwise utilize Applications on a public, production, commercial, or other similar purpose other than internal use for evaluation and the development of non-public, experimental Applications (any other public, production, commercial, or similar use requires a separate agreement with Genesis), or
15
- (iii) use or implement any undocumented feature or API or use any documented feature or API other than in accordance with applicable documentation. You agree to not to do any of the foregoing.
16
- C. Except as expressly provided herein, you may not:
17
- (a) reproduce, distribute, publicly display, or publicly perform any part of the Software;
18
- (b) decompile, reverse engineer, or otherwise access or attempt to access the source code for the Software not made available to you in source code form;
19
- (c) make or attempt to make any modification to, or otherwise create derivative works of the Software; or
20
- (d) remove, obscure, interfere with or circumvent any feature of the Software, including without limitation any copyright or other intellectual property notices, security, or access control mechanism;
21
- (f) use the Software to do anything illegal, including facilitating, promoting, or otherwise encouraging any illegal activities.
22
-
23
- 3. RESERVATION OF RIGHTS.
24
- A. The Software is owned by Genesis and licensed, not sold, to you. The Software, content, visual interfaces, interactive features, information, graphics, design, compilation, computer code, products, services, and all other elements of the Software and related documentation (the “Materials“), are protected by copyright, trade dress, patent, and trademark laws of the United States and other jurisdictions, international conventions, and all other relevant intellectual property and proprietary rights, and applicable laws (collectively, the “Intellectual Property Rights“).
25
- B. You agree that Genesis or its subsidiaries or affiliated companies and/or its third-party licensors own all legal right, title and interest in and to the Software and Materials, including any and all Intellectual Property Rights. Genesis reserves all rights not expressly granted in this Agreement. You do not acquire any right, interest or title to the Materials, whether by implication, estoppel, or otherwise, except for the limited rights set forth in this Agreement.
26
- C. You agree that the form and nature of the Software that Genesis provides may change without prior notice to you and that future versions of the Software may be incompatible with applications developed on previous of the Software. You agree that Genesis may stop (permanently or temporarily) providing the Software (or any features within the Software) to you or to users generally at Genesis’s sole discretion, without prior notice to you. Nothing in this Agreement gives you a right to use any of Genesis’s trade names, trademarks, service marks, logos, domain names, or other distinctive brand features.
27
-
28
- 4. TERMINATION.
29
- At Genesis’s sole discretion, Genesis may terminate your Developer License and cease provision of the Genesis Software with or without notice, effective immediately, for any reason or no reason at all. Upon the expiration or termination of this Agreement all rights and licenses granted under this Agreement will immediately terminate.
30
-
31
- 5. THIRD PARTY SOFTWARE.
32
- The Software consists of a package of components and may include references to certain third-party software (“Third Party Software”) that are provided by their authors under separate license terms (the “Third Party Terms”). Your use of any such Third-Party Software in conjunction with the Software in a manner consistent with this Agreement is permitted, however, you may have broader rights and/or restrictions under the applicable Third-Party Terms. Genesis cannot accept any responsibility for the Third-Party Software or your use thereof.
33
-
34
- 6. DISCLAIMER OF WARRANTIES.
35
- YOU EXPRESSLY UNDERSTAND AND AGREE THAT YOUR USE OF THE SOFTWARE IS AT YOUR SOLE RISK AND THAT THE SOFTWARE IS PROVIDED “AS IS” AND “AS AVAILABLE” WITHOUT WARRANTY OF ANY KIND FROM GENESIS. YOUR USE OF THE SOFTWARE AND ANY MATERIAL DOWNLOADED OR OTHERWISE OBTAINED THROUGH THE USE OF THE SOFTWARE IS AT YOUR OWN DISCRETION AND RISK AND YOU ARE SOLELY RESPONSIBLE FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR OTHER DEVICE OR LOSS OF DATA THAT RESULTS FROM SUCH USE. GENESIS FURTHER EXPRESSLY DISCLAIMS ALL WARRANTIES AND CONDITIONS OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. GENESIS EXPRESSLY DISCLAIMS ANY WARRANTIES OF ANY KIND WITH RESPECT TO THE ACCURACY OR FUNCTIONALITY OF THE SOFTWARE, AND WITH RESPECT TO THE ACCURACY, VALIDITY, OR COMPLETENESS OF ANY INFORMATION OR FEATURES AVAILABLE THROUGH THE SOFTWARE, OR THE QUALITY OR CONSISTENCY OF THE SOFTWARE OR RESULTS OBTAINED THROUGH ITS USE.
36
-
37
- 7. INDEMNIFICATION. To the maximum extent permitted by law, you agree to defend, indemnify, and hold harmless Genesis, its affiliates, and their respective directors, officers, employees, and agents, from and against any and all third-party claims, actions, suits, or proceedings, as well as any and all losses, liabilities, damages, costs, and expenses (including reasonable attorneys’ fees) arising out of or accruing from (a) your breach of this Developer License; (b) your use or misuse of the Genesis Software; (c) your negligence or willful misconduct; and/or (d) your use of the Genesis Software that actually or allegedly infringes any Intellectual Property Rights or privacy rights of any third party in any jurisdiction.
38
-
39
- 8. MISCELLANEOUS.
40
- (a) If you are agreeing to be bound by this Agreement on behalf of your employer or other entity, you represent and warrant that you have full legal authority to bind your employer or such entity to this Agreement. If you do not have the requisite authority, you may not accept this Agreement on behalf of your employer or other entity.
41
- (b) This Developer License shall be governed by, and construed in accordance with, the laws of the State of New York without regard to conflict of law principles. For all purposes of this Developer License, the parties’ consent to exclusive jurisdiction and venue in the state and federal courts located in New York County, New York.
42
- (c) Nothing in this Agreement, or in the nature of your access and use of the Genesis Software, shall create or is intended to create a joint-venture, partnership, or agency relationship between you and Genesis.
43
- (d) You may not assign or transfer this Agreement or any of your rights or obligations hereunder, absent the prior written consent of Genesis.
44
- (e) You agree that if Genesis does not exercise or enforce any legal right or remedy contained in this Agreement, this will not be taken to be a formal waiver of Genesis’s rights and that those rights or remedies will still be available to Genesis.
45
- (f) This Agreement constitutes the complete understanding between you and Genesis with respect to the subject matter contained herein, and supersedes all prior written or oral communications, understandings, and agreements.
46
- (g) If any provision of this Agreement is deemed to be illegal, invalid, or unenforceable, then that provision shall be removed, and the remaining terms will continue to be valid and enforceable.
@@ -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
- });
@@ -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
- });
@@ -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
- });