diff --git a/test/connect-lww.test.ts b/test/connect-lww.test.ts index 6e20805..3bc3a55 100644 --- a/test/connect-lww.test.ts +++ b/test/connect-lww.test.ts @@ -22,11 +22,14 @@ import { applyOps, mint, project, - type ConnectOp, - type DeleteNodeOp, - type Op, - type WorkflowJSON, - type WorkflowNode, +} from "../src/index.js"; +import type { + AddNodeOp, + ConnectOp, + DeleteNodeOp, + Op, + WorkflowJSON, + WorkflowNode, } from "../src/index.js"; import { canonicalize, loadCatalog } from "./helpers.js"; import { checkGraphInvariants } from "./graph-invariant-oracle.js"; @@ -148,7 +151,7 @@ function connectOp( }; } -function addEncoder(tag: string, actor: string, baseVersion: number, nodeId: number, text: string): Op { +function addEncoder(tag: string, actor: string, baseVersion: number, nodeId: number, text: string): AddNodeOp { return { op: "add_node", op_id: opId(tag), @@ -159,7 +162,7 @@ function addEncoder(tag: string, actor: string, baseVersion: number, nodeId: num class_type: "CLIPTextEncode", pos: [40, 300], node: encoderNode(nodeId, text), - } as Op; + }; } function deleteOp(tag: string, actor: string, baseVersion: number, nodeId: number, removed: number[]): DeleteNodeOp { @@ -226,6 +229,13 @@ function runOrder(base: WorkflowJSON, ops: Op[]): { json: string; wf: WorkflowJS const res = applyOps(doc, ops, catalog); const failed = res.outcomes.find((outcome) => outcome.outcome === "rejected"); expect(failed, `a legal interleaving must never abort the batch: ${JSON.stringify(failed)}`).toBeUndefined(); + const beforeReplayBytes = Y.encodeStateAsUpdate(doc); + const beforeReplayProjection = project(doc, catalog); + const replay = applyOps(doc, ops, catalog); + expect(replay.outcomes).toHaveLength(ops.length); + expect(replay.outcomes.every((outcome) => outcome.outcome === "no-op"), "identical replays must all be no-ops").toBe(true); + expect(Y.encodeStateAsUpdate(doc), "an identical same-document replay must be byte-identical").toEqual(beforeReplayBytes); + expect(project(doc, catalog), "an identical same-document replay must leave the exact projection unchanged").toEqual(beforeReplayProjection); const violations = checkGraphInvariants(doc); expect(violations, `graph invariant violation: ${JSON.stringify(violations)}`).toEqual([]); const wf = project(doc, catalog); @@ -461,8 +471,11 @@ describe("generated two-writer streams over a contested input", () => { }; } - for (let seed = 1; seed <= 12; seed++) { - it(`seed ${seed}: every interleaving converges`, () => { + it("all 12 seeds include both contending and independent input registers", () => { + let contendingCases = 0; + let independentCases = 0; + + for (let seed = 1; seed <= 12; seed++) { const rand = lcg(seed); const pick = (xs: T[]): T => xs[Math.floor(rand() * xs.length)]!; const bvA = pick([3, 5, 7, 9]); @@ -471,17 +484,32 @@ describe("generated two-writer streams over a contested input", () => { const srcB = pick([ENCODER, OTHER_ENCODER]); const slotA = pick([POSITIVE, 2]); const slotB = pick([POSITIVE, 2]); + const connectA = connectOp("j2", AGENT, bvA, 9801, srcA, SAMPLER, slotA); + const connectB = connectOp("j3", HUMAN, bvB, 9802, srcB, SAMPLER, slotB); const writerA: Op[] = [ addEncoder("j1", AGENT, bvA, FRESH, "generated"), - connectOp("j2", AGENT, bvA, 9801, srcA, SAMPLER, slotA), + connectA, ]; - const writerB: Op[] = [connectOp("j3", HUMAN, bvB, 9802, srcB, SAMPLER, slotB)]; + const writerB: Op[] = [connectB]; if (rand() < 0.5) writerB.push(deleteOp("j4", HUMAN, bvB, srcB, [9802])); - expectConvergent(wiredBaseWorkflow(), writerA, writerB); - }); - } + try { + expectConvergent(wiredBaseWorkflow(), writerA, writerB); + } catch (error) { + if (error instanceof Error) { + error.message = `seed ${seed} (slotA=${slotA}, slotB=${slotB}): ${error.message}`; + } + throw error; + } + if (connectA.to_node === connectB.to_node && connectA.to_slot === connectB.to_slot) contendingCases++; + else independentCases++; + } + + // The fixed 12-seed domain currently exercises six cases of each class. + expect(contendingCases, "generated seeds must exercise same-register contention").toBeGreaterThanOrEqual(6); + expect(independentCases, "generated seeds must preserve independent-register coverage").toBeGreaterThanOrEqual(6); + }); }); // --------------------------------------------------------------------------- @@ -519,7 +547,7 @@ describe("ops minted in one batch share a base_version", () => { const first = connectOp("k1", AGENT, 5, 9901, ENCODER, SAMPLER, POSITIVE); const second = connectOp("k0", AGENT, 5, 9902, OTHER_ENCODER, SAMPLER, POSITIVE); const { wf } = runOrder(baseWorkflow(), [first, second]); - // "k0…" < "k1…" by code point, so the FIRST spec wins despite arriving first. + // "k1…" > "k0…" by code point, so the greater op_id wins and selects link 9901. expect(inputLink(wf, SAMPLER, POSITIVE)).toBe(9901); expect(linkIds(wf)).toEqual([9901]); }); diff --git a/test/exhaustiveness.test.ts b/test/exhaustiveness.test.ts index c9db6ee..c2cb8d2 100644 --- a/test/exhaustiveness.test.ts +++ b/test/exhaustiveness.test.ts @@ -166,6 +166,7 @@ describe("widget-storage strategy classifiers", () => { it("agrees with the write side for every node of a real frontend-only session", () => { const doc = mint(noteWorkflow.base_workflow, catalog, "sha"); const nodes = doc.getMap>("nodes"); + let opaqueWithoutCatalogOrder = 0; expect(nodes.size).toBeGreaterThan(0); nodes.forEach((node) => { const type = String(node.get("type") ?? ""); @@ -173,8 +174,10 @@ describe("widget-storage strategy classifiers", () => { const expected = node.has(OPAQUE_WIDGETS_KEY) ? "opaque" : "named"; expect(widgetStorageOf(node)).toBe(expected); if (order === undefined && node.has(OPAQUE_WIDGETS_KEY)) { + opaqueWithoutCatalogOrder++; expect(widgetStorageFor(node.get(OPAQUE_WIDGETS_KEY), order)).toBe("opaque"); } }); + expect(opaqueWithoutCatalogOrder).toBeGreaterThan(0); }); }); diff --git a/test/fuzz-untrusted-input.test.ts b/test/fuzz-untrusted-input.test.ts index 6a25f9e..462c145 100644 --- a/test/fuzz-untrusted-input.test.ts +++ b/test/fuzz-untrusted-input.test.ts @@ -1,11 +1,8 @@ /** * Fuzz taxonomy: untrusted op envelopes and node payloads (#13, #14). * - * The saved corpus below carries two kinds of case. `#13` cases are live - * assertions: the untrusted-node-input guard has landed, so those payloads are - * rejected before any mutation. `#14` cases stay pinned with `it.fails` — the - * payload size/depth/cost bounds do not exist yet, and the pin is what tells us - * the day they do. + * The saved corpus below carries live assertions for issues #13 and #14. Those + * guards reject adversarial node payloads before any mutation. */ import { readFileSync, readdirSync } from "node:fs"; import { dirname, join } from "node:path"; @@ -13,6 +10,7 @@ import { fileURLToPath } from "node:url"; import * as fc from "fast-check"; import * as Y from "yjs"; import { describe, expect, it } from "vitest"; +import { appliedMap } from "../src/doc.js"; import { DEFERRED_OPS, FROZEN_OPS, @@ -70,6 +68,7 @@ describe("fuzz: malformed and adversarial op envelopes", () => { it("project never throws after accepted add_node ops with JSON payloads, nulls, and non-ASCII actors", () => { const valueArb = fc.jsonValue({ maxDepth: 5 }); + let projectedRuns = 0; fc.assert( fc.property(fc.string({ minLength: 1 }), valueArb, fc.integer({ min: 1, max: 1_000_000 }), (actor, value, id) => { const doc = mint(emptyWorkflow, catalog); @@ -86,10 +85,17 @@ describe("fuzz: malformed and adversarial op envelopes", () => { } as unknown as Op; const result = applyOps(doc, [op], catalog); - if (!result.outcomes.some((o) => o.outcome === "rejected")) expect(() => project(doc, catalog)).not.toThrow(); + if (!result.outcomes.some((o) => o.outcome === "rejected")) { + projectedRuns++; + expect(result.outcomes[0]).toMatchObject({ outcome: "applied", op_id: op.op_id }); + const projectedNode = project(doc, catalog).nodes.find((node) => node.id === id); + expect(projectedNode).toMatchObject({ id, type: "KSampler" }); + expect(projectedNode?.fuzz_payload).toEqual(value); + } }), FC_OPTIONS, ); + expect(projectedRuns).toBeGreaterThan(0); }); it("project never throws after accepted NaN/Infinity widget values", () => { @@ -199,24 +205,48 @@ describe("saved untrusted-input regression corpus", () => { nodes: [{ id: 1, type: "KSampler", widgets_values: [] }], links: [], }; + const trailing = { + op: "add_node", + op_id: "f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0", + actor: "corpus-control", + base_version: 1, + stamp: [1, "corpus-control"], + node_id: 2, + class_type: "KSampler", + pos: [0, 0], + node: { id: 2, type: "KSampler", widgets_values: [], fuzz_payload: "trailing-control" }, + } satisfies Op; + + const control = mint(base, catalog); + const controlResult = applyOps(control, [trailing], catalog); + expect(controlResult.outcomes[0]).toMatchObject({ outcome: "applied", op_id: trailing.op_id }); + expect(project(control, catalog).nodes.find((node) => node.id === 2)).toMatchObject({ + id: 2, + type: "KSampler", + fuzz_payload: "trailing-control", + }); + const doc = mint(base, catalog); const before = bytes(doc); - const result = applyOps(doc, [entry.op], catalog); + const rootNamesBefore = [...doc.share.keys()].sort(); + const result = applyOps(doc, [entry.op, trailing], catalog); - expect(result.outcomes.some((o) => o.outcome === "rejected")).toBe(true); + expect(result.outcomes[0]).toMatchObject({ outcome: "rejected", op_id: entry.op.op_id }); + expect(result.outcomes[1]).toMatchObject({ + outcome: "rejected", + op_id: trailing.op_id, + reason: { code: "batch_aborted" }, + }); expect(result.outcomes.filter((o) => o.outcome === "applied").map((o) => o.op_id)).toEqual([]); + expect(appliedMap(doc).has(entry.op.op_id)).toBe(false); + expect(appliedMap(doc).has(trailing.op_id)).toBe(false); expect(bytes(doc).equals(before)).toBe(true); + expect([...doc.share.keys()].sort()).toEqual(rootNamesBefore); + expect(project(doc, catalog).nodes.some((node) => node.id === 2)).toBe(false); expect(() => project(doc, catalog)).not.toThrow(); } for (const entry of corpus) { - if (entry.issue === 13) { - // #13 guard landed: the applier rejects these payloads before any write. - it(`#13: ${entry.name} is rejected before mutation`, () => { - rejectedBeforeMutation(entry); - }); - continue; - } it(`#${entry.issue}: ${entry.name} is rejected before mutation`, () => { rejectedBeforeMutation(entry); }); diff --git a/test/graph-invariant-oracle.test.ts b/test/graph-invariant-oracle.test.ts index 528929e..7b06d89 100644 --- a/test/graph-invariant-oracle.test.ts +++ b/test/graph-invariant-oracle.test.ts @@ -1,7 +1,8 @@ import * as Y from "yjs"; import { describe, expect, it } from "vitest"; import { linksMap, nodesMap } from "../src/doc.js"; -import { mint, type WorkflowJSON } from "../src/index.js"; +import { mint } from "../src/index.js"; +import type { WorkflowJSON } from "../src/index.js"; import { checkGraphInvariants } from "./graph-invariant-oracle.js"; const catalog = { types: {} };