import { describe, expect, it } from"vitest";import { answer, EVENTS_OPEN, mint, NOTES_OPEN, personalObject, put, refusal, TODOS_OPEN, PEOPLE_OPEN } from"./personal.ts";typeHit = { table: string; id: string; column: string; excerpt: string; at: string | null; title?: string; archived?: true };typeSearch = { q: string; hits: Hit[]; more: boolean };typePage = { table: string; records: { id: string; data: Record<string, unknown> }[]; next_page: number | null; total: number };const id = (n: number) => `5e000000-0000-4000-8000-${String(n).padStart(12, "0")}`;const THREAD_A = id(9001);const THREAD_B = id(9002);const MAIL = id(9003);const LONG = `${"The quarterly numbers came in late again and the board wants a summary before Friday. ".repeat(3)}Ask Priya Raman about the Tinfactory contract. ${"Everything else can wait until the offsite next month. ".repeat(3)}`;/** * Synthetic todos under two projects, events under a third, a person, a * secret and a note, each with its own time. Messages and Email are sealed (e3bca3c99) and * searched only in a window (blind-search.test.ts), so the clear search is * shown here over clear tables of the same shape: a dated row under a link, a * long body under a title. */asyncfunction seeded(slug: string): Promise<{ credential: string; owner: string }> {const made = await personalObject(slug, { open: [...TODOS_OPEN, ...NOTES_OPEN.open, ...PEOPLE_OPEN.open, ...EVENTS_OPEN.open] });await put(slug, [ { verb: "put", table: "project", id: THREAD_A, data: { slug: "lunch-club", name: "Lunch club" } }, { verb: "put", table: "project", id: THREAD_B, data: { slug: "family", name: "Family" } }, { verb: "put", table: "project", id: MAIL, data: { slug: "minutes", name: "Board minutes" } }, { verb: "put", table: "todo", id: id(1), data: { project: THREAD_A, title: "Lunch with Priya Raman at noon?", due_at: "2026-10-20T10:00:00.000Z" } }, { verb: "put", table: "todo", id: id(2), data: { project: THREAD_B, title: "PRIYA RAMAN says hello", due_at: "2026-10-01T08:00:00.000Z" } }, { verb: "put", table: "todo", id: id(3), data: { project: THREAD_A, title: "see you there", due_at: "2026-10-20T10:05:00.000Z" } }, { verb: "put", table: "todo", id: id(4), data: { project: THREAD_A, title: "Priya Raman cancelled", due_at: "2026-10-19T09:00:00.000Z", trashed_at: "2026-10-19" } }, { verb: "put", table: "event", id: id(11), data: { project: MAIL, title: "Contract renewal", calendar_name: "board@example.test", notes: LONG, starts_at: "2026-10-22T06:00:00.000Z" } }, { verb: "put", table: "event", id: id(12), data: { project: MAIL, title: "Weekly digest", calendar_name: "news@example.test", notes: "Nothing about anyone.", starts_at: "2026-10-23T06:00:00.000Z" } }, { verb: "put", table: "person", id: id(21), data: { full_name: "Priya Raman", org: "Tinfactory" } }, { verb: "put", table: "secret", id: id(31), data: { name: "Bank login", kind: "login", service: "bank", hint: "priya raman's birthday", sealed: "sealed-bytes" } }, ]);return made;}/** RULE (owner, 26 Oct 2026): a research session finds the records about a person, a company or a word without paging a table — search_records, newest first, a hit and a few words around it. */describe("search_records", () => {const SLUG = "search-records";let owner = "";let credential = ""; it("finds the words across tables, newest first, each hit its column, an excerpt and its time", async () => { ({ credential, owner } = await seeded(SLUG));const found = await answer<Search>(SLUG, owner, "search_records", { q: "priya raman" }); expect(found.q).toBe("priya raman"); expect(found.more).toBe(false);// The person is timed by when it was made (today); the event and the todos by their own dates. The binned todo is left out. expect(found.hits.map((h) => `${h.table}:${h.id}`)).toEqual([`person:${id(21)}`, `event:${id(11)}`, `todo:${id(1)}`, `todo:${id(2)}`]);const [person, email, lunch, hello] = found.hits; expect(person).toMatchObject({ table: "person", column: "full_name", excerpt: "Priya Raman" }); expect(person!.title).toBeUndefined(); expect(email).toMatchObject({ table: "event", column: "notes", at: "2026-10-22T06:00:00.000Z", title: "Contract renewal" }); expect(email!.excerpt).toMatch(/^….*AskPriyaRaman about the Tinfactory contract\..*…$/); expect(email!.excerpt.length).toBeLessThan(200); expect(lunch).toEqual({ table: "todo", id: id(1), column: "title", excerpt: "Lunch with Priya Raman at noon?", at: "2026-10-20T10:00:00.000Z" }); expect(hello).toMatchObject({ column: "title", excerpt: "PRIYA RAMAN says hello", at: "2026-10-01T08:00:00.000Z" });// A word is a word: the % and _ of LIKE are taken as written. expect((await answer<Search>(SLUG, owner, "search_records", { q: "pr%ya" })).hits).toEqual([]);// Words the stored JSON escapes (a quote, a backslash) are found as written.await put(SLUG, [{ verb: "put", table: "note", id: id(41), data: { title: "Standup", body: 'She said "ship it" \\o/' } }]);const quoted = await answer<Search>(SLUG, owner, "search_records", { q: '"SHIP IT" \\o/', table: "note" }); expect(quoted.hits).toMatchObject([{ table: "note", id: id(41), column: "body", excerpt: 'She said "ship it" \\o/', title: "Standup" }]); }); it("searches only the table or tables named, and refuses one it cannot read", async () => {const todos = await answer<Search>(SLUG, owner, "search_records", { q: "Priya", table: "todo" }); expect(todos.hits.map((h) => h.id)).toEqual([id(1), id(2)]);const two = await answer<Search>(SLUG, owner, "search_records", { q: "tinfactory", tables: ["event", "person"] }); expect(two.hits.map((h) => `${h.table}:${h.column}`)).toEqual(["person:org", "event:notes"]); expect(await refusal(SLUG, owner, "search_records", { q: "Priya", table: "todo", tables: ["event"] })).toBe("say table or tables, not both"); expect(await refusal(SLUG, owner, "search_records", { q: "Priya", table: "nope" })).toBe("No tool draws this table."); expect(await refusal(SLUG, owner, "search_records", { q: "Priya", tables: ["event", "mcp_token"] })).toBe("No tool draws this table."); expect(await refusal(SLUG, owner, "search_records", { table: "todo" })).toBe("q is required: the words to find — a name, a company, a word"); expect(await refusal(SLUG, owner, "search_records", { q: "p" })).toBe("q is at least two letters"); }); it("keeps the hits in a since/until window, a day read in the workspace's zone", async () => {const since = await answer<Search>(SLUG, owner, "search_records", { q: "priya", tables: ["todo", "event"], since: "2026-10-15" }); expect(since.hits.map((h) => h.id)).toEqual([id(11), id(1)]);// Until a day keeps that whole day in Asia/Kolkata.const until = await answer<Search>(SLUG, owner, "search_records", { q: "priya", tables: ["todo", "event"], until: "2026-10-20" }); expect(until.hits.map((h) => h.id)).toEqual([id(1), id(2)]);const instant = await answer<Search>(SLUG, owner, "search_records", { q: "priya", since: "2026-10-21T00:00:00Z", until: "2026-10-22T06:00:00Z" }); expect(instant.hits.map((h) => h.id)).toEqual([id(11)]); expect(await refusal(SLUG, owner, "search_records", { q: "priya", since: "2026-10-22", until: "2026-10-21" })).toBe("since is after until: the window holds no time"); expect(await refusal(SLUG, owner, "search_records", { q: "priya", since: "last week" })).toBe('since is a day (YYYY-MM-DD, in the reader\'s zone) or an instant with its offset (2026-10-25T09:00:00Z); "last week" is not one'); }); it("answers at most limit hits, 200 at the most, and says there were more", async () => {await put( SLUG,Array.from({ length: 205 }, (_, i) => ({ verb: "put", table: "todo", id: id(1000 + i), data: { project: THREAD_B, title: `zebra crossing ${i}`, due_at: newDate(Date.UTC(2026, 8, 1) + i * 60_000).toISOString() } })), );const first = await answer<Search>(SLUG, owner, "search_records", { q: "zebra" }); expect(first.hits).toHaveLength(25); expect(first.more).toBe(true); expect(first.hits[0]!.excerpt).toBe("zebra crossing 204");const capped = await answer<Search>(SLUG, owner, "search_records", { q: "zebra", limit: 5000 }); expect(capped.hits).toHaveLength(200); expect(capped.more).toBe(true);const three = await answer<Search>(SLUG, owner, "search_records", { q: "zebra crossing 20", limit: 3 }); expect(three.hits.map((h) => h.excerpt)).toEqual(["zebra crossing 204", "zebra crossing 203", "zebra crossing 202"]); expect(three.more).toBe(true); }); it("is a read: a read or lane token searches what it reads; never a token, a sealed value or a hint; a token without read is refused", async () => {const reader = await mint(SLUG, credential, "priya raman's reader", ["read"]);const lane = await mint(SLUG, credential, "Lane mac-a", ["read", "lane:widgets"], 7);const writer = await mint(SLUG, credential, "a writer", ["write"]);for (const token of [owner, reader, lane]) {const found = await answer<Search>(SLUG, token, "search_records", { q: "priya raman", limit: 200 });// The token named for her (an internal table) and the secret's hint are never searched. expect(found.hits.map((h) => h.table).sort()).toEqual(["event", "person", "todo", "todo"]); }const bank = await answer<Search>(SLUG, owner, "search_records", { q: "bank" }); expect(bank.hits.map((h) => `${h.table}:${h.column}`)).toEqual(["secret:name"]); expect((await answer<Search>(SLUG, owner, "search_records", { q: "sealed-bytes" })).hits).toEqual([]); expect(await refusal(SLUG, writer, "search_records", { q: "priya" })).toBe('this token does not have "read" access. Ask the owner to reconnect with read enabled.'); });});/** * RULE (owner, 26 Oct 2026): a session reads one thread's messages or one person's emails by where, in a window, without paging the table. * Messages and Email are sealed (e3bca3c99), so the clear reading is shown over a project's todos and a calendar's events. */describe("list_records where and since/until", () => {const SLUG = "search-list";let owner = ""; it("reads one project's todos by where, and one calendar's events", async () => { ({ owner } = await seeded(SLUG));const thread = await answer<Page>(SLUG, owner, "list_records", { table: "todo", where: { project: THREAD_A } });// Newest first by due_at; the binned one left out. expect(thread.records.map((r) => r.id)).toEqual([id(3), id(1)]);const both = await answer<Page>(SLUG, owner, "list_records", { table: "event", where: { calendar_name: "board@example.test", title: "Contract renewal" } }); expect(both.records.map((r) => r.id)).toEqual([id(11)]); }); it("keeps the records in a since/until window by the table's date", async () => {const window = await answer<Page>(SLUG, owner, "list_records", { table: "todo", where: { project: THREAD_A }, since: "2026-10-20T10:01:00Z" }); expect(window.records.map((r) => r.id)).toEqual([id(3)]); expect(window.total).toBe(1);const days = await answer<Page>(SLUG, owner, "list_records", { table: "todo", since: "2026-10-01", until: "2026-10-19" }); expect(days.records.map((r) => r.id)).toEqual([id(2)]);// A table with no date is timed by when its records were made.const made = await answer<Page>(SLUG, owner, "list_records", { table: "person", until: "2026-01-01" }); expect(made.total).toBe(0); expect(await refusal(SLUG, owner, "list_records", { table: "todo", ids: [id(1)], since: "2026-10-01" })).toBe("ids names the records itself; ask without since"); expect(await refusal(SLUG, owner, "list_records", { table: "todo", until: "soon" })).toBe('until is a day (YYYY-MM-DD, in the reader\'s zone) or an instant with its offset (2026-10-25T09:00:00Z); "soon" is not one'); });});