import { env, runInDurableObject } from "cloudflare:test"; /** * Who the owner is (src/owner.ts): the personal object names the person whose * phone is the sign-in number, makes one when nobody carries it, and serves * the link with the database's meta — every phone's copy takes "you" from * there. Every person and number here is invented. */ import { describe, expect, it } from "vitest"; import { ownerIds, UNNAMED_OWNER } from "../src/owner.ts"; import { bodyAad } from "../src/seal.ts"; import { arrivalPub, openArrivalRow } from "./arrival_keys.ts"; import { ADMIN, answer, DATABASE_ID, http, OWNER_PHONE, PERSONAL_TABLES, PERSONAL_TOOLS, personalObject, put, PEOPLE_OPEN, openTables } from "./personal.ts"; /** OWNER_PHONE's digits, as a handle's `normalised` holds a number. */ const DIGITS = OWNER_PHONE.slice(1); type Row = { id: string; table: string; data: Record; deleted?: boolean }; async function rows(slug: string, credential: string, table: string): Promise { const all: Row[] = []; let next: string | undefined; do { const page = await http<{ rows: Row[]; next?: string }>(slug, "GET", next ? `/v1/sync?cursor=${next}` : "/v1/sync?since=0", undefined, { authorization: `Bearer ${credential}` }); all.push(...page.body.rows); next = page.body.next; } while (next); return all.filter((r) => r.table === table && !r.deleted); } async function linked(slug: string, credential: string): Promise { const me = await http<{ business: { owner_person_id?: string | null } }>(slug, "GET", "/v1/me", undefined, { authorization: `Bearer ${credential}` }); expect(me.status, JSON.stringify(me.body)).toBe(200); return me.body.business.owner_person_id ?? null; } async function signIn(slug: string): Promise { const code = await http<{ challenge_id: string }>(slug, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "phone" }); const signed = await http<{ credential: string }>(slug, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "phone" }); expect(signed.status, JSON.stringify(signed.body)).toBe(200); return signed.body.credential; } describe("the owner a personal object names", () => { it("is a person made with the sign-in number when nobody carries it, named by the kit's line for an unnamed owner, the number confirmed", async () => { const slug = "owner-made"; const { credential } = await personalObject(slug, PEOPLE_OPEN); const id = await linked(slug, credential); expect(id).toEqual(expect.any(String)); const people = await rows(slug, credential, "person"); expect(people.map((p) => p.id)).toEqual([id]); // The 362 walk (6 Nov 2026): the card was titled "+1555…", so ink on People found nobody. The number stays the handle's. expect(people[0]!.data.full_name).toBe(UNNAMED_OWNER); expect(UNNAMED_OWNER).toBe("You"); const handles = await rows(slug, credential, "person_handle"); expect(handles).toHaveLength(1); expect(handles[0]!.data).toMatchObject({ person_id: id, kind: "phone", value: OWNER_PHONE, normalised: DIGITS, source: "manual", verified_at: expect.any(String) }); // The definitions a phone takes say it too, and a later request changes nothing. const defs = await http<{ business: { owner_person_id: string } }>(slug, "GET", "/v1/definitions", undefined, { authorization: `Bearer ${credential}` }); expect(defs.body.business.owner_person_id).toBe(id); expect(await linked(slug, credential)).toBe(id); expect(await rows(slug, credential, "person")).toHaveLength(1); }); it("is named what the owner gave at sign-in, and nobody is made before sign-in says it", async () => { const slug = "owner-named"; expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200); const code = await http<{ challenge_id: string; known: boolean }>(slug, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "phone" }); expect(code.body.known).toBe(false); await openTables(slug, PEOPLE_OPEN.open); const signed = await http<{ credential: string }>(slug, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: "phone", name: "Invented Walker" }); expect(signed.status, JSON.stringify(signed.body)).toBe(200); const id = await linked(slug, signed.body.credential); const people = await rows(slug, signed.body.credential, "person"); expect(people.map((p) => p.id)).toEqual([id]); expect(people[0]!.data.full_name).toBe("Invented Walker"); const handles = await rows(slug, signed.body.credential, "person_handle"); expect(handles.map((h) => h.data.value)).toEqual([OWNER_PHONE]); }); it("is the person an existing database already holds with the number, however it was written, and nobody new is made", async () => { const slug = "owner-found"; // An object as the owner's own was before the link: its People came first, its number after. expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200); const ids = await put(slug, [ { ref: "other", table: "person", data: { full_name: "Invented Other" } }, { ref: "me", table: "person", data: { full_name: "Invented Owner" } }, { ref: "otherPhone", table: "person_handle", data: { person_id: "@other", kind: "phone", value: "+91 90000 00077", normalised: "919000000077", source: "import" } }, { ref: "myPhone", table: "person_handle", data: { person_id: "@me", kind: "phone", value: "+91 90000 00009", normalised: DIGITS, source: "import" } }, ]); expect((await http(slug, "PUT", "/v1/admin/business", { owner_phone: OWNER_PHONE }, ADMIN)).status).toBe(200); const credential = await signIn(slug); expect(await linked(slug, credential)).toBe(ids.me); expect((await rows(slug, credential, "person")).map((p) => p.id).sort()).toEqual([ids.me, ids.other].sort()); expect(await rows(slug, credential, "person_handle")).toHaveLength(2); }); it("follows a merge to the person who stays, who now carries the number", async () => { const slug = "owner-merged"; const { credential, owner } = await personalObject(slug, PEOPLE_OPEN); const first = (await linked(slug, credential))!; const ids = await put(slug, [{ ref: "keep", table: "person", data: { full_name: "Invented Owner" } }]); await answer(slug, owner, "merge_people", { keep: ids.keep, merge: first }); expect(await linked(slug, credential)).toBe(ids.keep); const handles = await rows(slug, credential, "person_handle"); expect(handles.map((h) => h.data.person_id)).toEqual([ids.keep]); }); it("re-links a binned owner by number on the next request: another person carrying it, else a new person who takes the number's one handle", async () => { const slug = "owner-binned"; const { credential } = await personalObject(slug, PEOPLE_OPEN); const auth = { authorization: `Bearer ${credential}` }; const first = (await linked(slug, credential))!; const bin = async (id: string) => { const res = await http(slug, "POST", "/v1/act", { act: "update", args: { table: "person", id, data: { trashed_at: "2026-10-27T00:00:00.000Z" } } }, auth); expect(res.status, JSON.stringify(res.body)).toBe(200); }; // Nobody else carries the number: a new person, and the binned one's handle moves to them. await bin(first); const second = (await linked(slug, credential))!; expect(second).not.toBe(first); const handles = await rows(slug, credential, "person_handle"); expect(handles).toHaveLength(1); expect(handles[0]!.data).toMatchObject({ person_id: second, normalised: DIGITS }); // Someone else carries it when the owner is binned again: they are linked, and nobody is made. const ids = await put(slug, [ { ref: "twin", table: "person", data: { full_name: "Invented Twin" } }, { ref: "twinPhone", table: "person_handle", data: { person_id: "@twin", kind: "phone", value: "+44 7700 900009", normalised: "447700900009", source: "import" } }, ]); await http(slug, "POST", "/v1/act", { act: "update", args: { table: "person_handle", id: handles[0]!.id, data: { person_id: ids.twin } } }, auth); await bin(second); expect(await linked(slug, credential)).toBe(ids.twin); expect((await rows(slug, credential, "person")).filter((p) => !p.data.trashed_at).map((p) => p.id)).toEqual([ids.twin]); }); it("is named only by a personal object: a business serves no owner person", async () => { const slug = "owner-business"; expect((await http(slug, "PUT", "/v1/admin/business", { name: "Studio", kind: "business", owner_phone: OWNER_PHONE }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "studio" } }, ADMIN)).status).toBe(200); const meta = await http<{ business: Record }>(slug, "PUT", "/v1/admin/business", {}, ADMIN); expect("owner_person_id" in meta.body.business).toBe(false); }); }); /** * RULE (e2e rulings R1, R2; 16 Nov 2026): with People * sealed the object cannot find a contact by a sealed handle, so it makes the * owner's person and phone handle sealed at arrival, under the ids the * sign-in number derives, once an arrival key is held; until then it makes * nothing and every request still answers. The same number finds the same * card on every later request. */ describe("the owner a personal object names, People sealed", () => { it("is made sealed at arrival under the number's own ids once a phone has handed over the arrival key", async () => { const slug = "owner-sealed"; expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200); // The first request keeps the kit; then People are sealed, as the kit will seal them (still pending there: the importers match by handle). expect((await http(slug, "GET", "/v1/definitions", undefined, ADMIN)).status).toBeLessThan(500); await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_i, state) => { for (const [id, seal] of [["person", { clear: ["trashed_at", "archived_at"] }], ["person_handle", { clear: ["person_id", "kind", "position", "trashed_at"] }]] as const) state.storage.sql.exec("UPDATE definitions SET json = json_set(json, '$.seal', json(?)) WHERE id = ?", JSON.stringify(seal), `table:${id}`); }); const credential = await signIn(slug); expect(await linked(slug, credential), "no arrival key yet: nothing is made, and the request answers").toBeNull(); expect(await rows(slug, credential, "person")).toEqual([]); const handed = await http(slug, "POST", "/v1/act", { act: "create", args: { table: "seal_key", id: "arrival", data: { pub: await arrivalPub() } } }, { authorization: `Bearer ${credential}` }); expect(handed.status, JSON.stringify(handed.body)).toBe(200); const ids = ownerIds(DIGITS); expect(await linked(slug, credential)).toBe(ids.person); const [person] = await rows(slug, credential, "person"); expect(person!.id).toBe(ids.person); expect(String(person!.data.dek_enc)).toMatch(/^a1:/); expect(person!.data).not.toHaveProperty("full_name"); expect(await openArrivalRow(person!.data, bodyAad(DATABASE_ID, "person", ids.person), "person", ids.person)).toMatchObject({ full_name: UNNAMED_OWNER }); const [handle] = await rows(slug, credential, "person_handle"); expect(handle!.id).toBe(ids.handle); expect(handle!.data).toMatchObject({ person_id: ids.person, kind: "phone" }); expect(JSON.stringify(handle!.data)).not.toContain(DIGITS); expect(await openArrivalRow(handle!.data, bodyAad(DATABASE_ID, "person_handle", ids.handle), "person_handle", ids.handle)).toMatchObject({ normalised: DIGITS }); expect(await linked(slug, credential), "the same card on the next request").toBe(ids.person); expect(await rows(slug, credential, "person")).toHaveLength(1); }); });