@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.9 → 3.9.3-ultramodern.10

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 (91) hide show
  1. package/README.md +3 -1
  2. package/dist/cjs/backend-federation-security/index.js +73 -152
  3. package/dist/cjs/bff-policy/index.js +7 -0
  4. package/dist/cjs/bff-policy/node.js +20 -4
  5. package/dist/cjs/bff-policy/operationContracts.js +150 -135
  6. package/dist/cjs/bff-policy/operationIdentity.js +81 -0
  7. package/dist/cjs/bff-policy/producer.js +79 -0
  8. package/dist/cjs/localisedLoader.js +65 -0
  9. package/dist/cjs/localisedLoaderPlugin.js +2 -2
  10. package/dist/cjs/module-federation-css/remote.js +2 -1
  11. package/dist/cjs/static-serving/staticModuleFederation.js +49 -12
  12. package/dist/esm/backend-federation-security/index.mjs +73 -152
  13. package/dist/esm/bff-policy/index.mjs +1 -0
  14. package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
  15. package/dist/esm/bff-policy/producer.mjs +31 -0
  16. package/dist/esm/localisedLoader.mjs +24 -0
  17. package/dist/esm/localisedLoaderPlugin.mjs +2 -2
  18. package/dist/esm/module-federation-css/remote.mjs +2 -1
  19. package/dist/esm-node/backend-federation-security/index.mjs +73 -152
  20. package/dist/esm-node/bff-policy/index.mjs +1 -0
  21. package/dist/esm-node/bff-policy/node.mjs +1 -0
  22. package/dist/esm-node/bff-policy/operationContracts.mjs +5 -27
  23. package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
  24. package/dist/esm-node/bff-policy/producer.mjs +32 -0
  25. package/dist/esm-node/localisedLoader.mjs +25 -0
  26. package/dist/esm-node/localisedLoaderPlugin.mjs +2 -2
  27. package/dist/esm-node/module-federation-css/remote.mjs +2 -1
  28. package/dist/esm-node/static-serving/staticModuleFederation.mjs +49 -12
  29. package/dist/types/backend-federation-security/node.d.ts +1 -1
  30. package/dist/types/bff-policy/index.d.ts +1 -0
  31. package/dist/types/bff-policy/node.d.ts +1 -0
  32. package/dist/types/bff-policy/operationContracts.d.ts +2 -41
  33. package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
  34. package/dist/types/bff-policy/producer.d.ts +10 -0
  35. package/dist/types/localisedLoader.d.ts +15 -0
  36. package/dist/types/module-federation-css/remote.d.ts +2 -0
  37. package/dist/types/serverConfig.d.ts +4 -2
  38. package/dist/types/telemetry/runtimeEndpoints.d.ts +2 -13
  39. package/dist/types-esm/backend-federation-security/node.d.mts +1 -1
  40. package/dist/types-esm/bff-policy/index.d.mts +1 -0
  41. package/dist/types-esm/bff-policy/node.d.mts +1 -0
  42. package/dist/types-esm/bff-policy/operationContracts.d.mts +2 -41
  43. package/dist/types-esm/bff-policy/operationIdentity.d.mts +34 -0
  44. package/dist/types-esm/bff-policy/producer.d.mts +10 -0
  45. package/dist/types-esm/localisedLoader.d.mts +15 -0
  46. package/dist/types-esm/module-federation-css/remote.d.mts +2 -0
  47. package/dist/types-esm/serverConfig.d.mts +4 -2
  48. package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +2 -13
  49. package/package.json +48 -20
  50. package/src/backend-federation-security/index.ts +151 -318
  51. package/src/backend-federation-security/node.ts +24 -22
  52. package/src/bff-policy/index.ts +1 -0
  53. package/src/bff-policy/node.ts +1 -0
  54. package/src/bff-policy/operationContracts.ts +13 -86
  55. package/src/bff-policy/operationIdentity.ts +90 -0
  56. package/src/bff-policy/producer.ts +40 -0
  57. package/src/contract-gate-snapshot-store/resolve.ts +0 -1
  58. package/src/localisedLoader.ts +50 -0
  59. package/src/localisedLoaderPlugin.ts +6 -7
  60. package/src/module-federation-css/remote.ts +3 -1
  61. package/src/serverConfig.ts +7 -0
  62. package/src/static-serving/staticModuleFederation.ts +127 -22
  63. package/src/telemetry/runtimeEndpoints.ts +2 -2
  64. package/rslib.config.mts +0 -38
  65. package/rstest.config.mts +0 -18
  66. package/tests/backendFederationSecurity.test.ts +0 -416
  67. package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +0 -450
  68. package/tests/bff-policy/crossProjectPolicy.test.ts +0 -197
  69. package/tests/bff-policy/operationContracts.test.ts +0 -221
  70. package/tests/bff-policy/optionalZodPeer.test.ts +0 -30
  71. package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +0 -76
  72. package/tests/contractGateSnapshotObserver.test.ts +0 -347
  73. package/tests/contractGateSnapshotStore.test.ts +0 -225
  74. package/tests/env.test.ts +0 -54
  75. package/tests/helpers.ts +0 -20
  76. package/tests/localisedLoader.test.ts +0 -120
  77. package/tests/mfCache.test.ts +0 -63
  78. package/tests/moduleFederationCss.test.ts +0 -348
  79. package/tests/nativeErrorResponse.test.ts +0 -118
  80. package/tests/nativeServerAssembly.test.ts +0 -385
  81. package/tests/runtimeLifecycle.test.ts +0 -159
  82. package/tests/static-serving/ordering.test.ts +0 -214
  83. package/tests/static-serving/static.test.ts +0 -411
  84. package/tests/telemetry.test.ts +0 -248
  85. package/tests/telemetryHealthMonitor.test.ts +0 -183
  86. package/tests/telemetryHealthObservation.test.ts +0 -302
  87. package/tests/telemetryLifecycle.test.ts +0 -123
  88. package/tests/telemetryRegistryQueueMatrix.test.ts +0 -130
  89. package/tests/telemetryTraceparent.test.ts +0 -167
  90. package/tests/tsconfig.json +0 -11
  91. package/tsconfig.json +0 -10
