Cotton code
56 lines · 3.3 KB · TypeScriptRaw
// The object keeps its parsed definitions per instance (SLOW READ, 26 Nov 2026; src/business.ts `definitions`). Checked:// a second read hands back the same parse; a definition written through the act, or straight into SQLite, is seen by// the very next read; a table's new column is listed on the next request.import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { http, personalObject } from "./personal.ts";import type { DefinitionsAnswer } from "../src/protocol.ts";type Defs = { table(id: string): { raw: { name?: string }; columns: { key: string }[] } | undefined };const parsed = (instance: unknown) => (instance as { definitions(): Defs }).definitions();describe("the object's parsed definitions", () => {  it("are kept between reads and read again the moment any definition is written", async () => {    const slug = `definitions-cache-${Date.now()}`;    const { credential } = await personalObject(slug);    const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug));    const auth = { authorization: `Bearer ${credential}` };    const same = await runInDurableObject(stub, (instance) => parsed(instance) === parsed(instance));    expect(same, "nothing written: the same parse").toBe(true);    // Through the owner's definition act.    const book = (await http<DefinitionsAnswer>(slug, "GET", "/v1/definitions", undefined, auth)).body.tables.find((t) => t.id === "book")!;    const before = await runInDurableObject(stub, (instance) => parsed(instance));    expect((await http(slug, "POST", "/v1/act", { act: "definition", args: { kind: "table", definition: { ...book, name: "Synthetic shelf" } } }, auth)).status).toBe(200);    const afterAct = await runInDurableObject(stub, (instance) => {      const d = parsed(instance);      return { name: d.table("book")?.raw.name, fresh: d !== before };    });    expect(afterAct).toEqual({ name: "Synthetic shelf", fresh: true });    // Straight into SQLite, as a keep or a migration writes: seen by the next read in the same instance.    const raw = await runInDurableObject(stub, (instance, state) => {      parsed(instance);      const held = JSON.parse(state.storage.sql.exec<{ json: string }>("SELECT json FROM definitions WHERE id = 'table:book'").one().json) as { name: string; columns: { key: string }[] };      held.name = "Synthetic library";      held.columns = [...held.columns, { key: "synthetic_shelf_mark", type: "text" } as { key: string }];      state.storage.sql.exec("UPDATE definitions SET json = ? WHERE id = 'table:book'", JSON.stringify(held));      const d = parsed(instance).table("book");      return { name: d?.raw.name, column: d?.columns.some((c) => c.key === "synthetic_shelf_mark") };    });    expect(raw).toEqual({ name: "Synthetic library", column: true });    // And a request after it lists what was written.    const served = (await http<DefinitionsAnswer>(slug, "GET", "/v1/definitions", undefined, auth)).body.tables.find((t) => t.id === "book")!;    expect(served.name).toBe("Synthetic library");    // A deleted definition is gone on the next read.    const gone = await runInDurableObject(stub, (instance, state) => {      parsed(instance);      state.storage.sql.exec("DELETE FROM definitions WHERE id = 'table:book'");      return parsed(instance).table("book");    });    expect(gone).toBeUndefined();  });});