Cotton code
105 lines · 7.1 KB · TypeScriptRaw
/** * SERVER 27 (coordinator's review of EMPTY BIN 1 and AVATAR SEAL 4, 27 Nov 2026), with token scopes off, as the owner's * account runs today, and each credential kind as it really calls: *   H1. `import: true` logs a write only as having happened (never undone): a token never says it, on any act, at *       /v1/act or through run_act. The Mac importer says it with its device credential, which still may. *   H1/M1. A token's delete of a message, chat or photo already in the Bin is refused: it would take the row out of the *       phone's Bin before the owner saw it there. The owner's device deletes it as before. *   H2. /v1/admin/erase takes the admin token alone: an owner token is refused, scopes off. /v1/admin/reset too on live; *       on the dev and test Workers the owner's own token resets as well (a builder's dev-seed --reset). * Invented rows only. */import { describe, expect, it } from "vitest";import { resetTakesOwner } from "../src/business.ts";import { ADMIN, http, personalObject, put, tool } from "./personal.ts";const said = (r: { content: { text?: string }[] }): string => r.content.map((c) => c.text ?? "").join(" ");describe("SERVER 27: what a token never does", () => {  it("never writes as an import, never deletes from the Bin of messages, chats and photos; the device does both", async () => {    const slug = "server-27-acts";    const { credential, owner } = await personalObject(slug, { open: ["note", "message", "thread", "photo"] });    const phone = { authorization: `Bearer ${credential}` };    const token = { authorization: `Bearer ${owner}` };    const at = "2026-11-01T08:00:00.000Z";    const ids = await put(slug, [      { ref: "thread", table: "thread", data: { channel: "whatsapp", title: "Invented group", trashed_at: at } },      { ref: "timed", table: "message", data: { thread_id: "@thread", channel: "whatsapp", body: "ended by the timer", sent_at: at, trashed_at: at } },      { ref: "live", table: "message", data: { thread_id: "@thread", channel: "whatsapp", body: "still in the chat", sent_at: at } },      { ref: "photo", table: "photo", data: { key: "never-came-up", trashed_at: at } },      { ref: "photo2", table: "photo", data: { key: "also-never-came-up", trashed_at: at } },      { ref: "note", table: "note", data: { title: "An invented note" } },    ]);    const act = (who: Record<string, string>, name: string, args: Record<string, unknown>) => http<{ code?: string }>(slug, "POST", "/v1/act", { act: name, args }, who);    const code = async (p: Promise<{ status: number; body: { code?: string } }>) => {      const r = await p;      return `${r.status} ${r.body.code ?? ""}`.trim();    };    const runAct = (name: string, args: Record<string, unknown>) => tool(slug, owner, "run_act", { act: name, args });    // H1: import: true, refused to a token at /v1/act and through run_act, delete and update alike, binned row or live.    for (const [name, args] of [      ["delete", { table: "message", id: ids.timed, import: true }],      ["delete", { table: "message", id: ids.live, import: true }],      ["update", { table: "message", id: ids.live, data: { body: "overwritten" }, import: true }],      ["update", { table: "note", id: ids.note, data: { title: "overwritten" }, import: true }],      ["create", { table: "note", data: { title: "planted" }, import: true }],    ] as const) {      expect(await code(act(token, name, args)), `/v1/act ${name} ${JSON.stringify(args)}`).toBe("403 owner_only");      const ran = await runAct(name, args);      expect(ran.isError, `run_act ${name} ${JSON.stringify(args)}`).toBe(true);      expect(said(ran)).toMatch(/only the owner's own devices write as an import/);    }    // H1, M1: a token's plain delete of a message, chat or photo in the Bin is refused, at /v1/act and through run_act.    for (const [table, id] of [["message", ids.timed], ["thread", ids.thread], ["photo", ids.photo]] as const) {      expect(await code(act(token, "delete", { table, id })), `/v1/act delete ${table}`).toBe("403 in_the_bin");      const ran = await runAct("delete", { table, id });      expect(ran.isError, `run_act delete ${table}`).toBe(true);      expect(said(ran)).toMatch(/is in the Bin/);    }    // Still in the phone's Bin, untouched.    const binned = await tool(slug, owner, "list_bin");    const inBin = (JSON.parse(said(binned)) as { records: { id: string }[] }).records.map((r) => r.id);    for (const id of [ids.timed, ids.thread, ids.photo]) expect(inBin).toContain(id);    // A token's delete of a row not in the Bin goes on as before (undo holds it).    expect((await act(token, "delete", { table: "message", id: ids.live })).status).toBe(200);    // The owner's device: deletes a binned photo, and writes as an import (the Mac importer's device credential).    expect((await act(phone, "delete", { table: "photo", id: ids.photo })).status).toBe(200);    const imported = await act(phone, "delete", { table: "photo", id: ids.photo2, import: true });    expect(imported.status, JSON.stringify(imported.body)).toBe(200);  });  it("takes the admin token alone for erase, and for reset on live; on dev and the tests the owner's token resets too", async () => {    // Which Workers a reset takes the owner's token on: dev and test, never live (ENV_LABEL "production") or unnamed.    expect(resetTakesOwner({ ENV_LABEL: "production" })).toBe(false);    expect(resetTakesOwner({})).toBe(false);    expect(resetTakesOwner({ ENV_LABEL: "dev" })).toBe(true);    expect(resetTakesOwner({ ENV_LABEL: "test" })).toBe(true);    const slug = "server-27-admin";    const { credential, owner } = await personalObject(slug);    for (const who of [owner, credential]) {      const auth = { authorization: `Bearer ${who}` };      for (const body of [{ set: "unlinked_photos" }, { set: "unlinked_photos", confirm: "erase" }, { set: "tables", tables: ["mood"], confirm: "erase" }]) {        const erase = await http<{ code?: string }>(slug, "POST", "/v1/admin/erase", body, auth);        expect([erase.status, erase.body.code], `erase ${JSON.stringify(body)}`).toEqual([403, "admin_only"]);      }    }    // A device credential never resets (the /v1/admin routes take a ctn_ token or the admin token).    const byPhone = await http<{ code?: string }>(slug, "POST", "/v1/admin/reset", {}, { authorization: `Bearer ${credential}` });    expect([byPhone.status, byPhone.body.code], "reset by the phone").toEqual([403, "admin_only"]);    // The admin token, as the coordinator's tooling sends it.    const dry = await http<{ set?: string }>(slug, "POST", "/v1/admin/erase", { set: "unlinked_photos" }, ADMIN);    expect(dry.status, JSON.stringify(dry.body)).toBe(200);    expect((await http(slug, "POST", "/v1/admin/reset", {}, ADMIN)).status).toBe(200);    // This is the test Worker (ENV_LABEL "test"), so the owner's own token resets too, as dev-seed --reset sends it.    const other = "server-27-dev-reset";    const made = await personalObject(other);    const byOwner = await http<{ version?: number }>(other, "POST", "/v1/admin/reset", {}, { authorization: `Bearer ${made.owner}` });    expect(byOwner.status, JSON.stringify(byOwner.body)).toBe(200);  });});