import { describe, expect, it } from "vitest"; import { env, runInDurableObject } from "cloudflare:test"; import { Store } from "../src/store.ts"; import { answer, http, mint, personalObject, put, refusal, PEOPLE_OPEN } from "./personal.ts"; import { sealRow } from "./seal-device.ts"; interface Rec { id: string; data: Record; deleted_at: string | null; } /** RULE (Rust parity, rules/people/merge.rs): a duplicate person folds into the survivor in one write — nothing the survivor holds is lost or overwritten, every person link moves, the duplicate waits in the bin. */ describe("merge_people", () => { const slug = "mergers"; let owner = ""; let writer = ""; let ids: Record = {}; it("folds the duplicate in: fills, moves the card and every link, bins repeats and the duplicate, in one version", async () => { // Calls are sealed: this test is of the merge over open rows, so they are open here (a sealed one asks a window, below). const made = await personalObject(slug, { open: ["call", ...PEOPLE_OPEN.open] }); owner = made.owner; writer = await mint(slug, made.credential, "a writer", ["read", "write"]); ids = await put(slug, [ { ref: "asha", table: "person", data: { full_name: "Asha Rao", city: "Hyderabad" } }, { ref: "dup", table: "person", data: { full_name: "Asha R", org: "Tin Factory", city: "Chennai", device_contact_id: "card-1", device_contact_snapshot: "{}" } }, { ref: "other", table: "person", data: { full_name: "Bela" } }, { ref: "ashaPhone", table: "person_handle", data: { person_id: "@asha", kind: "phone", value: "+919000000101", normalised: "+919000000101" } }, { ref: "dupPhone", table: "person_handle", data: { person_id: "@dup", kind: "phone", value: "+919000000101", normalised: "+919000000101" } }, { ref: "dupEmail", table: "person_handle", data: { person_id: "@dup", kind: "email", value: "asha@example.test", normalised: "asha@example.test" } }, { ref: "note", table: "call", data: { person_id: "@dup", title: "Met at the conference" } }, { ref: "belaNote", table: "call", data: { person_id: "@other", title: "Bela's call" } }, ]); // The writer's first call stamps its last use, a version of its own; the merge is measured after it. await answer(slug, writer, "list_bin"); const before = await version(); const merged = await answer>(slug, writer, "merge_people", { keep: ids.asha, merge: ids.dup }); expect(await version()).toBe(before + 1); expect(merged).toMatchObject({ kept: { id: ids.asha, name: "Asha Rao" }, merged: { id: ids.dup, name: "Asha R" }, filled: ["org"], combined: [], device_card: "moved", repointed: { "person_handle.person_id": 2, "call.person_id": 1 }, repeats_binned: { person_handle: 1 }, said: "Merged Asha R into Asha Rao; Asha R waits in the bin", }); const asha = await answer(slug, owner, "get_record_by_table", { table: "person", id: ids.asha }); expect(asha.data).toMatchObject({ full_name: "Asha Rao", city: "Hyderabad", org: "Tin Factory", device_contact_id: "card-1" }); const handles = await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "person_handle", where: { person_id: ids.asha } }); expect(handles.records.map((r) => r.data.value).sort()).toEqual(["+919000000101", "asha@example.test"]); const bin = await answer<{ records: (Rec & { table: string })[] }>(slug, owner, "list_bin"); expect(bin.records.map((r) => [r.table, r.id]).sort()).toEqual([ ["person", ids.dup], ["person_handle", ids.dupPhone], ].sort()); const dup = bin.records.find((r) => r.id === ids.dup)!; expect(dup.data.device_contact_id).toBeUndefined(); expect(dup.data.full_name).toBe("Asha R"); const notes = await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "call", where: { person_id: ids.asha } }); expect(notes.records.map((r) => r.data.title)).toEqual(["Met at the conference"]); expect((await answer(slug, owner, "get_record_by_table", { table: "call", id: ids.belaNote })).data.person_id).toBe(ids.other); }); it("refuses one person twice, an id that is no person, a person in the bin, and a reader", async () => { expect(await refusal(slug, writer, "merge_people", { keep: ids.asha, merge: ids.asha })).toBe("keep and merge are the same person: name two people"); expect(await refusal(slug, writer, "merge_people", { keep: ids.asha, merge: ids.note })).toBe(`merge is not a person: there is no person ${JSON.stringify(ids.note)}`); expect(await refusal(slug, writer, "merge_people", { keep: ids.dup, merge: ids.other })).toBe("keep is a person in the bin: restore_from_bin brings them back first"); expect(await refusal(slug, writer, "merge_people", { keep: ids.asha })).toBe("merge is required: the id of the person folded into keep"); const reader = await mint(slug, (await signIn()).credential, "a reader", ["read"]); expect(await refusal(slug, reader, "merge_people", { keep: ids.asha, merge: ids.other })).toBe('this token does not have "write" access. Ask the owner to reconnect with write enabled.'); }); // RULE (e2e rulings R1): a duplicate's sealed rows (a call here) move by their clear person link, never read: the merge needs no window. it("moves a duplicate's sealed calls by their clear link, never reading them", async () => { const sealedSlug = "merge-sealed"; const made = await personalObject(sealedSlug, PEOPLE_OPEN); const writes = await mint(sealedSlug, made.credential, "a writer", ["read", "write"]); const held = await put(sealedSlug, [ { ref: "keep", table: "person", data: { full_name: "Asha Rao" } }, { ref: "dup", table: "person", data: { full_name: "Asha R" } }, ]); const note = await put(sealedSlug, [{ ref: "note", table: "call", id: "n-sealed", data: { person_id: held.dup, ...(await sealRow(new Uint8Array(32).fill(4), "call", "n-sealed", 1, { title: "Met at the conference" })) } }]); const merged = await answer>(sealedSlug, writes, "merge_people", { keep: held.keep, merge: held.dup }); expect(merged).toMatchObject({ repointed: { "call.person_id": 1 } }); const stored = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(sealedSlug)), (_i, state) => new Store(state.storage.sql, () => new Date()).get("call", note.note!)!.data); expect(stored.person_id).toBe(held.keep); expect(stored).not.toHaveProperty("title"); }); async function version(): Promise { return (await http<{ version: number }>(slug, "GET", "/v1/sync?since=0", undefined, { authorization: `Bearer ${owner}` })).body.version; } async function signIn(): Promise<{ credential: string }> { const code = await http<{ challenge_id: string }>(slug, "POST", "/v1/code", { phone: "+919000000009", device_id: "coordinator" }); return (await http<{ credential: string }>(slug, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "coordinator" })).body; } });