Cotton code
57 lines · 3.0 KB · TypeScriptRaw
/** * The send rule (cotton-rules send.rs) answers the same through the Worker's * WASM as it does natively: every case in its vectors, run here over the * vectors' stores through the asked door, lands what the Rust test wrote. */import { describe, expect, it } from "vitest";import vectors from "../../../crates/cotton-rules/src/send_vectors.json";import { tryPlan, tryRule } from "../src/rules/index.ts";type Row = Record<string, unknown> & { id: string };type Rows = Record<string, Row[]>;function asker(rows: Rows, media: Record<string, number>) {  const table = (t: string): Row[] => rows[t] ?? [];  return (question: string, args: Record<string, unknown>): unknown => {    if (question === "send.row") return table(String(args.table)).find((r) => r.id === args.id) ?? null;    if (question === "send.linked") return table(String(args.table)).filter((r) => r[String(args.column)] === args.id);    if (question === "send.find") {      const equals = args.equals as Record<string, unknown>;      return table(String(args.table))        .filter((r) => Object.entries(equals).every(([k, v]) => r[k] === v))        .map((r) => r.id);    }    if (question === "send.weigh") return media[String(args.key)] ?? null;    throw new Error(`no answer for ${question}`);  };}describe("send, two implementations", () => {  for (const c of vectors.cases) {    it(`lands ${c.name} as the core does`, () => {      const made = tryPlan<unknown>(        "send.contents",        { id: `send-${c.name}`, table: c.table, record: c.record, from: vectors.from, made_at: vectors.made_at, source_database: vectors.sender.database, tables: vectors.tables },        asker(vectors.sender.rows as unknown as Rows, vectors.sender.media),      );      const expected = (vectors.expect as Record<string, Record<string, unknown>>)[c.name]!;      if ("error" in made) {        expect({ refused: made.code }).toEqual(expected);        return;      }      const receiver = (vectors.receivers as Record<string, { database: string; rows: Rows }>)[c.receiver]!;      const land = { payload: made.ok, tables: vectors.tables, database: receiver.database, sent_by: null, sent_at: vectors.sent_at };      const ask = asker(receiver.rows, {});      const held = tryPlan<unknown>("send.held", land, ask);      const landing = tryPlan<unknown>("send.keep", land, ask);      expect({ payload: made.ok, held: "ok" in held ? held.ok : held, landing: "ok" in landing ? landing.ok : landing }).toEqual(expected);    });  }  it("says a table with no shape, or a sealed one, cannot be sent", () => {    expect(tryRule("send.sendable", { table: "todo", tables: vectors.tables })).toEqual({ ok: { sendable: false, code: "not_sendable", words: "This kind of record cannot be sent." } });    expect(tryRule<{ sendable: boolean }>("send.sendable", { table: "message", tables: vectors.tables })).toMatchObject({ ok: { sendable: false } });    expect(tryRule("send.sendable", { table: "book", tables: vectors.tables })).toEqual({ ok: { sendable: true } });  });});