@htilabs/platform-common-models 0.17.0 → 0.18.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/dist/agency-BHv54o79.d.ts +484 -0
- package/dist/agency-Ca4pCOOf.d.ts +342 -0
- package/dist/agency-DYCv839V.d.cts +342 -0
- package/dist/agency-Djp57S71.d.cts +484 -0
- package/dist/constants/index.cjs +952 -0
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +3 -1
- package/dist/constants/index.d.ts +3 -1
- package/dist/constants/index.js +920 -0
- package/dist/constants/index.js.map +1 -1
- package/dist/{cvcEntry-ctHTp2df.d.cts → cvcEntry-0BIB9sen.d.cts} +36 -36
- package/dist/{cvcEntry-ctHTp2df.d.ts → cvcEntry-0BIB9sen.d.ts} +36 -36
- package/dist/cvcs/index.cjs +114 -62
- package/dist/cvcs/index.cjs.map +1 -1
- package/dist/cvcs/index.d.cts +112 -59
- package/dist/cvcs/index.d.ts +112 -59
- package/dist/cvcs/index.js +114 -62
- package/dist/cvcs/index.js.map +1 -1
- package/dist/enums/index.cjs +1 -0
- package/dist/enums/index.cjs.map +1 -1
- package/dist/enums/index.d.cts +2 -1
- package/dist/enums/index.d.ts +2 -1
- package/dist/enums/index.js +1 -0
- package/dist/enums/index.js.map +1 -1
- package/dist/{index-4DiJrn7y.d.ts → index-CDoqrfc1.d.ts} +1 -1
- package/dist/{index-qreOzXYS.d.cts → index-Zu_mOJRn.d.cts} +1 -1
- package/dist/index.cjs +1654 -1077
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +1600 -1076
- package/dist/index.js.map +1 -1
- package/dist/models/index.d.cts +2 -2
- package/dist/models/index.d.ts +2 -2
- package/dist/org-type-BYQb9Qzq.d.ts +366 -0
- package/dist/org-type-D_FG4KdN.d.cts +366 -0
- package/dist/{platform-auth-CrF5U_JH.d.cts → platform-auth-CvAYFVhK.d.cts} +39 -0
- package/dist/{platform-auth-CrF5U_JH.d.ts → platform-auth-CvAYFVhK.d.ts} +39 -0
- package/dist/schemas/index.cjs +977 -2
- package/dist/schemas/index.cjs.map +1 -1
- package/dist/schemas/index.d.cts +14 -11
- package/dist/schemas/index.d.ts +14 -11
- package/dist/schemas/index.js +962 -1
- package/dist/schemas/index.js.map +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/validation/index.cjs +894 -2
- package/dist/validation/index.cjs.map +1 -1
- package/dist/validation/index.d.cts +5 -1
- package/dist/validation/index.d.ts +5 -1
- package/dist/validation/index.js +884 -1
- package/dist/validation/index.js.map +1 -1
- package/package.json +26 -27
- package/src/__tests__/__snapshots__/cvc-sot.snap +2 -2
- package/src/__tests__/cvc-sot-snapshot.test.ts +5 -5
- package/src/__tests__/cvc-structural.test.ts +1 -1
- package/src/__tests__/org-type-contract.test.ts +1353 -0
- package/src/__tests__/referral-entity-category-canon.test.ts +255 -81
- package/src/constants/ic.ts +45 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/org-type.ts +703 -0
- package/src/cvcs/index.ts +1 -1
- package/src/cvcs/{referralEntityCategory.v1.1.0.json → referralEntityCategory.v1.2.0.json} +111 -59
- package/src/generated/cvc-enums.ts +2 -1
- package/src/index.ts +23 -0
- package/src/schemas/agency.ts +481 -0
- package/src/schemas/index.ts +24 -0
- package/src/validation/agency.ts +119 -0
- package/src/validation/index.ts +1 -0
- package/dist/team-C4LJ5Tex.d.cts +0 -333
- package/dist/team-C4LJ5Tex.d.ts +0 -333
|
@@ -0,0 +1,1353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Organization type contract (`constants/org-type.ts`, `schemas/agency.ts`,
|
|
3
|
+
* `validation/agency.ts`) — the ONE definition of agency type every app joins
|
|
4
|
+
* on (Crysta ruling 2026-10-05, platform-common #187).
|
|
5
|
+
*
|
|
6
|
+
* Pins: the vocabulary is the SOT (no hand list survives a SOT change), every
|
|
7
|
+
* platform spelling resolves to one key and free text never does, the sector
|
|
8
|
+
* classifiers match the ruling table and canon's `/^ORGANIZATION_JUSTICE/`
|
|
9
|
+
* predicate, the session-role table returns CVC-69 keys that exist, and the
|
|
10
|
+
* Mongoose fragment rejects anything but a key.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import mongoose, { Schema } from "mongoose";
|
|
14
|
+
import { describe, it, expect } from "vitest";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
AGENCY_TYPE_KEYS,
|
|
18
|
+
HEALTHCARE_ORG_TYPE_KEYS,
|
|
19
|
+
LAW_ENFORCEMENT_ORG_TYPE_KEYS,
|
|
20
|
+
LEGACY_NUMERIC_TO_REFERRAL_ENTITY_KEY,
|
|
21
|
+
LEGAL_ORG_TYPE_KEYS,
|
|
22
|
+
NO_SECTOR_ORG_TYPE_KEYS,
|
|
23
|
+
ORG_TYPE_HEADING_KEYS,
|
|
24
|
+
ORG_TYPE_KEYS,
|
|
25
|
+
ORG_TYPE_SELECTABLE_KEYS,
|
|
26
|
+
ORG_TYPE_NONPICKABLE_LEAF_KEYS,
|
|
27
|
+
REPORT_AUTHORITY_ORG_TYPE_KEYS,
|
|
28
|
+
VICTIM_SERVING_ORG_TYPE_KEYS,
|
|
29
|
+
isAgencyTypeKey,
|
|
30
|
+
isHealthcareOrgType,
|
|
31
|
+
isLawEnforcementOrgType,
|
|
32
|
+
isLegalOrgType,
|
|
33
|
+
isOrgTypeKey,
|
|
34
|
+
isReportAuthorityOrgType,
|
|
35
|
+
isVictimServingOrgType,
|
|
36
|
+
orgTypeAncestry,
|
|
37
|
+
orgTypeCanonicalCode,
|
|
38
|
+
orgTypeDefaultSessionRole,
|
|
39
|
+
orgTypeDefaultSessionRoleOrNull,
|
|
40
|
+
orgTypeDefaultVsp,
|
|
41
|
+
orgTypeEnumConstant,
|
|
42
|
+
orgTypeLabel,
|
|
43
|
+
orgTypeSector,
|
|
44
|
+
resolveAgencyTypeKey,
|
|
45
|
+
resolveOrgTypeKey,
|
|
46
|
+
resolveOrgTypeKeyFromLegacyNumeric,
|
|
47
|
+
resolveReferralEntityKeyFromLegacyNumeric,
|
|
48
|
+
} from "../constants/org-type.js";
|
|
49
|
+
import { IC_58_LABELS, IC_58_REFERRAL_ENTITY } from "../constants/ic.js";
|
|
50
|
+
import type { AgencyTypeKey, OrgTypeKey } from "../constants/org-type.js";
|
|
51
|
+
import { referralEntityCategorySot } from "../cvcs/index.js";
|
|
52
|
+
import {
|
|
53
|
+
IncidentParticipationRoleKeys,
|
|
54
|
+
ReferralEntityCategoryKeys,
|
|
55
|
+
} from "../generated/cvc-enums.js";
|
|
56
|
+
import {
|
|
57
|
+
AGENCY_ORG_TYPE_VALIDATED_UPDATE_OPS,
|
|
58
|
+
ORG_TYPE_FIELD_NAMES,
|
|
59
|
+
addAgencyOrgTypeFields,
|
|
60
|
+
addOrgTypeFields,
|
|
61
|
+
agencyOrgTypeSchemaDefinition,
|
|
62
|
+
applyRunValidatorsIfUnset,
|
|
63
|
+
mirrorOrgTypeFields,
|
|
64
|
+
orgTypeSchemaDefinition,
|
|
65
|
+
orgTypeFieldsDiffer,
|
|
66
|
+
resolveEffectiveOrgType,
|
|
67
|
+
resolveEffectiveOrgTypeFields,
|
|
68
|
+
vspTwoStepHolds,
|
|
69
|
+
vspTwoStepHoldsOnUpdate,
|
|
70
|
+
} from "../schemas/agency.js";
|
|
71
|
+
import { ORG_TYPE_PARENT_KEY } from "../constants/org-type.js";
|
|
72
|
+
import { z } from "zod";
|
|
73
|
+
import {
|
|
74
|
+
AgencyOrgTypeFieldsSchema,
|
|
75
|
+
OrgTypeSelectableKeySchema,
|
|
76
|
+
SetOrgTypeInputSchema,
|
|
77
|
+
withOrgTypeFields,
|
|
78
|
+
} from "../validation/agency.js";
|
|
79
|
+
|
|
80
|
+
type SotRow = {
|
|
81
|
+
key: string;
|
|
82
|
+
canonicalCode?: string;
|
|
83
|
+
selectable: boolean;
|
|
84
|
+
};
|
|
85
|
+
const sotKeys = (pred: (e: SotRow) => boolean) =>
|
|
86
|
+
(referralEntityCategorySot.entries as SotRow[])
|
|
87
|
+
.filter(pred)
|
|
88
|
+
.map((e) => e.key);
|
|
89
|
+
const isOrgHalf = (e: SotRow) => (e.canonicalCode ?? "").startsWith("02-");
|
|
90
|
+
const isHeading = (e: SotRow) =>
|
|
91
|
+
isOrgHalf(e) &&
|
|
92
|
+
!e.selectable &&
|
|
93
|
+
/^02-\d\d-00-00$/.test(e.canonicalCode ?? "");
|
|
94
|
+
// SOT-derived twins of the hand-pinned tuples (formerly the `_sotOrgTypeKeySets` export).
|
|
95
|
+
const sot = {
|
|
96
|
+
headings: sotKeys(isHeading),
|
|
97
|
+
selectable: sotKeys((e) => isOrgHalf(e) && e.selectable),
|
|
98
|
+
all: sotKeys(isOrgHalf),
|
|
99
|
+
};
|
|
100
|
+
/** Every SOT key whose ancestry (by SOT parentCode) includes `root`. */
|
|
101
|
+
type LinkedRow = SotRow & { code: string; parentCode: string | null };
|
|
102
|
+
const subtree = (root: string): string[] => {
|
|
103
|
+
const rows = referralEntityCategorySot.entries as LinkedRow[];
|
|
104
|
+
const byCode = new Map(rows.map((r) => [r.code, r]));
|
|
105
|
+
const chain = (r: LinkedRow): string[] => {
|
|
106
|
+
const out: string[] = [];
|
|
107
|
+
let cur: LinkedRow | undefined = r;
|
|
108
|
+
while (cur) {
|
|
109
|
+
out.push(cur.key);
|
|
110
|
+
cur = cur.parentCode ? byCode.get(cur.parentCode) : undefined;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
};
|
|
114
|
+
return rows.filter((r) => chain(r).includes(root)).map((r) => r.key);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
describe("vocabulary is the SOT", () => {
|
|
118
|
+
it("ORG_TYPE_KEYS is exactly the organization half of CVC-48 (canon 02-*)", () => {
|
|
119
|
+
// The tuples are hand-pinned literals (so the TYPES narrow — pre-review D2);
|
|
120
|
+
// membership is pinned to the SOT here, order-insensitively.
|
|
121
|
+
const sorted = (a: readonly string[]) => [...a].sort();
|
|
122
|
+
expect(sorted(ORG_TYPE_KEYS)).toEqual(
|
|
123
|
+
sorted(sotKeys((e) => (e.canonicalCode ?? "").startsWith("02-"))),
|
|
124
|
+
);
|
|
125
|
+
expect(sorted(ORG_TYPE_KEYS)).toEqual(sorted(sot.all));
|
|
126
|
+
expect(ORG_TYPE_KEYS).toHaveLength(39);
|
|
127
|
+
for (const k of ORG_TYPE_KEYS)
|
|
128
|
+
expect(k in ReferralEntityCategoryKeys).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("selectable keys are the Level 2 + 3 leaves MINUS the non-pickable leaf; headings are the nine Level 1B rows", () => {
|
|
132
|
+
// Codebook: every organization-half row marked Selectable is offered to an
|
|
133
|
+
// admin EXCEPT `own_agency_internal` (02-08-01-00) — a referral value whose
|
|
134
|
+
// meaning the codebook resolves THROUGH the receiving agency's type (IC-58
|
|
135
|
+
// § III Edge-Case Handling); the prototype picker keeps it non-pickable, and
|
|
136
|
+
// only the platform's own organization carries it (registry `internal_hti`).
|
|
137
|
+
const sotSelectable = sotKeys(
|
|
138
|
+
(e) => (e.canonicalCode ?? "").startsWith("02-") && e.selectable,
|
|
139
|
+
);
|
|
140
|
+
expect([...ORG_TYPE_NONPICKABLE_LEAF_KEYS]).toEqual([
|
|
141
|
+
"own_agency_internal",
|
|
142
|
+
]);
|
|
143
|
+
expect(
|
|
144
|
+
[...ORG_TYPE_SELECTABLE_KEYS, ...ORG_TYPE_NONPICKABLE_LEAF_KEYS].sort(),
|
|
145
|
+
).toEqual([...sotSelectable].sort());
|
|
146
|
+
expect([...ORG_TYPE_SELECTABLE_KEYS]).toEqual(
|
|
147
|
+
sot.selectable.filter((k) => k !== "own_agency_internal"),
|
|
148
|
+
);
|
|
149
|
+
expect(resolveOrgTypeKey("own_agency_internal")).toBe(
|
|
150
|
+
"own_agency_internal",
|
|
151
|
+
);
|
|
152
|
+
// Storable (HTI Labs' own row carries it), resolvable, never offered.
|
|
153
|
+
expect(resolveAgencyTypeKey("own_agency_internal")).toBe(
|
|
154
|
+
"own_agency_internal",
|
|
155
|
+
);
|
|
156
|
+
expect(isAgencyTypeKey("own_agency_internal")).toBe(true);
|
|
157
|
+
expect(ORG_TYPE_SELECTABLE_KEYS).not.toContain("own_agency_internal");
|
|
158
|
+
// A school IS a kind of organization (Level 2 Required under Education).
|
|
159
|
+
expect(isAgencyTypeKey("school_academic_institution")).toBe(true);
|
|
160
|
+
expect([...ORG_TYPE_HEADING_KEYS]).toEqual(sot.headings);
|
|
161
|
+
expect([...ORG_TYPE_HEADING_KEYS]).toEqual([
|
|
162
|
+
"housing_shelter_continuum",
|
|
163
|
+
"hotlines",
|
|
164
|
+
"justice_protective_systems",
|
|
165
|
+
"health_behavioral_health",
|
|
166
|
+
"education",
|
|
167
|
+
"victim_community_service_providers",
|
|
168
|
+
"coordination_governance",
|
|
169
|
+
"internal",
|
|
170
|
+
"other_entity",
|
|
171
|
+
]);
|
|
172
|
+
expect(ORG_TYPE_SELECTABLE_KEYS).toHaveLength(29);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("AGENCY_TYPE_KEYS = selectable leaves + the non-pickable own-agency leaf + the pre-backfill placeholder `unknown`", () => {
|
|
176
|
+
expect([...AGENCY_TYPE_KEYS]).toEqual([
|
|
177
|
+
...ORG_TYPE_SELECTABLE_KEYS,
|
|
178
|
+
...ORG_TYPE_NONPICKABLE_LEAF_KEYS,
|
|
179
|
+
"unknown",
|
|
180
|
+
]);
|
|
181
|
+
expect(isAgencyTypeKey("unknown")).toBe(true);
|
|
182
|
+
expect(isOrgTypeKey("unknown")).toBe(false);
|
|
183
|
+
// Individuals and the other missingness leaves are never an agency type.
|
|
184
|
+
for (const k of [
|
|
185
|
+
"self_referred",
|
|
186
|
+
"peer",
|
|
187
|
+
"anonymous",
|
|
188
|
+
"data_not_collected",
|
|
189
|
+
"missingness_root",
|
|
190
|
+
]) {
|
|
191
|
+
expect(isAgencyTypeKey(k), k).toBe(false);
|
|
192
|
+
}
|
|
193
|
+
// Headings are org types (aggregation) but never an agency's stored type.
|
|
194
|
+
expect(isOrgTypeKey("justice_protective_systems")).toBe(true);
|
|
195
|
+
expect(isAgencyTypeKey("justice_protective_systems")).toBe(false);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("spelling resolution — one key, never a guess", () => {
|
|
200
|
+
it("resolves the SOT key, the Sean-spec enum constant, the canon dotted code and the positional code", () => {
|
|
201
|
+
for (const k of ORG_TYPE_KEYS) {
|
|
202
|
+
expect(resolveOrgTypeKey(k), k).toBe(k);
|
|
203
|
+
expect(resolveOrgTypeKey(orgTypeEnumConstant(k)), k).toBe(k);
|
|
204
|
+
expect(resolveOrgTypeKey(orgTypeCanonicalCode(k)), k).toBe(k);
|
|
205
|
+
expect(resolveOrgTypeKey(ReferralEntityCategoryKeys[k]), k).toBe(k);
|
|
206
|
+
}
|
|
207
|
+
expect(resolveOrgTypeKey("ORGANIZATION_JUSTICE_CHILDWELFARE")).toBe(
|
|
208
|
+
"child_welfare_cps",
|
|
209
|
+
);
|
|
210
|
+
expect(resolveOrgTypeKey("02-03-01-00")).toBe("child_welfare_cps");
|
|
211
|
+
expect(resolveOrgTypeKey(" Child_Welfare_CPS ")).toBe("child_welfare_cps");
|
|
212
|
+
expect(
|
|
213
|
+
resolveOrgTypeKey("organization_community_victimserviceprovider"),
|
|
214
|
+
).toBe("victim_service_provider");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("every org registry spelling (the 16 types the 56 directory orgs carry) resolves", () => {
|
|
218
|
+
const registry = [
|
|
219
|
+
"ORGANIZATION_HEALTH_HEALTHCARE",
|
|
220
|
+
"ORGANIZATION_COMMUNITY_VICTIMSERVICEPROVIDER",
|
|
221
|
+
"ORGANIZATION_JUSTICE_LAWENFORCEMENT_LOCAL",
|
|
222
|
+
"ORGANIZATION_JUSTICE_COURTS",
|
|
223
|
+
"ORGANIZATION_JUSTICE_LAWENFORCEMENT_FEDERAL",
|
|
224
|
+
"ORGANIZATION_JUSTICE_LAWENFORCEMENT_STATE",
|
|
225
|
+
"ORGANIZATION_GOVERNANCE_COALITION",
|
|
226
|
+
"ORGANIZATION_EDUCATION_SCHOOL",
|
|
227
|
+
"ORGANIZATION_COMMUNITY_CHILDADVOCACYCENTER",
|
|
228
|
+
"ORGANIZATION_JUSTICE_LAWENFORCEMENT_TRIBAL",
|
|
229
|
+
"ORGANIZATION_HOUSING_TEMPORARYSHELTER",
|
|
230
|
+
"ORGANIZATION_OTHER_UNSPECIFIED",
|
|
231
|
+
"ORGANIZATION_INTERNAL_OWNAGENCY",
|
|
232
|
+
"ORGANIZATION_COMMUNITY_COMMUNITYSERVICEPROVIDER",
|
|
233
|
+
"ORGANIZATION_JUSTICE_ADULTPROTECTIVE",
|
|
234
|
+
"ORGANIZATION_JUSTICE_CHILDWELFARE",
|
|
235
|
+
];
|
|
236
|
+
for (const c of registry) {
|
|
237
|
+
const k = resolveOrgTypeKey(c);
|
|
238
|
+
expect(k, c).not.toBeNull();
|
|
239
|
+
expect(isAgencyTypeKey(k), c).toBe(true);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("free text, Thread's retired local codes, PAVE's legacy codes and numeric IC-58 never resolve", () => {
|
|
244
|
+
for (const v of [
|
|
245
|
+
"Law Enforcement",
|
|
246
|
+
"Police Department",
|
|
247
|
+
"VSP",
|
|
248
|
+
"Victim Service Provider",
|
|
249
|
+
"Domestic Violence Shelter",
|
|
250
|
+
"multi_service",
|
|
251
|
+
"advocacy",
|
|
252
|
+
"sp",
|
|
253
|
+
"Department of Human Services",
|
|
254
|
+
"",
|
|
255
|
+
"le_local",
|
|
256
|
+
"vsp_local",
|
|
257
|
+
"child_welfare",
|
|
258
|
+
"medical_hospital",
|
|
259
|
+
"legal_aid",
|
|
260
|
+
"adult_protective",
|
|
261
|
+
"ORGANIZATION_LE",
|
|
262
|
+
"18",
|
|
263
|
+
"20",
|
|
264
|
+
18,
|
|
265
|
+
20,
|
|
266
|
+
null,
|
|
267
|
+
undefined,
|
|
268
|
+
{},
|
|
269
|
+
]) {
|
|
270
|
+
expect(resolveOrgTypeKey(v), String(v)).toBeNull();
|
|
271
|
+
expect(resolveAgencyTypeKey(v), String(v)).toBeNull();
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("resolveAgencyTypeKey additionally admits `unknown` in any spelling; never the other missingness leaves", () => {
|
|
276
|
+
expect(resolveAgencyTypeKey("unknown")).toBe("unknown");
|
|
277
|
+
expect(resolveAgencyTypeKey("EMPTY_RESPONSE_UNKNOWN")).toBe("unknown");
|
|
278
|
+
expect(resolveAgencyTypeKey("00-00-01-00")).toBe("unknown");
|
|
279
|
+
expect(resolveAgencyTypeKey("048-000-000-000-000-000-001")).toBe("unknown");
|
|
280
|
+
expect(resolveOrgTypeKey("unknown")).toBeNull();
|
|
281
|
+
expect(resolveAgencyTypeKey("anonymous")).toBeNull();
|
|
282
|
+
expect(resolveAgencyTypeKey("self_referred")).toBeNull();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("resolveAgencyTypeKey answers only STORABLE keys — a heading in any spelling is null, as the schemas would refuse it (pre-review D1)", () => {
|
|
286
|
+
// Every answer it gives passes the Mongoose enum + Zod; every heading is refused.
|
|
287
|
+
for (const spelling of [
|
|
288
|
+
"justice_protective_systems",
|
|
289
|
+
"ORGANIZATION_JUSTICE",
|
|
290
|
+
"02-03-00-00",
|
|
291
|
+
"education",
|
|
292
|
+
"victim_community_service_providers",
|
|
293
|
+
"ORGANIZATION_COMMUNITY",
|
|
294
|
+
"internal",
|
|
295
|
+
"ORGANIZATION_INTERNAL",
|
|
296
|
+
]) {
|
|
297
|
+
expect(resolveAgencyTypeKey(spelling), spelling).toBeNull();
|
|
298
|
+
expect(resolveOrgTypeKey(spelling), spelling).not.toBeNull(); // still an org type for aggregation / labels
|
|
299
|
+
}
|
|
300
|
+
for (const k of AGENCY_TYPE_KEYS) {
|
|
301
|
+
const r = resolveAgencyTypeKey(k);
|
|
302
|
+
expect(r).toBe(k);
|
|
303
|
+
expect(isAgencyTypeKey(r)).toBe(true);
|
|
304
|
+
expect(agencyOrgTypeSchemaDefinition.type.enum).toContain(r);
|
|
305
|
+
}
|
|
306
|
+
// The v1.0.0 leaf `education` became a heading in v1.1.0: no longer storable.
|
|
307
|
+
expect(resolveAgencyTypeKey("education")).toBeNull();
|
|
308
|
+
expect(resolveAgencyTypeKey("school_academic_institution")).toBe(
|
|
309
|
+
"school_academic_institution",
|
|
310
|
+
);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("the exported types narrow to the pinned tuples (pre-review D2) — compile-time pin", () => {
|
|
314
|
+
// These assignments must type-check; the @ts-expect-error lines must fail to.
|
|
315
|
+
const a: AgencyTypeKey = "child_welfare_cps";
|
|
316
|
+
const o: OrgTypeKey = "justice_protective_systems";
|
|
317
|
+
// @ts-expect-error — a heading is not a storable agency type
|
|
318
|
+
const bad1: AgencyTypeKey = "justice_protective_systems";
|
|
319
|
+
// @ts-expect-error — an individual referrer is not an org type
|
|
320
|
+
const bad2: OrgTypeKey = "self_referred";
|
|
321
|
+
// @ts-expect-error — a missingness leaf other than `unknown` is not storable
|
|
322
|
+
const bad3: AgencyTypeKey = "anonymous";
|
|
323
|
+
void [a, o, bad1, bad2, bad3];
|
|
324
|
+
expect(true).toBe(true);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it("labels are canon's titles", () => {
|
|
328
|
+
expect(orgTypeLabel("child_welfare_cps")).toBe("Child Welfare / CPS");
|
|
329
|
+
expect(orgTypeLabel("adult_protective_services")).toBe(
|
|
330
|
+
"Adult Protective Services (APS)",
|
|
331
|
+
);
|
|
332
|
+
expect(orgTypeLabel("not_a_type")).toBeNull(); // `null` on a miss, like every sibling accessor
|
|
333
|
+
expect(orgTypeCanonicalCode("not_a_type")).toBeNull();
|
|
334
|
+
expect(orgTypeEnumConstant("not_a_type")).toBeNull();
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
describe("legacy numeric bridge (Thread `Organization.type` / `CaseRecord.referralEntity`)", () => {
|
|
339
|
+
type LegacyN = keyof typeof LEGACY_NUMERIC_TO_REFERRAL_ENTITY_KEY;
|
|
340
|
+
it("covers every number ic.ts assigns, row for row by label, and nothing else", () => {
|
|
341
|
+
const numbers = Object.values(IC_58_REFERRAL_ENTITY).sort((a, b) => a - b);
|
|
342
|
+
const bridged = Object.keys(LEGACY_NUMERIC_TO_REFERRAL_ENTITY_KEY).map(
|
|
343
|
+
Number,
|
|
344
|
+
);
|
|
345
|
+
expect(bridged.sort((a, b) => a - b)).toEqual(numbers);
|
|
346
|
+
const norm = (t: string) => t.toLowerCase().replace(/[^a-z]/g, "");
|
|
347
|
+
// Label ↔ title, with the handful of spellings canon tightened since the numbers were cut.
|
|
348
|
+
const titleAlias: Record<string, string> = {
|
|
349
|
+
internalreferral: "ownagencyinternalreferral",
|
|
350
|
+
selfreferredwalkin: "selfreferred",
|
|
351
|
+
otherexternal: "other",
|
|
352
|
+
healthcareprovider: "healthcareprovidernoninpatientpsychiatric",
|
|
353
|
+
victimserviceprovider: "victimserviceprovideradvocacyprogram",
|
|
354
|
+
communitybasedserviceprovider:
|
|
355
|
+
"communitybasedserviceprovidergeneralsocialservices",
|
|
356
|
+
};
|
|
357
|
+
for (const n of numbers) {
|
|
358
|
+
const key = LEGACY_NUMERIC_TO_REFERRAL_ENTITY_KEY[n as LegacyN];
|
|
359
|
+
expect(key in ReferralEntityCategoryKeys, `${n} → ${key}`).toBe(true);
|
|
360
|
+
const label = norm(IC_58_LABELS[n as keyof typeof IC_58_LABELS]);
|
|
361
|
+
const title = norm(orgTypeLabel(key) ?? "");
|
|
362
|
+
expect(
|
|
363
|
+
titleAlias[label] ?? label,
|
|
364
|
+
`${n} "${IC_58_LABELS[n as keyof typeof IC_58_LABELS]}" ↔ ${key}`,
|
|
365
|
+
).toBe(title);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("number or decimal string → key; anything else null; the plain resolvers still refuse numbers", () => {
|
|
370
|
+
expect(resolveReferralEntityKeyFromLegacyNumeric(18)).toBe(
|
|
371
|
+
"law_enforcement",
|
|
372
|
+
);
|
|
373
|
+
expect(resolveReferralEntityKeyFromLegacyNumeric("18")).toBe(
|
|
374
|
+
"law_enforcement",
|
|
375
|
+
);
|
|
376
|
+
expect(resolveReferralEntityKeyFromLegacyNumeric(" 28 ")).toBe(
|
|
377
|
+
"victim_service_provider",
|
|
378
|
+
);
|
|
379
|
+
expect(resolveReferralEntityKeyFromLegacyNumeric(3)).toBe(
|
|
380
|
+
"family_guardian_friend",
|
|
381
|
+
);
|
|
382
|
+
expect(resolveReferralEntityKeyFromLegacyNumeric(0)).toBe("unknown");
|
|
383
|
+
for (const v of [
|
|
384
|
+
6,
|
|
385
|
+
9,
|
|
386
|
+
32,
|
|
387
|
+
-1,
|
|
388
|
+
1.5,
|
|
389
|
+
"",
|
|
390
|
+
"x",
|
|
391
|
+
"018",
|
|
392
|
+
null,
|
|
393
|
+
undefined,
|
|
394
|
+
{},
|
|
395
|
+
[18],
|
|
396
|
+
true,
|
|
397
|
+
]) {
|
|
398
|
+
expect(
|
|
399
|
+
resolveReferralEntityKeyFromLegacyNumeric(v),
|
|
400
|
+
String(v),
|
|
401
|
+
).toBeNull();
|
|
402
|
+
}
|
|
403
|
+
expect(resolveOrgTypeKey(18)).toBeNull();
|
|
404
|
+
expect(resolveOrgTypeKey("18")).toBeNull();
|
|
405
|
+
expect(resolveAgencyTypeKey(18)).toBeNull();
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it("org-type view: individuals and 0 are null; the three heading numbers are org types but NOT storable", () => {
|
|
409
|
+
for (const n of [0, 2, 3, 4, 5]) {
|
|
410
|
+
expect(resolveOrgTypeKeyFromLegacyNumeric(n), String(n)).toBeNull();
|
|
411
|
+
}
|
|
412
|
+
// 11 "Hotline" is canon's Level-2 LEAF (carried since 0.18.0), not the Hotlines heading.
|
|
413
|
+
expect(resolveOrgTypeKeyFromLegacyNumeric(11)).toBe("hotline");
|
|
414
|
+
expect(isAgencyTypeKey(resolveOrgTypeKeyFromLegacyNumeric(11))).toBe(true);
|
|
415
|
+
for (const [n, heading] of [
|
|
416
|
+
[17, "education"],
|
|
417
|
+
[19, "other_entity"],
|
|
418
|
+
[26, "victim_community_service_providers"],
|
|
419
|
+
] as const) {
|
|
420
|
+
expect(resolveOrgTypeKeyFromLegacyNumeric(n)).toBe(heading);
|
|
421
|
+
expect(
|
|
422
|
+
resolveAgencyTypeKey(resolveOrgTypeKeyFromLegacyNumeric(n)),
|
|
423
|
+
`${n} is a heading → not storable`,
|
|
424
|
+
).toBeNull();
|
|
425
|
+
}
|
|
426
|
+
// Thread's defaulted "Other External" (19) therefore reads as un-typed: fail closed.
|
|
427
|
+
expect(isVictimServingOrgType(resolveOrgTypeKeyFromLegacyNumeric(19))).toBe(
|
|
428
|
+
false,
|
|
429
|
+
);
|
|
430
|
+
expect(
|
|
431
|
+
orgTypeDefaultVsp(resolveOrgTypeKeyFromLegacyNumeric(19)),
|
|
432
|
+
).toBeNull();
|
|
433
|
+
// Every leaf number is storable and classifies like its key.
|
|
434
|
+
for (const n of [
|
|
435
|
+
11, 13, 14, 15, 18, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 1,
|
|
436
|
+
]) {
|
|
437
|
+
const k = resolveOrgTypeKeyFromLegacyNumeric(n);
|
|
438
|
+
expect(isAgencyTypeKey(k), String(n)).toBe(true);
|
|
439
|
+
}
|
|
440
|
+
expect(
|
|
441
|
+
isLawEnforcementOrgType(resolveOrgTypeKeyFromLegacyNumeric(18)),
|
|
442
|
+
).toBe(true);
|
|
443
|
+
expect(
|
|
444
|
+
isReportAuthorityOrgType(resolveOrgTypeKeyFromLegacyNumeric(20)),
|
|
445
|
+
).toBe(true);
|
|
446
|
+
expect(orgTypeDefaultVsp(resolveOrgTypeKeyFromLegacyNumeric(28))).toBe(
|
|
447
|
+
true,
|
|
448
|
+
);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
describe("hierarchy", () => {
|
|
453
|
+
it("ancestry walks leaf → heading", () => {
|
|
454
|
+
expect(orgTypeAncestry("local_law_enforcement")).toEqual([
|
|
455
|
+
"local_law_enforcement",
|
|
456
|
+
"law_enforcement",
|
|
457
|
+
"justice_protective_systems",
|
|
458
|
+
]);
|
|
459
|
+
expect(orgTypeSector("local_law_enforcement")).toBe(
|
|
460
|
+
"justice_protective_systems",
|
|
461
|
+
);
|
|
462
|
+
expect(orgTypeSector("national_hotline")).toBe("hotlines");
|
|
463
|
+
// Re-parented child (0.18.0): leaf → hotline → hotlines, codes unchanged.
|
|
464
|
+
expect(orgTypeAncestry("national_hotline")).toEqual([
|
|
465
|
+
"national_hotline",
|
|
466
|
+
"hotline",
|
|
467
|
+
"hotlines",
|
|
468
|
+
]);
|
|
469
|
+
expect(orgTypeSector("hotline")).toBe("hotlines");
|
|
470
|
+
expect(orgTypeSector("justice_protective_systems")).toBe(
|
|
471
|
+
"justice_protective_systems",
|
|
472
|
+
);
|
|
473
|
+
// A chain that does not end in a heading has no sector (pre-review D6): never "missingness_root".
|
|
474
|
+
expect(orgTypeSector("unknown")).toBeNull();
|
|
475
|
+
expect(orgTypeSector("self_referred")).toBeNull();
|
|
476
|
+
expect(orgTypeSector("missingness_root")).toBeNull();
|
|
477
|
+
for (const k of ORG_TYPE_KEYS)
|
|
478
|
+
expect(ORG_TYPE_HEADING_KEYS).toContain(orgTypeSector(k));
|
|
479
|
+
expect(orgTypeAncestry("nope")).toEqual([]);
|
|
480
|
+
expect(orgTypeSector("nope")).toBeNull();
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("ancestry terminates on a cyclic parentCode instead of hanging", () => {
|
|
484
|
+
// Mutate a leaf's parent to point back at itself for the duration of the test.
|
|
485
|
+
const rows = referralEntityCategorySot.entries as LinkedRow[];
|
|
486
|
+
const row = rows.find((r) => r.key === "local_law_enforcement")!;
|
|
487
|
+
const parent = rows.find((r) => r.key === "law_enforcement")!;
|
|
488
|
+
const saved = parent.parentCode;
|
|
489
|
+
parent.parentCode = row.code; // law_enforcement → local_law_enforcement → law_enforcement …
|
|
490
|
+
try {
|
|
491
|
+
expect(orgTypeAncestry("local_law_enforcement")).toEqual([
|
|
492
|
+
"local_law_enforcement",
|
|
493
|
+
"law_enforcement",
|
|
494
|
+
]);
|
|
495
|
+
expect(orgTypeSector("local_law_enforcement")).toBeNull();
|
|
496
|
+
} finally {
|
|
497
|
+
parent.parentCode = saved;
|
|
498
|
+
}
|
|
499
|
+
expect(orgTypeAncestry("local_law_enforcement")).toHaveLength(3);
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
describe("sector classifiers — the ruling table", () => {
|
|
504
|
+
it("victim-serving = VSP + CAC; not the residual provider, legal services or a coalition", () => {
|
|
505
|
+
expect([...VICTIM_SERVING_ORG_TYPE_KEYS]).toEqual([
|
|
506
|
+
"victim_service_provider",
|
|
507
|
+
"child_advocacy_center",
|
|
508
|
+
]);
|
|
509
|
+
expect(isVictimServingOrgType("victim_service_provider")).toBe(true);
|
|
510
|
+
expect(
|
|
511
|
+
isVictimServingOrgType("ORGANIZATION_COMMUNITY_CHILDADVOCACYCENTER"),
|
|
512
|
+
).toBe(true);
|
|
513
|
+
for (const k of [
|
|
514
|
+
"community_based_service_provider",
|
|
515
|
+
"legal_services_provider",
|
|
516
|
+
"coalition",
|
|
517
|
+
"mdt",
|
|
518
|
+
"victim_community_service_providers",
|
|
519
|
+
"temporary_shelter",
|
|
520
|
+
"Victim Service Provider",
|
|
521
|
+
]) {
|
|
522
|
+
expect(isVictimServingOrgType(k), k).toBe(false);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it("law enforcement = the law_enforcement leaf + its five Level 3 refinements", () => {
|
|
527
|
+
expect([...LAW_ENFORCEMENT_ORG_TYPE_KEYS]).toEqual([
|
|
528
|
+
"law_enforcement",
|
|
529
|
+
"local_law_enforcement",
|
|
530
|
+
"state_law_enforcement",
|
|
531
|
+
"federal_law_enforcement",
|
|
532
|
+
"tribal_law_enforcement",
|
|
533
|
+
"other_law_enforcement",
|
|
534
|
+
]);
|
|
535
|
+
expect([...LAW_ENFORCEMENT_ORG_TYPE_KEYS]).toEqual(
|
|
536
|
+
subtree("law_enforcement"),
|
|
537
|
+
); // hand-pinned = SOT subtree
|
|
538
|
+
expect(
|
|
539
|
+
isLawEnforcementOrgType("ORGANIZATION_JUSTICE_LAWENFORCEMENT_TRIBAL"),
|
|
540
|
+
).toBe(true);
|
|
541
|
+
expect(isLawEnforcementOrgType("courts_prosecution")).toBe(false);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it("legal = courts / prosecution + legal services providers", () => {
|
|
545
|
+
expect([...LEGAL_ORG_TYPE_KEYS]).toEqual([
|
|
546
|
+
"courts_prosecution",
|
|
547
|
+
"legal_services_provider",
|
|
548
|
+
]);
|
|
549
|
+
expect(isLegalOrgType("legal_services_provider")).toBe(true);
|
|
550
|
+
expect(isLegalOrgType("correction_probation")).toBe(false);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("healthcare = the health / behavioral health subtree", () => {
|
|
554
|
+
expect([...HEALTHCARE_ORG_TYPE_KEYS]).toEqual([
|
|
555
|
+
"health_behavioral_health",
|
|
556
|
+
"inpatient_psychiatric_facility",
|
|
557
|
+
"healthcare_provider",
|
|
558
|
+
]);
|
|
559
|
+
expect([...HEALTHCARE_ORG_TYPE_KEYS]).toEqual(
|
|
560
|
+
subtree("health_behavioral_health"),
|
|
561
|
+
);
|
|
562
|
+
expect(isHealthcareOrgType("ORGANIZATION_HEALTH_HEALTHCARE")).toBe(true);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("report authority = the whole Justice & Protective Systems subtree (canon /^ORGANIZATION_JUSTICE/)", () => {
|
|
566
|
+
const expected = ORG_TYPE_KEYS.filter((k) =>
|
|
567
|
+
orgTypeEnumConstant(k)?.startsWith("ORGANIZATION_JUSTICE"),
|
|
568
|
+
);
|
|
569
|
+
expect([...REPORT_AUTHORITY_ORG_TYPE_KEYS]).toEqual(expected);
|
|
570
|
+
expect([...REPORT_AUTHORITY_ORG_TYPE_KEYS]).toEqual(
|
|
571
|
+
subtree("justice_protective_systems"),
|
|
572
|
+
);
|
|
573
|
+
expect(REPORT_AUTHORITY_ORG_TYPE_KEYS).toContain(
|
|
574
|
+
"adult_protective_services",
|
|
575
|
+
);
|
|
576
|
+
expect(REPORT_AUTHORITY_ORG_TYPE_KEYS).toContain("child_welfare_cps");
|
|
577
|
+
expect(REPORT_AUTHORITY_ORG_TYPE_KEYS).toContain("courts_prosecution");
|
|
578
|
+
expect(
|
|
579
|
+
isReportAuthorityOrgType("ORGANIZATION_JUSTICE_ADULTPROTECTIVE"),
|
|
580
|
+
).toBe(true);
|
|
581
|
+
// Partner / service-provider orgs are not authorities (Thread CS-P1 pin).
|
|
582
|
+
for (const k of [
|
|
583
|
+
"victim_service_provider",
|
|
584
|
+
"temporary_shelter",
|
|
585
|
+
"healthcare_provider",
|
|
586
|
+
"legal_services_provider",
|
|
587
|
+
"child_advocacy_center",
|
|
588
|
+
"unknown",
|
|
589
|
+
"Child Protective Services",
|
|
590
|
+
]) {
|
|
591
|
+
expect(isReportAuthorityOrgType(k), k).toBe(false);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
it("no-sector = the platform's own agency and `unknown`", () => {
|
|
596
|
+
expect([...NO_SECTOR_ORG_TYPE_KEYS]).toEqual([
|
|
597
|
+
...subtree("internal"),
|
|
598
|
+
"unknown",
|
|
599
|
+
]);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it("every storable type lands in EXACTLY one session-role bucket (disjoint AND total — review T2)", () => {
|
|
603
|
+
// The buckets orgTypeDefaultSessionRole switches on, in its order. Totality:
|
|
604
|
+
// a key in none of them would fall through to "other" by accident, not by ruling.
|
|
605
|
+
const noSector = new Set<string>(NO_SECTOR_ORG_TYPE_KEYS);
|
|
606
|
+
const otherResponse = new Set<string>([
|
|
607
|
+
"outreach_project",
|
|
608
|
+
"temporary_shelter",
|
|
609
|
+
"residential_project",
|
|
610
|
+
"national_hotline",
|
|
611
|
+
"local_other_hotline",
|
|
612
|
+
"hotline",
|
|
613
|
+
"child_welfare_cps",
|
|
614
|
+
"juvenile_justice",
|
|
615
|
+
"correction_probation",
|
|
616
|
+
"adult_protective_services",
|
|
617
|
+
"school_academic_institution",
|
|
618
|
+
"community_based_service_provider",
|
|
619
|
+
"mdt",
|
|
620
|
+
"coalition",
|
|
621
|
+
"employer",
|
|
622
|
+
"benefits_public_assistance_office",
|
|
623
|
+
"other_organization",
|
|
624
|
+
]);
|
|
625
|
+
for (const k of AGENCY_TYPE_KEYS) {
|
|
626
|
+
const hits = [
|
|
627
|
+
noSector.has(k),
|
|
628
|
+
isVictimServingOrgType(k),
|
|
629
|
+
isLawEnforcementOrgType(k),
|
|
630
|
+
isLegalOrgType(k),
|
|
631
|
+
isHealthcareOrgType(k),
|
|
632
|
+
otherResponse.has(k),
|
|
633
|
+
].filter(Boolean).length;
|
|
634
|
+
expect(hits, k).toBe(1);
|
|
635
|
+
}
|
|
636
|
+
expect(
|
|
637
|
+
otherResponse.size +
|
|
638
|
+
noSector.size +
|
|
639
|
+
VICTIM_SERVING_ORG_TYPE_KEYS.length +
|
|
640
|
+
LAW_ENFORCEMENT_ORG_TYPE_KEYS.length +
|
|
641
|
+
LEGAL_ORG_TYPE_KEYS.length +
|
|
642
|
+
HEALTHCARE_ORG_TYPE_KEYS.filter((k) => isAgencyTypeKey(k)).length,
|
|
643
|
+
).toBe(AGENCY_TYPE_KEYS.length + 1); // +1: `internal` is a no-sector heading, never storable
|
|
644
|
+
// LE is both law enforcement AND a report authority (two different questions).
|
|
645
|
+
expect(
|
|
646
|
+
isLawEnforcementOrgType("law_enforcement") &&
|
|
647
|
+
isReportAuthorityOrgType("law_enforcement"),
|
|
648
|
+
).toBe(true);
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
describe("type-level vsp default", () => {
|
|
653
|
+
it("true only for victim_service_provider; false for other recognised leaves; null for unknown / own agency / CAC / headings", () => {
|
|
654
|
+
expect(orgTypeDefaultVsp("victim_service_provider")).toBe(true);
|
|
655
|
+
expect(
|
|
656
|
+
orgTypeDefaultVsp("ORGANIZATION_COMMUNITY_VICTIMSERVICEPROVIDER"),
|
|
657
|
+
).toBe(true);
|
|
658
|
+
// A CAC is NOT a default either way (ruled 2026-10-05): un-attested ⇒ null ⇒ release required.
|
|
659
|
+
expect(orgTypeDefaultVsp("child_advocacy_center")).toBeNull();
|
|
660
|
+
expect(
|
|
661
|
+
orgTypeDefaultVsp("ORGANIZATION_COMMUNITY_CHILDADVOCACYCENTER"),
|
|
662
|
+
).toBeNull();
|
|
663
|
+
// Headings cannot answer either (they are not storable, so they resolve to nothing).
|
|
664
|
+
expect(orgTypeDefaultVsp("victim_community_service_providers")).toBeNull();
|
|
665
|
+
expect(orgTypeDefaultVsp("coalition")).toBe(false);
|
|
666
|
+
expect(orgTypeDefaultVsp("law_enforcement")).toBe(false);
|
|
667
|
+
expect(orgTypeDefaultVsp("unknown")).toBeNull();
|
|
668
|
+
expect(orgTypeDefaultVsp("own_agency_internal")).toBeNull();
|
|
669
|
+
expect(orgTypeDefaultVsp("VSP")).toBeNull();
|
|
670
|
+
expect(orgTypeDefaultVsp(undefined)).toBeNull();
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
describe("default session role (CVC-69 ruling table)", () => {
|
|
675
|
+
it("returns only CVC-69 keys that exist in this package", () => {
|
|
676
|
+
for (const k of [...AGENCY_TYPE_KEYS, "VSP", undefined, null, ""]) {
|
|
677
|
+
const role = orgTypeDefaultSessionRole(k as string);
|
|
678
|
+
expect(role in IncidentParticipationRoleKeys, `${k} → ${role}`).toBe(
|
|
679
|
+
true,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
it("follows the table row by row", () => {
|
|
685
|
+
const T = orgTypeDefaultSessionRole;
|
|
686
|
+
// victim-serving → Victim Service provider staff
|
|
687
|
+
expect(T("victim_service_provider")).toBe("vsp_staff");
|
|
688
|
+
expect(T("child_advocacy_center")).toBe("vsp_staff");
|
|
689
|
+
// law enforcement → Law enforcement personnel
|
|
690
|
+
for (const k of LAW_ENFORCEMENT_ORG_TYPE_KEYS)
|
|
691
|
+
expect(T(k), k).toBe("law_enforcement_personnel");
|
|
692
|
+
// courts / prosecution / legal → Legal professional
|
|
693
|
+
expect(T("courts_prosecution")).toBe("legal_professional");
|
|
694
|
+
expect(T("legal_services_provider")).toBe("legal_professional");
|
|
695
|
+
// healthcare incl. behavioral health → Healthcare provider staff
|
|
696
|
+
expect(T("healthcare_provider")).toBe("healthcare_provider_staff");
|
|
697
|
+
expect(T("inpatient_psychiatric_facility")).toBe(
|
|
698
|
+
"healthcare_provider_staff",
|
|
699
|
+
);
|
|
700
|
+
// any other recognised org type → Other Response Personnel
|
|
701
|
+
for (const k of [
|
|
702
|
+
"child_welfare_cps",
|
|
703
|
+
"adult_protective_services",
|
|
704
|
+
"juvenile_justice",
|
|
705
|
+
"correction_probation",
|
|
706
|
+
"school_academic_institution",
|
|
707
|
+
"temporary_shelter",
|
|
708
|
+
"national_hotline",
|
|
709
|
+
"coalition",
|
|
710
|
+
"mdt",
|
|
711
|
+
"employer",
|
|
712
|
+
"benefits_public_assistance_office",
|
|
713
|
+
"other_organization",
|
|
714
|
+
"community_based_service_provider",
|
|
715
|
+
]) {
|
|
716
|
+
expect(T(k), k).toBe("other_response_personnel");
|
|
717
|
+
}
|
|
718
|
+
// no type / unknown / own agency → Victim Service provider staff
|
|
719
|
+
for (const k of [
|
|
720
|
+
"unknown",
|
|
721
|
+
"own_agency_internal",
|
|
722
|
+
"internal",
|
|
723
|
+
"",
|
|
724
|
+
undefined,
|
|
725
|
+
null,
|
|
726
|
+
"Some Unmapped Type",
|
|
727
|
+
"Department of Human Services",
|
|
728
|
+
"multi_service",
|
|
729
|
+
]) {
|
|
730
|
+
expect(T(k as string), String(k)).toBe("vsp_staff");
|
|
731
|
+
}
|
|
732
|
+
// Present-but-unresolvable free text → vsp_staff is the RULING (Personnel roles card,
|
|
733
|
+
// 2026-10-05: "unrecognized or empty types fall back to Victim Service provider staff"),
|
|
734
|
+
// and the ...OrNull sibling is how a consumer tells that guess from a read type.
|
|
735
|
+
const N = orgTypeDefaultSessionRoleOrNull;
|
|
736
|
+
expect(N("unknown")).toBe("vsp_staff"); // read, not guessed
|
|
737
|
+
expect(N("own_agency_internal")).toBe("vsp_staff"); // the platform's own org: ruled row, read not guessed
|
|
738
|
+
expect(N("victim_service_provider")).toBe("vsp_staff");
|
|
739
|
+
expect(N("local_law_enforcement")).toBe("law_enforcement_personnel");
|
|
740
|
+
for (const k of [
|
|
741
|
+
"",
|
|
742
|
+
undefined,
|
|
743
|
+
null,
|
|
744
|
+
"Some Unmapped Type",
|
|
745
|
+
"VSP",
|
|
746
|
+
"Law Enforcement",
|
|
747
|
+
"18",
|
|
748
|
+
"justice_protective_systems",
|
|
749
|
+
"internal",
|
|
750
|
+
"self_referred",
|
|
751
|
+
]) {
|
|
752
|
+
expect(N(k as string), String(k)).toBeNull();
|
|
753
|
+
expect(T(k as string), String(k)).toBe("vsp_staff");
|
|
754
|
+
}
|
|
755
|
+
for (const k of AGENCY_TYPE_KEYS) expect(N(k), k).toBe(T(k));
|
|
756
|
+
// Any spelling resolves the same way.
|
|
757
|
+
expect(T("ORGANIZATION_JUSTICE_LAWENFORCEMENT_LOCAL")).toBe(
|
|
758
|
+
"law_enforcement_personnel",
|
|
759
|
+
);
|
|
760
|
+
expect(T("02-04-02-00")).toBe("healthcare_provider_staff");
|
|
761
|
+
});
|
|
762
|
+
|
|
763
|
+
it("an attested `vsp: true` wins over the type — the table's first row (review of #190, Nate 3)", () => {
|
|
764
|
+
const T = orgTypeDefaultSessionRole;
|
|
765
|
+
const N = orgTypeDefaultSessionRoleOrNull;
|
|
766
|
+
// The flag can legitimately disagree with the type: it is captured, the sector is a default.
|
|
767
|
+
for (const k of [
|
|
768
|
+
"community_based_service_provider",
|
|
769
|
+
"local_law_enforcement",
|
|
770
|
+
"courts_prosecution",
|
|
771
|
+
"healthcare_provider",
|
|
772
|
+
"employer",
|
|
773
|
+
"unknown",
|
|
774
|
+
"",
|
|
775
|
+
null,
|
|
776
|
+
undefined,
|
|
777
|
+
"Law Enforcement",
|
|
778
|
+
"18",
|
|
779
|
+
]) {
|
|
780
|
+
expect(T(k as string, true), String(k)).toBe("vsp_staff");
|
|
781
|
+
expect(N(k as string, true), String(k)).toBe("vsp_staff"); // read, not guessed
|
|
782
|
+
}
|
|
783
|
+
// Only a strict true counts: false / null / undefined / "true" / 1 fall through to the type.
|
|
784
|
+
for (const v of [false, null, undefined, "true", 1, "yes"]) {
|
|
785
|
+
expect(T("local_law_enforcement", v), String(v)).toBe(
|
|
786
|
+
"law_enforcement_personnel",
|
|
787
|
+
);
|
|
788
|
+
expect(N("local_law_enforcement", v), String(v)).toBe(
|
|
789
|
+
"law_enforcement_personnel",
|
|
790
|
+
);
|
|
791
|
+
expect(N("Law Enforcement", v), String(v)).toBeNull();
|
|
792
|
+
expect(T("Law Enforcement", v), String(v)).toBe("vsp_staff");
|
|
793
|
+
}
|
|
794
|
+
// A CAC: victim-serving by type, and its attested flag may say false — the type row still answers.
|
|
795
|
+
expect(T("child_advocacy_center", false)).toBe("vsp_staff");
|
|
796
|
+
});
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
describe("agency schema fragment + Zod", () => {
|
|
800
|
+
it("adds `type` (enum = AGENCY_TYPE_KEYS, indexed) and the two attested booleans", () => {
|
|
801
|
+
const schema = addAgencyOrgTypeFields(new Schema({ name: String }));
|
|
802
|
+
const type = schema.path("type") as unknown as {
|
|
803
|
+
options: { enum?: string[]; index?: boolean };
|
|
804
|
+
};
|
|
805
|
+
expect(type.options.enum).toEqual([...AGENCY_TYPE_KEYS]);
|
|
806
|
+
expect(type.options.index).toBe(true);
|
|
807
|
+
for (const f of ["vsp", "vspPrivateNonprofit"]) {
|
|
808
|
+
expect(schema.path(f).instance, f).toBe("Boolean");
|
|
809
|
+
}
|
|
810
|
+
// `requires_roi` is NOT one of the organization-type fields (Crysta decision 1, 2026-10-08).
|
|
811
|
+
expect(schema.path("requiresRoi")).toBeUndefined();
|
|
812
|
+
expect(agencyOrgTypeSchemaDefinition.type.enum).not.toContain(
|
|
813
|
+
"self_referred",
|
|
814
|
+
);
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("REPLACE semantics (review T1): the consumer's `required` / `enum` / `default` on `type` are replaced wholesale", () => {
|
|
818
|
+
const schema = new Schema({
|
|
819
|
+
name: String,
|
|
820
|
+
type: { type: String, required: true, default: "VSP", lowercase: true }, // Thread's free-text shape
|
|
821
|
+
});
|
|
822
|
+
addAgencyOrgTypeFields(schema);
|
|
823
|
+
const t = schema.path("type") as unknown as {
|
|
824
|
+
options: Record<string, unknown>;
|
|
825
|
+
isRequired?: boolean;
|
|
826
|
+
};
|
|
827
|
+
expect(t.isRequired ?? false).toBe(false);
|
|
828
|
+
expect(t.options.required).toBeUndefined();
|
|
829
|
+
expect(t.options.default).toBeUndefined();
|
|
830
|
+
expect(t.options.lowercase).toBeUndefined();
|
|
831
|
+
expect(t.options.enum).toEqual([...AGENCY_TYPE_KEYS]);
|
|
832
|
+
expect(t.options.index).toBe(true);
|
|
833
|
+
// Other paths are untouched.
|
|
834
|
+
expect(schema.path("name").instance).toBe("String");
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
it("MG2 pin (review): a hydrated row carrying a NON-KEY `type` fails its next save of ANY field; a row with no `type` does not", () => {
|
|
838
|
+
// This is why the order is backfill → adopt. Mongoose validates `init`-ed paths
|
|
839
|
+
// on save, not only modified ones, so adopting the enum before the backfill breaks
|
|
840
|
+
// every agency that carries free text / "UNKNOWN" / "multi_service" — but not the
|
|
841
|
+
// (121 of 124 prod) rows that carry no `type` at all.
|
|
842
|
+
const Model = compile(addAgencyOrgTypeFields(new Schema({ name: String })));
|
|
843
|
+
// Model.hydrate = what a find() returns: an `init`-ed document, nothing modified.
|
|
844
|
+
for (const stored of [
|
|
845
|
+
"Law Enforcement",
|
|
846
|
+
"UNKNOWN",
|
|
847
|
+
"multi_service",
|
|
848
|
+
"18",
|
|
849
|
+
]) {
|
|
850
|
+
const legacy = Model.hydrate({
|
|
851
|
+
_id: new mongoose.Types.ObjectId(),
|
|
852
|
+
name: "Tulsa PD",
|
|
853
|
+
type: stored,
|
|
854
|
+
});
|
|
855
|
+
expect(legacy.isModified("type")).toBe(false);
|
|
856
|
+
legacy.set("name", "Tulsa Police Department");
|
|
857
|
+
expect(legacy.validateSync()?.errors.type, stored).toBeDefined();
|
|
858
|
+
legacy.set("type", "local_law_enforcement");
|
|
859
|
+
expect(legacy.validateSync(), stored).toBeUndefined();
|
|
860
|
+
}
|
|
861
|
+
const untyped = Model.hydrate({
|
|
862
|
+
_id: new mongoose.Types.ObjectId(),
|
|
863
|
+
name: "Zed Shelter",
|
|
864
|
+
});
|
|
865
|
+
untyped.set("name", "Zed Testorg Shelter");
|
|
866
|
+
expect(untyped.validateSync()).toBeUndefined();
|
|
867
|
+
const placeholder = Model.hydrate({
|
|
868
|
+
_id: new mongoose.Types.ObjectId(),
|
|
869
|
+
name: "x",
|
|
870
|
+
type: "unknown",
|
|
871
|
+
});
|
|
872
|
+
placeholder.set("name", "y");
|
|
873
|
+
expect(placeholder.validateSync()).toBeUndefined();
|
|
874
|
+
});
|
|
875
|
+
|
|
876
|
+
it("installs runValidators on the update paths by default; opt-out keeps the update hook (two-step) but not runValidators", () => {
|
|
877
|
+
type Hooked = { s: { hooks: { hasHooks: (n: string) => boolean } } };
|
|
878
|
+
const hooksOf = (schema: Schema) => (schema as unknown as Hooked).s.hooks;
|
|
879
|
+
const on = hooksOf(addAgencyOrgTypeFields(new Schema({ name: String })));
|
|
880
|
+
for (const op of AGENCY_ORG_TYPE_VALIDATED_UPDATE_OPS)
|
|
881
|
+
expect(on.hasHooks(op), op).toBe(true);
|
|
882
|
+
expect(on.hasHooks("save")).toBe(false);
|
|
883
|
+
expect(on.hasHooks("validate")).toBe(true); // the two-step on save (review Blocker 2)
|
|
884
|
+
const off = hooksOf(
|
|
885
|
+
addAgencyOrgTypeFields(new Schema({ name: String }), {
|
|
886
|
+
validateOnUpdate: false,
|
|
887
|
+
}),
|
|
888
|
+
);
|
|
889
|
+
// Review round 2026-10-08: the update hook is ALWAYS installed (it carries the
|
|
890
|
+
// two-step shape rule); opting out only stops it setting runValidators.
|
|
891
|
+
for (const op of AGENCY_ORG_TYPE_VALIDATED_UPDATE_OPS)
|
|
892
|
+
expect(off.hasHooks(op), op).toBe(true);
|
|
893
|
+
// The hook body: sets runValidators unless the caller already decided.
|
|
894
|
+
const q = (initial: Record<string, unknown>) => {
|
|
895
|
+
let opts = { ...initial };
|
|
896
|
+
const self = {
|
|
897
|
+
getOptions: () => opts,
|
|
898
|
+
setOptions: (o: Record<string, unknown>) => {
|
|
899
|
+
opts = { ...opts, ...o };
|
|
900
|
+
return self;
|
|
901
|
+
},
|
|
902
|
+
};
|
|
903
|
+
return self;
|
|
904
|
+
};
|
|
905
|
+
const a = q({});
|
|
906
|
+
applyRunValidatorsIfUnset(a as never);
|
|
907
|
+
expect(a.getOptions().runValidators).toBe(true);
|
|
908
|
+
const b = q({ runValidators: false });
|
|
909
|
+
applyRunValidatorsIfUnset(b as never);
|
|
910
|
+
expect(b.getOptions().runValidators).toBe(false);
|
|
911
|
+
const c = q({ runValidators: true, upsert: true });
|
|
912
|
+
applyRunValidatorsIfUnset(c as never);
|
|
913
|
+
expect(c.getOptions()).toEqual({ runValidators: true, upsert: true });
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
it("rejects free text and headings on write; accepts a key or `unknown`", () => {
|
|
917
|
+
const schema = addAgencyOrgTypeFields(new Schema({ name: String }));
|
|
918
|
+
const Model = compile(schema);
|
|
919
|
+
expect(
|
|
920
|
+
new Model({ name: "x", type: "child_welfare_cps" }).validateSync(),
|
|
921
|
+
).toBeUndefined();
|
|
922
|
+
expect(
|
|
923
|
+
new Model({ name: "x", type: "unknown" }).validateSync(),
|
|
924
|
+
).toBeUndefined();
|
|
925
|
+
expect(new Model({ name: "x" }).validateSync()).toBeUndefined(); // not required until the backfill lands
|
|
926
|
+
for (const bad of [
|
|
927
|
+
"Law Enforcement",
|
|
928
|
+
"le_local",
|
|
929
|
+
"justice_protective_systems",
|
|
930
|
+
"ORGANIZATION_JUSTICE_CHILDWELFARE",
|
|
931
|
+
]) {
|
|
932
|
+
expect(
|
|
933
|
+
new Model({ name: "x", type: bad }).validateSync()?.errors.type,
|
|
934
|
+
bad,
|
|
935
|
+
).toBeDefined();
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
it("Zod: the Admin Hub may set any selectable leaf but never `unknown`; stored agencies may carry `unknown`", () => {
|
|
940
|
+
expect(OrgTypeSelectableKeySchema.options).toEqual([
|
|
941
|
+
...ORG_TYPE_SELECTABLE_KEYS,
|
|
942
|
+
]);
|
|
943
|
+
expect(SetOrgTypeInputSchema.safeParse({ type: "unknown" }).success).toBe(
|
|
944
|
+
false,
|
|
945
|
+
);
|
|
946
|
+
expect(
|
|
947
|
+
SetOrgTypeInputSchema.safeParse({
|
|
948
|
+
type: "victim_service_provider",
|
|
949
|
+
vsp: true,
|
|
950
|
+
}).success,
|
|
951
|
+
).toBe(true);
|
|
952
|
+
expect(
|
|
953
|
+
AgencyOrgTypeFieldsSchema.safeParse({ type: "unknown" }).success,
|
|
954
|
+
).toBe(true);
|
|
955
|
+
expect(AgencyOrgTypeFieldsSchema.safeParse({}).success).toBe(true);
|
|
956
|
+
expect(AgencyOrgTypeFieldsSchema.safeParse({ type: "VSP" }).success).toBe(
|
|
957
|
+
false,
|
|
958
|
+
);
|
|
959
|
+
expect(AgencyOrgTypeFieldsSchema.safeParse({ vsp: "yes" }).success).toBe(
|
|
960
|
+
false,
|
|
961
|
+
);
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
it("SetOrgTypeInputSchema (review T3): `type` is required, a heading is refused, unknown keys are refused (.strict)", () => {
|
|
965
|
+
expect(SetOrgTypeInputSchema.safeParse({}).success).toBe(false);
|
|
966
|
+
expect(SetOrgTypeInputSchema.safeParse({ vsp: true }).success).toBe(false);
|
|
967
|
+
expect(SetOrgTypeInputSchema.safeParse({ type: null }).success).toBe(false);
|
|
968
|
+
for (const heading of ORG_TYPE_HEADING_KEYS) {
|
|
969
|
+
expect(
|
|
970
|
+
SetOrgTypeInputSchema.safeParse({ type: heading }).success,
|
|
971
|
+
heading,
|
|
972
|
+
).toBe(false);
|
|
973
|
+
}
|
|
974
|
+
for (const leaf of ORG_TYPE_SELECTABLE_KEYS) {
|
|
975
|
+
expect(
|
|
976
|
+
SetOrgTypeInputSchema.safeParse({ type: leaf }).success,
|
|
977
|
+
leaf,
|
|
978
|
+
).toBe(true);
|
|
979
|
+
}
|
|
980
|
+
expect(
|
|
981
|
+
SetOrgTypeInputSchema.safeParse({
|
|
982
|
+
type: "victim_service_provider",
|
|
983
|
+
typeOverride: "x",
|
|
984
|
+
}).success,
|
|
985
|
+
).toBe(false);
|
|
986
|
+
expect(
|
|
987
|
+
SetOrgTypeInputSchema.safeParse({
|
|
988
|
+
type: "victim_service_provider",
|
|
989
|
+
vsp: true,
|
|
990
|
+
vspPrivateNonprofit: false,
|
|
991
|
+
}).success,
|
|
992
|
+
).toBe(true);
|
|
993
|
+
// `requiresRoi` is not a key of the strict write shape any more.
|
|
994
|
+
expect(
|
|
995
|
+
SetOrgTypeInputSchema.safeParse({
|
|
996
|
+
type: "victim_service_provider",
|
|
997
|
+
requiresRoi: false,
|
|
998
|
+
}).success,
|
|
999
|
+
).toBe(false);
|
|
1000
|
+
// The read-ish fields schema is still a WRITE shape: legacy free text / null never parse.
|
|
1001
|
+
expect(
|
|
1002
|
+
AgencyOrgTypeFieldsSchema.safeParse({ type: "Law Enforcement" }).success,
|
|
1003
|
+
).toBe(false);
|
|
1004
|
+
expect(AgencyOrgTypeFieldsSchema.safeParse({ type: null }).success).toBe(
|
|
1005
|
+
false,
|
|
1006
|
+
);
|
|
1007
|
+
});
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
// A throwaway model on a fresh Mongoose instance, no connection: enough for
|
|
1011
|
+
// `validateSync` (same device as team-directory-fields.test.ts).
|
|
1012
|
+
let modelSeq = 0;
|
|
1013
|
+
function compile(schema: Schema) {
|
|
1014
|
+
const m = new mongoose.Mongoose();
|
|
1015
|
+
modelSeq += 1;
|
|
1016
|
+
return m.model(`OrgTypeContractTest${modelSeq}`, schema, "agencies");
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
describe("Org home, Agency mirror (UDD three-object split, 2026-10-02)", () => {
|
|
1020
|
+
it("ONE definition: the Org mount and the Agency mount add the same three paths with the same enum", () => {
|
|
1021
|
+
const org = addOrgTypeFields(
|
|
1022
|
+
new Schema({ name: String, type: { type: Number, min: 10, max: 31 } }),
|
|
1023
|
+
); // PAVE's numeric Organization.type
|
|
1024
|
+
const agency = addAgencyOrgTypeFields(
|
|
1025
|
+
new Schema({ name: String, organizationId: Schema.Types.ObjectId }),
|
|
1026
|
+
);
|
|
1027
|
+
expect(agencyOrgTypeSchemaDefinition).toBe(orgTypeSchemaDefinition);
|
|
1028
|
+
expect([...ORG_TYPE_FIELD_NAMES]).toEqual([
|
|
1029
|
+
"type",
|
|
1030
|
+
"vsp",
|
|
1031
|
+
"vspPrivateNonprofit",
|
|
1032
|
+
]);
|
|
1033
|
+
for (const f of ORG_TYPE_FIELD_NAMES) {
|
|
1034
|
+
expect(org.path(f).instance, f).toBe(agency.path(f).instance);
|
|
1035
|
+
}
|
|
1036
|
+
// The numeric Organization.type is REPLACED by the key enum (the migration the Org mount is for).
|
|
1037
|
+
const t = org.path("type") as unknown as {
|
|
1038
|
+
instance: string;
|
|
1039
|
+
options: { enum?: string[]; min?: number };
|
|
1040
|
+
};
|
|
1041
|
+
expect(t.instance).toBe("String");
|
|
1042
|
+
expect(t.options.enum).toEqual([...AGENCY_TYPE_KEYS]);
|
|
1043
|
+
expect(t.options.min).toBeUndefined();
|
|
1044
|
+
// An Agency keeps its FK to the Org — that join is what makes an Org-with-an-Account an Agency.
|
|
1045
|
+
expect(agency.path("organizationId").instance).toBe("ObjectId");
|
|
1046
|
+
});
|
|
1047
|
+
|
|
1048
|
+
it("mirrorOrgTypeFields copies exactly the three fields, keeps absent absent, never coerces a false", () => {
|
|
1049
|
+
expect(
|
|
1050
|
+
mirrorOrgTypeFields({
|
|
1051
|
+
_id: "org-1",
|
|
1052
|
+
name: "Tulsa PD",
|
|
1053
|
+
type: "local_law_enforcement",
|
|
1054
|
+
vsp: false,
|
|
1055
|
+
requiresRoi: true,
|
|
1056
|
+
website: "x",
|
|
1057
|
+
} as Record<string, unknown>),
|
|
1058
|
+
).toEqual({ type: "local_law_enforcement", vsp: false });
|
|
1059
|
+
// Never attested on the Org → never attested on the Agency (tri-state, GDR § 1.2).
|
|
1060
|
+
const m = mirrorOrgTypeFields({ type: "child_advocacy_center" });
|
|
1061
|
+
expect(m).toEqual({ type: "child_advocacy_center" });
|
|
1062
|
+
expect("vsp" in m).toBe(false);
|
|
1063
|
+
expect(mirrorOrgTypeFields(null)).toEqual({});
|
|
1064
|
+
expect(mirrorOrgTypeFields(undefined)).toEqual({});
|
|
1065
|
+
// The copy validates against the Agency mount (round trip).
|
|
1066
|
+
const Agency = compile(
|
|
1067
|
+
addAgencyOrgTypeFields(new Schema({ name: String })),
|
|
1068
|
+
);
|
|
1069
|
+
const doc = new Agency({
|
|
1070
|
+
name: "a",
|
|
1071
|
+
...mirrorOrgTypeFields({
|
|
1072
|
+
type: "victim_service_provider",
|
|
1073
|
+
vsp: true,
|
|
1074
|
+
vspPrivateNonprofit: true,
|
|
1075
|
+
}),
|
|
1076
|
+
});
|
|
1077
|
+
expect(doc.validateSync()).toBeUndefined();
|
|
1078
|
+
});
|
|
1079
|
+
|
|
1080
|
+
it("Zod: the HMIS VSP two-step — step 2 only when step 1 is true", () => {
|
|
1081
|
+
expect(
|
|
1082
|
+
SetOrgTypeInputSchema.safeParse({
|
|
1083
|
+
type: "victim_service_provider",
|
|
1084
|
+
vsp: true,
|
|
1085
|
+
vspPrivateNonprofit: true,
|
|
1086
|
+
}).success,
|
|
1087
|
+
).toBe(true);
|
|
1088
|
+
expect(
|
|
1089
|
+
SetOrgTypeInputSchema.safeParse({
|
|
1090
|
+
type: "victim_service_provider",
|
|
1091
|
+
vsp: true,
|
|
1092
|
+
vspPrivateNonprofit: false,
|
|
1093
|
+
}).success,
|
|
1094
|
+
).toBe(true);
|
|
1095
|
+
expect(
|
|
1096
|
+
SetOrgTypeInputSchema.safeParse({ type: "victim_service_provider" })
|
|
1097
|
+
.success,
|
|
1098
|
+
).toBe(true);
|
|
1099
|
+
for (const bad of [
|
|
1100
|
+
{ type: "victim_service_provider", vspPrivateNonprofit: true },
|
|
1101
|
+
{
|
|
1102
|
+
type: "victim_service_provider",
|
|
1103
|
+
vsp: false,
|
|
1104
|
+
vspPrivateNonprofit: true,
|
|
1105
|
+
},
|
|
1106
|
+
{
|
|
1107
|
+
type: "victim_service_provider",
|
|
1108
|
+
vsp: false,
|
|
1109
|
+
vspPrivateNonprofit: false,
|
|
1110
|
+
},
|
|
1111
|
+
]) {
|
|
1112
|
+
const r = SetOrgTypeInputSchema.safeParse(bad);
|
|
1113
|
+
expect(r.success, JSON.stringify(bad)).toBe(false);
|
|
1114
|
+
if (!r.success)
|
|
1115
|
+
expect(r.error.issues[0]?.path).toEqual(["vspPrivateNonprofit"]);
|
|
1116
|
+
expect(
|
|
1117
|
+
AgencyOrgTypeFieldsSchema.safeParse(bad).success,
|
|
1118
|
+
JSON.stringify(bad),
|
|
1119
|
+
).toBe(false);
|
|
1120
|
+
}
|
|
1121
|
+
// The referral-only leaf is not a settable org type.
|
|
1122
|
+
expect(
|
|
1123
|
+
SetOrgTypeInputSchema.safeParse({ type: "own_agency_internal" }).success,
|
|
1124
|
+
).toBe(false);
|
|
1125
|
+
expect(
|
|
1126
|
+
SetOrgTypeInputSchema.safeParse({ type: "school_academic_institution" })
|
|
1127
|
+
.success,
|
|
1128
|
+
).toBe(true);
|
|
1129
|
+
});
|
|
1130
|
+
});
|
|
1131
|
+
|
|
1132
|
+
// ─── Review round ORG-TYPE-REVIEW-ROUND-2026-10-08 (Kirby blockers 1 + 2, should-fixes, nits) ─────
|
|
1133
|
+
|
|
1134
|
+
describe("Blocker 1 — read precedence: the Org wins once linked", () => {
|
|
1135
|
+
const org = { type: "victim_service_provider", vsp: true, website: "x" };
|
|
1136
|
+
const staleCopy = { type: "local_law_enforcement", vsp: false };
|
|
1137
|
+
|
|
1138
|
+
it("a LINKED agency reads its Org, not its copy", () => {
|
|
1139
|
+
const agency = { organizationId: "org-1", ...staleCopy };
|
|
1140
|
+
expect(resolveEffectiveOrgTypeFields(org, agency)).toEqual({
|
|
1141
|
+
type: "victim_service_provider",
|
|
1142
|
+
vsp: true,
|
|
1143
|
+
});
|
|
1144
|
+
expect(resolveEffectiveOrgType(org, agency)).toBe(
|
|
1145
|
+
"victim_service_provider",
|
|
1146
|
+
);
|
|
1147
|
+
});
|
|
1148
|
+
|
|
1149
|
+
it("an UNLINKED agency reads its own copy (the backfill's confirmed value)", () => {
|
|
1150
|
+
const agency = { organizationId: null, ...staleCopy };
|
|
1151
|
+
expect(resolveEffectiveOrgTypeFields(org, agency)).toEqual(staleCopy);
|
|
1152
|
+
expect(resolveEffectiveOrgType(org, agency)).toBe("local_law_enforcement");
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
it("linked but the caller did not join the Org → the copy (no guessing, no throw)", () => {
|
|
1156
|
+
const agency = { organizationId: "org-1", ...staleCopy };
|
|
1157
|
+
expect(resolveEffectiveOrgTypeFields(null, agency)).toEqual(staleCopy);
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1160
|
+
it("neither carries anything → {} / null — 'answers nobody rather than guessing' (2026-10-07)", () => {
|
|
1161
|
+
expect(
|
|
1162
|
+
resolveEffectiveOrgTypeFields(null, { organizationId: null }),
|
|
1163
|
+
).toEqual({});
|
|
1164
|
+
expect(resolveEffectiveOrgType(null, null)).toBeNull();
|
|
1165
|
+
expect(resolveEffectiveOrgType({}, { organizationId: "org-1" })).toBeNull();
|
|
1166
|
+
});
|
|
1167
|
+
|
|
1168
|
+
it("orgTypeFieldsDiffer names the drifted fields; absent and null are equal ('not captured')", () => {
|
|
1169
|
+
expect(
|
|
1170
|
+
orgTypeFieldsDiffer(org, { type: "victim_service_provider", vsp: true }),
|
|
1171
|
+
).toEqual([]);
|
|
1172
|
+
expect(orgTypeFieldsDiffer(org, staleCopy)).toEqual(["type", "vsp"]);
|
|
1173
|
+
expect(
|
|
1174
|
+
orgTypeFieldsDiffer({ type: "hotline", vsp: null }, { type: "hotline" }),
|
|
1175
|
+
).toEqual([]);
|
|
1176
|
+
expect(
|
|
1177
|
+
orgTypeFieldsDiffer({ type: "hotline" }, { type: "hotline", vsp: false }),
|
|
1178
|
+
).toEqual(["vsp"]);
|
|
1179
|
+
expect(orgTypeFieldsDiffer(null, null)).toEqual([]);
|
|
1180
|
+
});
|
|
1181
|
+
});
|
|
1182
|
+
|
|
1183
|
+
describe("Blocker 2 — the VSP two-step is enforced at the mount and in the mirror, tri-state preserved", () => {
|
|
1184
|
+
it("the predicate: step 2 needs step 1 true; absent / null step 2 always holds", () => {
|
|
1185
|
+
expect(vspTwoStepHolds({})).toBe(true);
|
|
1186
|
+
expect(vspTwoStepHolds({ vsp: false })).toBe(true);
|
|
1187
|
+
expect(vspTwoStepHolds({ vsp: true, vspPrivateNonprofit: true })).toBe(
|
|
1188
|
+
true,
|
|
1189
|
+
);
|
|
1190
|
+
expect(vspTwoStepHolds({ vsp: true, vspPrivateNonprofit: false })).toBe(
|
|
1191
|
+
true,
|
|
1192
|
+
);
|
|
1193
|
+
expect(vspTwoStepHolds({ vsp: false, vspPrivateNonprofit: null })).toBe(
|
|
1194
|
+
true,
|
|
1195
|
+
);
|
|
1196
|
+
expect(vspTwoStepHolds({ vspPrivateNonprofit: true })).toBe(false);
|
|
1197
|
+
expect(vspTwoStepHolds({ vsp: false, vspPrivateNonprofit: false })).toBe(
|
|
1198
|
+
false,
|
|
1199
|
+
);
|
|
1200
|
+
expect(vspTwoStepHolds({ vsp: null, vspPrivateNonprofit: true })).toBe(
|
|
1201
|
+
false,
|
|
1202
|
+
);
|
|
1203
|
+
});
|
|
1204
|
+
|
|
1205
|
+
it("on save: a malformed document fails validation on `vspPrivateNonprofit`; a well-formed or never-captured one passes", async () => {
|
|
1206
|
+
// `validate()` (not `validateSync()`) runs the pre('validate') middleware — the path `save()` takes.
|
|
1207
|
+
const errOf = async (doc: { validate: () => Promise<void> }) => {
|
|
1208
|
+
try {
|
|
1209
|
+
await doc.validate();
|
|
1210
|
+
return undefined;
|
|
1211
|
+
} catch (e) {
|
|
1212
|
+
return e as { errors: Record<string, { message: string }> };
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
const M = compile(addAgencyOrgTypeFields(new Schema({ name: String })));
|
|
1216
|
+
const bad = await errOf(
|
|
1217
|
+
new M({
|
|
1218
|
+
name: "x",
|
|
1219
|
+
type: "victim_service_provider",
|
|
1220
|
+
vspPrivateNonprofit: true,
|
|
1221
|
+
}),
|
|
1222
|
+
);
|
|
1223
|
+
expect(bad?.errors.vspPrivateNonprofit?.message).toMatch(/step 2/);
|
|
1224
|
+
expect(
|
|
1225
|
+
await errOf(
|
|
1226
|
+
new M({
|
|
1227
|
+
name: "x",
|
|
1228
|
+
type: "victim_service_provider",
|
|
1229
|
+
vsp: true,
|
|
1230
|
+
vspPrivateNonprofit: true,
|
|
1231
|
+
}),
|
|
1232
|
+
),
|
|
1233
|
+
).toBeUndefined();
|
|
1234
|
+
expect(
|
|
1235
|
+
await errOf(new M({ name: "x", type: "victim_service_provider" })),
|
|
1236
|
+
).toBeUndefined();
|
|
1237
|
+
expect(
|
|
1238
|
+
await errOf(
|
|
1239
|
+
new M({ name: "x", type: "child_advocacy_center", vsp: false }),
|
|
1240
|
+
),
|
|
1241
|
+
).toBeUndefined();
|
|
1242
|
+
// Never coerced: a never-captured step stays absent after validation.
|
|
1243
|
+
const doc = new M({ name: "x", type: "child_advocacy_center" });
|
|
1244
|
+
await doc.validate();
|
|
1245
|
+
expect(doc.get("vsp")).toBeUndefined();
|
|
1246
|
+
expect(doc.get("vspPrivateNonprofit")).toBeUndefined();
|
|
1247
|
+
// The Org mount carries the same hook.
|
|
1248
|
+
const O = compile(addOrgTypeFields(new Schema({ name: String })));
|
|
1249
|
+
const orgErr = await errOf(
|
|
1250
|
+
new O({ name: "o", vspPrivateNonprofit: false }),
|
|
1251
|
+
);
|
|
1252
|
+
expect(orgErr?.errors.vspPrivateNonprofit).toBeDefined();
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
it("on update: step 2 is only ever written together with `vsp: true` (top level or $set); clearing always holds", () => {
|
|
1256
|
+
expect(
|
|
1257
|
+
vspTwoStepHoldsOnUpdate({
|
|
1258
|
+
$set: { vsp: true, vspPrivateNonprofit: true },
|
|
1259
|
+
}),
|
|
1260
|
+
).toBe(true);
|
|
1261
|
+
expect(
|
|
1262
|
+
vspTwoStepHoldsOnUpdate({ vsp: true, vspPrivateNonprofit: false }),
|
|
1263
|
+
).toBe(true);
|
|
1264
|
+
expect(
|
|
1265
|
+
vspTwoStepHoldsOnUpdate({ $set: { vspPrivateNonprofit: true } }),
|
|
1266
|
+
).toBe(false);
|
|
1267
|
+
expect(
|
|
1268
|
+
vspTwoStepHoldsOnUpdate({
|
|
1269
|
+
$set: { vsp: false, vspPrivateNonprofit: true },
|
|
1270
|
+
}),
|
|
1271
|
+
).toBe(false);
|
|
1272
|
+
expect(
|
|
1273
|
+
vspTwoStepHoldsOnUpdate({ $set: { vspPrivateNonprofit: null } }),
|
|
1274
|
+
).toBe(true);
|
|
1275
|
+
expect(
|
|
1276
|
+
vspTwoStepHoldsOnUpdate({ $unset: { vspPrivateNonprofit: 1 } }),
|
|
1277
|
+
).toBe(true);
|
|
1278
|
+
expect(vspTwoStepHoldsOnUpdate({ $set: { type: "hotline" } })).toBe(true);
|
|
1279
|
+
expect(vspTwoStepHoldsOnUpdate(null)).toBe(true);
|
|
1280
|
+
});
|
|
1281
|
+
|
|
1282
|
+
it("the update-op hook is installed even with validateOnUpdate: false (a shape rule, not a validator option)", () => {
|
|
1283
|
+
for (const opt of [{}, { validateOnUpdate: false }]) {
|
|
1284
|
+
const schema = addAgencyOrgTypeFields(new Schema({ name: String }), opt);
|
|
1285
|
+
type Pres = { s: { hooks: { _pres: Map<string, unknown[]> } } };
|
|
1286
|
+
const hooks = (schema as unknown as Pres).s.hooks._pres;
|
|
1287
|
+
for (const op of AGENCY_ORG_TYPE_VALIDATED_UPDATE_OPS) {
|
|
1288
|
+
expect(
|
|
1289
|
+
hooks.get(op)?.length ?? 0,
|
|
1290
|
+
`${op} ${JSON.stringify(opt)}`,
|
|
1291
|
+
).toBeGreaterThan(0);
|
|
1292
|
+
}
|
|
1293
|
+
expect(hooks.get("validate")?.length ?? 0).toBeGreaterThan(0);
|
|
1294
|
+
}
|
|
1295
|
+
});
|
|
1296
|
+
|
|
1297
|
+
it("the mirror asserts the two-step — a malformed Org is not laundered onto the agency — and drops a null (review nit)", () => {
|
|
1298
|
+
expect(() =>
|
|
1299
|
+
mirrorOrgTypeFields({
|
|
1300
|
+
type: "victim_service_provider",
|
|
1301
|
+
vspPrivateNonprofit: true,
|
|
1302
|
+
}),
|
|
1303
|
+
).toThrow(/step 2/);
|
|
1304
|
+
expect(
|
|
1305
|
+
mirrorOrgTypeFields({
|
|
1306
|
+
type: "hotline",
|
|
1307
|
+
vsp: null,
|
|
1308
|
+
vspPrivateNonprofit: null,
|
|
1309
|
+
}),
|
|
1310
|
+
).toEqual({ type: "hotline" });
|
|
1311
|
+
});
|
|
1312
|
+
|
|
1313
|
+
it("withOrgTypeFields() adds the fields AND the rule in one call (the extend-without-refine footgun is closed)", () => {
|
|
1314
|
+
const S = withOrgTypeFields({ name: z.string() });
|
|
1315
|
+
expect(
|
|
1316
|
+
S.safeParse({
|
|
1317
|
+
name: "x",
|
|
1318
|
+
type: "hotline",
|
|
1319
|
+
vsp: true,
|
|
1320
|
+
vspPrivateNonprofit: true,
|
|
1321
|
+
}).success,
|
|
1322
|
+
).toBe(true);
|
|
1323
|
+
expect(
|
|
1324
|
+
S.safeParse({ name: "x", type: "hotline", vspPrivateNonprofit: true })
|
|
1325
|
+
.success,
|
|
1326
|
+
).toBe(false);
|
|
1327
|
+
expect(S.safeParse({ name: "x" }).success).toBe(true);
|
|
1328
|
+
expect(S.safeParse({ name: "x", type: "hotlines" }).success).toBe(false); // the heading, not the leaf
|
|
1329
|
+
});
|
|
1330
|
+
});
|
|
1331
|
+
|
|
1332
|
+
describe("should-fix — key → parent key companion map (no code-prefix arithmetic)", () => {
|
|
1333
|
+
it("every org-half key is present; headings map to null; the hotline children map to the `hotline` LEAF, not the `hotlines` heading", () => {
|
|
1334
|
+
for (const k of ORG_TYPE_KEYS)
|
|
1335
|
+
expect(k in ORG_TYPE_PARENT_KEY, k).toBe(true);
|
|
1336
|
+
for (const h of ORG_TYPE_HEADING_KEYS)
|
|
1337
|
+
expect(ORG_TYPE_PARENT_KEY[h], h).toBeNull();
|
|
1338
|
+
expect(ORG_TYPE_PARENT_KEY.hotline).toBe("hotlines");
|
|
1339
|
+
expect(ORG_TYPE_PARENT_KEY.national_hotline).toBe("hotline");
|
|
1340
|
+
expect(ORG_TYPE_PARENT_KEY.local_other_hotline).toBe("hotline");
|
|
1341
|
+
expect(ORG_TYPE_PARENT_KEY.local_law_enforcement).toBe("law_enforcement");
|
|
1342
|
+
expect(ORG_TYPE_PARENT_KEY.law_enforcement).toBe(
|
|
1343
|
+
"justice_protective_systems",
|
|
1344
|
+
);
|
|
1345
|
+
// Agrees with the ancestry walk for every key.
|
|
1346
|
+
for (const k of ORG_TYPE_KEYS) {
|
|
1347
|
+
const chain = orgTypeAncestry(k);
|
|
1348
|
+
expect(ORG_TYPE_PARENT_KEY[k], k).toBe(
|
|
1349
|
+
chain.length > 1 ? chain[1] : null,
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
});
|