@htilabs/platform-common-models 0.60.0 → 0.61.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.
@@ -0,0 +1,836 @@
1
+ /**
2
+ * The `byPurpose` virtual fork on THE share resolver (0.61.0): an authored
3
+ * purpose-specific partner rule (`partnerPurposeRule`) is the governing
4
+ * policy when no policy is attached to the relationship — posture from the
5
+ * rule (via `relationshipPosture`), scope from clamp 5, as the prototype.
6
+ *
7
+ * Contract of record: platform-prototype `src/data.js` @ aa2c63a —
8
+ * `governingPolicyFor` L55006-L55028 ("An authored byPurpose sub-row with no
9
+ * explicit attachment is the ruled fork-with-one-attachment … this rung only
10
+ * NAMES what governs") and `effectiveCeiling` L55376 / L55391 ("The virtual
11
+ * byPurpose fork adds NAMING only": a virtual policy's posture is never
12
+ * re-applied).
13
+ */
14
+ import { describe, expect, it } from "vitest";
15
+
16
+ import { coreInformationScopeSot } from "../../cvcs/index.js";
17
+ import {
18
+ createInformationScopeResolver,
19
+ scopeMigrationsFromSot,
20
+ scopeRowsFromSot,
21
+ type CoreInformationScopeSot,
22
+ } from "../../services/informationScope.js";
23
+ import {
24
+ HTI_SHARE_PLATFORM_STARTER,
25
+ SHARE_LEVEL_LABEL,
26
+ SHARE_POSTURE_RANK,
27
+ derivePlatformDefault,
28
+ resolveShare,
29
+ shareRowCeiling,
30
+ type SharePlatformDefault,
31
+ type SharePolicyDoc,
32
+ type SharePosture,
33
+ type ShareRecipient,
34
+ type ShareResolution,
35
+ type ShareResolveInput,
36
+ } from "../index.js";
37
+
38
+ const sot = coreInformationScopeSot as CoreInformationScopeSot;
39
+ const scope = createInformationScopeResolver({
40
+ rows: scopeRowsFromSot(sot),
41
+ migrations: scopeMigrationsFromSot(sot),
42
+ });
43
+
44
+ const A = "agency-htilabs";
45
+ const T = "team-htilabs-inv-analysts";
46
+ const T2 = "team-htilabs-impl-support";
47
+ const ROW = "tr-zz8-ia-palomar";
48
+ const REQUIRED_BY_PURPOSE: Record<string, string[]> = {
49
+ mandatory_reporting: ["identifying"],
50
+ authority_reporting: ["identifying"],
51
+ };
52
+
53
+ const pdFor = (purpose: string): SharePlatformDefault =>
54
+ derivePlatformDefault(
55
+ scope,
56
+ purpose,
57
+ HTI_SHARE_PLATFORM_STARTER.scopeByPurpose[purpose] ?? null,
58
+ { required: REQUIRED_BY_PURPOSE[purpose] ?? [] },
59
+ );
60
+
61
+ /** The same seed as `resolveShare.test.ts` (the one-resolver flow + the account's day-one defaults), plus a pair-row link. */
62
+ const seeds = (): SharePolicyDoc[] => [
63
+ {
64
+ id: "pol-referral-agency-standard",
65
+ name: "Referral — agency standard",
66
+ purpose: "referral",
67
+ posture: "open",
68
+ edited: false,
69
+ infoScope: ["identifying", "contact", "services", "risk", "notes"],
70
+ assignments: [{ scope: "account" }],
71
+ },
72
+ {
73
+ id: "pol-mandatory-reporting-agency-standard",
74
+ name: "Mandatory reporting — agency standard",
75
+ purpose: "mandatory_reporting",
76
+ posture: "open",
77
+ edited: false,
78
+ infoScope: ["identifying", "contact", "risk"],
79
+ assignments: [{ scope: "account" }],
80
+ },
81
+ {
82
+ id: "pol-case-collaboration-agency-standard",
83
+ name: "Case collaboration — agency standard",
84
+ purpose: "case_collaboration",
85
+ posture: "open",
86
+ edited: false,
87
+ infoScope: ["identifying", "contact", "services", "risk"],
88
+ assignments: [{ scope: "account" }],
89
+ },
90
+ {
91
+ id: "pol-discretionary-supervisor-approval",
92
+ name: "Discretionary — supervisor approval",
93
+ purpose: "discretionary_share",
94
+ posture: "approval",
95
+ edited: true,
96
+ infoScope: ["identifying", "contact"],
97
+ assignments: [{ scope: "account" }],
98
+ },
99
+ {
100
+ id: "pol-zz8-palomar",
101
+ familyId: "pol-zz8-palomar",
102
+ name: "ZZ8 Referral — Palomar",
103
+ version: 4,
104
+ purpose: "referral",
105
+ posture: "open",
106
+ edited: true,
107
+ infoScope: ["identifying", "contact", "services", "risk"],
108
+ assignments: [{ scope: "account_partner", partnerId: "palomar" }],
109
+ origin: { kind: "fork", parentId: "pol-referral-agency-standard" },
110
+ },
111
+ {
112
+ id: "pol-zz8-team",
113
+ name: "ZZ8 Referral — Investigative Analysts",
114
+ version: 2,
115
+ purpose: "referral",
116
+ posture: "approval",
117
+ edited: true,
118
+ infoScope: ["identifying", "services", "notes"],
119
+ assignments: [{ scope: "team", teamId: T }],
120
+ },
121
+ {
122
+ id: "pol-zz8-team-mand",
123
+ name: "ZZ8 Mandatory — Investigative Analysts",
124
+ purpose: "mandatory_reporting",
125
+ posture: "blocked",
126
+ edited: true,
127
+ infoScope: null,
128
+ assignments: [{ scope: "team", teamId: T }],
129
+ },
130
+ {
131
+ id: "pol-zz8-team-disc",
132
+ name: "ZZ8 Discretionary — Investigative Analysts",
133
+ purpose: "discretionary_share",
134
+ posture: "blocked",
135
+ edited: true,
136
+ infoScope: null,
137
+ assignments: [{ scope: "team", teamId: T }],
138
+ },
139
+ {
140
+ id: "pol-zz8-row-case",
141
+ name: "ZZ8 Case collaboration — IA ↔ Palomar row",
142
+ purpose: "case_collaboration",
143
+ posture: "open",
144
+ edited: true,
145
+ infoScope: ["identifying", "services"],
146
+ assignments: [
147
+ { scope: "team_pair", teamRowId: ROW, teamId: T, partnerId: "palomar" },
148
+ ],
149
+ },
150
+ ];
151
+
152
+ const ACME: ShareRecipient = { kind: "partner", id: "acme" };
153
+ const LABEL: Record<string, string> = {
154
+ referral: "Referral",
155
+ mandatory_reporting: "Mandatory reporting",
156
+ authority_reporting: "Authority reporting",
157
+ case_collaboration: "Case collaboration",
158
+ discretionary_share: "Discretionary share",
159
+ };
160
+
161
+ const base = (
162
+ over: Partial<ShareResolveInput> & { purpose: string },
163
+ policies: SharePolicyDoc[] = seeds(),
164
+ ): ShareResolveInput => ({
165
+ scope,
166
+ policies,
167
+ platformDefault: pdFor(over.purpose),
168
+ purposeLabel: LABEL[over.purpose],
169
+ sender: { accountId: A, teamId: T2 },
170
+ recipient: ACME,
171
+ ...over,
172
+ });
173
+
174
+ const rule = (posture: SharePosture) => ({
175
+ posture,
176
+ partnerName: "Acme Shelter",
177
+ });
178
+
179
+ /** Everything but the names: what may leave and how. */
180
+ const binding = (r: ShareResolution) => ({
181
+ allowed: r.allowed,
182
+ posture: r.posture,
183
+ postureSource: r.postureSource,
184
+ excluded: r.excluded,
185
+ reportCarveOut: r.reportCarveOut,
186
+ allowedCategories: r.allowedCategories,
187
+ rows: shareRowCeiling(r, scope),
188
+ });
189
+
190
+ describe("resolveShare — an authored purpose-specific partner rule is NAMED as its fork (data.js L55006-L55028)", () => {
191
+ it("BP1 — no relationship policy: the rule governs by name (pol-bp-<partner>-<purpose>, relationship, virtual); what binds is unchanged", () => {
192
+ const without = resolveShare(
193
+ base({ purpose: "referral", relationshipPosture: "approval" }),
194
+ );
195
+ const r = resolveShare(
196
+ base({
197
+ purpose: "referral",
198
+ relationshipPosture: "approval",
199
+ partnerPurposeRule: rule("approval"),
200
+ }),
201
+ );
202
+ expect(without.governing?.id).toBe("pol-referral-agency-standard");
203
+ expect(r.governing).toEqual({
204
+ id: "pol-bp-acme-referral",
205
+ familyId: "pol-bp-acme-referral",
206
+ name: "Referral — Acme Shelter variant",
207
+ version: 1,
208
+ level: "relationship",
209
+ levelLabel: SHARE_LEVEL_LABEL.relationship,
210
+ posture: "approval",
211
+ infoScope: null,
212
+ infoScopeItems: null,
213
+ edited: true,
214
+ origin: "fork",
215
+ rulePath:
216
+ "policies.partnerOverrides[acme].byPurpose[referral] (purpose-specific rule, reported as its fork)",
217
+ virtual: true,
218
+ });
219
+ expect(r.rulePath).toBe(
220
+ "policies.partnerOverrides[acme].byPurpose[referral] (purpose-specific rule, reported as its fork)",
221
+ );
222
+ expect(r.teamCeiling).toBeNull();
223
+ expect(binding(r)).toEqual(binding(without));
224
+ // every category answers as before; an allowed one's rule path names what governs
225
+ for (const [g, e] of Object.entries(r.scope))
226
+ expect({ ...e, rulePath: "" }).toEqual({ ...without.scope[g], rulePath: "" });
227
+ expect(r.scope["identifying"]?.rulePath).toBe(r.rulePath);
228
+ });
229
+
230
+ it("BP1b — the posture starts from the rule, never from relationshipPosture (data.js L55342): rule blocked over relationshipPosture open blocks; rule open over relationshipPosture blocked allows", () => {
231
+ for (const rel of [undefined, null, "open", "approval"] as const) {
232
+ const r = resolveShare(
233
+ base({
234
+ purpose: "referral",
235
+ relationshipPosture: rel,
236
+ partnerPurposeRule: rule("blocked"),
237
+ }),
238
+ );
239
+ expect(r.governing?.posture).toBe("blocked");
240
+ expect(r.posture).toBe("blocked");
241
+ expect(r.postureSource).toBe("relationship");
242
+ expect(r.allowed).toBe(false);
243
+ expect(r.excluded?.reason).toBe(
244
+ "Partner is blocked for this purpose by your account's sharing policy",
245
+ );
246
+ }
247
+ const open = resolveShare(
248
+ base({
249
+ purpose: "referral",
250
+ relationshipPosture: "blocked",
251
+ partnerPurposeRule: rule("open"),
252
+ }),
253
+ );
254
+ expect(open.posture).toBe("open");
255
+ expect(open.allowed).toBe(true);
256
+ });
257
+
258
+ it("BP1c — over the Platform default (no account default): the fork names, the starter's starting scope still binds", () => {
259
+ const policies = seeds().filter((p) => p.id !== "pol-referral-agency-standard");
260
+ const without = resolveShare(base({ purpose: "referral" }, policies));
261
+ const r = resolveShare(
262
+ base({ purpose: "referral", partnerPurposeRule: rule("open") }, policies),
263
+ );
264
+ expect(without.governing?.level).toBe("platform_starter");
265
+ expect(r.governing?.id).toBe("pol-bp-acme-referral");
266
+ expect(binding(r)).toEqual(binding(without));
267
+ });
268
+
269
+ it("BP2 — an attached relationship policy still wins over the rule; the answer is unchanged", () => {
270
+ const over = {
271
+ purpose: "referral",
272
+ recipient: { kind: "partner" as const, id: "palomar" },
273
+ };
274
+ const without = resolveShare(base(over));
275
+ const r = resolveShare(base({ ...over, partnerPurposeRule: rule("open") }));
276
+ expect(r.governing?.id).toBe("pol-zz8-palomar");
277
+ expect(r.governing?.virtual).toBeUndefined();
278
+ expect(r).toEqual(without);
279
+ });
280
+
281
+ it("BP3 — the team-to-team row still wins over both; the posture still starts from the rule (the answer is the no-rule answer at the rule's posture)", () => {
282
+ const over = {
283
+ purpose: "case_collaboration",
284
+ sender: { accountId: A, teamId: T },
285
+ recipient: { kind: "partner" as const, id: "palomar" },
286
+ teamRowId: ROW,
287
+ };
288
+ const without = resolveShare(
289
+ base({ ...over, relationshipPosture: "approval" }),
290
+ );
291
+ const r = resolveShare(
292
+ base({ ...over, partnerPurposeRule: rule("approval") }),
293
+ );
294
+ expect(r.posture).toBe("approval");
295
+ expect(r.governing).toMatchObject({
296
+ id: "pol-zz8-row-case",
297
+ level: "team_row",
298
+ });
299
+ expect(r).toEqual(without);
300
+ });
301
+
302
+ it("BP4 — the sending team's own policy is a ceiling over the fork (data.js done()): only what both allow, the stricter posture", () => {
303
+ const over = {
304
+ purpose: "referral",
305
+ sender: { accountId: A, teamId: T },
306
+ };
307
+ const without = resolveShare(base(over));
308
+ const r = resolveShare(
309
+ base({ ...over, partnerPurposeRule: rule("open") }),
310
+ );
311
+ expect(r.governing?.id).toBe("pol-bp-acme-referral");
312
+ expect(r.teamCeiling).toMatchObject({ id: "pol-zz8-team", level: "team" });
313
+ expect(r.rulePath).toBe(
314
+ `policies.partnerOverrides[acme].byPurpose[referral] (purpose-specific rule, reported as its fork) ∩ ${r.teamCeiling?.rulePath} (the sending team's own policy is a ceiling — only what both allow)`,
315
+ );
316
+ expect(r.posture).toBe("approval");
317
+ expect(r.postureSource).toBe("team_ceiling");
318
+ expect(r.scope["contact"]?.allowed).toBe(false);
319
+ expect(r.scope["contact"]?.rulePath).toMatch(
320
+ /pol-zz8-team.*a ceiling over “Referral — Acme Shelter variant”/,
321
+ );
322
+ expect(binding(r)).toEqual(binding(without));
323
+ });
324
+
325
+ it("BP4b — the team's own policy that is also the account default becomes a ceiling over the fork (it is not the governing one) — and nothing widens", () => {
326
+ const policies = seeds()
327
+ .filter((p) => p.id !== "pol-referral-agency-standard")
328
+ .map((p) =>
329
+ p.id === "pol-zz8-team"
330
+ ? { ...p, assignments: [...(p.assignments ?? []), { scope: "account" as const }] }
331
+ : p,
332
+ );
333
+ const over = { purpose: "referral", sender: { accountId: A, teamId: T } };
334
+ const without = resolveShare(base(over, policies));
335
+ const r = resolveShare(
336
+ base({ ...over, partnerPurposeRule: rule("open") }, policies),
337
+ );
338
+ expect(without.governing?.id).toBe("pol-zz8-team");
339
+ expect(without.teamCeiling).toBeNull();
340
+ expect(r.governing?.id).toBe("pol-bp-acme-referral");
341
+ expect(r.teamCeiling?.id).toBe("pol-zz8-team");
342
+ expect(r.posture).toBe(without.posture);
343
+ expect(r.allowedCategories).toEqual(without.allowedCategories);
344
+ expect(shareRowCeiling(r, scope)).toEqual(shareRowCeiling(without, scope));
345
+ });
346
+
347
+ it("BP5 — reports: a rule saying blocked still carves out a report, in data.js's words (L55436-L55446); a discretionary share is excluded with data.js's purpose-rule sentence (L55447-L55448)", () => {
348
+ for (const purpose of ["mandatory_reporting", "authority_reporting"]) {
349
+ const over = { purpose, relationshipPosture: "blocked" as const };
350
+ const without = resolveShare(base(over));
351
+ const r = resolveShare(
352
+ base({ ...over, partnerPurposeRule: rule("blocked") }),
353
+ );
354
+ expect(r.allowed).toBe(true);
355
+ expect(r.posture).toBe("blocked");
356
+ expect(r.reportCarveOut).toEqual({
357
+ note: "Partner is blocked for ordinary sharing — reachable for reports",
358
+ rulePath: `reports-never-blocked-at-partner-layer (Crysta ruling 2026-08-23; policies.partnerOverrides[acme].byPurpose[${purpose}].sharingPosture=blocked does not bind a legal duty — who may send is a role/function question)`,
359
+ });
360
+ expect(without.reportCarveOut?.note).toMatch(/^Blocked for ordinary sharing/);
361
+ expect({ ...binding(r), reportCarveOut: null }).toEqual({
362
+ ...binding(without),
363
+ reportCarveOut: null,
364
+ });
365
+ expect(r.governing?.id).toBe(`pol-bp-acme-${purpose}`);
366
+ }
367
+ const disc = { purpose: "discretionary_share", relationshipPosture: "blocked" as const };
368
+ const without = resolveShare(base(disc));
369
+ const r = resolveShare(base({ ...disc, partnerPurposeRule: rule("blocked") }));
370
+ expect(r.allowed).toBe(false);
371
+ expect(without.excluded?.reason).toBe(
372
+ "Blocked for this purpose by the relationship",
373
+ );
374
+ expect(r.excluded).toEqual({
375
+ reason:
376
+ "Partner is blocked for this purpose by your account's sharing policy",
377
+ rulePath:
378
+ "policies.partnerOverrides[acme].byPurpose[discretionary_share].sharingPosture=blocked (Share Partners — purpose-specific rule)",
379
+ });
380
+ });
381
+
382
+ it("BP6 — inside the account, for an unknown purpose, or with a posture that is not one of the three, the rule names nothing", () => {
383
+ const inside = base({
384
+ purpose: "referral",
385
+ sender: { accountId: A, teamId: T },
386
+ recipient: { kind: "sibling_team", teamId: T2 },
387
+ });
388
+ expect(
389
+ resolveShare({ ...inside, partnerPurposeRule: rule("approval") }),
390
+ ).toEqual(resolveShare(inside));
391
+ const unknown = { ...base({ purpose: "referral" }), purpose: "zz-unknown", platformDefault: null };
392
+ const u = resolveShare({ ...unknown, partnerPurposeRule: rule("open") });
393
+ expect(u.governing).toBeNull();
394
+ expect(u).toEqual(resolveShare(unknown));
395
+ const bad = base({ purpose: "referral" });
396
+ expect(
397
+ resolveShare({
398
+ ...bad,
399
+ partnerPurposeRule: { posture: "sometimes" as unknown as SharePosture },
400
+ }),
401
+ ).toEqual(resolveShare(bad));
402
+ });
403
+
404
+ it("BP7 — a rule passed for __external__ is ignored (data.js L55333: __external__ stays scope-only); the fork's name falls back to the partner id", () => {
405
+ for (const posture of ["open", "approval", "blocked"] as const) {
406
+ const ext = base({
407
+ purpose: "referral",
408
+ recipient: { kind: "external" },
409
+ relationshipPosture: "approval",
410
+ });
411
+ expect(
412
+ resolveShare({ ...ext, partnerPurposeRule: { posture } }),
413
+ ).toEqual(resolveShare(ext));
414
+ }
415
+ const r = resolveShare(
416
+ base({ purpose: "referral", partnerPurposeRule: { posture: "open" } }),
417
+ );
418
+ expect(r.governing?.name).toBe("Referral — acme variant");
419
+ });
420
+ });
421
+
422
+ // ─── The vector table: every combination the suite's vectors exercise ────────
423
+
424
+ const PURPOSES = [
425
+ "referral",
426
+ "mandatory_reporting",
427
+ "authority_reporting",
428
+ "case_collaboration",
429
+ "discretionary_share",
430
+ ];
431
+ const RECIPIENTS: ShareRecipient[] = [
432
+ { kind: "partner", id: "palomar" },
433
+ { kind: "partner", id: "acme" },
434
+ { kind: "external" },
435
+ { kind: "sibling_team", teamId: T2 },
436
+ ];
437
+ const TEAMS = [T, T2, null];
438
+ const POSTURES: (SharePosture | null)[] = [null, "open", "approval", "blocked"];
439
+ const POLICY_SETS: [string, () => SharePolicyDoc[]][] = [
440
+ ["seeds", seeds],
441
+ ["no account defaults", () => seeds().filter((p) => !(p.assignments ?? []).some((a) => a.scope === "account"))],
442
+ [
443
+ "edited account defaults",
444
+ () => seeds().map((p) => ((p.assignments ?? []).some((a) => a.scope === "account") ? { ...p, edited: true, posture: "approval" as const } : p)),
445
+ ],
446
+ ];
447
+ const LEGACY: (string[] | null)[] = [null, ["identifying", "contact", "risk"]];
448
+
449
+ const table = (): ShareResolveInput[] => {
450
+ const out: ShareResolveInput[] = [];
451
+ for (const [, policies] of POLICY_SETS)
452
+ for (const purpose of PURPOSES)
453
+ for (const recipient of RECIPIENTS)
454
+ for (const teamId of TEAMS)
455
+ for (const relationshipPosture of POSTURES)
456
+ for (const legacyCeiling of LEGACY)
457
+ out.push(
458
+ base(
459
+ {
460
+ purpose,
461
+ recipient,
462
+ sender: { accountId: A, teamId },
463
+ relationshipPosture,
464
+ legacyCeiling,
465
+ teamRowId:
466
+ teamId === T && recipient.kind === "partner" && recipient.id === "palomar" ? ROW : null,
467
+ },
468
+ policies(),
469
+ ),
470
+ );
471
+ return out;
472
+ };
473
+
474
+ describe("resolveShare — partnerPurposeRule over the vector table", () => {
475
+ const vectors = table();
476
+
477
+ it("absent, null or undefined: every answer is the same answer", () => {
478
+ expect(vectors.length).toBeGreaterThan(1000);
479
+ for (const v of vectors) {
480
+ const r = resolveShare(v);
481
+ expect(resolveShare({ ...v, partnerPurposeRule: null })).toEqual(r);
482
+ expect(resolveShare({ ...v, partnerPurposeRule: undefined })).toEqual(r);
483
+ expect(r.governing?.virtual).toBeUndefined();
484
+ }
485
+ });
486
+
487
+ it("under the fork, the posture is the rule's and the team ceiling's — whatever relationshipPosture says, and the account default is not consulted (data.js L55342, L55391)", () => {
488
+ let forks = 0;
489
+ for (const v of vectors) {
490
+ for (const posture of ["open", "approval", "blocked"] as const) {
491
+ const r = resolveShare({ ...v, partnerPurposeRule: rule(posture) });
492
+ if (!r.governing?.virtual) continue;
493
+ forks += 1;
494
+ const tcPosture = r.teamCeiling?.posture ?? "open";
495
+ const expected =
496
+ SHARE_POSTURE_RANK[tcPosture] > SHARE_POSTURE_RANK[posture]
497
+ ? tcPosture
498
+ : posture;
499
+ expect(r.posture).toBe(expected);
500
+ expect(r.postureSource).toBe(
501
+ expected === posture ? "relationship" : "team_ceiling",
502
+ );
503
+ }
504
+ }
505
+ expect(forks).toBeGreaterThan(0);
506
+ });
507
+
508
+ it("the fork is named only where no pair row or relationship policy governs; elsewhere the ladder answers as before", () => {
509
+ for (const v of vectors) {
510
+ const r = resolveShare({ ...v, partnerPurposeRule: rule("approval") });
511
+ const was = resolveShare(v);
512
+ if (r.governing?.virtual) {
513
+ expect(r.governing.level).toBe("relationship");
514
+ expect(["agency_default", "platform_starter"]).toContain(
515
+ was.governing?.level,
516
+ );
517
+ } else {
518
+ expect(r.governing).toEqual(was.governing);
519
+ }
520
+ }
521
+ });
522
+ });
523
+
524
+ // ─── The prototype's answers under the fork, pinned old (0.60.0) → new ──────
525
+
526
+ const leavesOf = (g: string): string[] => {
527
+ const out: string[] = [];
528
+ const walk = (c: string): void => {
529
+ const kids = scope
530
+ .childrenOf(c)
531
+ .filter((k) => scope.row(k)?.status !== "retired");
532
+ if (kids.length === 0) out.push(c);
533
+ else kids.forEach(walk);
534
+ };
535
+ scope.childrenOf(g).forEach(walk);
536
+ return out;
537
+ };
538
+
539
+ /** The account's discretionary default, edited, with the posture given. */
540
+ const withDiscDefault = (
541
+ posture: SharePosture,
542
+ extra: Partial<SharePolicyDoc> = {},
543
+ ): SharePolicyDoc[] =>
544
+ seeds().map((p) =>
545
+ p.id === "pol-discretionary-supervisor-approval"
546
+ ? { ...p, posture, ...extra }
547
+ : p,
548
+ );
549
+ const DISC = "Discretionary — supervisor approval";
550
+
551
+ describe("resolveShare — under the fork the answer is the prototype's (data.js effectiveCeiling)", () => {
552
+ it("(a) account default blocked + rule open → allowed under the fork, open (0.60.0: blocked by the default)", () => {
553
+ const over = { purpose: "discretionary_share", relationshipPosture: "open" as const };
554
+ const old = resolveShare(base(over, withDiscDefault("blocked")));
555
+ expect(old.allowed).toBe(false);
556
+ expect(old.excluded?.reason).toBe(
557
+ `Blocked for this purpose by your account's “${DISC}” policy`,
558
+ );
559
+ const r = resolveShare(
560
+ base({ ...over, partnerPurposeRule: rule("open") }, withDiscDefault("blocked")),
561
+ );
562
+ expect(r.governing?.id).toBe("pol-bp-acme-discretionary_share");
563
+ expect(r.allowed).toBe(true);
564
+ expect(r.excluded).toBeNull();
565
+ expect(r.posture).toBe("open");
566
+ expect(r.postureSource).toBe("relationship");
567
+ });
568
+
569
+ it("(b) account default open + rule blocked → blocked with the purpose-rule sentence; reports carved out", () => {
570
+ const disc = resolveShare(
571
+ base(
572
+ {
573
+ purpose: "discretionary_share",
574
+ relationshipPosture: "blocked",
575
+ partnerPurposeRule: rule("blocked"),
576
+ },
577
+ withDiscDefault("open"),
578
+ ),
579
+ );
580
+ expect(disc.allowed).toBe(false);
581
+ expect(disc.excluded?.reason).toBe(
582
+ "Partner is blocked for this purpose by your account's sharing policy",
583
+ );
584
+ const rep = resolveShare(
585
+ base({
586
+ purpose: "mandatory_reporting",
587
+ relationshipPosture: "blocked",
588
+ partnerPurposeRule: rule("blocked"),
589
+ }),
590
+ );
591
+ expect(rep.allowed).toBe(true);
592
+ expect(rep.posture).toBe("blocked");
593
+ expect(rep.reportCarveOut?.note).toBe(
594
+ "Partner is blocked for ordinary sharing — reachable for reports",
595
+ );
596
+ });
597
+
598
+ it("(c) rule approval → approval, from the relationship chain, whatever the default says (0.60.0 over a blocked default: blocked)", () => {
599
+ for (const d of ["open", "approval", "blocked"] as const) {
600
+ const over = { purpose: "discretionary_share", relationshipPosture: "approval" as const };
601
+ const r = resolveShare(
602
+ base({ ...over, partnerPurposeRule: rule("approval") }, withDiscDefault(d)),
603
+ );
604
+ expect(r.allowed).toBe(true);
605
+ expect(r.posture).toBe("approval");
606
+ expect(r.postureSource).toBe("relationship");
607
+ const old = resolveShare(base(over, withDiscDefault(d)));
608
+ expect(old.posture).toBe(d === "blocked" ? "blocked" : "approval");
609
+ }
610
+ });
611
+
612
+ it("(d) the team ceiling still composes: a blocked team ceiling blocks under the fork, named as the team's policy", () => {
613
+ const r = resolveShare(
614
+ base(
615
+ {
616
+ purpose: "discretionary_share",
617
+ sender: { accountId: A, teamId: T },
618
+ relationshipPosture: "open",
619
+ partnerPurposeRule: rule("open"),
620
+ },
621
+ withDiscDefault("open"),
622
+ ),
623
+ );
624
+ expect(r.governing?.id).toBe("pol-bp-acme-discretionary_share");
625
+ expect(r.teamCeiling?.id).toBe("pol-zz8-team-disc");
626
+ expect(r.postureSource).toBe("team_ceiling");
627
+ expect(r.allowed).toBe(false);
628
+ expect(r.excluded?.reason).toBe(
629
+ "Blocked for this purpose by your account's “ZZ8 Discretionary — Investigative Analysts” policy",
630
+ );
631
+ });
632
+
633
+ it("(e1) scope: an EDITED account default's list still binds as clamp 5 (data.js's projected sharingScopeEdits[__default__], L55163, L55504) — same categories, named as the projection", () => {
634
+ const over = { purpose: "discretionary_share", relationshipPosture: "open" as const };
635
+ const old = resolveShare(base(over, withDiscDefault("open")));
636
+ const r = resolveShare(
637
+ base({ ...over, partnerPurposeRule: rule("open") }, withDiscDefault("open")),
638
+ );
639
+ expect(r.allowedCategories).toEqual(old.allowedCategories);
640
+ expect(r.allowedCategories).toContain("contact");
641
+ expect(r.allowedCategories).not.toContain("services");
642
+ expect(old.scope["services"]?.rulePath).toMatch(
643
+ /^sharingPurposePolicies\[pol-discretionary-supervisor-approval\]\.infoScope/,
644
+ );
645
+ expect(r.scope["services"]).toMatchObject({
646
+ allowed: false,
647
+ reason: "Outside the payload for this purpose",
648
+ rulePath:
649
+ "policies.sharingScopeEdits[__default__][discretionary_share] (projected from your account default policy)",
650
+ });
651
+ });
652
+
653
+ it("(e2) scope: a SYNCED (unedited) account default projects nothing — the Platform default's starting scope binds, as before", () => {
654
+ const over = { purpose: "referral" };
655
+ const old = resolveShare(base(over));
656
+ const r = resolveShare(base({ ...over, partnerPurposeRule: rule("open") }));
657
+ expect(binding(r)).toEqual(binding(old));
658
+ const withheld = Object.keys(old.scope).filter((g) => !old.scope[g]?.allowed);
659
+ expect(withheld.length).toBeGreaterThan(0);
660
+ for (const g of withheld)
661
+ expect(r.scope[g]?.rulePath).toBe(old.scope[g]?.rulePath);
662
+ });
663
+
664
+ it("(e3) scope: the edited account default's field groups (infoScopeItems) STILL bind under the fork — deliberately narrower than data.js, whose 5c (L55658) drops them (pending the owner's word)", () => {
665
+ const row = leavesOf("identifying")[0] as string;
666
+ expect(row).toBeTruthy();
667
+ const pols = withDiscDefault("open", { infoScopeItems: { identifying: [row] } });
668
+ const over = { purpose: "discretionary_share" };
669
+ const old = resolveShare(base(over, pols));
670
+ const r = resolveShare(base({ ...over, partnerPurposeRule: rule("open") }, pols));
671
+ const was = old.scope["identifying"]?.excludedItems ?? [];
672
+ const now = r.scope["identifying"]?.excludedItems ?? [];
673
+ expect(was.length).toBeGreaterThan(0);
674
+ expect([...now].sort()).toEqual([...was].sort());
675
+ expect(shareRowCeiling(r, scope)).toEqual(shareRowCeiling(old, scope));
676
+ expect(r.scope["identifying"]?.excludedReason).toBe(
677
+ old.scope["identifying"]?.excludedReason,
678
+ );
679
+ });
680
+
681
+ it("(e5) scope: with legacyCeiling passed, the edited account default's list still binds under the fork (narrower than data.js L55504, as 0.60.0 bound both)", () => {
682
+ const r = resolveShare(
683
+ base(
684
+ {
685
+ purpose: "discretionary_share",
686
+ legacyCeiling: ["identifying", "contact", "risk"],
687
+ partnerPurposeRule: rule("open"),
688
+ },
689
+ withDiscDefault("open"),
690
+ ),
691
+ );
692
+ expect(r.governing?.virtual).toBe(true);
693
+ expect(r.allowedCategories).not.toContain("risk");
694
+ expect(r.scope["risk"]).toMatchObject({
695
+ allowed: false,
696
+ reason: "Outside the payload for this purpose",
697
+ rulePath:
698
+ "policies.sharingScopeEdits[__default__][discretionary_share] (projected from your account default policy)",
699
+ });
700
+ });
701
+
702
+ it("(e4) scope: the legacy cell chain still binds over the fork (clamp 5's first rung, cpEdit / seedOverride)", () => {
703
+ const r = resolveShare(
704
+ base({
705
+ purpose: "referral",
706
+ legacyCeiling: ["identifying"],
707
+ partnerPurposeRule: rule("open"),
708
+ }),
709
+ );
710
+ expect(r.allowedCategories).not.toContain("contact");
711
+ expect(r.scope["contact"]?.rulePath).toMatch(/legacy cell chain/);
712
+ });
713
+ });
714
+
715
+ // ─── Review r1: the cause, the starting posture, and the guard ──────────────
716
+
717
+ describe("resolveShare — partnerPurposeRule review r1", () => {
718
+ const palomarBlocked = (): SharePolicyDoc[] =>
719
+ seeds().map((p) =>
720
+ p.id === "pol-zz8-palomar" ? { ...p, posture: "blocked" as const } : p,
721
+ );
722
+ const palomar = { kind: "partner" as const, id: "palomar" };
723
+
724
+ it("rule open + an attached relationship policy that blocked → blocked, and the cause named is the POLICY (a deliberate correction of data.js L55447)", () => {
725
+ const r = resolveShare(
726
+ base(
727
+ { purpose: "referral", recipient: palomar, partnerPurposeRule: rule("open") },
728
+ palomarBlocked(),
729
+ ),
730
+ );
731
+ expect(r.governing?.id).toBe("pol-zz8-palomar");
732
+ expect(r.allowed).toBe(false);
733
+ expect(r.postureSource).toBe("policy");
734
+ expect(r.excluded).toEqual({
735
+ reason: "Blocked for this purpose by your account's “ZZ8 Referral — Palomar” policy",
736
+ rulePath:
737
+ "sharingPurposePolicies[pol-zz8-palomar].posture=blocked (policy “ZZ8 Referral — Palomar” — attached to this relationship)",
738
+ });
739
+ });
740
+
741
+ it("rule open + a team-row policy that blocked → blocked, the row's policy named", () => {
742
+ const pols = seeds().map((p) =>
743
+ p.id === "pol-zz8-row-case" ? { ...p, posture: "blocked" as const } : p,
744
+ );
745
+ const r = resolveShare(
746
+ base(
747
+ {
748
+ purpose: "case_collaboration",
749
+ sender: { accountId: A, teamId: T },
750
+ recipient: palomar,
751
+ teamRowId: ROW,
752
+ partnerPurposeRule: rule("open"),
753
+ },
754
+ pols,
755
+ ),
756
+ );
757
+ expect(r.governing?.level).toBe("team_row");
758
+ expect(r.allowed).toBe(false);
759
+ expect(r.excluded?.rulePath).toMatch(
760
+ /^sharingPurposePolicies\[pol-zz8-row-case\]\.posture=blocked/,
761
+ );
762
+ });
763
+
764
+ it("rule blocked + an attached relationship policy saying open → blocked, by the rule", () => {
765
+ const r = resolveShare(
766
+ base({
767
+ purpose: "referral",
768
+ recipient: palomar,
769
+ sender: { accountId: A, teamId: T2 },
770
+ relationshipPosture: "open",
771
+ partnerPurposeRule: rule("blocked"),
772
+ }),
773
+ );
774
+ expect(r.governing?.id).toBe("pol-zz8-palomar");
775
+ expect(r.allowed).toBe(false);
776
+ expect(r.excluded?.rulePath).toBe(
777
+ "policies.partnerOverrides[palomar].byPurpose[referral].sharingPosture=blocked (Share Partners — purpose-specific rule)",
778
+ );
779
+ });
780
+
781
+ it("a report under the fork with a policy block (the team's) is carved out, naming the policy", () => {
782
+ const r = resolveShare(
783
+ base({
784
+ purpose: "mandatory_reporting",
785
+ sender: { accountId: A, teamId: T },
786
+ partnerPurposeRule: rule("open"),
787
+ }),
788
+ );
789
+ expect(r.governing?.virtual).toBe(true);
790
+ expect(r.allowed).toBe(true);
791
+ expect(r.posture).toBe("blocked");
792
+ expect(r.postureSource).toBe("team_ceiling");
793
+ expect(r.reportCarveOut?.note).toBe(
794
+ "The “ZZ8 Mandatory — Investigative Analysts” policy blocks ordinary sharing — reachable for reports",
795
+ );
796
+ });
797
+
798
+ it("guard over the vector table (relationshipPosture varied independently of the rule): with a rule, nothing leaves that the no-rule answer at the rule's posture — the account default's POSTURE left out under the fork, the one approved change — would not let leave", () => {
799
+ const isDefault = (p: SharePolicyDoc, purpose: string) =>
800
+ p.purpose === purpose &&
801
+ (p.assignments ?? []).some((a) => a.scope === "account");
802
+ let checked = 0;
803
+ for (const v of table()) {
804
+ for (const posture of ["open", "approval", "blocked"] as const) {
805
+ const r = resolveShare({ ...v, partnerPurposeRule: rule(posture) });
806
+ if (v.recipient.kind !== "partner") {
807
+ expect(r).toEqual(resolveShare(v));
808
+ continue;
809
+ }
810
+ const ref = resolveShare({
811
+ ...v,
812
+ relationshipPosture: posture,
813
+ policies: r.governing?.virtual
814
+ ? v.policies.map((p) =>
815
+ isDefault(p, v.purpose) ? { ...p, posture: null } : p,
816
+ )
817
+ : v.policies,
818
+ });
819
+ checked += 1;
820
+ expect(SHARE_POSTURE_RANK[r.posture]).toBeGreaterThanOrEqual(
821
+ SHARE_POSTURE_RANK[ref.posture],
822
+ );
823
+ if (r.allowed) expect(ref.allowed).toBe(true);
824
+ for (const g of r.allowedCategories)
825
+ expect(ref.allowedCategories).toContain(g);
826
+ const refRows = shareRowCeiling(ref, scope);
827
+ for (const c of shareRowCeiling(r, scope))
828
+ expect(
829
+ refRows.includes(c) ||
830
+ scope.ancestorsOf(c).some((a) => refRows.includes(a)),
831
+ ).toBe(true);
832
+ }
833
+ }
834
+ expect(checked).toBeGreaterThan(1000);
835
+ });
836
+ });