@genesislcap/mock-server 15.68.0 → 15.69.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.
@@ -1,10 +1,13 @@
1
1
  // The Genesis platform a GSF 8.15.x app runs beside its own processes: auth
2
- // (users, profiles, rights, the key-value store), notify's alerts, and the
3
- // RESOURCES every platform PBC registers. Data comes from ./platform/*.json,
4
- // which scripts/platform-preset/generate.ts builds from real recordings and the
5
- // PBCs' own seed files; behaviour and texts cite the PBC scripts at the
6
- // versions the showcase server pins (server/gradle.properties: GSF 8.15.29,
7
- // auth 8.15.5, genesis-notify 8.15.16).
2
+ // (users, profiles, rights, the key-value store), notify's alerts, the
3
+ // RESOURCES every platform product registers, and the tables, queries and
4
+ // request servers its products' screens read (./products.ts). Data comes from
5
+ // ./platform/*.json, which scripts/platform-preset/generate.ts builds from
6
+ // real recordings and the products' own dictionaries, scripts and seed files;
7
+ // behaviour and texts cite the product scripts at the versions the showcase
8
+ // server pins (server/gradle.properties: GSF 8.15.29, auth 8.15.5,
9
+ // genesis-notify 8.15.16, reporting 8.15.3, file-server 8.15.9,
10
+ // reconciliation 8.15.4).
8
11
  import { NackError } from '../protocol/errors.ts';
