64 lines · 4.0 KB · TypeScriptRaw
/** * Merge and unmerge two people, the server's half (PEOPLE-DESIGN b.3, CONTACTS-ANSWERS.md §5): every clear link to * the duplicate is repointed in one write, the duplicate is archived with merged_into, a sealed link is left for the * phone and named, and an unmerge puts back exactly what the merge moved. Synthetic people only. */import { describe, expect, it } from "vitest";import { http, personalObject } from "./personal.ts";type Row = { id: string; data: Record<string, unknown> };const SLUG = "person-merge";describe("merging two people on the server", () => { it("repoints every clear link in one write, archives the duplicate, and unmerge puts back exactly what moved", async () => { const { credential } = await personalObject(SLUG, { open: ["person", "person_handle", "event"] }); const auth = { authorization: `Bearer ${credential}` }; const act = <T = Record<string, unknown>>(name: string, args: Record<string, unknown>) => http<T>(SLUG, "POST", "/v1/act", { act: name, args }, auth); const make = async (table: string, data: Record<string, unknown>): Promise<string> => { const id = crypto.randomUUID(); const made = await act("create", { table, id, data }); expect(made.status, JSON.stringify(made.body)).toBe(200); return id; }; const row = async (table: string, id: string): Promise<Row> => { const got = await http<{ rows: Row[] }>(SLUG, "GET", `/v1/rows?table=${table}&ids=${id}`, undefined, auth); expect(got.status, JSON.stringify(got.body)).toBe(200); return got.body.rows[0]!; }; const asha = await make("person", { full_name: "Asha Heron", language: "en" }); const ashaM = await make("person", { full_name: "Asha H", language: "en" }); const handle = await make("person_handle", { person_id: ashaM, kind: "phone", value: "+12025550142" }); const both = await make("event", { title: "Weir walk", starts_at: "2026-11-20T09:00:00Z", people: [asha, ashaM] }); const one = await make("event", { title: "Kiln day", starts_at: "2026-11-21T09:00:00Z", people: [ashaM] }); expect((await act<{ code: string }>("merge_people", { keep: asha, drop: asha })).body.code).toBe("same_person"); const merged = await act<{ id: string; moved: Record<string, number>; left: string[] }>("merge_people", { keep: asha, drop: ashaM }); expect(merged.status, JSON.stringify(merged.body)).toBe(200); expect(merged.body.moved).toMatchObject({ "person_handle.person_id": 1, "event.people": 2 }); // A link inside a seal is the phone's to move. expect(merged.body.left).toContain("film.recommended_by"); expect((await row("person_handle", handle)).data.person_id).toBe(asha); expect((await row("event", both)).data.people).toEqual([asha]); expect((await row("event", one)).data.people).toEqual([asha]); const away = await row("person", ashaM); expect(away.data.merged_into).toBe(asha); expect(away.data.archived_at).toBeTruthy(); expect((await act<{ code: string }>("merge_people", { keep: ashaM, drop: asha })).body.code).toBe("merged_away"); // A handle added after the merge stays with the person kept. const later = await make("person_handle", { person_id: asha, kind: "email", value: "asha@example.com" }); const undone = await act<{ ids: string[] }>("unmerge_people", { id: asha }); expect(undone.status, JSON.stringify(undone.body)).toBe(200); expect(undone.body.ids).toEqual([asha, ashaM]); expect((await row("person_handle", handle)).data.person_id).toBe(ashaM); expect((await row("person_handle", later)).data.person_id).toBe(asha); expect((await row("event", both)).data.people).toEqual([asha, ashaM]); expect((await row("event", one)).data.people).toEqual([ashaM]); const back = await row("person", ashaM); expect(back.data.merged_into ?? null).toBeNull(); expect(back.data.archived_at ?? null).toBeNull(); expect((await act<{ code: string }>("unmerge_people", { id: asha })).body.code).toBe("nothing_to_unmerge"); }, 300_000);});