@htilabs/platform-common-models 0.29.0 → 0.30.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,782 @@
1
+ /**
2
+ * The shared in-app partner add (pl-bt1, pl-e98, 0.30.0): `mintPartnerContact`
3
+ * and `ensurePocIndexes` over an in-memory double that enforces the unique
4
+ * indexes (`./mongoDouble.ts`). The cases are the platform store's own
5
+ * `addPartnerContact` suite (platform `test/lib/points-of-contact/repository.test.ts`
6
+ * § "addPartnerContact" and the index cases), with stored rows seeded directly
7
+ * where the platform's suite used its other writers. The same writer against a
8
+ * real MongoDB: `./writer.mongo.test.ts`.
9
+ */
10
+ import mongoose from "mongoose";
11
+ import type { Db } from "mongodb";
12
+ import { beforeEach, describe, expect, it } from "vitest";
13
+
14
+ import {
15
+ POC_IN_APP_PENDING_CAP_PER_ACTOR,
16
+ POC_IN_APP_PENDING_CAP_PER_PARTNER,
17
+ POC_IN_APP_PENDING_INDEX,
18
+ POC_IN_APP_PENDING_INDEX_FILTER,
19
+ POC_IN_APP_PENDING_INDEX_KEY,
20
+ POC_PARTNER_ADD_APPS,
21
+ POINTS_OF_CONTACT_COLLECTION,
22
+ POINT_OF_CONTACT_INDEXES,
23
+ PARTNER_CONTACT_FAILURE_REASONS,
24
+ checkPocIndexes,
25
+ ensurePocIndexes,
26
+ mintPartnerContact,
27
+ pocDuplicateKeyFailure,
28
+ pocIdentityKey,
29
+ pocRowsWithIdentity,
30
+ pocRuleViolations,
31
+ type PartnerContactResult,
32
+ type MintPartnerContactArgs,
33
+ type PocInAppActor,
34
+ type PocWriteDbLike,
35
+ type PocWriterDriver,
36
+ type PointOfContactDoc,
37
+ } from "../index.js";
38
+ import { mongoDouble, type Doc, type MongoDouble } from "./mongoDouble.js";
39
+
40
+ const { ObjectId } = mongoose.mongo;
41
+ const DRIVER = { ObjectId };
42
+
43
+ // A mongodb `Db` and a mongoose connection's `.db` are both the writer's handle.
44
+ const _driverDb: PocWriteDbLike = null as unknown as Db;
45
+ const _mongooseDb: PocWriteDbLike = null as unknown as NonNullable<mongoose.Connection["db"]>;
46
+ const _driver: PocWriterDriver = DRIVER;
47
+ const _mongooseDriver: PocWriterDriver = { ObjectId: mongoose.Types.ObjectId };
48
+ void [_driverDb, _mongooseDb, _driver, _mongooseDriver];
49
+
50
+ const NOW = new Date("2026-10-14T15:00:00Z");
51
+ const REF = "REFERRALS_MAGIC_LINK";
52
+
53
+ const AGENCY_A = "a0a0a0a0a0a0a0a0a0a0a0a0";
54
+ const AGENCY_B = "b0b0b0b0b0b0b0b0b0b0b0b0";
55
+ const AGENCY_D = "d0d0d0d0d0d0d0d0d0d0d0d0"; // organizationId names an org that does not name it back
56
+ const ORG_A = "0a0a0a0a0a0a0a0a0a0a0a0a"; // agency A's genuine mirror org
57
+ const ORG_A2 = "0a2a0a0a0a0a0a0a0a0a0a0a"; // an org agency A CREATED (organizations.agencyId = A only)
58
+ const ORG_B = "0b0b0b0b0b0b0b0b0b0b0b0b"; // agency B's genuine mirror (ids stored as strings)
59
+ const ORG_C = "0c0c0c0c0c0c0c0c0c0c0c0c"; // an org-only partner: no agency
60
+
61
+ const ADVOCATE: PocInAppActor = { id: "auth0|advocate" };
62
+ const CAROL: PocInAppActor = { id: "auth0|carol" };
63
+ const DESK = {
64
+ display_name: "Intake Desk",
65
+ emails: [{ value: "Desk@C.org" }],
66
+ phones: [{ value: "555-0199" }],
67
+ };
68
+
69
+ let m: MongoDouble;
70
+ let db: PocWriteDbLike;
71
+ const rows = () => m.store.get(POINTS_OF_CONTACT_COLLECTION) ?? [];
72
+ const row = (id: string) => rows().find((r) => r.id === id)!;
73
+ const idOf = (r: PartnerContactResult) => (r.ok ? r.value.id : "(refused)");
74
+
75
+ const add = (
76
+ partner: unknown,
77
+ contact: Record<string, unknown>,
78
+ actor: unknown = ADVOCATE,
79
+ app: unknown = "thread",
80
+ handle: PocWriteDbLike = db,
81
+ ) =>
82
+ mintPartnerContact(
83
+ handle,
84
+ { app, partner, contact, actor, now: NOW } as unknown as MintPartnerContactArgs,
85
+ DRIVER,
86
+ );
87
+
88
+ let seq = 0;
89
+ /** A stored row as the platform's other writers leave it (Admin Hub: confirmed, origin admin_hub). */
90
+ function stored(over: Partial<PointOfContactDoc> & { emails: PointOfContactDoc["emails"] }): Doc {
91
+ const base = {
92
+ user_id: null,
93
+ person_id: null,
94
+ display_name: over.display_name ?? "Someone",
95
+ emails: over.emails,
96
+ };
97
+ const doc: PointOfContactDoc = {
98
+ id: `poc-seeded-${++seq}`,
99
+ subject_type: "agency",
100
+ subject_id: AGENCY_A,
101
+ agency_id: AGENCY_A,
102
+ org_id: ORG_A,
103
+ ...base,
104
+ phones: [],
105
+ purposes: [REF],
106
+ urgent_methods: [],
107
+ is_primary: false,
108
+ is_confidential: false,
109
+ active: true,
110
+ notes: null,
111
+ status: "confirmed",
112
+ origin: "admin_hub",
113
+ added_in_app: "platform",
114
+ identity_key: pocIdentityKey(base),
115
+ confirmed_by: null,
116
+ confirmed_at: null,
117
+ deactivated_by: null,
118
+ deactivated_at: null,
119
+ version: 1,
120
+ createdBy: "auth0|alice",
121
+ createdAt: "2026-10-01T00:00:00.000Z",
122
+ updatedBy: "auth0|alice",
123
+ updatedAt: "2026-10-01T00:00:00.000Z",
124
+ ...over,
125
+ };
126
+ const list = m.store.get(POINTS_OF_CONTACT_COLLECTION) ?? [];
127
+ list.push({ ...doc });
128
+ m.store.set(POINTS_OF_CONTACT_COLLECTION, list);
129
+ return doc;
130
+ }
131
+ const one = (value: string) => [{ value, is_preferred: true }];
132
+
133
+ beforeEach(async () => {
134
+ m = mongoDouble();
135
+ db = m.handle();
136
+ // The platform creates the indexes (its own handle); the writer only checks them.
137
+ await ensurePocIndexes(m.handle());
138
+ m.store.set("agencies", [
139
+ { _id: new ObjectId(AGENCY_A), entity_id: "agency-a", name: "A", organizationId: new ObjectId(ORG_A) },
140
+ { _id: new ObjectId(AGENCY_B), entity_id: "agency-b", name: "B", organizationId: ORG_B },
141
+ { _id: new ObjectId(AGENCY_D), entity_id: "agency-d", name: "D", organizationId: new ObjectId(ORG_C) },
142
+ ]);
143
+ m.store.set("registry_agencies", [
144
+ { _id: "reg-row-a", entity_id: "agency-htilabs-a", id: AGENCY_A }, // maps to A by A's String(_id)
145
+ { _id: "reg-row-z", entity_id: "agency-registry-only" }, // no agencies row carries its forms
146
+ ]);
147
+ m.store.set("organizations", [
148
+ { _id: new ObjectId(ORG_A), name: "A", agencyId: new ObjectId(AGENCY_A) },
149
+ { _id: new ObjectId(ORG_A2), name: "A's referral partner", agencyId: new ObjectId(AGENCY_A) },
150
+ { _id: new ObjectId(ORG_B), name: "B", agencyId: AGENCY_B },
151
+ { _id: new ObjectId(ORG_C), name: "C (no agency)" },
152
+ ]);
153
+ });
154
+
155
+ describe("mintPartnerContact: the shared in-app partner add (pl-bt1)", () => {
156
+ it("an off-platform org: a pending contact on the org, tagged Referrals (magic link), with a receipt and nothing else", async () => {
157
+ const r = await add({ org_id: ORG_C.toUpperCase() }, DESK);
158
+ expect(r).toEqual({
159
+ ok: true,
160
+ value: {
161
+ id: expect.stringMatching(/^poc-[0-9a-f]{24}$/),
162
+ status: "pending",
163
+ created: true,
164
+ subject_type: "org",
165
+ subject_id: ORG_C,
166
+ agency_id: null,
167
+ org_id: ORG_C,
168
+ },
169
+ });
170
+ expect(row(idOf(r))).toEqual({
171
+ id: idOf(r),
172
+ subject_type: "org",
173
+ subject_id: ORG_C,
174
+ agency_id: null,
175
+ org_id: ORG_C,
176
+ user_id: null,
177
+ person_id: null,
178
+ display_name: "Intake Desk",
179
+ emails: [{ value: "desk@c.org", is_preferred: true }],
180
+ phones: [{ value: "555-0199", is_preferred: true }],
181
+ purposes: [REF],
182
+ urgent_methods: [],
183
+ is_primary: false,
184
+ is_confidential: false,
185
+ active: true,
186
+ notes: null,
187
+ status: "pending",
188
+ origin: "in_app",
189
+ added_in_app: "thread",
190
+ identity_key: "writein:intake desk|desk@c.org",
191
+ confirmed_by: null,
192
+ confirmed_at: null,
193
+ deactivated_by: null,
194
+ deactivated_at: null,
195
+ version: 1,
196
+ createdBy: "auth0|advocate",
197
+ createdAt: NOW.toISOString(),
198
+ updatedBy: "auth0|advocate",
199
+ updatedAt: NOW.toISOString(),
200
+ });
201
+ });
202
+
203
+ it("owner resolution: an agency in any accepted form, an org with a genuine-mirror agency lands on the agency (K-D1), a created-by org stays an org", async () => {
204
+ for (const form of ["agency-a", AGENCY_A, AGENCY_A.toUpperCase(), "agency-htilabs-a"]) {
205
+ const r = await add({ agency_id: form }, { display_name: "A", emails: [{ value: `${form}@a.org` }] });
206
+ expect(r, form).toMatchObject({
207
+ ok: true,
208
+ value: { subject_type: "agency", subject_id: AGENCY_A, agency_id: AGENCY_A, org_id: ORG_A, status: "pending" },
209
+ });
210
+ }
211
+ expect(await add({ org_id: ORG_B }, { display_name: "B", emails: [{ value: "desk@b.org" }] })).toMatchObject({
212
+ value: { subject_type: "agency", subject_id: AGENCY_B, agency_id: AGENCY_B, org_id: ORG_B },
213
+ });
214
+ expect(await add({ agency_id: AGENCY_B, org_id: ORG_B }, { display_name: "B2", emails: [{ value: "two@b.org" }] })).toMatchObject({
215
+ value: { subject_type: "agency", subject_id: AGENCY_B },
216
+ });
217
+ expect(await add({ org_id: ORG_A2 }, { display_name: "O2", emails: [{ value: "o2@o2.org" }] })).toMatchObject({
218
+ value: { subject_type: "org", subject_id: ORG_A2, agency_id: null, org_id: ORG_A2 },
219
+ });
220
+ // D names ORG_C, but ORG_C does not name D back: D's rows carry no org, ORG_C stays org-only.
221
+ expect(await add({ agency_id: "agency-d" }, { display_name: "D", emails: [{ value: "d@d.org" }] })).toMatchObject({
222
+ value: { subject_type: "agency", subject_id: AGENCY_D, org_id: null },
223
+ });
224
+ expect(await add({ org_id: ORG_C }, { display_name: "C", emails: [{ value: "c@c.org" }] })).toMatchObject({
225
+ value: { subject_type: "org", agency_id: null },
226
+ });
227
+ });
228
+
229
+ it("refuses a partner that does not resolve or is not one partner, and a selector that is not just directory ids", async () => {
230
+ const body = { display_name: "X", emails: [{ value: "x@x.org" }] };
231
+ expect(await add({ org_id: "0f0f0f0f0f0f0f0f0f0f0f0f" }, body)).toMatchObject({ ok: false, status: 400, reason: "owner_not_found" });
232
+ expect(await add({ agency_id: "agency-zz" }, body)).toMatchObject({ status: 400, reason: "owner_not_found" });
233
+ expect(await add({ agency_id: "agency-registry-only" }, body)).toMatchObject({ reason: "owner_not_found" });
234
+ expect(await add({ agency_id: "agency-a", org_id: "0f0f0f0f0f0f0f0f0f0f0f0f" }, body)).toMatchObject({ reason: "owner_not_found" });
235
+ expect(await add({ agency_id: "agency-a", org_id: ORG_B }, body)).toMatchObject({ status: 400, reason: "owner_mismatch" });
236
+ expect(await add({ agency_id: "agency-a", org_id: ORG_A2 }, body)).toMatchObject({ reason: "owner_mismatch" });
237
+ expect(await add({ agency_id: "agency-d", org_id: ORG_C }, body)).toMatchObject({ reason: "owner_mismatch" });
238
+ for (const partner of [undefined, null, {}, { org_id: "" }, { org_id: ORG_C, subject_type: "program" }, { org_id: ORG_C, subject_id: "p" }, "agency-a"]) {
239
+ expect(await add(partner, body), JSON.stringify(partner)).toMatchObject({ status: 400, reason: "invalid_partner" });
240
+ }
241
+ expect(rows()).toHaveLength(0);
242
+ });
243
+
244
+ it("an email is required (an off-platform partner has no other way to be reached), a name too", async () => {
245
+ for (const contact of [
246
+ { display_name: "No Mail" },
247
+ { display_name: "Empty", emails: [] },
248
+ { display_name: "Bad", emails: [{ value: "not-an-email" }] },
249
+ { display_name: "Twice", emails: [{ value: "a@c.org" }, { value: "A@c.org" }] },
250
+ { emails: [{ value: "x@c.org" }] },
251
+ { display_name: "Single", email: "x@c.org" },
252
+ ]) {
253
+ expect(await add({ org_id: ORG_C }, contact), JSON.stringify(contact)).toMatchObject({ status: 400, reason: "invalid_contact" });
254
+ }
255
+ expect(rows()).toHaveLength(0);
256
+ });
257
+
258
+ it("the adder cannot set primary, confidential, notes, a seat / person link, a status or an owner; the app and actor are checked", async () => {
259
+ const body = { display_name: "D", emails: [{ value: "d@c.org" }] };
260
+ for (const [key, v] of [
261
+ ["is_primary", true],
262
+ ["is_confidential", true],
263
+ ["notes", "x"],
264
+ ["user_id", "5ea7a0000000000000000001"],
265
+ ["person_id", "p1"],
266
+ ["status", "confirmed"],
267
+ ["origin", "admin_hub"],
268
+ ["agency_id", AGENCY_A],
269
+ ["subject_type", "agency"],
270
+ ] as const) {
271
+ expect(await add({ org_id: ORG_C }, { ...body, [key]: v }), key).toMatchObject({ status: 400, reason: "invalid_contact" });
272
+ }
273
+ expect(POC_PARTNER_ADD_APPS).toEqual(["pave", "thread", "leads"]);
274
+ for (const app of ["platform", "bogus", "", "PAVE", null]) {
275
+ expect(await add({ org_id: ORG_C }, body, ADVOCATE, app), String(app)).toMatchObject({ status: 400, reason: "invalid_app" });
276
+ }
277
+ for (const actor of [{ id: "" }, { id: " " }, {}, { id: 7 }, null, "auth0|x"]) {
278
+ expect(await add({ org_id: ORG_C }, body, actor), JSON.stringify(actor)).toMatchObject({ status: 400, reason: "actor_required" });
279
+ }
280
+ // Checked in the platform's order: actor, app, contact, partner.
281
+ expect(await add("bad", { nope: 1 }, { id: "" }, "bogus")).toMatchObject({ reason: "actor_required" });
282
+ expect(await add("bad", { nope: 1 }, ADVOCATE, "bogus")).toMatchObject({ reason: "invalid_app" });
283
+ expect(await add("bad", { nope: 1 })).toMatchObject({ reason: "invalid_contact" });
284
+ expect(rows()).toHaveLength(0);
285
+ });
286
+
287
+ it("each app is stamped as added_in_app", async () => {
288
+ for (const app of POC_PARTNER_ADD_APPS) {
289
+ const r = await add({ org_id: ORG_C }, { display_name: app, emails: [{ value: `${app}@c.org` }] }, ADVOCATE, app);
290
+ expect(row(idOf(r))).toMatchObject({ added_in_app: app, origin: "in_app", status: "pending" });
291
+ }
292
+ });
293
+
294
+ it("idempotent per partner and email: the adder's retry, or a visible contact that already has the tag, answers with that contact", async () => {
295
+ const first = await add({ org_id: ORG_C }, DESK);
296
+ m.writes.length = 0;
297
+ const again = await add({ org_id: ORG_C.toUpperCase() }, { display_name: " intake desk ", emails: [{ value: "DESK@c.org" }] });
298
+ expect(again).toEqual({ ok: true, value: { ...(first.ok && first.value), created: false } });
299
+ expect(m.writes).toEqual([]);
300
+ expect(rows()).toHaveLength(1);
301
+
302
+ // Through the org or its agency, the same partner (K-D1).
303
+ const a = await add({ agency_id: "agency-a" }, { display_name: "A", emails: [{ value: "a@a.org" }] });
304
+ expect(await add({ org_id: ORG_A }, { display_name: "A", emails: [{ value: "a@a.org" }] })).toMatchObject({ value: { id: idOf(a), created: false } });
305
+
306
+ // A visible, tagged contact the partner already lists: nothing written, its status reported.
307
+ const listed = stored({ display_name: "Intake", emails: one("intake@a.org"), purposes: [REF, "TRAINING"] });
308
+ expect(await add({ agency_id: AGENCY_A }, { display_name: " INTAKE ", emails: [{ value: "Intake@A.org" }] })).toEqual({
309
+ ok: true,
310
+ value: { id: listed.id, status: "confirmed", created: false, subject_type: "agency", subject_id: AGENCY_A, agency_id: AGENCY_A, org_id: ORG_A },
311
+ });
312
+
313
+ // The adder's own pending add, then the partner lists the same person itself: the visible one answers.
314
+ const pend = idOf(await add({ agency_id: AGENCY_A }, { display_name: "Lee", emails: [{ value: "lee@a.org" }] }));
315
+ const own = stored({ display_name: "Lee", emails: one("lee@a.org") });
316
+ expect(own.id).not.toBe(pend);
317
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Lee", emails: [{ value: "lee@a.org" }] })).toMatchObject({
318
+ value: { id: own.id, status: "confirmed", created: false },
319
+ });
320
+ });
321
+
322
+ it("dedupe follows the identity rule (name + preferred email), never a shared mailbox: Jane, Dan and Eve each land", async () => {
323
+ const intake = (preferred: string) => [{ value: preferred, is_preferred: true }, { value: "intake@a.org" }];
324
+ // Jane shares Bob's secondary mailbox; Bob is a visible tagged contact. Jane is her own row.
325
+ const bob = stored({ display_name: "Bob", emails: [{ value: "bob@a.org", is_preferred: true }, { value: "intake@a.org", is_preferred: false }] });
326
+ const jane = await add({ agency_id: AGENCY_A }, { display_name: "Jane", emails: intake("jane@a.org") });
327
+ expect(jane).toMatchObject({ ok: true, value: { created: true, status: "pending" } });
328
+ expect(idOf(jane)).not.toBe(bob.id);
329
+ expect(row(idOf(jane))).toMatchObject({ display_name: "Jane", identity_key: "writein:jane|jane@a.org" });
330
+
331
+ // Dan shares the mailbox with the adder's own pending add (Jane): Dan is his own row too,
332
+ // and the pending unique index (identity_key, not emails) lets both wait.
333
+ const dan = await add({ agency_id: AGENCY_A }, { display_name: "Dan", emails: intake("dan@a.org") });
334
+ expect(dan).toMatchObject({ ok: true, value: { created: true } });
335
+ expect(new Set([idOf(jane), idOf(dan), bob.id]).size).toBe(3);
336
+
337
+ // A removed add of the adder's shares the mailbox: it does not block Eve (no false contact_deactivated).
338
+ stored({
339
+ display_name: "Gone",
340
+ emails: [{ value: "gone@a.org", is_preferred: true }, { value: "intake@a.org", is_preferred: false }],
341
+ status: "pending",
342
+ origin: "in_app",
343
+ added_in_app: "thread",
344
+ createdBy: ADVOCATE.id,
345
+ active: false,
346
+ });
347
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Eve", emails: intake("eve@a.org") })).toMatchObject({
348
+ ok: true,
349
+ value: { created: true },
350
+ });
351
+ expect(rows().filter((r) => r.origin === "in_app" && r.active)).toHaveLength(3);
352
+ });
353
+
354
+ it("the same name and preferred email is one person: a second add by the adder answers with the first, whatever its other emails", async () => {
355
+ const first = await add({ org_id: ORG_C }, { display_name: "Desk", emails: [{ value: "desk@c.org" }, { value: "a@c.org" }] });
356
+ const again = await add({ org_id: ORG_C }, { display_name: " desk ", emails: [{ value: "b@c.org" }, { value: "DESK@c.org", is_preferred: true }] });
357
+ expect(again).toMatchObject({ ok: true, value: { id: idOf(first), created: false } });
358
+ // The same mailbox under another preferred email, or another name, is another identity.
359
+ expect(await add({ org_id: ORG_C }, { display_name: "Desk", emails: [{ value: "a@c.org" }, { value: "desk@c.org" }] })).toMatchObject({
360
+ value: { created: true },
361
+ });
362
+ expect(await add({ org_id: ORG_C }, { display_name: "Front", emails: [{ value: "desk@c.org" }] })).toMatchObject({ value: { created: true } });
363
+ // A visible tagged contact answers only on its identity too.
364
+ const listed = stored({ display_name: "Intake", emails: [{ value: "intake@a.org", is_preferred: true }, { value: "front@a.org", is_preferred: false }] });
365
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Intake", emails: [{ value: "intake@a.org" }] })).toMatchObject({
366
+ value: { id: listed.id, created: false },
367
+ });
368
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Intake", emails: [{ value: "front@a.org" }] })).toMatchObject({
369
+ value: { created: true },
370
+ });
371
+ });
372
+
373
+ it("not idempotent across what the caller may not see: another adder's pending row, a confidential contact, an untagged or removed one", async () => {
374
+ const mine = idOf(await add({ org_id: ORG_C }, DESK));
375
+ const carols = await add({ org_id: ORG_C }, DESK, CAROL);
376
+ expect(carols).toMatchObject({ ok: true, value: { created: true, status: "pending" } });
377
+ expect(idOf(carols)).not.toBe(mine);
378
+
379
+ stored({ display_name: "Conf", emails: one("conf@a.org"), is_confidential: true });
380
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Conf", emails: [{ value: "conf@a.org" }] })).toMatchObject({ value: { created: true, status: "pending" } });
381
+
382
+ stored({ display_name: "Billing", emails: one("bill@a.org"), purposes: ["BILLING"] });
383
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Billing", emails: [{ value: "bill@a.org" }] })).toMatchObject({ value: { created: true } });
384
+
385
+ stored({ display_name: "Gone", emails: one("gone@a.org"), active: false });
386
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Gone", emails: [{ value: "gone@a.org" }] })).toMatchObject({ value: { created: true } });
387
+
388
+ // The adder's own pending add on ANOTHER partner is not this partner's.
389
+ expect(await add({ org_id: ORG_A2 }, DESK)).toMatchObject({
390
+ value: { created: true, subject_id: ORG_A2 },
391
+ });
392
+ });
393
+
394
+ it("always carries Referrals (magic link): an explicit [] or a list without it gets it, and the visible match is never vacuous", async () => {
395
+ const none = await add({ org_id: ORG_C }, { display_name: "None", emails: [{ value: "none@c.org" }], purposes: [] });
396
+ expect(row(idOf(none)).purposes).toEqual([REF]);
397
+ const billing = await add({ org_id: ORG_C }, { display_name: "Bill", emails: [{ value: "bill@c.org" }], purposes: ["BILLING", REF] });
398
+ expect(row(idOf(billing)).purposes).toEqual([REF, "BILLING"]);
399
+ stored({ display_name: "Billing", emails: one("billing@a.org"), purposes: ["BILLING"] });
400
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Billing", emails: [{ value: "billing@a.org" }], purposes: [] })).toMatchObject({
401
+ value: { created: true, status: "pending" },
402
+ });
403
+ });
404
+
405
+ it("the adder's own pending add answers a retry whatever purposes it asks for (one outstanding add per adder, subject and email)", async () => {
406
+ const first = idOf(await add({ org_id: ORG_C }, DESK));
407
+ expect(await add({ org_id: ORG_C }, { ...DESK, purposes: ["TRAINING"] })).toMatchObject({ value: { id: first, created: false } });
408
+ expect(rows()).toHaveLength(1);
409
+ });
410
+
411
+ it("two simultaneous identical first adds leave one row; the loser answers with the winner's", async () => {
412
+ const other = { ...DESK, emails: [{ value: "desk@c.org" }, { value: "second@c.org" }] };
413
+ const [a, b] = await Promise.all([add({ org_id: ORG_C }, DESK), add({ org_id: ORG_C }, other)]);
414
+ expect(rows()).toHaveLength(1);
415
+ expect(idOf(a)).toBe(idOf(b));
416
+ expect([a, b].map((r) => (r.ok ? r.value.created : null)).sort()).toEqual([false, true]);
417
+ });
418
+
419
+ it("a duplicate_pending_add the re-read cannot explain is returned as the refusal", async () => {
420
+ m.failNextInsert(
421
+ Object.assign(new Error(`E11000 duplicate key error index: ${POC_IN_APP_PENDING_INDEX} dup key: { : "desk@c.org" }`), { code: 11000 }),
422
+ );
423
+ expect(await add({ org_id: ORG_C }, DESK)).toMatchObject({ ok: false, status: 409, reason: "duplicate_pending_add" });
424
+ });
425
+
426
+ it("maps the other unique indexes by name, then key pattern; never returns the driver's message (it echoes the email)", async () => {
427
+ const dup = (message: string, keyPattern?: Record<string, number>) =>
428
+ m.failNextInsert(Object.assign(new Error(message), { code: 11000, keyPattern }));
429
+ const cases: [() => void, string][] = [
430
+ [() => dup(`E11000 index: ${POINT_OF_CONTACT_INDEXES.identity} dup key: { : "writein:x|desk@c.org" }`), "duplicate_contact"],
431
+ [() => dup("E11000 dup key", { agency_id: 1, subject_type: 1, subject_id: 1, identity_key: 1 }), "duplicate_contact"],
432
+ [() => dup(`E11000 index: ${POINT_OF_CONTACT_INDEXES.primary} dup key`), "primary_exists"],
433
+ [() => dup(`E11000 index: ${POINT_OF_CONTACT_INDEXES.id} dup key`), "duplicate_contact_id"],
434
+ [() => dup("E11000 index: someone_elses_index dup key: { : \"desk@c.org\" }"), "duplicate_key"],
435
+ ];
436
+ for (const [arm, reason] of cases) {
437
+ arm();
438
+ const r = await add({ org_id: ORG_C }, DESK);
439
+ expect(r).toMatchObject({ ok: false, status: 409, reason });
440
+ expect(JSON.stringify(r)).not.toContain("desk@c.org");
441
+ }
442
+ // Not a duplicate key: it propagates.
443
+ m.failNextInsert(Object.assign(new Error("connection reset"), { code: 6 }));
444
+ await expect(add({ org_id: ORG_C }, DESK)).rejects.toThrow("connection reset");
445
+ expect(rows()).toHaveLength(0);
446
+ });
447
+
448
+ it("a partner's removal of the adder's contact sticks: re-adding it is 409 contact_deactivated; a relisted visible contact still answers", async () => {
449
+ stored({
450
+ display_name: "Desk",
451
+ emails: one("desk@a.org"),
452
+ status: "pending",
453
+ origin: "in_app",
454
+ added_in_app: "thread",
455
+ createdBy: ADVOCATE.id,
456
+ active: false,
457
+ deactivated_by: "auth0|alice",
458
+ deactivated_at: "2026-10-02T00:00:00.000Z",
459
+ });
460
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Desk", emails: [{ value: "desk@a.org" }] })).toEqual({
461
+ ok: false,
462
+ status: 409,
463
+ reason: "contact_deactivated",
464
+ message: expect.any(String),
465
+ });
466
+ expect(rows()).toHaveLength(1);
467
+ // Another adder never learns of it: theirs lands pending.
468
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Desk", emails: [{ value: "desk@a.org" }] }, CAROL)).toMatchObject({ value: { created: true } });
469
+ // The partner lists the person itself: that visible contact answers the first adder.
470
+ const listed = stored({ display_name: "Desk", emails: one("desk@a.org") });
471
+ expect(await add({ agency_id: AGENCY_A }, { display_name: "Desk", emails: [{ value: "desk@a.org" }] })).toMatchObject({
472
+ value: { id: listed.id, created: false, status: "confirmed" },
473
+ });
474
+ });
475
+
476
+ it("caps the adder's outstanding pending adds: per partner, then across the network (429); a retry is never capped", async () => {
477
+ expect([POC_IN_APP_PENDING_CAP_PER_PARTNER, POC_IN_APP_PENDING_CAP_PER_ACTOR]).toEqual([5, 25]);
478
+ const mails = (n: number, dom: string) =>
479
+ Array.from({ length: n }, (_, i) => ({ display_name: `P${i}`, emails: [{ value: `p${i}@${dom}` }] }));
480
+ for (const b of mails(POC_IN_APP_PENDING_CAP_PER_PARTNER, "c.org")) expect((await add({ org_id: ORG_C }, b)).ok).toBe(true);
481
+ expect(await add({ org_id: ORG_C }, { display_name: "One more", emails: [{ value: "more@c.org" }] })).toMatchObject({
482
+ ok: false,
483
+ status: 429,
484
+ reason: "too_many_pending_adds",
485
+ });
486
+ expect(await add({ org_id: ORG_C }, { display_name: "P0", emails: [{ value: "p0@c.org" }] })).toMatchObject({ value: { created: false } });
487
+ expect((await add({ org_id: ORG_C }, { display_name: "Carol's", emails: [{ value: "more@c.org" }] }, CAROL)).ok).toBe(true);
488
+ // Confirmed adds no longer count.
489
+ rows().find((r) => (r.emails as { value: string }[])[0]?.value === "p0@c.org")!.status = "confirmed";
490
+ expect((await add({ org_id: ORG_C }, { display_name: "Now fits", emails: [{ value: "more@c.org" }] })).ok).toBe(true);
491
+
492
+ // Across partners: fill up to the network cap with ad hoc org-only partners.
493
+ const orgs = m.store.get("organizations")!;
494
+ let pending = rows().filter((r) => r.createdBy === ADVOCATE.id && r.status === "pending").length;
495
+ for (let i = 0; pending < POC_IN_APP_PENDING_CAP_PER_ACTOR; i++) {
496
+ const id = new ObjectId();
497
+ orgs.push({ _id: id, name: `X${i}` });
498
+ const n = Math.min(
499
+ POC_IN_APP_PENDING_CAP_PER_PARTNER,
500
+ POC_IN_APP_PENDING_CAP_PER_ACTOR - pending,
501
+ );
502
+ for (const b of mails(n, `x${i}.org`)) {
503
+ expect((await add({ org_id: String(id) }, b)).ok).toBe(true);
504
+ pending++;
505
+ }
506
+ }
507
+ const fresh = new ObjectId();
508
+ orgs.push({ _id: fresh, name: "Fresh" });
509
+ expect(await add({ org_id: String(fresh) }, { display_name: "Z", emails: [{ value: "z@fresh.org" }] })).toMatchObject({
510
+ status: 429,
511
+ reason: "too_many_pending_adds",
512
+ });
513
+ });
514
+
515
+ it("parallel adds to one partner (the double's interleaving; atomic ops, not proof of race safety): at most the cap survive, every 429 left no row", async () => {
516
+ const results = await Promise.all(
517
+ Array.from({ length: 12 }, (_, i) => add({ org_id: ORG_C }, { display_name: `Par${i}`, emails: [{ value: `par${i}@c.org` }] })),
518
+ );
519
+ const kept = rows().filter((r) => r.createdBy === ADVOCATE.id && r.status === "pending");
520
+ expect(kept.length).toBeLessThanOrEqual(POC_IN_APP_PENDING_CAP_PER_PARTNER);
521
+ const okIds = results.filter((r) => r.ok).map(idOf).sort();
522
+ expect(okIds).toEqual(kept.map((r) => r.id as string).sort());
523
+ for (const r of results.filter((x) => !x.ok)) expect(r).toMatchObject({ status: 429, reason: "too_many_pending_adds" });
524
+ // A burst may withdraw rows that would have fit (count-based: the bound, at the price of
525
+ // spurious 429s); once it settles, sequential adds fill up to the cap and no further.
526
+ for (let i = kept.length; i < POC_IN_APP_PENDING_CAP_PER_PARTNER; i++) {
527
+ expect((await add({ org_id: ORG_C }, { display_name: `Seq${i}`, emails: [{ value: `seq${i}@c.org` }] })).ok).toBe(true);
528
+ }
529
+ expect(await add({ org_id: ORG_C }, { display_name: "Over", emails: [{ value: "over@c.org" }] })).toMatchObject({ status: 429 });
530
+ });
531
+
532
+ it("parallel adds across partners (the double's interleaving; not proof of race safety): at most the network cap survive", async () => {
533
+ const orgs = m.store.get("organizations")!;
534
+ const targets = Array.from({ length: 8 }, (_, i) => {
535
+ const id = new ObjectId();
536
+ orgs.push({ _id: id, name: `Net${i}` });
537
+ return String(id);
538
+ });
539
+ await Promise.all(
540
+ targets.flatMap((org, i) =>
541
+ Array.from({ length: 4 }, (_, j) => add({ org_id: org }, { display_name: `N${i}-${j}`, emails: [{ value: `n${i}-${j}@net.org` }] })),
542
+ ),
543
+ );
544
+ expect(rows().filter((r) => r.createdBy === ADVOCATE.id && r.status === "pending").length).toBeLessThanOrEqual(POC_IN_APP_PENDING_CAP_PER_ACTOR);
545
+ });
546
+
547
+ it("refuses a canonical singleton or a PAVE 2.0-shape org as the partner; a PAVE string id does not resolve", async () => {
548
+ const canon = new ObjectId();
549
+ const pave = new ObjectId();
550
+ m.store.get("organizations")!.push(
551
+ { _id: canon, name: "Unknown", canonicalKey: "UNKNOWN" },
552
+ { _id: pave, id: "org-pave-1", name: "PAVE org", type: "CVC48_KEY", acronym: "PO", location: {} },
553
+ );
554
+ expect(await add({ org_id: String(canon) }, DESK)).toMatchObject({ status: 409, reason: "canonical_org" });
555
+ expect(await add({ org_id: String(pave) }, DESK)).toMatchObject({ status: 409, reason: "foreign_row_shape" });
556
+ expect(await add({ org_id: "org-pave-1" }, DESK)).toMatchObject({ status: 400, reason: "owner_not_found" });
557
+ expect(rows()).toHaveLength(0);
558
+ });
559
+
560
+ it("a stored row on the partner that does not parse fails loudly", async () => {
561
+ m.store.set(POINTS_OF_CONTACT_COLLECTION, [
562
+ { id: "poc-broken", agency_id: null, subject_type: "org", subject_id: ORG_C, active: true, emails: [{ value: "desk@c.org" }], identity_key: "writein:intake desk|desk@c.org", origin: "carrier-pigeon" },
563
+ ]);
564
+ await expect(add({ org_id: ORG_C }, DESK)).rejects.toThrow(/poc-broken/);
565
+ });
566
+
567
+ it("a read error propagates; it is never read as 'unknown partner'", async () => {
568
+ const broken: PocWriteDbLike = {
569
+ collection: (name) => {
570
+ const c = m.handle().collection(name);
571
+ return name === "agencies"
572
+ ? { ...c, find: () => ({ toArray: async () => { throw new Error("read failed"); } }) }
573
+ : c;
574
+ },
575
+ };
576
+ await expect(add({ agency_id: "agency-a" }, DESK, ADVOCATE, "thread", broken)).rejects.toThrow("read failed");
577
+ });
578
+
579
+ it("every refusal code it returns is declared, and the rule violations are among them", () => {
580
+ const reasons = new Set<string>(PARTNER_CONTACT_FAILURE_REASONS);
581
+ const violations = pocRuleViolations({ user_id: "u", person_id: "p", display_name: null, emails: [{ value: "a@b.c", is_preferred: true }], phones: [], purposes: [] });
582
+ for (const v of violations) expect(reasons.has(v.reason), v.reason).toBe(true);
583
+ for (const r of ["owner_not_found", "owner_mismatch", "canonical_org", "foreign_row_shape", "contact_deactivated", "too_many_pending_adds", "duplicate_pending_add"]) {
584
+ expect(reasons.has(r), r).toBe(true);
585
+ }
586
+ });
587
+ });
588
+
589
+ describe("ensurePocIndexes", () => {
590
+ const ixList = () =>
591
+ (m.indexes.get(POINTS_OF_CONTACT_COLLECTION) ?? []).map((i) => [
592
+ i.name,
593
+ i.key,
594
+ !!i.unique,
595
+ i.partialFilterExpression ?? null,
596
+ ]);
597
+ const ix = POINT_OF_CONTACT_INDEXES;
598
+ const FULL = [
599
+ [ix.id, { id: 1 }, true, null],
600
+ [ix.subject, { subject_type: 1, subject_id: 1, active: 1 }, false, null],
601
+ [ix.agencyPurpose, { agency_id: 1, purposes: 1, active: 1 }, false, null],
602
+ [ix.orgPurpose, { org_id: 1, purposes: 1, active: 1 }, false, null],
603
+ [ix.status, { status: 1 }, false, null],
604
+ [ix.identity, { agency_id: 1, subject_type: 1, subject_id: 1, identity_key: 1 }, true, { active: true, status: "confirmed" }],
605
+ [ix.primary, { agency_id: 1, subject_type: 1, subject_id: 1 }, true, { is_primary: true, active: true, status: "confirmed" }],
606
+ [
607
+ "pointsofcontact_inapp_pending_unique",
608
+ { agency_id: 1, subject_type: 1, subject_id: 1, identity_key: 1, createdBy: 1 },
609
+ true,
610
+ { status: "pending", origin: "in_app", active: true },
611
+ ],
612
+ ["pointsofcontact_pending_queue", { status: 1, createdAt: 1, id: 1 }, false, null],
613
+ ];
614
+
615
+ it("creates the platform's full index set, in the platform's order (the platform calls it)", async () => {
616
+ expect(ixList()).toEqual(FULL);
617
+ });
618
+
619
+ it("is idempotent and memoized per handle; a failed run is retried", async () => {
620
+ let creates = 0;
621
+ const counted = (): PocWriteDbLike => ({
622
+ collection: (name) => {
623
+ const c = m.handle().collection(name);
624
+ return { ...c, createIndex: async (k, o) => (creates++, c.createIndex(k, o)) };
625
+ },
626
+ });
627
+ const h = counted();
628
+ await Promise.all([ensurePocIndexes(h), ensurePocIndexes(h)]);
629
+ await ensurePocIndexes(h);
630
+ expect(creates).toBe(FULL.length);
631
+ expect(ixList()).toEqual(FULL);
632
+ // Another handle runs again and changes nothing.
633
+ await ensurePocIndexes(counted());
634
+ expect(creates).toBe(2 * FULL.length);
635
+ expect(ixList()).toEqual(FULL);
636
+
637
+ let calls = 0;
638
+ const flaky: PocWriteDbLike = {
639
+ collection: (name) => {
640
+ const c = m.handle().collection(name);
641
+ return {
642
+ ...c,
643
+ createIndex: async (k, o) =>
644
+ // The second create (the first is the `id` index, tolerated when equivalent).
645
+ ++calls === 2 ? Promise.reject(new Error("not primary")) : c.createIndex(k, o),
646
+ };
647
+ },
648
+ };
649
+ await expect(ensurePocIndexes(flaky)).rejects.toThrow("not primary");
650
+ await expect(ensurePocIndexes(flaky)).resolves.toBeUndefined();
651
+ });
652
+
653
+ it("a server refusal (e.g. a same-name index with another spec) propagates; nothing is dropped", async () => {
654
+ m.indexes.set(POINTS_OF_CONTACT_COLLECTION, [
655
+ { name: POC_IN_APP_PENDING_INDEX, key: { email: 1 }, unique: true },
656
+ ]);
657
+ await expect(ensurePocIndexes(db)).rejects.toThrow(/same name/);
658
+ const named = m.indexes
659
+ .get(POINTS_OF_CONTACT_COLLECTION)!
660
+ .filter((i) => i.name === POC_IN_APP_PENDING_INDEX);
661
+ expect(named).toHaveLength(1);
662
+ });
663
+ });
664
+
665
+ describe("the pieces the platform store imports (index, matcher, duplicate-key mapping agree)", () => {
666
+ it("the in-app pending index sits with the others, keyed on the scalar identity_key", () => {
667
+ expect(POINT_OF_CONTACT_INDEXES.inAppPending).toBe(POC_IN_APP_PENDING_INDEX);
668
+ expect(POINT_OF_CONTACT_INDEXES.pendingQueue).toBe("pointsofcontact_pending_queue");
669
+ expect(POC_IN_APP_PENDING_INDEX_KEY).toEqual({
670
+ agency_id: 1,
671
+ subject_type: 1,
672
+ subject_id: 1,
673
+ identity_key: 1,
674
+ createdBy: 1,
675
+ });
676
+ expect(POC_IN_APP_PENDING_INDEX_FILTER).toEqual({ status: "pending", origin: "in_app", active: true });
677
+ });
678
+
679
+ it("pocRowsWithIdentity: the subject's rows with this identity, any status or activity; a shared mailbox is not an identity", async () => {
680
+ const a = stored({ display_name: "A", emails: [{ value: "a@a.org", is_preferred: true }, { value: "s@a.org", is_preferred: false }] });
681
+ const gone = stored({ display_name: "a", emails: one("a@a.org"), active: false, status: "pending" });
682
+ stored({ display_name: "S", emails: one("s@a.org") });
683
+ stored({ display_name: "A", emails: one("a@a.org"), subject_type: "program", subject_id: "prog-1" });
684
+ const subject = { agency_id: AGENCY_A, subject_type: "agency", subject_id: AGENCY_A } as const;
685
+ const ids = async (key: string) =>
686
+ (await pocRowsWithIdentity(db, subject, key)).map((d) => d.id).sort();
687
+ expect(await ids("writein:a|a@a.org")).toEqual([a.id, gone.id].sort());
688
+ expect(await ids("writein:a|s@a.org")).toEqual([]);
689
+ });
690
+
691
+ it("pocDuplicateKeyFailure: by index name, then key pattern; null for anything else", () => {
692
+ const e = (message: string, keyPattern?: Record<string, number>) =>
693
+ Object.assign(new Error(message), { code: 11000, keyPattern });
694
+ expect(pocDuplicateKeyFailure(e("E11000", { ...POC_IN_APP_PENDING_INDEX_KEY }))).toMatchObject({ status: 409, reason: "duplicate_pending_add" });
695
+ expect(pocDuplicateKeyFailure(e(`E11000 index: ${POC_IN_APP_PENDING_INDEX}`))).toMatchObject({ reason: "duplicate_pending_add" });
696
+ expect(pocDuplicateKeyFailure(e("E11000", { agency_id: 1, subject_type: 1, subject_id: 1 }))).toMatchObject({ reason: "primary_exists" });
697
+ expect(pocDuplicateKeyFailure(e("E11000", { id: 1 }))).toMatchObject({ reason: "duplicate_contact_id" });
698
+ expect(pocDuplicateKeyFailure({ message: "plain E11000 text, no code" })).toMatchObject({ reason: "duplicate_key" });
699
+ expect(pocDuplicateKeyFailure(new Error("network"))).toBeNull();
700
+ expect(pocDuplicateKeyFailure(null)).toBeNull();
701
+ });
702
+ });
703
+
704
+ describe("the writer only checks the indexes (least privilege, fail closed)", () => {
705
+ const counting = () => {
706
+ const calls = { createIndex: 0, indexes: 0 };
707
+ const handle: PocWriteDbLike = {
708
+ collection: (name) => {
709
+ const c = m.handle().collection(name);
710
+ return {
711
+ ...c,
712
+ createIndex: async (k, o) => (calls.createIndex++, c.createIndex(k, o)),
713
+ indexes: async () => (calls.indexes++, c.indexes()),
714
+ };
715
+ },
716
+ };
717
+ return { handle, calls };
718
+ };
719
+
720
+ it("never creates an index; one listIndexes per handle once the check passes", async () => {
721
+ const { handle, calls } = counting();
722
+ expect((await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", handle)).ok).toBe(true);
723
+ expect((await add({ org_id: ORG_C }, { display_name: "Two", emails: [{ value: "two@c.org" }] }, ADVOCATE, "thread", handle)).ok).toBe(true);
724
+ expect(calls).toEqual({ createIndex: 0, indexes: 1 });
725
+ });
726
+
727
+ it("no collection (NamespaceNotFound): indexes_missing, 503, nothing written, nothing created; the next add re-checks", async () => {
728
+ m.indexes.delete(POINTS_OF_CONTACT_COLLECTION);
729
+ const { handle, calls } = counting();
730
+ expect(await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", handle)).toEqual({
731
+ ok: false,
732
+ status: 503,
733
+ reason: "indexes_missing",
734
+ message: expect.any(String),
735
+ });
736
+ expect(rows()).toHaveLength(0);
737
+ expect(m.indexes.has(POINTS_OF_CONTACT_COLLECTION)).toBe(false);
738
+ // Not memoized: once the platform creates them, the same handle writes.
739
+ await ensurePocIndexes(m.handle());
740
+ expect((await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", handle)).ok).toBe(true);
741
+ expect(calls.createIndex).toBe(0);
742
+ expect(calls.indexes).toBe(2);
743
+ });
744
+
745
+ it("a wrong spec is missing: the pending index's key, unique flag or partial filter, or a non-unique id index", async () => {
746
+ const list = () => m.indexes.get(POINTS_OF_CONTACT_COLLECTION)!;
747
+ const pending = () => list().find((i) => i.name === POC_IN_APP_PENDING_INDEX)!;
748
+ const good = { ...pending() };
749
+ const variants: [string, () => void][] = [
750
+ ["multikey emails.value key", () => Object.assign(pending(), { key: { agency_id: 1, subject_type: 1, subject_id: 1, "emails.value": 1, createdBy: 1 } })],
751
+ ["key order", () => Object.assign(pending(), { key: { subject_type: 1, agency_id: 1, subject_id: 1, identity_key: 1, createdBy: 1 } })],
752
+ ["not unique", () => Object.assign(pending(), { unique: false })],
753
+ ["partial filter", () => Object.assign(pending(), { partialFilterExpression: { status: "pending" } })],
754
+ ["no partial filter", () => Object.assign(pending(), { partialFilterExpression: undefined })],
755
+ ["dropped", () => list().splice(list().indexOf(pending()), 1)],
756
+ ];
757
+ for (const [label, mutate] of variants) {
758
+ mutate();
759
+ expect(await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", m.handle()), label).toMatchObject({
760
+ status: 503,
761
+ reason: "indexes_missing",
762
+ });
763
+ const others = list().filter((i) => i.name !== POC_IN_APP_PENDING_INDEX);
764
+ m.indexes.set(POINTS_OF_CONTACT_COLLECTION, [...others, { ...good }]);
765
+ }
766
+ const idIx = list().find((i) => i.name === POINT_OF_CONTACT_INDEXES.id)!;
767
+ idIx.unique = false;
768
+ expect(await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", m.handle())).toMatchObject({ reason: "indexes_missing" });
769
+ idIx.unique = true;
770
+ // The partial filter matches whatever its field order; the index name is not part of the check.
771
+ pending().partialFilterExpression = { active: true, origin: "in_app", status: "pending" };
772
+ pending().name = "renamed_by_someone";
773
+ expect((await add({ org_id: ORG_C }, DESK, ADVOCATE, "thread", m.handle())).ok).toBe(true);
774
+ expect(rows()).toHaveLength(1);
775
+ });
776
+
777
+ it("checkPocIndexes is exported for a caller's readiness probe", async () => {
778
+ expect(await checkPocIndexes(m.handle())).toBeNull();
779
+ m.indexes.delete(POINTS_OF_CONTACT_COLLECTION);
780
+ expect(await checkPocIndexes(m.handle())).toMatchObject({ status: 503, reason: "indexes_missing" });
781
+ });
782
+ });