@@ -1,221 +0,0 @@
1
- import 'reflect-metadata';
2
- import { z } from 'zod';
3
- import {
4
- buildOperationContractMap,
5
- createOperationContractHash,
6
- createOperationEntries,
7
- DEFAULT_OPERATION_VERSION,
8
- deriveOperationVersion,
9
- type OperationContractSource,
10
- } from '../../src/bff-policy/operationContracts';
11
-
12
- const createSchemaHandler = (schema: z.ZodType) => {
13
- const handler = () => ({ ok: true });
14
- Reflect.defineMetadata('DATA', schema, handler);
15
- return handler;
16
- };
17
-
18
- describe('operation contract utilities', () => {
19
- test('creates deterministic operation entries and aggregate hashes', () => {
20
- const handlers = [
21
- {
22
- name: 'beta',
23
- httpMethod: 'post',
24
- routePath: '/api/beta',
25
- filename: '/api/module-a.ts',
26
- },
27
- {
28
- name: 'alpha',
29
- httpMethod: 'get',
30
- routePath: '/api/alpha',
31
- filename: '/api/module-a.ts',
32
- },
33
- ] satisfies OperationContractSource[];
34
-
35
- const entries = createOperationEntries(handlers);
36
- expect(entries.map(item => item.name)).toEqual(['alpha', 'beta']);
37
- });
38
-
39
- test('builds per-operation contracts keyed by route and operation id', () => {
40
- const handlers = [
41
- {
42
- name: 'getCustomer',
43
- httpMethod: 'get',
44
- routePath: '/api/customer',
45
- filename: '/api/crm.ts',
46
- },
47
- {
48
- name: 'listDeals',
49
- httpMethod: 'get',
50
- routePath: '/api/deals',
51
- filename: '/api/crm.ts',
52
- },
53
- {
54
- name: 'createInvoice',
55
- httpMethod: 'post',
56
- routePath: '/api/invoice',
57
- filename: '/api/billing.ts',
58
- },
59
- ] satisfies OperationContractSource[];
60
-
61
- const contracts = buildOperationContractMap({
62
- handlers,
63
- requestId: 'erp-producer',
64
- });
65
-
66
- expect(contracts['GET:/api/customer']?.requestId).toBe('erp-producer');
67
- expect(contracts['GET:/api/customer']?.operationVersion).toBe(1);
68
- expect(contracts['operation:erp-producer:getCustomer']?.method).toBe('GET');
69
- expect(contracts['POST:/api/invoice']?.schemaHash).toBeTruthy();
70
- expect(contracts['GET:/api/customer']?.schemaHash).not.toBe(
71
- contracts['POST:/api/invoice']?.schemaHash,
72
- );
73
- });
74
-
75
- test('schema hash changes when an operation input schema changes', () => {
76
- const baseRoute = {
77
- name: 'createCustomer',
78
- httpMethod: 'POST',
79
- routePath: '/api/customer',
80
- filename: '/api/crm.ts',
81
- };
82
-
83
- const contractsV1 = buildOperationContractMap({
84
- handlers: [
85
- {
86
- ...baseRoute,
87
- handler: createSchemaHandler(z.object({ name: z.string() })),
88
- },
89
- ],
90
- requestId: 'crm',
91
- });
92
- const contractsV2 = buildOperationContractMap({
93
- handlers: [
94
- {
95
- ...baseRoute,
96
- handler: createSchemaHandler(
97
- z.object({ name: z.string(), email: z.string() }),
98
- ),
99
- },
100
- ],
101
- requestId: 'crm',
102
- });
103
- const contractsV1Again = buildOperationContractMap({
104
- handlers: [
105
- {
106
- ...baseRoute,
107
- handler: createSchemaHandler(z.object({ name: z.string() })),
108
- },
109
- ],
110
- requestId: 'crm',
111
- });
112
-
113
- // changing the zod schema (the actual API contract) rotates the hash...
114
- expect(contractsV1['POST:/api/customer']!.schemaHash).not.toBe(
115
- contractsV2['POST:/api/customer']!.schemaHash,
116
- );
117
- // ...and the same schema produces the same hash.
118
- expect(contractsV1['POST:/api/customer']!.schemaHash).toBe(
119
- contractsV1Again['POST:/api/customer']!.schemaHash,
120
- );
121
- });
122
-
123
- test('per-operation hashes are stable across route reordering', () => {
124
- const handlerA = {
125
- name: 'alpha',
126
- httpMethod: 'GET',
127
- routePath: '/api/alpha',
128
- filename: '/api/module.ts',
129
- handler: createSchemaHandler(z.object({ q: z.string() })),
130
- };
131
- const handlerB = {
132
- name: 'beta',
133
- httpMethod: 'POST',
134
- routePath: '/api/beta',
135
- filename: '/api/module.ts',
136
- };
137
-
138
- const ordered = buildOperationContractMap({
139
- handlers: [handlerA, handlerB],
140
- requestId: 'crm',
141
- });
142
- const reversed = buildOperationContractMap({
143
- handlers: [handlerB, handlerA],
144
- requestId: 'crm',
145
- });
146
-
147
- expect(ordered['GET:/api/alpha']!.schemaHash).toBe(
148
- reversed['GET:/api/alpha']!.schemaHash,
149
- );
150
- expect(ordered['POST:/api/beta']!.schemaHash).toBe(
151
- reversed['POST:/api/beta']!.schemaHash,
152
- );
153
- });
154
-
155
- test('adding an unrelated operation does not rotate sibling hashes', () => {
156
- const existing = {
157
- name: 'getCustomer',
158
- httpMethod: 'GET',
159
- routePath: '/api/customer',
160
- filename: '/api/crm.ts',
161
- };
162
- const before = buildOperationContractMap({
163
- handlers: [existing],
164
- requestId: 'crm',
165
- });
166
- const after = buildOperationContractMap({
167
- handlers: [
168
- existing,
169
- {
170
- name: 'deleteCustomer',
171
- httpMethod: 'DELETE',
172
- routePath: '/api/customer/:id',
173
- filename: '/api/crm.ts',
174
- },
175
- ],
176
- requestId: 'crm',
177
- });
178
-
179
- // regression guard for the old per-module grouping, where adding any
180
- // endpoint to a lambda file rotated every sibling operation hash.
181
- expect(after['GET:/api/customer']!.schemaHash).toBe(
182
- before['GET:/api/customer']!.schemaHash,
183
- );
184
- });
185
-
186
- test('hashes are scoped by requestId', () => {
187
- const handler = {
188
- name: 'getCustomer',
189
- httpMethod: 'GET',
190
- routePath: '/api/customer',
191
- };
192
- const a = createOperationContractHash(handler, 'producer-a');
193
- const b = createOperationContractHash(handler, 'producer-b');
194
- expect(a).not.toBe(b);
195
- });
196
-
197
- test('buildOperationContractMap propagates the operation version', () => {
198
- const contracts = buildOperationContractMap({
199
- handlers: [
200
- {
201
- name: 'getCustomer',
202
- httpMethod: 'GET',
203
- routePath: '/api/customer',
204
- },
205
- ],
206
- requestId: 'crm',
207
- operationVersion: 4,
208
- });
209
- expect(contracts['GET:/api/customer']!.operationVersion).toBe(4);
210
- });
211
-
212
- test('deriveOperationVersion derives the semver major', () => {
213
- expect(deriveOperationVersion('2.3.1')).toBe(2);
214
- expect(deriveOperationVersion('v3.0.0')).toBe(3);
215
- expect(deriveOperationVersion('0.4.2')).toBe(0);
216
- expect(deriveOperationVersion('not-a-version')).toBe(
217
- DEFAULT_OPERATION_VERSION,
218
- );
219
- expect(deriveOperationVersion(undefined)).toBe(DEFAULT_OPERATION_VERSION);
220
- });
221
- });
@@ -1,30 +0,0 @@
1
- import path from 'node:path';
2
- import { build } from 'esbuild';
3
-
4
- describe('zod stays an optional peer', () => {
5
- test.each([
6
- 'src/bff-policy/index.ts',
7
- 'src/bff-policy/node.ts',
8
- ])('%s bundles without an eager zod dependency', async entry => {
9
- await expect(
10
- build({
11
- bundle: true,
12
- entryPoints: [path.resolve(__dirname, '../..', entry)],
13
- format: 'esm',
14
- packages: 'external',
15
- platform: 'node',
16
- plugins: [
17
- {
18
- name: 'reject-eager-zod',
19
- setup(buildApi) {
20
- buildApi.onResolve({ filter: /^zod$/ }, () => {
21
- throw new Error('optional zod peer entered the eager graph');
22
- });
23
- },
24
- },
25
- ],
26
- write: false,
27
- }),
28
- ).resolves.toBeDefined();
29
- });
30
- });
@@ -1,76 +0,0 @@
1
- import {
2
- buildOperationContractMap,
3
- type OperationContractSource,
4
- } from '../../src/bff-policy/operationContracts';
5
- import { resolveCrossProjectPolicy } from '../../src/bff-policy/resolveCrossProjectPolicy';
6
-
7
- const createHandlers = (): Array<
8
- OperationContractSource & { routeName: string }
9
- > => [
10
- {
11
- name: 'getHello',
12
- httpMethod: 'GET',
13
- routeName: '/hello',
14
- routePath: '/hello',
15
- filename: 'api/hello.ts',
16
- handler: () => 'hello',
17
- },
18
- ];
19
-
20
- describe('resolveCrossProjectPolicy', () => {
21
- test('returns undefined without policy config or cross-project marker', () => {
22
- expect(
23
- resolveCrossProjectPolicy({
24
- handlers: createHandlers(),
25
- requestId: 'crm',
26
- }),
27
- ).toBeUndefined();
28
- });
29
-
30
- test('keeps explicit user switches over derived defaults', () => {
31
- const resolved = resolveCrossProjectPolicy({
32
- crossProjectPolicy: {
33
- enabled: false,
34
- requireEnvelope: false,
35
- allowUnknownOperations: true,
36
- denyStatus: 451,
37
- },
38
- handlers: createHandlers(),
39
- isCrossProjectServer: true,
40
- });
41
-
42
- expect(resolved).toMatchObject({
43
- enabled: false,
44
- requireEnvelope: false,
45
- allowUnknownOperations: true,
46
- denyStatus: 451,
47
- });
48
- });
49
-
50
- test('generated contracts override user-provided entries for the same key', () => {
51
- const handlers = createHandlers();
52
- const generated = buildOperationContractMap({
53
- handlers,
54
- requestId: 'crm',
55
- });
56
- const resolved = resolveCrossProjectPolicy({
57
- crossProjectPolicy: {
58
- enabled: true,
59
- expectedOperationContracts: {
60
- 'GET:/hello': { schemaHash: 'user-pinned', operationVersion: 9 },
61
- 'POST:/extra': { schemaHash: 'kept', operationVersion: 2 },
62
- },
63
- },
64
- handlers,
65
- requestId: 'crm',
66
- });
67
-
68
- expect(resolved?.expectedOperationContracts['GET:/hello']).toEqual(
69
- generated['GET:/hello'],
70
- );
71
- expect(resolved?.expectedOperationContracts['POST:/extra']).toEqual({
72
- schemaHash: 'kept',
73
- operationVersion: 2,
74
- });
75
- });
76
- });
@@ -1,347 +0,0 @@
1
- import fs from 'fs';
2
- import os from 'os';
3
- import path from 'path';
4
- import type { GateSnapshot } from '../src/contract-gate-snapshot-store';
5
- import { ContractGateSnapshotObserver } from '../src/contractGateSnapshotObserver';
6
- import { TelemetryHealthMonitor, TelemetryRegistry } from '../src/telemetry';
7
-
8
- const makeTempDir = () =>
9
- fs.mkdtempSync(path.join(os.tmpdir(), 'modern-contract-gate-observer-'));
10
-
11
- describe('contract gate observer', () => {
12
- test('syncs gate snapshot and updates health observations', async () => {
13
- const dir = makeTempDir();
14
- const snapshotPath = path.join(dir, 'contract-gates.json');
15
-
16
- const registry = new TelemetryRegistry({
17
- service: 'svc',
18
- module: 'server',
19
- environment: 'test',
20
- flushIntervalMs: 60_000,
21
- });
22
- const monitor = new TelemetryHealthMonitor({
23
- registry,
24
- minConsecutiveFailedEvaluations: 1,
25
- });
26
- const observer = new ContractGateSnapshotObserver({
27
- monitor,
28
- gateSnapshotPath: snapshotPath,
29
- pollIntervalMs: 50,
30
- gateStaleAfterMs: 60_000,
31
- });
32
-
33
- try {
34
- fs.writeFileSync(
35
- snapshotPath,
36
- JSON.stringify(
37
- {
38
- schemaVersion: 1,
39
- updatedAt: Date.now(),
40
- gates: {
41
- 'release-candidate-contract-gates': {
42
- passed: true,
43
- updatedAt: Date.now(),
44
- },
45
- },
46
- },
47
- null,
48
- 2,
49
- ),
50
- );
51
-
52
- await observer.start();
53
- const healthyDecision = monitor.evaluate();
54
- expect(healthyDecision.failures).toHaveLength(0);
55
-
56
- fs.writeFileSync(
57
- snapshotPath,
58
- JSON.stringify(
59
- {
60
- schemaVersion: 1,
61
- updatedAt: Date.now(),
62
- gates: {
63
- 'release-candidate-contract-gates': {
64
- passed: false,
65
- reason: 'runtime compatibility drift',
66
- updatedAt: Date.now(),
67
- },
68
- },
69
- },
70
- null,
71
- 2,
72
- ),
73
- );
74
-
75
- await observer.syncOnce();
76
- const rollbackDecision = monitor.evaluate();
77
- expect(rollbackDecision.state).toBe('unhealthy');
78
- expect(
79
- rollbackDecision.failures.some(
80
- item =>
81
- item.reason === 'contract_gate_failed' &&
82
- item.gate === 'release-candidate-contract-gates',
83
- ),
84
- ).toBe(true);
85
- } finally {
86
- observer.stop();
87
- await registry.shutdown();
88
- fs.rmSync(dir, { recursive: true, force: true });
89
- }
90
- });
91
-
92
- test('marks unchanged gate snapshots stale after the stale window elapses', async () => {
93
- const dir = makeTempDir();
94
- const snapshotPath = path.join(dir, 'contract-gates.json');
95
- const originalDateNow = Date.now;
96
- let now = 1_000_000;
97
- Date.now = () => now;
98
-
99
- const registry = new TelemetryRegistry({
100
- service: 'svc',
101
- module: 'server',
102
- environment: 'test',
103
- flushIntervalMs: 60_000,
104
- });
105
- const monitor = new TelemetryHealthMonitor({
106
- registry,
107
- minConsecutiveFailedEvaluations: 1,
108
- });
109
- fs.writeFileSync(
110
- snapshotPath,
111
- JSON.stringify(
112
- {
113
- schemaVersion: 1,
114
- updatedAt: now,
115
- gates: {
116
- 'module-onboarding-certification-gates': {
117
- passed: true,
118
- updatedAt: now,
119
- },
120
- },
121
- },
122
- null,
123
- 2,
124
- ),
125
- );
126
-
127
- const observer = new ContractGateSnapshotObserver({
128
- monitor,
129
- gateSnapshotPath: snapshotPath,
130
- gateStaleAfterMs: 1_000,
131
- });
132
-
133
- try {
134
- await observer.syncOnce();
135
- expect(monitor.evaluate().failures).toHaveLength(0);
136
-
137
- now += 1_001;
138
- await observer.syncOnce();
139
-
140
- const decision = monitor.evaluate();
141
- expect(decision.state).toBe('unhealthy');
142
- expect(
143
- decision.failures.some(
144
- item =>
145
- item.reason === 'contract_gate_failed' &&
146
- item.gate === 'module-onboarding-certification-gates',
147
- ),
148
- ).toBe(true);
149
- } finally {
150
- Date.now = originalDateNow;
151
- observer.stop();
152
- await registry.shutdown();
153
- fs.rmSync(dir, { recursive: true, force: true });
154
- }
155
- });
156
-
157
- test('ignores older snapshot reads that resolve after newer syncs', async () => {
158
- const registry = new TelemetryRegistry({
159
- service: 'svc',
160
- module: 'server',
161
- environment: 'test',
162
- flushIntervalMs: 60_000,
163
- });
164
- const monitor = new TelemetryHealthMonitor({
165
- registry,
166
- minConsecutiveFailedEvaluations: 1,
167
- });
168
- const snapshotResolvers: Array<
169
- (snapshot: GateSnapshot | undefined) => void
170
- > = [];
171
- const observer = new ContractGateSnapshotObserver({
172
- monitor,
173
- gateSnapshotStore: {
174
- name: 'deferred',
175
- readSnapshot: () =>
176
- new Promise<GateSnapshot | undefined>(resolve => {
177
- snapshotResolvers.push(resolve);
178
- }),
179
- writeSnapshot: async () => {},
180
- },
181
- gateStaleAfterMs: 0,
182
- });
183
-
184
- try {
185
- const olderSync = observer.syncOnce();
186
- const newerSync = observer.syncOnce();
187
- expect(snapshotResolvers).toHaveLength(2);
188
-
189
- snapshotResolvers[1]?.({
190
- schemaVersion: 1,
191
- updatedAt: 2_000,
192
- gates: {
193
- 'module-onboarding-certification-gates': {
194
- passed: true,
195
- updatedAt: 2_000,
196
- },
197
- },
198
- });
199
- await newerSync;
200
- expect(monitor.evaluate().failures).toHaveLength(0);
201
-
202
- snapshotResolvers[0]?.({
203
- schemaVersion: 1,
204
- updatedAt: 1_000,
205
- gates: {
206
- 'module-onboarding-certification-gates': {
207
- passed: false,
208
- reason: 'older snapshot resolved late',
209
- updatedAt: 1_000,
210
- },
211
- },
212
- });
213
- await olderSync;
214
-
215
- expect(monitor.evaluate().failures).toHaveLength(0);
216
- } finally {
217
- observer.stop();
218
- await registry.shutdown();
219
- }
220
- });
221
-
222
- test('clears previously applied gate failures when snapshots omit them', async () => {
223
- const dir = makeTempDir();
224
- const snapshotPath = path.join(dir, 'contract-gates.json');
225
-
226
- const registry = new TelemetryRegistry({
227
- service: 'svc',
228
- module: 'server',
229
- environment: 'test',
230
- flushIntervalMs: 60_000,
231
- });
232
- const monitor = new TelemetryHealthMonitor({
233
- registry,
234
- minConsecutiveFailedEvaluations: 1,
235
- });
236
- const observer = new ContractGateSnapshotObserver({
237
- monitor,
238
- gateSnapshotPath: snapshotPath,
239
- gateStaleAfterMs: 60_000,
240
- });
241
-
242
- try {
243
- const now = Date.now();
244
- fs.writeFileSync(
245
- snapshotPath,
246
- JSON.stringify(
247
- {
248
- schemaVersion: 1,
249
- updatedAt: now,
250
- gates: {
251
- 'runtime-mf-fallback-health': {
252
- passed: false,
253
- reason: 'runtime_fallback:remote_load_failed',
254
- updatedAt: now,
255
- },
256
- },
257
- },
258
- null,
259
- 2,
260
- ),
261
- );
262
-
263
- await observer.syncOnce();
264
- expect(
265
- monitor
266
- .evaluate()
267
- .failures.some(
268
- item =>
269
- item.reason === 'contract_gate_failed' &&
270
- item.gate === 'runtime-mf-fallback-health',
271
- ),
272
- ).toBe(true);
273
-
274
- fs.writeFileSync(
275
- snapshotPath,
276
- JSON.stringify(
277
- {
278
- schemaVersion: 1,
279
- updatedAt: Date.now(),
280
- gates: {},
281
- },
282
- null,
283
- 2,
284
- ),
285
- );
286
-
287
- await observer.syncOnce();
288
- expect(monitor.evaluate().failures).toHaveLength(0);
289
- } finally {
290
- observer.stop();
291
- await registry.shutdown();
292
- fs.rmSync(dir, { recursive: true, force: true });
293
- }
294
- });
295
-
296
- test('observes recovered runtime fallback gates after expiry window', async () => {
297
- const dir = makeTempDir();
298
- const snapshotPath = path.join(dir, 'contract-gates.json');
299
-
300
- const registry = new TelemetryRegistry({
301
- service: 'svc',
302
- module: 'server',
303
- environment: 'test',
304
- flushIntervalMs: 60_000,
305
- });
306
- const monitor = new TelemetryHealthMonitor({
307
- registry,
308
- minConsecutiveFailedEvaluations: 1,
309
- });
310
- const now = Date.now();
311
- fs.writeFileSync(
312
- snapshotPath,
313
- JSON.stringify(
314
- {
315
- schemaVersion: 1,
316
- updatedAt: now,
317
- gates: {
318
- 'runtime-mf-fallback-health': {
319
- passed: false,
320
- reason: 'runtime_fallback:remote_load_failed',
321
- updatedAt: now - 1_000,
322
- expiresAt: now - 100,
323
- },
324
- },
325
- },
326
- null,
327
- 2,
328
- ),
329
- );
330
-
331
- const observer = new ContractGateSnapshotObserver({
332
- monitor,
333
- gateSnapshotPath: snapshotPath,
334
- gateStaleAfterMs: 60_000,
335
- });
336
-
337
- try {
338
- await observer.syncOnce();
339
- const decision = monitor.evaluate();
340
- expect(decision.failures).toHaveLength(0);
341
- } finally {
342
- observer.stop();
343
- await registry.shutdown();
344
- fs.rmSync(dir, { recursive: true, force: true });
345
- }
346
- });
347
- });