9
12
  import type {
10
13
  AuthUser,
@@ -12,6 +15,9 @@ import type {
12
15
  EventHandlerCtx,
13
16
  FieldDef,
14
17
  MockServerConfig,
18
+ QueryDef,
19
+ RequestingUser,
20
+ RequestReplyDef,
15
21
  ResourceItem,
16
22
  Row,
17
23
  TableDef,
@@ -19,8 +25,14 @@ import type {
19
25
  import defaultUser from './platform/default-user.json' with { type: 'json' };
20
26
  import platformProfileRights from './platform/profile-rights.json' with { type: 'json' };
21
27
  import platformProfiles from './platform/profiles.json' with { type: 'json' };
22
- import platformResources from './platform/resources.json' with { type: 'json' };
23
28
  import platformRights from './platform/rights.json' with { type: 'json' };
29
+ import {
30
+ includedProducts,
31
+ ofIncluded,
32
+ type PlatformProduct,
33
+ productBacking,
34
+ productResources,
35
+ } from './products.ts';
24
36
 
25
37
  export interface PlatformUser {
26
38
  username: string;
@@ -62,6 +74,10 @@ export interface PlatformPresetOptions {
62
74
  // session token on its WebSocket, so the mock can't tell who it is; the real
63
75
  // server knows from the session cookie. Default: the first user.
64
76
  sessionUser?: string;
77
+ // The platform products the app includes beside GSF and auth, which are
78
+ // always in: their resources are listed and backed, the others' aren't.
79
+ // Default: all of them.
80
+ products?: readonly PlatformProduct[];
65
81
  }
66
82
 
67
83
  const DEFAULT_PASSWORD = 'admin';
@@ -369,6 +385,11 @@ const LOGIN_USER_FIELDS = new Set(['FIRST_NAME', 'LAST_NAME', 'EMAIL_ADDRESS', '
369
385
  * README's "Platform preset".
370
386
  */
371
387
  export function platformPreset(options: PlatformPresetOptions = {}): MockServerConfig {
388
+ const included = includedProducts(options.products);
389
+ const notify = included.has('genesis-notify');
390
+ if (!notify && options.seed?.NOTIFY_ALERT?.length) {
391
+ throw new Error("seed.NOTIFY_ALERT needs the 'genesis-notify' product");
392
+ }
372
393
  const users = options.users ?? [DEFAULT_USER];
373
394
  const fallback = options.sessionUser ?? users[0]?.username;
374
395
  const seed: Required<PlatformSeed> = {
@@ -391,9 +412,16 @@ export function platformPreset(options: PlatformPresetOptions = {}): MockServerC
391
412
  user: user && pick(user, LOGIN_USER_FIELDS),
392
413
  }),
393
414
  );
415
+ // NOTIFY_ALERT is genesis-notify's; the rest are GSF's and auth's.
416
+ const { NOTIFY_ALERT, ...always } = platformTables(seed, users);
417
+ const tables = notify ? { ...always, NOTIFY_ALERT } : always;
418
+ // What the included products' screens read, generated from their scripts;
419
+ // the pieces written out here replace any of the same name.
420
+ const generated = productBacking(included, tables);
394
421
  return {
395
- tables: platformTables(seed, users),
422
+ tables: { ...generated.tables, ...tables },
396
423
  views: {
424
+ ...generated.views,
397
425
  // genesis-view-dictionary.kts:2 (GSF v8.15.29): USER and its
398
426
  // USER_ATTRIBUTES, without the password, RECORD_ID and TIMESTAMP columns.
399
427
  USER_VIEW: {
@@ -442,62 +470,92 @@ export function platformPreset(options: PlatformPresetOptions = {}): MockServerC
442
470
  fields: ['RIGHT_CODE', 'RIGHT_DESCRIPTION'],
443
471
  },
444
472
  },
445
- // auth-dataserver.kts (auth v8.15.5) and genesis-notify-rules-dataserver.kts
446
- // (genesis-notify v8.15.16).
473
+ // The preset's own, over the generated ones, for the included products.
447
474
  queries: {
448
- ALL_USERS: { source: 'USER_VIEW' },
449
- ALL_PROFILES: { source: 'PROFILE_PREFIXED' },
450
- ALL_RIGHTS: { source: 'RIGHT_PREFIXED' },
451
- ALL_PROFILE_RIGHTS: { source: 'PROFILE_RIGHT_VIEW' },
452
- ALL_PROFILE_USERS: { source: 'PROFILE_USER_VIEW' },
453
- // `permissioning { auth { filterWithUserName { userName == data.userName } } }`
454
- ALL_NOTIFY_ALERT_RECORDS: {
455
- source: 'NOTIFY_ALERT',
456
- auth: { rowFilter: (row, user) => row.USER_NAME === (user.userName ?? fallback) },
457
- },
475
+ ...generated.queries,
476
+ ...ofIncluded(ownQueries(fallback), 'DATASERVER', included),
458
477
  },
459
- // auth-reqrep.kts (auth v8.15.5).
460
478
  requestReplies: {
461
- // :57 request { CODE } reply { CODE DESCRIPTION }, so its rows carry no
462
- // RECORD_ID.
463
- RIGHT: { source: 'RIGHT', fields: ['CODE', 'DESCRIPTION'] },
464
- // :7 request { NAME withPrefix PROFILE } reply { ..., STATUS, DESCRIPTION }
465
- PROFILE: {
466
- source: 'PROFILE_PREFIXED',
467
- requestFields: ['PROFILE_NAME'],
468
- fields: ['PROFILE_NAME', 'STATUS', 'DESCRIPTION'],
469
- },
470
- // :212 request { USER_NAME } reply { KV_KEY withAlias "KEY" }
471
- KEY_VALUE_LIST: {
472
- requestFields: ['USER_NAME'],
473
- resolver: ({ request, store }) =>
474
- keyValues(store.getAllRows('KEY_VALUE'), request).map((row) => ({ KEY: row.KV_KEY })),
475
- metadataRows: [{ KEY: 'example' }],
476
- },
477
- // :232 request { USER_NAME, KV_KEY withAlias "KEY" }
478
- // reply { KV_KEY withAlias "KEY", KV_VALUE withAlias "VALUE" }
479
- KEY_VALUE_READ: {
480
- requestFields: ['USER_NAME', 'KEY'],
481
- resolver: ({ request, store }) =>
482
- keyValues(store.getAllRows('KEY_VALUE'), request).map((row) => ({
483
- KEY: row.KV_KEY,
484
- VALUE: rawValue(row.KV_VALUE),
485
- })),
486
- metadataRows: [{ KEY: 'example', VALUE: '' }],
487
- },
479
+ ...generated.requestReplies,
480
+ ...ofIncluded(ownRequestReplies(), 'REQUEST_SERVER', included),
488
481
  },
489
- eventHandlers: { ...keyValueHandlers(fallback), ...alertHandlers(fallback) },
482
+ eventHandlers: ofIncluded(
483
+ { ...keyValueHandlers(fallback), ...alertHandlers(fallback) },
484
+ 'EVENT_HANDLER',
485
+ included,
486
+ ),
490
487
  // The DETAILS classes of the alert events, which their names alone would
491
488
  // make NOTIFY_ALERT table events.
492
- eventSchemas: {
493
- EVENT_DISMISS_NOTIFY_ALERT: {
494
- table: 'NOTIFY_ALERT',
495
- pkOnly: true,
496
- className: 'global.genesis.gen.dao.NotifyAlert.ByAlertId',
489
+ eventSchemas: ofIncluded(
490
+ {
491
+ EVENT_DISMISS_NOTIFY_ALERT: {
492
+ table: 'NOTIFY_ALERT',
493
+ pkOnly: true,
494
+ className: 'global.genesis.gen.dao.NotifyAlert.ByAlertId',
495
+ },
496
+ EVENT_DISMISS_ALL_NOTIFY_ALERT: { fields: [], className: 'kotlin.Unit' },
497
497
  },
498
- EVENT_DISMISS_ALL_NOTIFY_ALERT: { fields: [], className: 'kotlin.Unit' },
499
- },
498
+ 'EVENT_HANDLER',
499
+ included,
500
+ ),
500
501
  auth: { users: authUsers },
501
- additionalResources: [...(platformResources as ResourceItem[]), ...(options.resources ?? [])],
502
+ additionalResources: [...productResources(included), ...(options.resources ?? [])],
503
+ };
504
+ }
505
+
506
+ // auth-dataserver.kts (auth v8.15.5) and genesis-notify-rules-dataserver.kts
507
+ // (genesis-notify v8.15.16): the ones the generated backing can't say, as
508
+ // they rename columns or filter the alerts by user.
509
+ function ownQueries(fallback: string | undefined): Record<string, QueryDef> {
510
+ const ownAlert = (row: Row, user: RequestingUser) =>
511
+ row.USER_NAME === (user.userName ?? fallback);
512
+ return {
513
+ ALL_USERS: { source: 'USER_VIEW' },
514
+ ALL_PROFILES: { source: 'PROFILE_PREFIXED' },
515
+ ALL_RIGHTS: { source: 'RIGHT_PREFIXED' },
516
+ ALL_PROFILE_RIGHTS: { source: 'PROFILE_RIGHT_VIEW' },
517
+ ALL_PROFILE_USERS: { source: 'PROFILE_USER_VIEW' },
518
+ // `permissioning { auth { filterWithUserName { userName == data.userName } } }`
519
+ ALL_NOTIFY_ALERT_RECORDS: { source: 'NOTIFY_ALERT', auth: { rowFilter: ownAlert } },
520
+ // :11, the same with `filter { data.alertStatus == AlertStatus.NEW &&
521
+ // data.showToast }`.
522
+ ALL_TOAST_NOTIFICATIONS: {
523
+ source: 'NOTIFY_ALERT',
524
+ filter: (row) => row.ALERT_STATUS === 'NEW' && row.SHOW_TOAST === true,
525
+ auth: { rowFilter: ownAlert },
526
+ },
527
+ };
528
+ }
529
+
530
+ // auth-reqrep.kts (auth v8.15.5): the ones that rename columns.
531
+ function ownRequestReplies(): Record<string, RequestReplyDef> {
532
+ return {
533
+ // :57 request { CODE } reply { CODE DESCRIPTION }, so its rows carry no
534
+ // RECORD_ID.
535
+ RIGHT: { source: 'RIGHT', fields: ['CODE', 'DESCRIPTION'] },
536
+ // :7 request { NAME withPrefix PROFILE } reply { ..., STATUS, DESCRIPTION }
537
+ PROFILE: {
538
+ source: 'PROFILE_PREFIXED',
539
+ requestFields: ['PROFILE_NAME'],
540
+ fields: ['PROFILE_NAME', 'STATUS', 'DESCRIPTION'],
541
+ },
542
+ // :212 request { USER_NAME } reply { KV_KEY withAlias "KEY" }
543
+ KEY_VALUE_LIST: {
544
+ requestFields: ['USER_NAME'],
545
+ resolver: ({ request, store }) =>
546
+ keyValues(store.getAllRows('KEY_VALUE'), request).map((row) => ({ KEY: row.KV_KEY })),
547
+ metadataRows: [{ KEY: 'example' }],
548
+ },
549
+ // :232 request { USER_NAME, KV_KEY withAlias "KEY" }
550
+ // reply { KV_KEY withAlias "KEY", KV_VALUE withAlias "VALUE" }
551
+ KEY_VALUE_READ: {
552
+ requestFields: ['USER_NAME', 'KEY'],
553
+ resolver: ({ request, store }) =>
554
+ keyValues(store.getAllRows('KEY_VALUE'), request).map((row) => ({
555
+ KEY: row.KV_KEY,
556
+ VALUE: rawValue(row.KV_VALUE),
557
+ })),
558
+ metadataRows: [{ KEY: 'example', VALUE: '' }],
559
+ },
502
560
  };
503
561
  }
@@ -0,0 +1,180 @@
1
+ // The platform's products: which one registers each platform resource, and
2
+ // what backs the resources that read a table or view. Both are generated
3
+ // (scripts/platform-preset/generate.ts) from each product's dictionaries and
4
+ // scripts at the versions the showcase server pins: GSF 8.15.29 ('genesis'),
5
+ // auth 8.15.5, genesis-notify 8.15.16, reporting 8.15.3, file-server 8.15.9
6
+ // and reconciliation 8.15.4.
7
+ import type {
8
+ MockServerConfig,
9
+ QueryDef,
10
+ RequestReplyDef,
11
+ ResourceItem,
12
+ TableDef,
13
+ ViewDef,
14
+ } from '../types.ts';
15
+ import platformBacking from './platform/backing.json' with { type: 'json' };
16
+ import platformResources from './platform/resources.json' with { type: 'json' };
17
+
18
+ /**
19
+ * A platform product an app may include beside GSF and auth, by its GSF
20
+ * product name: Create's Notifications, Reporting, Document Management and
21
+ * Reconciliation.
22
+ */
23
+ export type PlatformProduct = 'genesis-notify' | 'reporting' | 'file-server' | 'reconciliation';
24
+
25
+ export const PLATFORM_PRODUCTS: readonly PlatformProduct[] = [
26
+ 'genesis-notify',
27
+ 'reporting',
28
+ 'file-server',
29
+ 'reconciliation',
30
+ ];
31
+
32
+ // In every app.
33
+ const ALWAYS = ['genesis', 'auth'];
34
+
35
+ // backing.json's entries: the engine's definitions, plus where they come from.
36
+ interface Provenance {
37
+ product: string;
38
+ // The script and line that declares it.
39
+ script: string;
40
+ // The tables it reads, through its view if it has one.
41
+ reads: string[];
42
+ // Script blocks the mock doesn't model (row filters, permissioning, a
43
+ // typed request server's Kotlin reply).
44
+ unmodelled?: string[];
45
+ }
46
+
47
+ interface Backing {
48
+ tables: Record<string, TableDef & { dictionary: string }>;
49
+ views: Record<
50
+ string,
51
+ Omit<ViewDef, 'derived' | 'derivedTypes'> & {
52
+ dictionary: string;
53
+ derivedTypes?: Record<string, string>;
54
+ }
55
+ >;
56
+ queries: Record<string, QueryDef & Provenance>;
57
+ requestReplies: Record<string, RequestReplyDef & Provenance>;
58
+ }
59
+
60
+ const backing = platformBacking as unknown as Backing;
61
+
62
+ type AttributedResource = ResourceItem & { PRODUCT: string };
63
+ const attributed = platformResources as AttributedResource[];
64
+ const productByKey = new Map(
65
+ attributed.map((resource) => [
66
+ `${resource.RESOURCE_TYPE}:${resource.RESOURCE_NAME}`,
67
+ resource.PRODUCT,
68
+ ]),
69
+ );
70
+
71
+ // The products a preset serves: GSF, auth and the chosen ones (all of them
72
+ // when none are given).
73
+ export function includedProducts(products?: readonly string[]): Set<string> {
74
+ for (const product of products ?? []) {
75
+ if (!PLATFORM_PRODUCTS.includes(product as PlatformProduct)) {
76
+ throw new Error(
77
+ `Unknown platform product '${product}' (one of ${PLATFORM_PRODUCTS.join(', ')}; GSF and auth are always in)`,
78
+ );
79
+ }
80
+ }
81
+ return new Set([...ALWAYS, ...(products ?? PLATFORM_PRODUCTS)]);
82
+ }
83
+
84
+ // The product that registers a platform resource, if it is one.
85
+ export function productOf(type: ResourceItem['RESOURCE_TYPE'], name: string): string | undefined {
86
+ return productByKey.get(`${type}:${name}`);
87
+ }
88
+
89
+ // The platform resources the included products register, as the real server
90
+ // lists them.
91
+ export function productResources(included: Set<string>): ResourceItem[] {
92
+ return attributed
93
+ .filter((resource) => included.has(resource.PRODUCT))
94
+ .map(({ RESOURCE_NAME, RESOURCE_TYPE }) => ({ RESOURCE_NAME, RESOURCE_TYPE }));
95
+ }
96
+
97
+ // Keeps the entries of a preset's own record whose resource an included
98
+ // product registers (or that isn't a platform resource at all).
99
+ export function ofIncluded<T>(
100
+ record: Record<string, T>,
101
+ type: ResourceItem['RESOURCE_TYPE'],
102
+ included: Set<string>,
103
+ ): Record<string, T> {
104
+ return Object.fromEntries(
105
+ Object.entries(record).filter(([name]) => {
106
+ const product = productOf(type, name);
107
+ return product === undefined || included.has(product);
108
+ }),
109
+ );
110
+ }
111
+
112
+ // A derived field's value is the product's Kotlin, which isn't modelled: the
113
+ // column is declared, and null. Its tables start empty, so no row shows it.
114
+ const unmodelledValue = () => null;
115
+
116
+ /**
117
+ * The generated backing for the included products, a fresh copy each call:
118
+ * their queries and request servers over a table or view, those tables
119
+ * (empty, or holding the product's own seed rows) and views. GSF's and auth's are served only over
120
+ * the tables `presetTables` fills (users, profiles, rights): the rest of
121
+ * theirs (audits, approvals, cron rules, RIGHT_SUMMARY) are filled at runtime
122
+ * by platform processes the mock doesn't run, so an empty table would be a
123
+ * wrong answer rather than a fresh one. A table in `presetTables` is the
124
+ * caller's, and isn't repeated here.
125
+ */
126
+ export function productBacking(
127
+ included: Set<string>,
128
+ presetTables: Record<string, TableDef>,
129
+ ): Pick<MockServerConfig, 'tables' | 'views' | 'queries' | 'requestReplies'> {
130
+ const tables = new Set<string>();
131
+ const views = new Set<string>();
132
+ const pick = <T extends Provenance>(entries: Record<string, T>) =>
133
+ Object.fromEntries(
134
+ Object.entries(entries).flatMap(([name, entry]) => {
135
+ const {
136
+ product,
137
+ script: _script,
138
+ reads,
139
+ unmodelled: _unmodelled,
140
+ ...definition
141
+ } = structuredClone(entry);
142
+ if (!included.has(product)) return [];
143
+ if (ALWAYS.includes(product) && !reads.every((table) => table in presetTables)) return [];
144
+ for (const table of reads) tables.add(table);
145
+ const { source } = definition as { source?: string };
146
+ if (source && source in backing.views) views.add(source);
147
+ return [[name, definition]];
148
+ }),
149
+ );
150
+ const queries = pick(backing.queries) as Record<string, QueryDef>;
151
+ const requestReplies = pick(backing.requestReplies) as Record<string, RequestReplyDef>;
152
+ return {
153
+ tables: Object.fromEntries(
154
+ [...tables]
155
+ .filter((name) => !(name in presetTables))
156
+ .map((name) => {
157
+ const { dictionary: _dictionary, ...table } = structuredClone(backing.tables[name]);
158
+ return [name, table];
159
+ }),
160
+ ),
161
+ views: Object.fromEntries(
162
+ [...views].map((name) => {
163
+ const {
164
+ dictionary: _dictionary,
165
+ derivedTypes,
166
+ ...view
167
+ } = structuredClone(backing.views[name]);
168
+ const derived = derivedTypes && {
169
+ derivedTypes: derivedTypes as ViewDef['derivedTypes'],
170
+ derived: Object.fromEntries(
171
+ Object.keys(derivedTypes).map((field) => [field, unmodelledValue]),
172
+ ),
173
+ };
174
+ return [name, { ...view, ...derived }];
175
+ }),
176
+ ),
177
+ queries,
178
+ requestReplies,
179
+ };
180
+ }