/** * An import never matches a binned person (owner, 26 Oct 2026): a lookup of a * person by phone, email, handle, Contacts card, name or face cluster skips a * person in the bin (`trashed_at`, where merge_people puts the duplicate it * folds away) and a tombstone (`deleted = 1`), so a merged or binned * duplicate is never pointed at again. Every person, number and address here * is invented. */import { describe, expect, it } from"vitest";import { peopleByHandle, resolveContact } from"../src/import/contacts.ts";importtype { Handle } from"../src/import/handles.ts";import { PhotoReader } from"../src/import/photos.ts";import { NOW, TestFile } from"./import-file.ts";const BINNED = { trashed_at: "2026-08-21T00:00:00.000Z" };const phone = (value: string): Handle => ({ kind: "phone", value });const email = (value: string): Handle => ({ kind: "email", value });/** A person kept, a duplicate of them in the bin and one gone for good, all claiming the same number and address. */asyncfunction duplicates(): Promise<TestFile> {const file = awaitTestFile.open("bin");for (const [id, extra] of [["person-kept", {}], ["person-binned", BINNED], ["person-gone", {}]] asconst) {await file.seedPerson(id, "Invented Person", { device_contact_id: `card-${id}`, ...extra });await file.seedPhone(`${id}-phone`, id, "919000000071");await file.seedPhone(`${id}-email`, id, "invented@example.org", "email"); }await file.within((o) => o.store.query("UPDATE rows SET deleted = 1 WHERE table_id = 'person' AND id = 'person-gone'"));return file;}describe("an import's person lookups", () => { it("find the kept person by number and by address, never the binned or the gone duplicate", async () => {const file = await duplicates();const people = await file.within((_o, reader) => peopleByHandle(reader)); expect(people.get("phone:919000000071")?.id).toBe("person-kept"); expect(people.get("email:invented@example.org")?.id).toBe("person-kept");// The object's own owner is a person too (src/owner.ts).const owner = await file.owner();const names = await file.within((_o, reader) => reader.peopleByName().map((p) => p.id)); expect(names.filter((id) => id !== owner)).toEqual(["person-kept"]); }); it("find nobody when the only person claiming a handle is binned", async () => {const file = awaitTestFile.open("bin");await file.seedPerson("person-binned", "Invented Person", BINNED);await file.seedPhone("person-binned-phone", "person-binned", "919000000072");const people = await file.within((_o, reader) => peopleByHandle(reader)); expect(people.has("phone:919000000072")).toBe(false); }); it("read a contact pointing at a binned person as pointing at nobody, so the import points it at the kept one", async () => {const file = await duplicates();await file.seedContact("contact-1", "919000000071", "Invented");await file.within((o) => o.store.query("UPDATE rows SET data = json_set(data, '$.person_id', 'person-binned') WHERE id = 'contact-1'")); expect((await file.within((_o, reader) => reader.contactByPhone("919000000071")))?.person_id).toBeNull();let minted = 0;const resolved = await file.within((_o, reader) => resolveContact({ db: reader, namespace: "ws", handle: phone("919000000071"), displayName: null, personId: null, now: NOW, newId: () => `contact-new-${++minted}`, people: peopleByHandle(reader) }), );await file.apply(resolved.changes); expect(resolved.contactId).toBe("contact-1"); expect((await file.one("message_contact", (r) => r.id === "contact-1"))?.data.person_id).toBe("person-kept");const byAddress = await file.within((_o, reader) => resolveContact({ db: reader, namespace: "ws", handle: email("invented@example.org"), displayName: null, personId: null, now: NOW, newId: () => `contact-new-${++minted}`, people: peopleByHandle(reader) }), );await file.apply(byAddress.changes); expect((await file.one("message_contact", (r) => r.id === byAddress.contactId))?.data.person_id).toBe("person-kept"); }); it("never take a binned person's Contacts card or face cluster, nor list them among the people a face may be", async () => {const file = await duplicates();for (const [n, person] of [[1, "person-binned"], [2, "person-binned"], [3, "person-kept"]] asconst) {await file.seed("face", `face-${n}`, { source: "apple", cluster_id: "cluster-a", person_id: person, created_at: NOW, updated_at: NOW }); }const owner = await file.owner();const read = await file.within((o) => {const photos = newPhotoReader(o.store);return { majority: photos.majorityPerson("cluster-a"), kept: photos.byContact("card-person-kept"), binned: photos.byContact("card-person-binned"), gone: photos.byContact("card-person-gone"), people: photos.activePeople().map((p) => p.id).filter((id) => id !== owner), }; }); expect(read).toEqual({ majority: "person-kept", kept: "person-kept", binned: null, gone: null, people: ["person-kept"] }); });});