@htilabs/platform-common-models 0.55.0 → 0.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/dist/authz/index.cjs +353 -2
- package/dist/authz/index.cjs.map +1 -1
- package/dist/authz/index.d.cts +320 -1
- package/dist/authz/index.d.ts +320 -1
- package/dist/authz/index.js +339 -1
- package/dist/authz/index.js.map +1 -1
- package/dist/index.cjs +351 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +338 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/authz-resolve.test.ts +5 -0
- package/src/__tests__/authz-viewing-policy.test.ts +685 -0
- package/src/__tests__/authz-viewing-scope.test.ts +1037 -0
- package/src/authz/index.ts +2 -0
- package/src/authz/viewingPolicy.ts +525 -0
- package/src/authz/viewingScope.ts +359 -0
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `resolveViewingPolicy` — the account's internal-viewing policy for one acting
|
|
3
|
+
* team, chosen from the rows the app read. Ported case for case from PAVE's
|
|
4
|
+
* `packages/db/src/lookups/viewingPolicy.test.ts` (pave-2.0 develop 41f5f916),
|
|
5
|
+
* with the I/O cases (agency → account, store absent, read error) moved to the
|
|
6
|
+
* app's inputs (`accountId`, `docs: null`).
|
|
7
|
+
*
|
|
8
|
+
* Pinned:
|
|
9
|
+
* - the acting team's own row REPLACES the account-wide rule — higher or lower
|
|
10
|
+
* (Crysta 2026-10-06 ruling (a)), never the lower of the two;
|
|
11
|
+
* - no governing row is `source: "none"`, level `null`;
|
|
12
|
+
* - anything that stops the policy being read is `unavailable` (level 0), never
|
|
13
|
+
* `"none"`, and a malformed team row never falls back to the account rule;
|
|
14
|
+
* - a governing row with no `visibleGroups` reads as an empty list;
|
|
15
|
+
* - contract § 2's truth table, through `viewingLevelFromFunctions`.
|
|
16
|
+
*/
|
|
17
|
+
import { Types } from "mongoose";
|
|
18
|
+
import { describe, expect, it } from "vitest";
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
type GoverningViewingPolicy,
|
|
22
|
+
resolveViewingPolicy,
|
|
23
|
+
teamViewingLevelFromPolicy,
|
|
24
|
+
unavailableViewingPolicy,
|
|
25
|
+
type ViewingLevel,
|
|
26
|
+
viewingLevelFromFunctions,
|
|
27
|
+
viewingPolicyVocabulary,
|
|
28
|
+
} from "../authz/index.js";
|
|
29
|
+
import { coreInformationScopeSot } from "../cvcs/index.js";
|
|
30
|
+
import {
|
|
31
|
+
type CoreInformationScopeSot,
|
|
32
|
+
createInformationScopeResolver,
|
|
33
|
+
scopeMigrationsFromSot,
|
|
34
|
+
scopeRowsFromSot,
|
|
35
|
+
} from "../services/informationScope.js";
|
|
36
|
+
|
|
37
|
+
const sot = coreInformationScopeSot as unknown as CoreInformationScopeSot;
|
|
38
|
+
const SCOPE = createInformationScopeResolver({
|
|
39
|
+
rows: scopeRowsFromSot(sot),
|
|
40
|
+
migrations: scopeMigrationsFromSot(sot),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
interface World {
|
|
44
|
+
accountId: string;
|
|
45
|
+
teamA: string;
|
|
46
|
+
teamB: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const hex = (): string => new Types.ObjectId().toHexString();
|
|
50
|
+
|
|
51
|
+
function world(): World {
|
|
52
|
+
return { accountId: hex(), teamA: hex(), teamB: hex() };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function accountRow(w: World, over: Record<string, unknown> = {}): Record<string, unknown> {
|
|
56
|
+
return dropAbsent({
|
|
57
|
+
id: `viewpol-${w.accountId}`,
|
|
58
|
+
familyId: `viewpol-${w.accountId}`,
|
|
59
|
+
version: 1,
|
|
60
|
+
species: "viewing",
|
|
61
|
+
purpose: null,
|
|
62
|
+
level: 2,
|
|
63
|
+
visibleGroups: ["identifying", "risk"],
|
|
64
|
+
scopeVocab: 4,
|
|
65
|
+
assignments: [{ scope: "account" }],
|
|
66
|
+
account_id: w.accountId,
|
|
67
|
+
active: true,
|
|
68
|
+
...over,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function teamRow(
|
|
73
|
+
w: World,
|
|
74
|
+
teamId: string,
|
|
75
|
+
over: Record<string, unknown> = {},
|
|
76
|
+
): Record<string, unknown> {
|
|
77
|
+
return dropAbsent({
|
|
78
|
+
id: `viewpol-${w.accountId}--${teamId}`,
|
|
79
|
+
familyId: `viewpol-${w.accountId}--${teamId}`,
|
|
80
|
+
version: 1,
|
|
81
|
+
species: "viewing",
|
|
82
|
+
purpose: null,
|
|
83
|
+
level: 1,
|
|
84
|
+
visibleGroups: ["identifying"],
|
|
85
|
+
scopeVocab: 4,
|
|
86
|
+
assignments: [{ scope: "team", teamId }],
|
|
87
|
+
teamIds: [teamId],
|
|
88
|
+
account_id: w.accountId,
|
|
89
|
+
active: true,
|
|
90
|
+
...over,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** An override of `undefined` means the field is ABSENT on disk. */
|
|
95
|
+
function dropAbsent(r: Record<string, unknown>): Record<string, unknown> {
|
|
96
|
+
return Object.fromEntries(Object.entries(r).filter(([, v]) => v !== undefined));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function read(
|
|
100
|
+
w: World,
|
|
101
|
+
teamId: string | null,
|
|
102
|
+
...rows: Record<string, unknown>[]
|
|
103
|
+
): GoverningViewingPolicy {
|
|
104
|
+
return resolveViewingPolicy(rows, { accountId: w.accountId, teamId }, SCOPE);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
describe("resolveViewingPolicy (registry_policies species viewing)", () => {
|
|
108
|
+
it("the account-wide rule governs a team with no row of its own", () => {
|
|
109
|
+
const w = world();
|
|
110
|
+
expect(read(w, w.teamA, accountRow(w), teamRow(w, w.teamB))).toEqual({
|
|
111
|
+
source: "account",
|
|
112
|
+
level: 2,
|
|
113
|
+
visibleGroups: ["identifying", "risk"],
|
|
114
|
+
scopeVocab: 4,
|
|
115
|
+
policy: { id: `viewpol-${w.accountId}`, familyId: `viewpol-${w.accountId}`, version: 1 },
|
|
116
|
+
accountId: w.accountId,
|
|
117
|
+
rulePath: `viewpol-${w.accountId} v1 (the account-wide viewing rule — no row for this team)`,
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("rows that are all retired read as none, and say so", () => {
|
|
122
|
+
const w = world();
|
|
123
|
+
expect(read(w, w.teamA, accountRow(w, { active: false }))).toMatchObject({
|
|
124
|
+
source: "none",
|
|
125
|
+
level: null,
|
|
126
|
+
visibleGroups: null,
|
|
127
|
+
retiredRowsOnly: true,
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("no row at all, or a live row beside a retired one, does not say so", () => {
|
|
132
|
+
const w = world();
|
|
133
|
+
expect(read(w, w.teamA)).toMatchObject({ source: "none", retiredRowsOnly: false });
|
|
134
|
+
const live = read(w, w.teamA, accountRow(w), teamRow(w, w.teamB, { active: false }));
|
|
135
|
+
expect(live).toMatchObject({ source: "account" });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("the team's own row REPLACES a higher account rule (it narrows)", () => {
|
|
139
|
+
const w = world();
|
|
140
|
+
expect(read(w, w.teamA, accountRow(w, { level: 3 }), teamRow(w, w.teamA))).toMatchObject({
|
|
141
|
+
source: "team",
|
|
142
|
+
level: 1,
|
|
143
|
+
visibleGroups: ["identifying"],
|
|
144
|
+
policy: { id: `viewpol-${w.accountId}--${w.teamA}` },
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("the team's own row REPLACES a lower account rule (ruling (a): it may allow more)", () => {
|
|
149
|
+
const w = world();
|
|
150
|
+
expect(
|
|
151
|
+
read(w, w.teamA, accountRow(w, { level: 1 }), teamRow(w, w.teamA, { level: 3 })),
|
|
152
|
+
).toMatchObject({ source: "team", level: 3 });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("matches an ObjectId-typed teamId and an ObjectId-typed account_id", () => {
|
|
156
|
+
const w = world();
|
|
157
|
+
expect(
|
|
158
|
+
read(
|
|
159
|
+
w,
|
|
160
|
+
w.teamA,
|
|
161
|
+
teamRow(w, w.teamA, {
|
|
162
|
+
account_id: new Types.ObjectId(w.accountId),
|
|
163
|
+
assignments: [{ scope: "team", teamId: new Types.ObjectId(w.teamA) }],
|
|
164
|
+
}),
|
|
165
|
+
),
|
|
166
|
+
).toMatchObject({ source: "team", level: 1 });
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("a seat on no team reads the account-wide rule", () => {
|
|
170
|
+
const w = world();
|
|
171
|
+
expect(read(w, null, accountRow(w), teamRow(w, w.teamA))).toMatchObject({
|
|
172
|
+
source: "account",
|
|
173
|
+
level: 2,
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("no governing row is source none — no level is picked", () => {
|
|
178
|
+
const w = world();
|
|
179
|
+
expect(read(w, w.teamA, teamRow(w, w.teamB))).toEqual({
|
|
180
|
+
source: "none",
|
|
181
|
+
level: null,
|
|
182
|
+
visibleGroups: null,
|
|
183
|
+
scopeVocab: null,
|
|
184
|
+
policy: null,
|
|
185
|
+
accountId: w.accountId,
|
|
186
|
+
retiredRowsOnly: false,
|
|
187
|
+
rulePath: "no viewing policy on file — the role level alone decides",
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("another Account's rows never govern", () => {
|
|
192
|
+
const w = world();
|
|
193
|
+
expect(
|
|
194
|
+
read(
|
|
195
|
+
w,
|
|
196
|
+
w.teamA,
|
|
197
|
+
accountRow(w, { account_id: hex() }),
|
|
198
|
+
teamRow(w, w.teamA, { account_id: "acct-other" }),
|
|
199
|
+
teamRow(w, w.teamA, { account_id: undefined }),
|
|
200
|
+
),
|
|
201
|
+
).toMatchObject({ source: "none" });
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("a non-viewing species never governs (a share policy is a different kind)", () => {
|
|
205
|
+
const w = world();
|
|
206
|
+
expect(
|
|
207
|
+
read(w, w.teamA, accountRow(w, { species: "team", purpose: "referral" })),
|
|
208
|
+
).toMatchObject({ source: "none" });
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("a retired version (active: false) never governs", () => {
|
|
212
|
+
const w = world();
|
|
213
|
+
expect(
|
|
214
|
+
read(w, w.teamA, accountRow(w), teamRow(w, w.teamA, { active: false })),
|
|
215
|
+
).toMatchObject({ source: "account", level: 2 });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("two live versions of one family read the highest version", () => {
|
|
219
|
+
const w = world();
|
|
220
|
+
expect(
|
|
221
|
+
read(
|
|
222
|
+
w,
|
|
223
|
+
w.teamA,
|
|
224
|
+
teamRow(w, w.teamA, { version: 1, level: 3 }),
|
|
225
|
+
teamRow(w, w.teamA, { version: 2, level: 0 }),
|
|
226
|
+
),
|
|
227
|
+
).toMatchObject({ source: "team", level: 0, policy: { version: 2 } });
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("a stamped row with no whitelist reads as an EMPTY list (contract § 4), not the full record", () => {
|
|
231
|
+
const w = world();
|
|
232
|
+
for (const visibleGroups of [null, undefined]) {
|
|
233
|
+
expect(
|
|
234
|
+
read(w, w.teamA, accountRow(w, { visibleGroups, scopeVocab: 5, version: null })),
|
|
235
|
+
).toMatchObject({
|
|
236
|
+
source: "account",
|
|
237
|
+
visibleGroups: [],
|
|
238
|
+
scopeVocab: 5,
|
|
239
|
+
policy: { version: null },
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("a governing row with no stamp is unavailable, whatever else it holds", () => {
|
|
245
|
+
it.each([
|
|
246
|
+
["no list, null stamp, level 3", { visibleGroups: null, scopeVocab: null }],
|
|
247
|
+
["no list, absent stamp, level 3", { visibleGroups: undefined, scopeVocab: undefined }],
|
|
248
|
+
["a list, null stamp, level 3", { scopeVocab: null }],
|
|
249
|
+
["a list, absent stamp, level 3", { scopeVocab: undefined }],
|
|
250
|
+
["level 2, no stamp", { level: 2, visibleGroups: null, scopeVocab: null }],
|
|
251
|
+
["level 1, no stamp", { level: 1, scopeVocab: undefined }],
|
|
252
|
+
["level 0, no stamp", { level: 0, visibleGroups: [], scopeVocab: null }],
|
|
253
|
+
])("the ACCOUNT row: %s", (_label, over) => {
|
|
254
|
+
const w = world();
|
|
255
|
+
for (const teamId of [null, w.teamA]) {
|
|
256
|
+
expect(read(w, teamId, accountRow(w, { level: 3, ...over }))).toEqual(
|
|
257
|
+
unavailableViewingPolicy("unsupported_scope_vocab", `viewpol-${w.accountId}`),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it.each([
|
|
263
|
+
["no list, null stamp", { level: 3, visibleGroups: null, scopeVocab: null }],
|
|
264
|
+
["no list, absent stamp", { level: 3, scopeVocab: undefined, visibleGroups: undefined }],
|
|
265
|
+
["level 1, no stamp", { level: 1, scopeVocab: null }],
|
|
266
|
+
])("the TEAM row: %s — and never falls back to a stamped account rule", (_label, over) => {
|
|
267
|
+
const w = world();
|
|
268
|
+
expect(
|
|
269
|
+
read(w, w.teamA, accountRow(w, { level: 3, scopeVocab: 5 }), teamRow(w, w.teamA, over)),
|
|
270
|
+
).toMatchObject({
|
|
271
|
+
source: "unavailable",
|
|
272
|
+
level: 0,
|
|
273
|
+
reason: "unsupported_scope_vocab",
|
|
274
|
+
policyId: `viewpol-${w.accountId}--${w.teamA}`,
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("a row that does not govern is never read: another team's, or the account rule a team row replaces", () => {
|
|
279
|
+
const w = world();
|
|
280
|
+
expect(
|
|
281
|
+
read(
|
|
282
|
+
w,
|
|
283
|
+
w.teamA,
|
|
284
|
+
accountRow(w, { level: 3, visibleGroups: null, scopeVocab: null }),
|
|
285
|
+
teamRow(w, w.teamA, { level: 2, scopeVocab: 5 }),
|
|
286
|
+
teamRow(w, w.teamB, { level: 3, visibleGroups: null, scopeVocab: null }),
|
|
287
|
+
),
|
|
288
|
+
).toMatchObject({ source: "team", scopeVocab: 5 });
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("reads no `category_rest` row: `identifying.other` opens nothing on this axis", () => {
|
|
293
|
+
const w = world();
|
|
294
|
+
const rows = [
|
|
295
|
+
accountRow(w, { visibleGroups: ["identifying.other"], scopeVocab: 5 }),
|
|
296
|
+
teamRow(w, w.teamA, { visibleGroups: ["identifying.other", "risk"], scopeVocab: 5 }),
|
|
297
|
+
];
|
|
298
|
+
expect(read(w, null, ...rows)).toMatchObject({
|
|
299
|
+
source: "account",
|
|
300
|
+
visibleGroups: [],
|
|
301
|
+
scopeVocab: 5,
|
|
302
|
+
});
|
|
303
|
+
expect(read(w, w.teamA, ...rows)).toMatchObject({ source: "team", visibleGroups: ["risk"] });
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("an empty stamped whitelist reads as stored, and a stamp-3 list of v3 categories is valid", () => {
|
|
307
|
+
const w = world();
|
|
308
|
+
const rows = [
|
|
309
|
+
accountRow(w, { visibleGroups: [], scopeVocab: 4 }),
|
|
310
|
+
teamRow(w, w.teamA, {
|
|
311
|
+
visibleGroups: ["identifying", "risk", "health_accommodation"],
|
|
312
|
+
scopeVocab: 3,
|
|
313
|
+
}),
|
|
314
|
+
];
|
|
315
|
+
expect(read(w, null, ...rows)).toMatchObject({
|
|
316
|
+
source: "account",
|
|
317
|
+
visibleGroups: [],
|
|
318
|
+
scopeVocab: 4,
|
|
319
|
+
});
|
|
320
|
+
expect(read(w, w.teamA, ...rows)).toMatchObject({ source: "team", scopeVocab: 3 });
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("a hub-saved stamp-5 row (the prototype's v4 vocabulary) reads as stored", () => {
|
|
324
|
+
const w = world();
|
|
325
|
+
const rows = [
|
|
326
|
+
accountRow(w, {
|
|
327
|
+
level: 3,
|
|
328
|
+
visibleGroups: ["relationships_household", "health_status"],
|
|
329
|
+
scopeVocab: 5,
|
|
330
|
+
}),
|
|
331
|
+
teamRow(w, w.teamA, { visibleGroups: ["identifying"], scopeVocab: 4 }),
|
|
332
|
+
];
|
|
333
|
+
expect(read(w, null, ...rows)).toMatchObject({
|
|
334
|
+
source: "account",
|
|
335
|
+
level: 3,
|
|
336
|
+
visibleGroups: ["relationships_household", "health_status"],
|
|
337
|
+
scopeVocab: 5,
|
|
338
|
+
});
|
|
339
|
+
expect(read(w, w.teamA, ...rows)).toMatchObject({
|
|
340
|
+
source: "team",
|
|
341
|
+
visibleGroups: ["identifying"],
|
|
342
|
+
scopeVocab: 4,
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("every category a PAVE / Thread / LEADS account is offered reads at stamp 5 (contract § 1)", () => {
|
|
347
|
+
const w = world();
|
|
348
|
+
const offered = [
|
|
349
|
+
"notes",
|
|
350
|
+
"identifying",
|
|
351
|
+
"contact",
|
|
352
|
+
"health_accommodation",
|
|
353
|
+
"immigration_legal_status",
|
|
354
|
+
"risk",
|
|
355
|
+
"services",
|
|
356
|
+
"instruments",
|
|
357
|
+
"relationships_household",
|
|
358
|
+
"network_associations",
|
|
359
|
+
"commercial_activity",
|
|
360
|
+
"investigative_work_product",
|
|
361
|
+
"object_identifiers",
|
|
362
|
+
];
|
|
363
|
+
expect(
|
|
364
|
+
read(w, null, accountRow(w, { level: 3, visibleGroups: offered, scopeVocab: 5 })),
|
|
365
|
+
).toMatchObject({ source: "account", visibleGroups: offered });
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("does not mutate or alias the stored list", () => {
|
|
369
|
+
const w = world();
|
|
370
|
+
const stored = ["identifying", "risk"];
|
|
371
|
+
const r = read(w, null, accountRow(w, { visibleGroups: stored }));
|
|
372
|
+
expect(r.visibleGroups).toEqual(stored);
|
|
373
|
+
expect(r.visibleGroups).not.toBe(stored);
|
|
374
|
+
expect(stored).toEqual(["identifying", "risk"]);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
describe("one spelling per id (an upper-case hex id never misses its row)", () => {
|
|
378
|
+
it("an UPPER-case acting team still finds its own narrower row", () => {
|
|
379
|
+
const w = world();
|
|
380
|
+
expect(
|
|
381
|
+
read(w, w.teamA.toUpperCase(), accountRow(w, { level: 3 }), teamRow(w, w.teamA)),
|
|
382
|
+
).toMatchObject({ source: "team", level: 1 });
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("a padded acting team id still finds its own row", () => {
|
|
386
|
+
const w = world();
|
|
387
|
+
expect(
|
|
388
|
+
read(w, ` ${w.teamA} `, accountRow(w, { level: 3 }), teamRow(w, w.teamA)),
|
|
389
|
+
).toMatchObject({ source: "team", level: 1 });
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("a row stored with an UPPER-case team id is this team's row", () => {
|
|
393
|
+
const w = world();
|
|
394
|
+
expect(
|
|
395
|
+
read(
|
|
396
|
+
w,
|
|
397
|
+
w.teamA,
|
|
398
|
+
accountRow(w, { level: 3 }),
|
|
399
|
+
teamRow(w, w.teamA, {
|
|
400
|
+
assignments: [{ scope: "team", teamId: w.teamA.toUpperCase() }],
|
|
401
|
+
}),
|
|
402
|
+
),
|
|
403
|
+
).toMatchObject({ source: "team", level: 1 });
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("a row stored with an UPPER-case account_id is the Account's", () => {
|
|
407
|
+
const w = world();
|
|
408
|
+
expect(read(w, null, accountRow(w, { account_id: w.accountId.toUpperCase() }))).toMatchObject(
|
|
409
|
+
{ source: "account", level: 2 },
|
|
410
|
+
);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it("an UPPER-case accountId in the query still finds the Account's rows", () => {
|
|
414
|
+
const w = world();
|
|
415
|
+
expect(
|
|
416
|
+
resolveViewingPolicy(
|
|
417
|
+
[accountRow(w)],
|
|
418
|
+
{ accountId: w.accountId.toUpperCase(), teamId: null },
|
|
419
|
+
SCOPE,
|
|
420
|
+
),
|
|
421
|
+
).toMatchObject({ source: "account", level: 2 });
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it("another team's row is not read — not even its active flag", () => {
|
|
426
|
+
const w = world();
|
|
427
|
+
expect(
|
|
428
|
+
read(w, w.teamA, accountRow(w), teamRow(w, w.teamB, { active: undefined })),
|
|
429
|
+
).toMatchObject({ source: "account", level: 2 });
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
describe("fails closed — unavailable (level 0), never none", () => {
|
|
433
|
+
it("the account could not be resolved", () => {
|
|
434
|
+
for (const accountId of ["", " ", null, undefined]) {
|
|
435
|
+
expect(resolveViewingPolicy([], { accountId, teamId: null }, SCOPE)).toEqual(
|
|
436
|
+
unavailableViewingPolicy("account_unresolved"),
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("the read failed (no list handed in)", () => {
|
|
442
|
+
const w = world();
|
|
443
|
+
for (const docs of [null, undefined]) {
|
|
444
|
+
const query = { accountId: w.accountId, teamId: w.teamA };
|
|
445
|
+
expect(resolveViewingPolicy(docs, query, SCOPE)).toEqual(
|
|
446
|
+
unavailableViewingPolicy("read_error"),
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("a candidate that is not a document", () => {
|
|
452
|
+
const w = world();
|
|
453
|
+
expect(
|
|
454
|
+
resolveViewingPolicy(
|
|
455
|
+
[accountRow(w), null],
|
|
456
|
+
{ accountId: w.accountId, teamId: null },
|
|
457
|
+
SCOPE,
|
|
458
|
+
),
|
|
459
|
+
).toMatchObject({ source: "unavailable", reason: "read_error" });
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("the app's own outcome codes take the same shape", () => {
|
|
463
|
+
expect(unavailableViewingPolicy("policy_store_absent")).toEqual({
|
|
464
|
+
source: "unavailable",
|
|
465
|
+
level: 0,
|
|
466
|
+
visibleGroups: [],
|
|
467
|
+
scopeVocab: null,
|
|
468
|
+
policy: null,
|
|
469
|
+
reason: "policy_store_absent",
|
|
470
|
+
policyId: null,
|
|
471
|
+
rulePath: "viewing policy unavailable: policy_store_absent — no view of other teams' cases",
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it.each([
|
|
476
|
+
["a string level", { level: "3" }, "malformed_level"],
|
|
477
|
+
["a level of 4", { level: 4 }, "malformed_level"],
|
|
478
|
+
["a fractional level", { level: 2.5 }, "malformed_level"],
|
|
479
|
+
["a negative level", { level: -1 }, "malformed_level"],
|
|
480
|
+
["an absent level", { level: undefined }, "malformed_level"],
|
|
481
|
+
["a whitelist that is not a list of strings", { visibleGroups: ["risk", 7] }, "malformed_visible_groups"],
|
|
482
|
+
["a string whitelist", { visibleGroups: "risk" }, "malformed_visible_groups"],
|
|
483
|
+
["an object whitelist", { visibleGroups: { risk: true } }, "malformed_visible_groups"],
|
|
484
|
+
["a string vocabulary stamp", { scopeVocab: "4" }, "malformed_scope_vocab"],
|
|
485
|
+
["a zero vocabulary stamp", { scopeVocab: 0 }, "malformed_scope_vocab"],
|
|
486
|
+
["a fractional vocabulary stamp", { scopeVocab: 4.5 }, "malformed_scope_vocab"],
|
|
487
|
+
["a vocabulary stamp the reader cannot read (1)", { scopeVocab: 1 }, "unsupported_scope_vocab"],
|
|
488
|
+
["a stamp it cannot read even with no whitelist", { visibleGroups: null, scopeVocab: 1 }, "unsupported_scope_vocab"],
|
|
489
|
+
["a future vocabulary stamp (6)", { scopeVocab: 6 }, "unsupported_scope_vocab"],
|
|
490
|
+
["a future stamp even with no whitelist (7)", { visibleGroups: null, scopeVocab: 7 }, "unsupported_scope_vocab"],
|
|
491
|
+
["a stamp before the Instruments catalog (2)", { visibleGroups: ["identifying"], scopeVocab: 2 }, "unsupported_scope_vocab"],
|
|
492
|
+
["a stamp-4 list naming a v4-only code (read against the v3 tree)", { visibleGroups: ["relationships_household"], scopeVocab: 4 }, "unknown_visible_group"],
|
|
493
|
+
["an unstamped whitelist", { scopeVocab: null }, "unsupported_scope_vocab"],
|
|
494
|
+
["an unknown whitelist code", { visibleGroups: ["identifying", "not_a_scope_code"] }, "unknown_visible_group"],
|
|
495
|
+
["a retired whitelist code", { visibleGroups: ["identifying", "hiv_status"] }, "unknown_visible_group"],
|
|
496
|
+
["an absent active flag", { active: undefined }, "malformed_active"],
|
|
497
|
+
["a null active flag", { active: null }, "malformed_active"],
|
|
498
|
+
["a string active flag", { active: "true" }, "malformed_active"],
|
|
499
|
+
])(
|
|
500
|
+
"a governing team row with %s — and never falls back to the account rule",
|
|
501
|
+
(_label, over, reason) => {
|
|
502
|
+
const w = world();
|
|
503
|
+
const r = read(w, w.teamA, accountRow(w, { level: 3 }), teamRow(w, w.teamA, over));
|
|
504
|
+
expect(r).toEqual(
|
|
505
|
+
unavailableViewingPolicy(
|
|
506
|
+
reason as Parameters<typeof unavailableViewingPolicy>[0],
|
|
507
|
+
`viewpol-${w.accountId}--${w.teamA}`,
|
|
508
|
+
),
|
|
509
|
+
);
|
|
510
|
+
expect(teamViewingLevelFromPolicy(r)).toBe(0);
|
|
511
|
+
},
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
it.each([
|
|
515
|
+
["a level of 4", { level: 4 }, "malformed_level"],
|
|
516
|
+
["an unknown whitelist code", { visibleGroups: ["nope"] }, "unknown_visible_group"],
|
|
517
|
+
["a null active flag", { active: null }, "malformed_active"],
|
|
518
|
+
])("a governing ACCOUNT row with %s", (_label, over, reason) => {
|
|
519
|
+
const w = world();
|
|
520
|
+
expect(read(w, w.teamA, accountRow(w, over))).toMatchObject({
|
|
521
|
+
source: "unavailable",
|
|
522
|
+
level: 0,
|
|
523
|
+
reason,
|
|
524
|
+
policyId: `viewpol-${w.accountId}`,
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it.each([
|
|
529
|
+
["no assignments", { assignments: undefined }],
|
|
530
|
+
["an empty assignment list", { assignments: [] }],
|
|
531
|
+
["an unknown scope", { assignments: [{ scope: "team_pair", teamId: "x" }] }],
|
|
532
|
+
["a team entry with no team", { assignments: [{ scope: "team" }] }],
|
|
533
|
+
["a team entry with an empty team", { assignments: [{ scope: "team", teamId: "" }] }],
|
|
534
|
+
["a null entry", { assignments: [null] }],
|
|
535
|
+
["two team assignments", { assignments: [{ scope: "team", teamId: hex() }, { scope: "team", teamId: hex() }] }],
|
|
536
|
+
["a mixed account + team assignment", { assignments: [{ scope: "account" }, { scope: "team", teamId: hex() }] }],
|
|
537
|
+
])(
|
|
538
|
+
"a live viewing row with %s cannot be placed (it might be this team's narrowing)",
|
|
539
|
+
(_label, over) => {
|
|
540
|
+
const w = world();
|
|
541
|
+
expect(
|
|
542
|
+
read(w, w.teamA, accountRow(w, { level: 3 }), teamRow(w, w.teamB, { ...over, id: "viewpol-odd" })),
|
|
543
|
+
).toEqual(unavailableViewingPolicy("unreadable_assignment", "viewpol-odd"));
|
|
544
|
+
},
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
it("two live rows for one place with no version order", () => {
|
|
548
|
+
const w = world();
|
|
549
|
+
expect(
|
|
550
|
+
read(
|
|
551
|
+
w,
|
|
552
|
+
w.teamA,
|
|
553
|
+
teamRow(w, w.teamA, { id: "viewpol-x", familyId: "fam-x", level: 3 }),
|
|
554
|
+
teamRow(w, w.teamA, { id: "viewpol-y", familyId: "fam-y", level: 1 }),
|
|
555
|
+
),
|
|
556
|
+
).toMatchObject({ source: "unavailable", reason: "ambiguous_rows" });
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it("two live versions of one family with the same version", () => {
|
|
560
|
+
const w = world();
|
|
561
|
+
expect(
|
|
562
|
+
read(
|
|
563
|
+
w,
|
|
564
|
+
w.teamA,
|
|
565
|
+
accountRow(w, { version: 2, level: 3 }),
|
|
566
|
+
accountRow(w, { version: 2, level: 1 }),
|
|
567
|
+
),
|
|
568
|
+
).toMatchObject({ source: "unavailable", reason: "ambiguous_rows" });
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it("two live versions of one family where one carries no version", () => {
|
|
572
|
+
const w = world();
|
|
573
|
+
expect(
|
|
574
|
+
read(
|
|
575
|
+
w,
|
|
576
|
+
w.teamA,
|
|
577
|
+
accountRow(w, { version: 2, level: 3 }),
|
|
578
|
+
accountRow(w, { version: undefined, level: 1 }),
|
|
579
|
+
),
|
|
580
|
+
).toMatchObject({ source: "unavailable", reason: "ambiguous_rows" });
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("a malformed governing team row hides an otherwise valid account rule (no fallback)", () => {
|
|
584
|
+
const w = world();
|
|
585
|
+
const r = read(w, w.teamA, accountRow(w, { level: 3, scopeVocab: 5, visibleGroups: null }), teamRow(w, w.teamA, { level: "2" }));
|
|
586
|
+
expect(r.source).toBe("unavailable");
|
|
587
|
+
expect(r.level).toBe(0);
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
describe("teamViewingLevelFromPolicy — what reaches viewingLevelFromFunctions", () => {
|
|
593
|
+
it("is the row's level when a row governs, null when none does, 0 when unavailable", () => {
|
|
594
|
+
const w = world();
|
|
595
|
+
expect(teamViewingLevelFromPolicy(read(w, w.teamA, teamRow(w, w.teamA, { level: 2 })))).toBe(2);
|
|
596
|
+
expect(teamViewingLevelFromPolicy(read(w, w.teamA, accountRow(w, { level: 0 })))).toBe(0);
|
|
597
|
+
expect(teamViewingLevelFromPolicy(read(w, w.teamA))).toBeNull();
|
|
598
|
+
expect(teamViewingLevelFromPolicy(unavailableViewingPolicy("read_error"))).toBe(0);
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
describe("viewingPolicyVocabulary", () => {
|
|
603
|
+
it("reads 3, 4 and 5 and nothing else", () => {
|
|
604
|
+
expect(viewingPolicyVocabulary(3)).toEqual({
|
|
605
|
+
scopeVersion: 3,
|
|
606
|
+
oneHomeCarry: true,
|
|
607
|
+
identifyingAlias: true,
|
|
608
|
+
});
|
|
609
|
+
expect(viewingPolicyVocabulary(4)).toEqual({
|
|
610
|
+
scopeVersion: 3,
|
|
611
|
+
oneHomeCarry: false,
|
|
612
|
+
identifyingAlias: true,
|
|
613
|
+
});
|
|
614
|
+
expect(viewingPolicyVocabulary(5)).toEqual({
|
|
615
|
+
scopeVersion: 4,
|
|
616
|
+
oneHomeCarry: false,
|
|
617
|
+
identifyingAlias: false,
|
|
618
|
+
});
|
|
619
|
+
for (const v of [0, 1, 2, 6, 7, "5", null, undefined, 4.5]) {
|
|
620
|
+
expect(viewingPolicyVocabulary(v)).toBeNull();
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// ─── Contract § 2: the truth table ───────────────────────────────────────────
|
|
626
|
+
|
|
627
|
+
const ROLE_FUNCTIONS: Record<ViewingLevel, string[]> = {
|
|
628
|
+
3: ["viewing_level_3_components", "viewing_level_1_search"],
|
|
629
|
+
2: ["viewing_level_2_autocomplete"],
|
|
630
|
+
1: ["viewing_level_1_search"],
|
|
631
|
+
0: ["case_view"],
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
describe("contract § 2 — role level × governing row → the seat's level on another team's case", () => {
|
|
635
|
+
const w = world();
|
|
636
|
+
const L3_ALL = accountRow(w, {
|
|
637
|
+
level: 3,
|
|
638
|
+
scopeVocab: 5,
|
|
639
|
+
visibleGroups: ["notes", "identifying", "contact", "health_accommodation", "risk", "services", "instruments", "relationships_household"],
|
|
640
|
+
});
|
|
641
|
+
const L3_RISK_OFF = accountRow(w, {
|
|
642
|
+
level: 3,
|
|
643
|
+
scopeVocab: 5,
|
|
644
|
+
visibleGroups: ["notes", "identifying", "contact", "health_accommodation", "services", "instruments", "relationships_household"],
|
|
645
|
+
});
|
|
646
|
+
const L3_EMPTY = accountRow(w, { level: 3, scopeVocab: 5, visibleGroups: [] });
|
|
647
|
+
const row = (level: number) => accountRow(w, { level, scopeVocab: 5 });
|
|
648
|
+
const MALFORMED = accountRow(w, { level: 3, scopeVocab: 9 });
|
|
649
|
+
|
|
650
|
+
it.each([
|
|
651
|
+
// role, governing rows, expected seat level, expected policy source
|
|
652
|
+
[3, [], 3, "none"],
|
|
653
|
+
[2, [], 2, "none"],
|
|
654
|
+
[0, [], 0, "none"],
|
|
655
|
+
[0, [L3_ALL], 0, "account"],
|
|
656
|
+
[3, [L3_ALL], 3, "account"],
|
|
657
|
+
[3, [L3_RISK_OFF], 3, "account"],
|
|
658
|
+
[3, [L3_EMPTY], 3, "account"],
|
|
659
|
+
[3, [row(2)], 2, "account"],
|
|
660
|
+
[3, [row(1)], 1, "account"],
|
|
661
|
+
[3, [row(0)], 0, "account"],
|
|
662
|
+
[1, [row(3)], 1, "account"],
|
|
663
|
+
[3, [MALFORMED], 0, "unavailable"],
|
|
664
|
+
[2, [MALFORMED], 0, "unavailable"],
|
|
665
|
+
] as const)("role %i with %j → level %i (%s)", (role, rows, expected, source) => {
|
|
666
|
+
const policy = resolveViewingPolicy(rows, { accountId: w.accountId, teamId: w.teamA }, SCOPE);
|
|
667
|
+
expect(policy.source).toBe(source);
|
|
668
|
+
const teamLevel = teamViewingLevelFromPolicy(policy);
|
|
669
|
+
expect(viewingLevelFromFunctions(ROLE_FUNCTIONS[role], teamLevel)).toBe(expected);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it("a team row lower than the account rule narrows the team; higher widens it — the role still caps", () => {
|
|
673
|
+
const rows = [
|
|
674
|
+
row(3),
|
|
675
|
+
teamRow(w, w.teamA, { level: 1, scopeVocab: 5 }),
|
|
676
|
+
teamRow(w, w.teamB, { level: 3, scopeVocab: 5, visibleGroups: [] }),
|
|
677
|
+
];
|
|
678
|
+
const a = resolveViewingPolicy(rows, { accountId: w.accountId, teamId: w.teamA }, SCOPE);
|
|
679
|
+
expect(viewingLevelFromFunctions(ROLE_FUNCTIONS[3], teamViewingLevelFromPolicy(a))).toBe(1);
|
|
680
|
+
const low = [row(1), teamRow(w, w.teamA, { level: 3, scopeVocab: 5 })];
|
|
681
|
+
const b = resolveViewingPolicy(low, { accountId: w.accountId, teamId: w.teamA }, SCOPE);
|
|
682
|
+
expect(viewingLevelFromFunctions(ROLE_FUNCTIONS[3], teamViewingLevelFromPolicy(b))).toBe(3);
|
|
683
|
+
expect(viewingLevelFromFunctions(ROLE_FUNCTIONS[2], teamViewingLevelFromPolicy(b))).toBe(2);
|
|
684
|
+
});
|
|
685
|
+
});
|