@htilabs/platform-common-models 0.2.0-beta.4 → 0.3.0-beta.5
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 +38 -0
- package/dist/constants/index.cjs +0 -843
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +146 -5
- package/dist/constants/index.d.ts +146 -5
- package/dist/constants/index.js +0 -786
- package/dist/constants/index.js.map +1 -1
- package/dist/{platform-role-AHv1l5Og.d.cts → cvcEntry-wqktWfzE.d.cts} +231 -37
- package/dist/{platform-role-AHv1l5Og.d.ts → cvcEntry-wqktWfzE.d.ts} +231 -37
- package/dist/cvcs/index.cjs +2184 -0
- package/dist/cvcs/index.cjs.map +1 -0
- package/dist/cvcs/index.d.cts +2384 -0
- package/dist/cvcs/index.d.ts +2384 -0
- package/dist/cvcs/index.js +2154 -0
- package/dist/cvcs/index.js.map +1 -0
- package/dist/index--NCIPluF.d.cts +378 -0
- package/dist/index-CFOrHA8l.d.ts +378 -0
- package/dist/index.cjs +2962 -843
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +217 -3
- package/dist/index.d.ts +217 -3
- package/dist/index.js +2934 -786
- package/dist/index.js.map +1 -1
- package/dist/models/index.cjs +211 -0
- package/dist/models/index.cjs.map +1 -1
- package/dist/models/index.d.cts +3 -292
- package/dist/models/index.d.ts +3 -292
- package/dist/models/index.js +209 -0
- package/dist/models/index.js.map +1 -1
- package/dist/{platform-auth-BTbQIY3b.d.cts → platform-auth-C-G2qeGM.d.cts} +1 -297
- package/dist/{platform-auth-BTbQIY3b.d.ts → platform-auth-C-G2qeGM.d.ts} +1 -297
- package/dist/schemas/index.cjs +195 -729
- package/dist/schemas/index.cjs.map +1 -1
- package/dist/schemas/index.d.cts +11 -11
- package/dist/schemas/index.d.ts +11 -11
- package/dist/schemas/index.js +189 -729
- package/dist/schemas/index.js.map +1 -1
- package/dist/services/index.cjs +1442 -0
- package/dist/services/index.cjs.map +1 -0
- package/dist/services/index.d.cts +305 -0
- package/dist/services/index.d.ts +305 -0
- package/dist/services/index.js +1417 -0
- package/dist/services/index.js.map +1 -0
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/validation/index.cjs +261 -0
- package/dist/validation/index.cjs.map +1 -0
- package/dist/validation/index.d.cts +251 -0
- package/dist/validation/index.d.ts +251 -0
- package/dist/validation/index.js +228 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +36 -12
- package/src/__tests__/__snapshots__/cvc-sot.snap +22 -0
- package/src/__tests__/cvc-sot-snapshot.test.ts +184 -0
- package/src/__tests__/cvc-structural.test.ts +130 -0
- package/src/constants/ic.ts +0 -925
- package/src/constants/service-catalog.ts +146 -4
- package/src/cvcs/index.ts +31 -0
- package/src/cvcs/missingness.v1.0.0.json +50 -0
- package/src/cvcs/serviceCategories.v1.0.0.json +1947 -0
- package/src/cvcs/serviceMechanisms.v1.0.0.json +47 -0
- package/src/cvcs/serviceStates.v1.0.0.json +106 -0
- package/src/generated/cvc-enums.ts +215 -0
- package/src/index.ts +61 -1
- package/src/models/cvcEntryModel.ts +32 -0
- package/src/models/cvcRegistryModel.ts +35 -0
- package/src/models/index.ts +8 -0
- package/src/schemas/cvcEntry.ts +158 -0
- package/src/schemas/cvcRegistry.ts +149 -0
- package/src/schemas/index.ts +16 -0
- package/src/services/__tests__/cvcRegistryService.test.ts +614 -0
- package/src/services/cvcRegistryService.ts +729 -0
- package/src/services/index.ts +21 -0
- package/src/validation/cvc.ts +414 -0
- package/src/validation/index.ts +15 -0
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @htilabs/platform-common-models — CvcRegistryService tests
|
|
3
|
+
*
|
|
4
|
+
* These tests cover every scenario listed in the co-am3.3 bead:
|
|
5
|
+
* - `resolveEntries` blacklist with cascading descendants
|
|
6
|
+
* - `resolveEntries` whitelist + override (inclusion wins over exclusion)
|
|
7
|
+
* - `basedOn` inheritance (child overrides on top of parent)
|
|
8
|
+
* - Scope fallback (agency → app → platform-default)
|
|
9
|
+
* - Cache invalidation on `activate()` / `deprecate()`
|
|
10
|
+
* - Lifecycle guards (`activate(draft)` ok, `activate(active)` fails, etc.)
|
|
11
|
+
*
|
|
12
|
+
* Two test surfaces:
|
|
13
|
+
*
|
|
14
|
+
* 1. **Pure helpers** (`cascadeExclusions`, `applyEntryOverrides`,
|
|
15
|
+
* `resolveRegistryWithInheritance`) are tested with in-memory
|
|
16
|
+
* fixtures and injected loaders — no Mongoose required.
|
|
17
|
+
*
|
|
18
|
+
* 2. **Class methods** (`activate`, `deprecate`, `getActiveRegistry`,
|
|
19
|
+
* cache invariants) are tested against a tiny in-memory fake
|
|
20
|
+
* Mongoose connection — see `createFakeConnection` below. The fake
|
|
21
|
+
* implements only the surface of the Model API that the service
|
|
22
|
+
* actually uses, and it plays the role of `mongodb-memory-server`
|
|
23
|
+
* without the binary download.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
27
|
+
import { Types } from "mongoose";
|
|
28
|
+
import type { Connection } from "mongoose";
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
CvcRegistryService,
|
|
32
|
+
cascadeExclusions,
|
|
33
|
+
applyEntryOverrides,
|
|
34
|
+
resolveRegistryWithInheritance,
|
|
35
|
+
type ICvcEntry,
|
|
36
|
+
type ICvcRegistry,
|
|
37
|
+
} from "../cvcRegistryService.js";
|
|
38
|
+
|
|
39
|
+
// ─── Fixtures ────────────────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Minimal CVC entry factory for tests. Defaults are valid for the
|
|
43
|
+
* 7-segment positional code format and the schema's required fields, so
|
|
44
|
+
* each test only specifies the bits that matter to it.
|
|
45
|
+
*/
|
|
46
|
+
function makeEntry(overrides: Partial<ICvcEntry> & Pick<ICvcEntry, "code">): ICvcEntry {
|
|
47
|
+
// Spread overrides last so callers can override every default — including
|
|
48
|
+
// domain/version when staging multi-domain fixtures.
|
|
49
|
+
return {
|
|
50
|
+
domain: "service_categories",
|
|
51
|
+
version: "1.0.0",
|
|
52
|
+
key: overrides.code.replace(/-/g, "_"),
|
|
53
|
+
title: `Entry ${overrides.code}`,
|
|
54
|
+
selectable: true,
|
|
55
|
+
parentCode: null,
|
|
56
|
+
childCodes: [],
|
|
57
|
+
definition: null,
|
|
58
|
+
...overrides,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A small two-level service_categories tree:
|
|
64
|
+
*
|
|
65
|
+
* 01-00-00-00-00-00-00 (root, not selectable)
|
|
66
|
+
* 01-01-00-00-00-00-00 (selectable)
|
|
67
|
+
* 01-02-00-00-00-00-00 (selectable)
|
|
68
|
+
* 01-02-01-00-00-00-00 (selectable, descendant of 01-02)
|
|
69
|
+
* 02-00-00-00-00-00-00 (separate root, selectable)
|
|
70
|
+
*/
|
|
71
|
+
function buildSampleTree(): ICvcEntry[] {
|
|
72
|
+
return [
|
|
73
|
+
makeEntry({
|
|
74
|
+
code: "01-00-00-00-00-00-00",
|
|
75
|
+
selectable: false,
|
|
76
|
+
childCodes: ["01-01-00-00-00-00-00", "01-02-00-00-00-00-00"],
|
|
77
|
+
}),
|
|
78
|
+
makeEntry({
|
|
79
|
+
code: "01-01-00-00-00-00-00",
|
|
80
|
+
parentCode: "01-00-00-00-00-00-00",
|
|
81
|
+
}),
|
|
82
|
+
makeEntry({
|
|
83
|
+
code: "01-02-00-00-00-00-00",
|
|
84
|
+
parentCode: "01-00-00-00-00-00-00",
|
|
85
|
+
childCodes: ["01-02-01-00-00-00-00"],
|
|
86
|
+
}),
|
|
87
|
+
makeEntry({
|
|
88
|
+
code: "01-02-01-00-00-00-00",
|
|
89
|
+
parentCode: "01-02-00-00-00-00-00",
|
|
90
|
+
}),
|
|
91
|
+
makeEntry({ code: "02-00-00-00-00-00-00" }),
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function makeRegistry(overrides: Partial<ICvcRegistry> = {}): ICvcRegistry {
|
|
96
|
+
return {
|
|
97
|
+
_id: new Types.ObjectId(),
|
|
98
|
+
version: "1.0.0",
|
|
99
|
+
name: "test registry",
|
|
100
|
+
application: null,
|
|
101
|
+
agencyId: null,
|
|
102
|
+
status: "draft",
|
|
103
|
+
cvcVersions: { service_categories: "1.0.0" },
|
|
104
|
+
excludedEntries: [],
|
|
105
|
+
includedEntries: [],
|
|
106
|
+
basedOn: null,
|
|
107
|
+
...overrides,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─── Pure helper tests ───────────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
describe("cascadeExclusions", () => {
|
|
114
|
+
it("returns just the input set when no codes have children", () => {
|
|
115
|
+
const entries = buildSampleTree();
|
|
116
|
+
const result = cascadeExclusions(new Set(["01-01-00-00-00-00-00"]), entries);
|
|
117
|
+
expect([...result]).toEqual(["01-01-00-00-00-00-00"]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("cascades through descendants of an excluded parent", () => {
|
|
121
|
+
const entries = buildSampleTree();
|
|
122
|
+
// Excluding 01-02 should also pick up 01-02-01.
|
|
123
|
+
const result = cascadeExclusions(new Set(["01-02-00-00-00-00-00"]), entries);
|
|
124
|
+
expect(result.has("01-02-00-00-00-00-00")).toBe(true);
|
|
125
|
+
expect(result.has("01-02-01-00-00-00-00")).toBe(true);
|
|
126
|
+
expect(result.has("01-01-00-00-00-00-00")).toBe(false);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("cascades through multi-level subtrees from a top-level exclusion", () => {
|
|
130
|
+
const entries = buildSampleTree();
|
|
131
|
+
// Excluding the root 01-00 should sweep the whole 01-* subtree.
|
|
132
|
+
const result = cascadeExclusions(new Set(["01-00-00-00-00-00-00"]), entries);
|
|
133
|
+
expect(result.size).toBe(4);
|
|
134
|
+
expect(result.has("02-00-00-00-00-00-00")).toBe(false);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("applyEntryOverrides", () => {
|
|
139
|
+
it("returns the full pool in default mode (no overrides)", () => {
|
|
140
|
+
const entries = buildSampleTree();
|
|
141
|
+
const result = applyEntryOverrides([], [], entries);
|
|
142
|
+
expect(result).toHaveLength(entries.length);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("removes excluded codes and their descendants in blacklist mode", () => {
|
|
146
|
+
const entries = buildSampleTree();
|
|
147
|
+
const result = applyEntryOverrides(
|
|
148
|
+
["01-02-00-00-00-00-00"],
|
|
149
|
+
[],
|
|
150
|
+
entries,
|
|
151
|
+
);
|
|
152
|
+
const codes = result.map((e) => e.code);
|
|
153
|
+
expect(codes).not.toContain("01-02-00-00-00-00-00");
|
|
154
|
+
expect(codes).not.toContain("01-02-01-00-00-00-00"); // descendant cascade
|
|
155
|
+
expect(codes).toContain("01-01-00-00-00-00-00");
|
|
156
|
+
expect(codes).toContain("02-00-00-00-00-00-00");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("keeps only listed codes in whitelist mode", () => {
|
|
160
|
+
const entries = buildSampleTree();
|
|
161
|
+
const result = applyEntryOverrides(
|
|
162
|
+
[],
|
|
163
|
+
["01-01-00-00-00-00-00", "02-00-00-00-00-00-00"],
|
|
164
|
+
entries,
|
|
165
|
+
);
|
|
166
|
+
const codes = result.map((e) => e.code);
|
|
167
|
+
expect(codes).toEqual([
|
|
168
|
+
"01-01-00-00-00-00-00",
|
|
169
|
+
"02-00-00-00-00-00-00",
|
|
170
|
+
]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("force-includes whitelisted codes even when their parent is excluded", () => {
|
|
174
|
+
const entries = buildSampleTree();
|
|
175
|
+
// Exclude the entire 01-* subtree, then pull 01-02-01 back in.
|
|
176
|
+
const result = applyEntryOverrides(
|
|
177
|
+
["01-00-00-00-00-00-00"],
|
|
178
|
+
["01-02-01-00-00-00-00"],
|
|
179
|
+
entries,
|
|
180
|
+
);
|
|
181
|
+
const codes = result.map((e) => e.code);
|
|
182
|
+
expect(codes).toContain("01-02-01-00-00-00-00");
|
|
183
|
+
expect(codes).not.toContain("01-00-00-00-00-00-00");
|
|
184
|
+
expect(codes).not.toContain("01-01-00-00-00-00-00");
|
|
185
|
+
expect(codes).not.toContain("01-02-00-00-00-00-00");
|
|
186
|
+
expect(codes).toContain("02-00-00-00-00-00-00");
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("resolveRegistryWithInheritance", () => {
|
|
191
|
+
it("resolves a standalone registry with no basedOn", async () => {
|
|
192
|
+
const registry = makeRegistry();
|
|
193
|
+
const entries = buildSampleTree();
|
|
194
|
+
const result = await resolveRegistryWithInheritance(
|
|
195
|
+
registry,
|
|
196
|
+
async () => null,
|
|
197
|
+
async (domain, version) =>
|
|
198
|
+
domain === "service_categories" && version === "1.0.0" ? entries : [],
|
|
199
|
+
);
|
|
200
|
+
expect(result.mergedVersions).toEqual({ service_categories: "1.0.0" });
|
|
201
|
+
expect(result.entries).toHaveLength(entries.length);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("merges parent versions under child versions, child wins per-domain", async () => {
|
|
205
|
+
// A ← B ← C: C's overrides apply at the immediate-parent step. The
|
|
206
|
+
// locked spec constrains chains to one inheritance level, so the
|
|
207
|
+
// service consults only the direct parent (B). A is in the topology
|
|
208
|
+
// but not visited — this test asserts both that B's pins are merged
|
|
209
|
+
// and that C's pins win when domains overlap.
|
|
210
|
+
const registryA = makeRegistry({
|
|
211
|
+
version: "1.0.0",
|
|
212
|
+
name: "A",
|
|
213
|
+
cvcVersions: {
|
|
214
|
+
service_categories: "0.9.0",
|
|
215
|
+
service_states: "1.0.0",
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
const registryB = makeRegistry({
|
|
219
|
+
version: "1.0.0",
|
|
220
|
+
name: "B",
|
|
221
|
+
basedOn: registryA._id,
|
|
222
|
+
// B overrides service_categories, inherits service_states from A.
|
|
223
|
+
// (In practice, the in-spec resolver only sees one level deep, so
|
|
224
|
+
// B's effective state is what we set here.)
|
|
225
|
+
cvcVersions: {
|
|
226
|
+
service_categories: "1.0.0",
|
|
227
|
+
service_states: "1.0.0",
|
|
228
|
+
service_mechanisms: "1.0.0",
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
const registryC = makeRegistry({
|
|
232
|
+
version: "1.0.0",
|
|
233
|
+
name: "C",
|
|
234
|
+
basedOn: registryB._id,
|
|
235
|
+
// C overrides service_categories again. Inherits B's other pins.
|
|
236
|
+
cvcVersions: {
|
|
237
|
+
service_categories: "1.1.0",
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const loadedRegistries = new Map<string, ICvcRegistry>([
|
|
242
|
+
[registryA._id.toString(), registryA],
|
|
243
|
+
[registryB._id.toString(), registryB],
|
|
244
|
+
[registryC._id.toString(), registryC],
|
|
245
|
+
]);
|
|
246
|
+
const loadedEntries = new Map<string, ICvcEntry[]>([
|
|
247
|
+
[
|
|
248
|
+
"service_categories@1.1.0",
|
|
249
|
+
[makeEntry({ code: "01-01-00-00-00-00-00", version: "1.1.0" })],
|
|
250
|
+
],
|
|
251
|
+
[
|
|
252
|
+
"service_states@1.0.0",
|
|
253
|
+
[
|
|
254
|
+
makeEntry({
|
|
255
|
+
code: "03-00-00-00-00-00-00",
|
|
256
|
+
domain: "service_states",
|
|
257
|
+
}),
|
|
258
|
+
],
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
"service_mechanisms@1.0.0",
|
|
262
|
+
[
|
|
263
|
+
makeEntry({
|
|
264
|
+
code: "04-00-00-00-00-00-00",
|
|
265
|
+
domain: "service_mechanisms",
|
|
266
|
+
}),
|
|
267
|
+
],
|
|
268
|
+
],
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
const result = await resolveRegistryWithInheritance(
|
|
272
|
+
registryC,
|
|
273
|
+
async (id) => loadedRegistries.get(id.toString()) ?? null,
|
|
274
|
+
async (domain, version) => loadedEntries.get(`${domain}@${version}`) ?? [],
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
// C's service_categories pin (1.1.0) wins; B's other pins are inherited.
|
|
278
|
+
expect(result.mergedVersions).toEqual({
|
|
279
|
+
service_categories: "1.1.0",
|
|
280
|
+
service_states: "1.0.0",
|
|
281
|
+
service_mechanisms: "1.0.0",
|
|
282
|
+
});
|
|
283
|
+
expect(result.entries).toHaveLength(3);
|
|
284
|
+
expect(result.entries.map((e) => e.code).sort()).toEqual([
|
|
285
|
+
"01-01-00-00-00-00-00",
|
|
286
|
+
"03-00-00-00-00-00-00",
|
|
287
|
+
"04-00-00-00-00-00-00",
|
|
288
|
+
]);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("throws when basedOn points to a missing registry", async () => {
|
|
292
|
+
const ghostId = new Types.ObjectId();
|
|
293
|
+
const registry = makeRegistry({ basedOn: ghostId });
|
|
294
|
+
await expect(
|
|
295
|
+
resolveRegistryWithInheritance(
|
|
296
|
+
registry,
|
|
297
|
+
async () => null,
|
|
298
|
+
async () => [],
|
|
299
|
+
),
|
|
300
|
+
).rejects.toThrow(/basedOn registry not found/);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("normalises a Mongoose Map<string,string> for cvcVersions", async () => {
|
|
304
|
+
const registry = makeRegistry({
|
|
305
|
+
cvcVersions: new Map([["service_categories", "1.0.0"]]),
|
|
306
|
+
});
|
|
307
|
+
const entries = buildSampleTree();
|
|
308
|
+
const result = await resolveRegistryWithInheritance(
|
|
309
|
+
registry,
|
|
310
|
+
async () => null,
|
|
311
|
+
async () => entries,
|
|
312
|
+
);
|
|
313
|
+
expect(result.mergedVersions).toEqual({ service_categories: "1.0.0" });
|
|
314
|
+
expect(result.entries).toHaveLength(entries.length);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// ─── In-memory fake Mongoose Connection ──────────────────────────────────────
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Lightweight stand-in for a Mongoose `Model` that supports just the
|
|
322
|
+
* subset of the API the service uses: `findOne`, `find`, `findById`,
|
|
323
|
+
* `findByIdAndUpdate`, `updateOne`. Each method returns a chainable query
|
|
324
|
+
* object exposing `.lean()`, `.sort()`, and `.exec()`.
|
|
325
|
+
*
|
|
326
|
+
* Documents are kept in a plain array per "collection". The fake doesn't
|
|
327
|
+
* implement Mongoose's full filter language — only equality matching on
|
|
328
|
+
* the fields the service touches (`_id`, `application`, `agencyId`,
|
|
329
|
+
* `version`, `status`, `domain`).
|
|
330
|
+
*/
|
|
331
|
+
type AnyDoc = Record<string, unknown> & { _id: Types.ObjectId };
|
|
332
|
+
|
|
333
|
+
function matchesFilter(doc: AnyDoc, filter: Record<string, unknown>): boolean {
|
|
334
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
335
|
+
const docValue = doc[key];
|
|
336
|
+
if (value instanceof Types.ObjectId && docValue instanceof Types.ObjectId) {
|
|
337
|
+
if (!value.equals(docValue)) return false;
|
|
338
|
+
} else if (value === null) {
|
|
339
|
+
if (docValue !== null && docValue !== undefined) return false;
|
|
340
|
+
} else if (docValue !== value) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function createFakeQuery<T>(resolver: () => T) {
|
|
348
|
+
// Mongoose's chainable queries expose lean/sort/exec, all of which
|
|
349
|
+
// ultimately resolve to the same value in our fake. We only need them
|
|
350
|
+
// to no-op so the call sites compile.
|
|
351
|
+
const q = {
|
|
352
|
+
lean: <U>() => createFakeQuery<U>(resolver as unknown as () => U),
|
|
353
|
+
sort: () => q,
|
|
354
|
+
exec: async () => resolver(),
|
|
355
|
+
};
|
|
356
|
+
return q;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function createFakeModel<T extends AnyDoc>(initialDocs: T[] = []) {
|
|
360
|
+
const docs: T[] = [...initialDocs];
|
|
361
|
+
|
|
362
|
+
return {
|
|
363
|
+
docs,
|
|
364
|
+
findOne(filter: Record<string, unknown>) {
|
|
365
|
+
return createFakeQuery(() => docs.find((d) => matchesFilter(d, filter)) ?? null);
|
|
366
|
+
},
|
|
367
|
+
find(filter: Record<string, unknown>) {
|
|
368
|
+
return createFakeQuery(() => docs.filter((d) => matchesFilter(d, filter)));
|
|
369
|
+
},
|
|
370
|
+
findById(id: Types.ObjectId) {
|
|
371
|
+
return createFakeQuery(() => docs.find((d) => d._id.equals(id)) ?? null);
|
|
372
|
+
},
|
|
373
|
+
findByIdAndUpdate(
|
|
374
|
+
id: Types.ObjectId,
|
|
375
|
+
update: { $set?: Partial<T> },
|
|
376
|
+
) {
|
|
377
|
+
return createFakeQuery(() => {
|
|
378
|
+
const doc = docs.find((d) => d._id.equals(id));
|
|
379
|
+
if (!doc) return null;
|
|
380
|
+
if (update.$set) Object.assign(doc, update.$set);
|
|
381
|
+
return doc;
|
|
382
|
+
});
|
|
383
|
+
},
|
|
384
|
+
updateOne(filter: Record<string, unknown>, update: { $set?: Partial<T> }) {
|
|
385
|
+
return {
|
|
386
|
+
exec: async () => {
|
|
387
|
+
const doc = docs.find((d) => matchesFilter(d, filter));
|
|
388
|
+
if (doc && update.$set) Object.assign(doc, update.$set);
|
|
389
|
+
return { acknowledged: true };
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Build a fake `Connection` whose `model()` returns one of two pre-built
|
|
398
|
+
* fake models keyed on the model name the service compiles into them
|
|
399
|
+
* (`CvcRegistry` / `CvcEntry`). The shape mirrors what `compileModel`
|
|
400
|
+
* eventually does inside the service constructor.
|
|
401
|
+
*/
|
|
402
|
+
function createFakeConnection(
|
|
403
|
+
registries: ICvcRegistry[],
|
|
404
|
+
entries: ICvcEntry[],
|
|
405
|
+
): {
|
|
406
|
+
connection: Connection;
|
|
407
|
+
registryModel: ReturnType<typeof createFakeModel<ICvcRegistry & AnyDoc>>;
|
|
408
|
+
entryModel: ReturnType<typeof createFakeModel<ICvcEntry & AnyDoc>>;
|
|
409
|
+
} {
|
|
410
|
+
const registryModel = createFakeModel<ICvcRegistry & AnyDoc>(
|
|
411
|
+
registries as Array<ICvcRegistry & AnyDoc>,
|
|
412
|
+
);
|
|
413
|
+
// Entries don't normally carry an `_id` in the service contract, but the
|
|
414
|
+
// fake needs one for its index. Synthesize one per row.
|
|
415
|
+
const entriesWithIds = entries.map((e) => ({
|
|
416
|
+
...e,
|
|
417
|
+
_id: new Types.ObjectId(),
|
|
418
|
+
})) as Array<ICvcEntry & AnyDoc>;
|
|
419
|
+
const entryModel = createFakeModel<ICvcEntry & AnyDoc>(entriesWithIds);
|
|
420
|
+
|
|
421
|
+
const connection = {
|
|
422
|
+
model: (name: string) => {
|
|
423
|
+
if (name === "CvcRegistry") return registryModel;
|
|
424
|
+
if (name === "CvcEntry") return entryModel;
|
|
425
|
+
throw new Error(`createFakeConnection: unexpected model name ${name}`);
|
|
426
|
+
},
|
|
427
|
+
} as unknown as Connection;
|
|
428
|
+
|
|
429
|
+
return { connection, registryModel, entryModel };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// ─── Service class tests ─────────────────────────────────────────────────────
|
|
433
|
+
|
|
434
|
+
describe("CvcRegistryService — lifecycle guards", () => {
|
|
435
|
+
let draftRegistry: ICvcRegistry;
|
|
436
|
+
let activeRegistry: ICvcRegistry;
|
|
437
|
+
let service: CvcRegistryService;
|
|
438
|
+
|
|
439
|
+
beforeEach(() => {
|
|
440
|
+
draftRegistry = makeRegistry({
|
|
441
|
+
version: "2.0.0",
|
|
442
|
+
name: "draft v2",
|
|
443
|
+
status: "draft",
|
|
444
|
+
});
|
|
445
|
+
activeRegistry = makeRegistry({
|
|
446
|
+
version: "1.0.0",
|
|
447
|
+
name: "active v1",
|
|
448
|
+
status: "active",
|
|
449
|
+
dateActivated: new Date("2026-01-01"),
|
|
450
|
+
});
|
|
451
|
+
const { connection } = createFakeConnection(
|
|
452
|
+
[draftRegistry, activeRegistry],
|
|
453
|
+
[],
|
|
454
|
+
);
|
|
455
|
+
service = new CvcRegistryService(connection);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it("activate(draft) succeeds and demotes the previous active", async () => {
|
|
459
|
+
const result = await service.activate(draftRegistry._id);
|
|
460
|
+
expect(result.status).toBe("active");
|
|
461
|
+
expect(result.dateActivated).toBeInstanceOf(Date);
|
|
462
|
+
|
|
463
|
+
// The old active should now be deprecated.
|
|
464
|
+
const old = await service.getRegistry({}, "1.0.0");
|
|
465
|
+
expect(old?.status).toBe("deprecated");
|
|
466
|
+
expect(old?.dateDeactivated).toBeInstanceOf(Date);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it("activate(active) fails", async () => {
|
|
470
|
+
await expect(service.activate(activeRegistry._id)).rejects.toThrow(
|
|
471
|
+
/expected 'draft'/,
|
|
472
|
+
);
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it("deprecate(active) succeeds", async () => {
|
|
476
|
+
const result = await service.deprecate(activeRegistry._id);
|
|
477
|
+
expect(result.status).toBe("deprecated");
|
|
478
|
+
expect(result.dateDeactivated).toBeInstanceOf(Date);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("deprecate(draft) fails", async () => {
|
|
482
|
+
await expect(service.deprecate(draftRegistry._id)).rejects.toThrow(
|
|
483
|
+
/expected 'active'/,
|
|
484
|
+
);
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it("activate fails for an unknown id", async () => {
|
|
488
|
+
await expect(service.activate(new Types.ObjectId())).rejects.toThrow(
|
|
489
|
+
/not found/,
|
|
490
|
+
);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
describe("CvcRegistryService — scope fallback", () => {
|
|
495
|
+
it("returns the app-level registry when no agency-specific one exists", async () => {
|
|
496
|
+
const appRegistry = makeRegistry({
|
|
497
|
+
version: "1.0.0",
|
|
498
|
+
name: "thread app default",
|
|
499
|
+
application: "thread",
|
|
500
|
+
agencyId: null,
|
|
501
|
+
status: "active",
|
|
502
|
+
});
|
|
503
|
+
const { connection } = createFakeConnection([appRegistry], []);
|
|
504
|
+
const service = new CvcRegistryService(connection);
|
|
505
|
+
|
|
506
|
+
const agencyId = new Types.ObjectId();
|
|
507
|
+
const result = await service.getActiveRegistry({
|
|
508
|
+
application: "thread",
|
|
509
|
+
agencyId,
|
|
510
|
+
});
|
|
511
|
+
expect(result).not.toBeNull();
|
|
512
|
+
expect(result?._id.equals(appRegistry._id)).toBe(true);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it("prefers an agency-specific registry over the app default", async () => {
|
|
516
|
+
const agencyId = new Types.ObjectId();
|
|
517
|
+
const appRegistry = makeRegistry({
|
|
518
|
+
version: "1.0.0",
|
|
519
|
+
name: "thread app default",
|
|
520
|
+
application: "thread",
|
|
521
|
+
status: "active",
|
|
522
|
+
});
|
|
523
|
+
const agencyRegistry = makeRegistry({
|
|
524
|
+
version: "1.0.0",
|
|
525
|
+
name: "thread agency override",
|
|
526
|
+
application: "thread",
|
|
527
|
+
agencyId,
|
|
528
|
+
status: "active",
|
|
529
|
+
});
|
|
530
|
+
const { connection } = createFakeConnection(
|
|
531
|
+
[appRegistry, agencyRegistry],
|
|
532
|
+
[],
|
|
533
|
+
);
|
|
534
|
+
const service = new CvcRegistryService(connection);
|
|
535
|
+
|
|
536
|
+
const result = await service.getActiveRegistry({
|
|
537
|
+
application: "thread",
|
|
538
|
+
agencyId,
|
|
539
|
+
});
|
|
540
|
+
expect(result?._id.equals(agencyRegistry._id)).toBe(true);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it("falls back to the platform default when no app registry exists", async () => {
|
|
544
|
+
const platformRegistry = makeRegistry({
|
|
545
|
+
version: "1.0.0",
|
|
546
|
+
name: "platform default",
|
|
547
|
+
application: null,
|
|
548
|
+
agencyId: null,
|
|
549
|
+
status: "active",
|
|
550
|
+
});
|
|
551
|
+
const { connection } = createFakeConnection([platformRegistry], []);
|
|
552
|
+
const service = new CvcRegistryService(connection);
|
|
553
|
+
|
|
554
|
+
const result = await service.getActiveRegistry({
|
|
555
|
+
application: "thread",
|
|
556
|
+
});
|
|
557
|
+
expect(result?._id.equals(platformRegistry._id)).toBe(true);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("returns null when fallback is disabled and no exact match exists", async () => {
|
|
561
|
+
const appRegistry = makeRegistry({
|
|
562
|
+
version: "1.0.0",
|
|
563
|
+
application: "thread",
|
|
564
|
+
status: "active",
|
|
565
|
+
});
|
|
566
|
+
const { connection } = createFakeConnection([appRegistry], []);
|
|
567
|
+
const service = new CvcRegistryService(connection, { scopeFallback: false });
|
|
568
|
+
|
|
569
|
+
const result = await service.getActiveRegistry({
|
|
570
|
+
application: "thread",
|
|
571
|
+
agencyId: new Types.ObjectId(),
|
|
572
|
+
});
|
|
573
|
+
expect(result).toBeNull();
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
describe("CvcRegistryService — cache invalidation", () => {
|
|
578
|
+
it("clears the resolved-entry cache on activate()", async () => {
|
|
579
|
+
const draftRegistry = makeRegistry({ version: "2.0.0", status: "draft" });
|
|
580
|
+
const activeRegistry = makeRegistry({
|
|
581
|
+
version: "1.0.0",
|
|
582
|
+
status: "active",
|
|
583
|
+
});
|
|
584
|
+
const entry = makeEntry({ code: "01-00-00-00-00-00-00" });
|
|
585
|
+
const { connection } = createFakeConnection(
|
|
586
|
+
[draftRegistry, activeRegistry],
|
|
587
|
+
[entry],
|
|
588
|
+
);
|
|
589
|
+
const service = new CvcRegistryService(connection);
|
|
590
|
+
|
|
591
|
+
// Warm the cache by resolving the active registry once.
|
|
592
|
+
await service.resolveEntries(activeRegistry._id);
|
|
593
|
+
expect(service.__cacheKeysForTests).toContain(activeRegistry._id.toString());
|
|
594
|
+
|
|
595
|
+
await service.activate(draftRegistry._id);
|
|
596
|
+
expect(service.__cacheKeysForTests).toEqual([]);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it("clears the resolved-entry cache on deprecate()", async () => {
|
|
600
|
+
const activeRegistry = makeRegistry({
|
|
601
|
+
version: "1.0.0",
|
|
602
|
+
status: "active",
|
|
603
|
+
});
|
|
604
|
+
const entry = makeEntry({ code: "01-00-00-00-00-00-00" });
|
|
605
|
+
const { connection } = createFakeConnection([activeRegistry], [entry]);
|
|
606
|
+
const service = new CvcRegistryService(connection);
|
|
607
|
+
|
|
608
|
+
await service.resolveEntries(activeRegistry._id);
|
|
609
|
+
expect(service.__cacheKeysForTests).toContain(activeRegistry._id.toString());
|
|
610
|
+
|
|
611
|
+
await service.deprecate(activeRegistry._id);
|
|
612
|
+
expect(service.__cacheKeysForTests).toEqual([]);
|
|
613
|
+
});
|
|
614
|
+
});
|