Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions test/graph-invariant-oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ describe("checkGraphInvariants test oracle", () => {
expect(checkGraphInvariants(doc).filter((item) => item.invariant === "I3")).toHaveLength(2);
});

it("reports a short link tuple as I3 without changing the doc", () => {
const doc = freshDoc();
linksMap(doc).set("short", ["short", 1, 0, 2]);
const before = Y.encodeStateAsUpdate(doc);

expect(checkGraphInvariants(doc).filter((item) => item.invariant === "I3")).toEqual([
{
invariant: "I3",
path: 'links["short"]',
message: "link entry is not a tuple with node endpoints",
},
]);
expect(Y.encodeStateAsUpdate(doc)).toEqual(before);
});

it("reports negative source and destination slots as I4 without changing the doc", () => {
const doc = freshDoc();
const links = linksMap(doc);
links.delete("10");
links.set("negative", ["negative", 1, -1, 2, -2, "X"]);
const before = Y.encodeStateAsUpdate(doc);

expect(checkGraphInvariants(doc).filter((item) => item.invariant === "I4")).toEqual([
{
invariant: "I4",
path: 'links["negative"][2]',
message: "source output (1, -1) does not advertise link negative",
},
{
invariant: "I4",
path: 'links["negative"][4]',
message: "destination input (2, -2) does not carry link negative",
},
]);
expect(Y.encodeStateAsUpdate(doc)).toEqual(before);
});

it("reports links-to-slots disagreement", () => {
const doc = freshDoc();
const source = nodesMap(doc).get("1")!;
Expand All @@ -64,4 +101,35 @@ describe("checkGraphInvariants test oracle", () => {

expect(checkGraphInvariants(doc).map((item) => item.invariant)).toContain("I5");
});

it("reports code-unit-ordered non-ASCII I5 diagnostics independently of link insertion order", () => {
const diagnosticsFor = (keys: string[]) => {
const doc = freshDoc();
const links = linksMap(doc);
links.delete("10");
for (const key of keys) links.set(key, [key, 1, 0, 2, 0, "X"]);

return checkGraphInvariants(doc).filter((item) => item.invariant === "I5");
};
const expected = [
{
invariant: "I5",
path: 'links["z"]',
message: "input register (2, 0) is also claimed by link y",
},
{
invariant: "I5",
path: 'links["ä"]',
message: "input register (2, 0) is also claimed by link y",
},
{
invariant: "I5",
path: 'links["é"]',
message: "input register (2, 0) is also claimed by link y",
},
];

expect(diagnosticsFor(["y", "z", "ä", "é"])).toEqual(expected);
expect(diagnosticsFor(["é", "ä", "z", "y"])).toEqual(expected);
});
});
17 changes: 12 additions & 5 deletions test/graph-invariant-oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function sameLink(a: unknown, b: unknown): boolean {
return a === b || (a != null && b != null && linkKey(a) === linkKey(b));
}

function compareCodeUnits(a: string, b: string): number {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}

function violation(
out: GraphInvariantViolation[],
invariant: GraphInvariant,
Expand Down Expand Up @@ -88,11 +94,12 @@ export function checkGraphInvariants(doc: Y.Doc): GraphInvariantViolation[] {
// endpoint-side references. I4 is not attempted when I3 already fails for
// that endpoint, which keeps one missing node from producing duplicate noise.
const inputClaims = new Map<string, string>();
links.forEach((rawTuple, linkMapKey) => {
const linkEntries = [...links.entries()].sort(([a], [b]) => compareCodeUnits(a, b));
for (const [linkMapKey, rawTuple] of linkEntries) {
const path = `links[${JSON.stringify(linkMapKey)}]`;
if (!Array.isArray(rawTuple) || rawTuple.length < 5) {
violation(violations, "I3", path, "link entry is not a tuple with node endpoints");
return;
continue;
}

const tuple = rawTuple as unknown[];
Expand Down Expand Up @@ -173,12 +180,12 @@ export function checkGraphInvariants(doc: Y.Doc): GraphInvariantViolation[] {
);
}
}
});
}

return violations.sort((a, b) => {
const invariant = INVARIANT_ORDER[a.invariant] - INVARIANT_ORDER[b.invariant];
if (invariant !== 0) return invariant;
const path = a.path.localeCompare(b.path);
return path !== 0 ? path : a.message.localeCompare(b.message);
const path = compareCodeUnits(a.path, b.path);
return path !== 0 ? path : compareCodeUnits(a.message, b.message);
});
}
5 changes: 3 additions & 2 deletions test/opid-payload-reuse.regression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Y from "yjs";
import { describe, expect, it } from "vitest";
import { applyOps, mint, project, type SetWidgetOp } from "../src/index.js";
import { canonicalOp } from "../src/applier.js";
import { sha256Hex } from "../src/digest.js";
import { appliedMap } from "../src/doc.js";
import { loadCatalog, loadLwwVectors } from "./helpers.js";

Expand Down Expand Up @@ -69,7 +68,9 @@ describe("regression: op_id reuse with a changed payload (#12)", () => {
'"op":"set_widget","op_id":"same0000000000000000000000000000",' +
'"stamp":[5,"human:a"],"value":25,"widget":"steps"}';
expect(canonicalOp(firstOp())).toBe(canonical);
expect(appliedMap(doc).get(firstOp().op_id)).toBe(sha256Hex(canonical));
expect(appliedMap(doc).get(firstOp().op_id)).toBe(
"58cfa9fd689bf1126598dfa798a0bfaa41b45bad2a55edfdcc952afb221f5bd3",
);
// Bounded: 64 hex chars regardless of payload size (schema §4 amendment A8).
expect(String(appliedMap(doc).get(firstOp().op_id))).toHaveLength(64);
});
Expand Down
46 changes: 44 additions & 2 deletions test/pbt-convergence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ interface Scenario {
retryMask: boolean[];
}

interface Coverage {
runs: number;
deletes: number;
clears: number;
retries: number;
permutations: number;
}

const scenarioArb: fc.Arbitrary<Scenario> = fc.record({
actors: fc.integer({ min: 1, max: 4 }),
nodes: fc.integer({ min: 2, max: 8 }),
Expand Down Expand Up @@ -127,15 +135,39 @@ function withRetries(ops: Op[], mask: boolean[]): Op[] {
return [...adjacent, ...delayed];
}

function applyInBatches(doc: Y.Doc, ops: Op[], sizes: number[]): void {
function assertNonVacuous(coverage: Coverage): void {
expect(coverage.runs).toBe(FC_OPTIONS.numRuns);
expect(coverage.deletes, "no generated operation stream included delete_node").toBeGreaterThan(0);
expect(coverage.clears, "no generated operation stream included clear").toBeGreaterThan(0);
expect(coverage.retries, "no stream passed to applyOps contained a duplicate retry").toBeGreaterThan(0);
expect(coverage.permutations, "no applied permutation differed from forward order").toBeGreaterThan(0);
}

function applyInBatches(doc: Y.Doc, ops: Op[], sizes: number[]): ReturnType<typeof applyOps>["outcomes"] {
let cursor = 0;
let batch = 0;
const outcomes: ReturnType<typeof applyOps>["outcomes"] = [];
while (cursor < ops.length) {
const size = sizes[batch++ % sizes.length]!;
const result = applyOps(doc, ops.slice(cursor, cursor + size), catalog);
expect(result.outcomes.find((outcome) => outcome.outcome === "rejected")).toBeUndefined();
outcomes.push(...result.outcomes);
cursor += size;
}
return outcomes;
}

function assertRetriesAreNoOps(ops: Op[], outcomes: ReturnType<typeof applyOps>["outcomes"]): void {
expect(outcomes).toHaveLength(ops.length);
const seen = new Set<string>();
for (const [index, op] of ops.entries()) {
const outcome = outcomes[index]!;
expect(outcome.op_id).toBe(op.op_id);
if (seen.has(op.op_id)) {
expect(outcome.outcome, `retry occurrence ${index} for ${op.op} ${op.op_id} was not a no-op`).toBe("no-op");
}
seen.add(op.op_id);
}
}

function fork(snapshot: Uint8Array): Y.Doc {
Expand All @@ -146,22 +178,32 @@ function fork(snapshot: Uint8Array): Y.Doc {

describe("property-based convergence and idempotency", () => {
it("converges across actor counts, causal permutations, batches, and duplicate retries", () => {
const coverage: Coverage = { runs: 0, deletes: 0, clears: 0, retries: 0, permutations: 0 };
fc.assert(
fc.property(scenarioArb, (scenario) => {
const causalPhases = phases(scenario);
const forward = causalPhases.flat();
const permuted = ordered(causalPhases, scenario.orderKeys);
const retried = withRetries(permuted, scenario.retryMask);
const snapshot = Y.encodeStateAsUpdate(mint({ nodes: [], links: [] }, catalog));
const a = fork(snapshot);
const b = fork(snapshot);

applyInBatches(a, forward, [forward.length || 1]);
applyInBatches(b, withRetries(permuted, scenario.retryMask), scenario.batchSizes);
const retryOutcomes = applyInBatches(b, retried, scenario.batchSizes);
assertRetriesAreNoOps(retried, retryOutcomes);

coverage.runs += 1;
if (forward.some((op) => op.op === "delete_node")) coverage.deletes += 1;
if (forward.some((op) => op.op === "clear")) coverage.clears += 1;
if (new Set(retried.map((op) => op.op_id)).size < retried.length) coverage.retries += 1;
if (permuted.some((op, index) => op.op_id !== forward[index]?.op_id)) coverage.permutations += 1;

expect(JSON.stringify(project(b, catalog))).toBe(JSON.stringify(project(a, catalog)));
}),
FC_OPTIONS,
);
assertNonVacuous(coverage);
});

// reset_doc is a DEFERRED op (rejected until un-deferred by amendment), so it
Expand Down
72 changes: 48 additions & 24 deletions test/reject-no-mutation.regression.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import * as Y from "yjs";
import { describe, expect, it } from "vitest";
import {
applyOps,
mint,
project,
type ConnectOp,
type Op,
type WidgetCatalog,
type WorkflowJSON,
} from "../src/index.js";
import { applyOps, mint, project } from "../src/index.js";
import type { ConnectOp, Op, WidgetCatalog, WorkflowJSON } from "../src/index.js";
import { appliedMap } from "../src/doc.js";
import { loadCatalog } from "./helpers.js";
import { checkGraphInvariants } from "./graph-invariant-oracle.js";
Expand Down Expand Up @@ -557,21 +550,52 @@ describe("regression: rejected connect ops leave document bytes unchanged (#10)"
* the schema list to be updated, which is what the header sentence claims.
*/
it("§2.5 item 4 (source axis) still diverges — pinning the carve-out", () => {
const projections = [[
{ op: "connect", op_id: opId("cv4a"), actor: "human:x", base_version: 5, stamp: [5, "human:x"],
link_id: 9707, from_node: 300, from_slot: 5, to_node: 700, to_slot: 0, link_type: "IMAGE" },
{ op: "delete_node", op_id: opId("cv4d"), actor: "human:d", base_version: 4, stamp: [4, "human:d"], node_id: 300 },
], [
{ op: "delete_node", op_id: opId("cv4d"), actor: "human:d", base_version: 4, stamp: [4, "human:d"], node_id: 300 },
{ op: "connect", op_id: opId("cv4a"), actor: "human:x", base_version: 5, stamp: [5, "human:x"],
link_id: 9707, from_node: 300, from_slot: 5, to_node: 700, to_slot: 0, link_type: "IMAGE" },
]].map((order) => {
const doc = mint(convergenceWorkflow, catalog);
for (const op of order) applyOps(doc, [op as unknown as Op], catalog);
return JSON.stringify(project(doc, catalog));
const bad = {
op: "connect", op_id: opId("cv4a"), actor: "human:x", base_version: 5, stamp: [5, "human:x"],
link_id: 9707, from_node: 300, from_slot: 5, to_node: 700, to_slot: 0, link_type: "IMAGE",
};
const tail = {
op: "add_node", op_id: opId("cv4t"), actor: "human:x", base_version: 5, stamp: [5, "human:x"],
node_id: 904, class_type: "LoadImage", pos: [0, 0],
node: { id: 904, type: "LoadImage", inputs: [], outputs: [], widgets_values: [], pos: [0, 0] },
};
const del = {
op: "delete_node", op_id: opId("cv4d"), actor: "human:d", base_version: 4,
stamp: [4, "human:d"], node_id: 300,
};
const seed = mint(convergenceWorkflow, catalog);
const snapshot = Y.encodeStateAsUpdate(seed);
const runs = [[[bad, tail], [del]], [[del], [bad, tail]]].map((batches, runIndex) => {
const doc = new Y.Doc();
Y.applyUpdate(doc, snapshot);
const outcomes = batches.flatMap((batch, batchIndex) => {
const beforeRejectedBatch = runIndex === 0 && batchIndex === 0
? Buffer.from(Y.encodeStateAsUpdate(doc))
: null;
const batchOutcomes = applyOps(doc, batch as unknown as Op[], catalog).outcomes;
if (beforeRejectedBatch !== null) {
expect(Buffer.from(Y.encodeStateAsUpdate(doc)).equals(beforeRejectedBatch)).toBe(true);
expect(appliedMap(doc).has(bad.op_id)).toBe(false);
expect(appliedMap(doc).has(tail.op_id)).toBe(false);
}
return batchOutcomes;
});
return { outcomes, projection: project(doc, catalog) };
});
// NOT equal — this is the documented residual, not a passing property.
expect(projections[0]).not.toEqual(projections[1]);
const sourcePresent = runs[0]!;
const sourceDeleted = runs[1]!;

// Behavioral witnesses: while the source exists the state-dependent slot
// check rejects and aborts the tail; after source deletion the same connect
// is an accepted delete-wins no-op and the tail applies.
expect(sourcePresent.outcomes.map(({ outcome }) => outcome)).toEqual(["rejected", "rejected", "applied"]);
expect(sourcePresent.outcomes[0]).toMatchObject({ outcome: "rejected", reason: { code: "output_slot_missing" } });
expect(sourcePresent.outcomes[1]).toMatchObject({ outcome: "rejected", reason: { code: "batch_aborted" } });
expect(sourceDeleted.outcomes.map(({ outcome }) => outcome)).toEqual(["applied", "no-op", "applied"]);
expect(sourcePresent.projection.nodes.some((node) => node.id === 904)).toBe(false);
expect(sourceDeleted.projection.nodes.some((node) => node.id === 904)).toBe(true);
// NOT equal — abort-remainder exposes the documented residual.
expect(sourcePresent.projection).not.toEqual(sourceDeleted.projection);
});

it("§2.5 item 5 (destination axis) still diverges — pinning the carve-out", () => {
Expand Down Expand Up @@ -835,7 +859,7 @@ describe("regression: rejected connect ops leave document bytes unchanged (#10)"
op: "set_widget", op_id: opId("uncloneable-sw"), actor: "human:z", base_version: 9,
stamp: [9, "human:z"], node_id: 700, widget: "inputcount",
value: (() => undefined) as unknown,
} as unknown as ConnectOp;
} as unknown as Op;
expect(rejected(applyOps(doc, [op], catalog))).toMatchObject({ code: "malformed_op" });
expect(Buffer.from(Y.encodeStateAsUpdate(doc)).equals(before)).toBe(true);
});
Expand Down
10 changes: 8 additions & 2 deletions test/types/invalid-states.negative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* the document; `test/invalid-op-states.test.ts` is the matching runtime
* audit, including the states the wire still accepts.
*/
import type * as Y from "yjs";
import type {
AddNodeOp,
ClearOp,
Expand All @@ -33,6 +34,7 @@ import type {
SetWidgetOp,
TopLevelSetWidgetOp,
WireOp,
applyOps,
} from "../../src/index.js";

const env = {
Expand Down Expand Up @@ -194,16 +196,20 @@ const reset: ResetDocOp = {
// @ts-expect-error #17: `reset_doc` is deferred; it is a `WireOp`, not an `Op`.
const resetAsOp: Op = reset;

declare function applyOpsSignature(ops: Op[]): void;
declare const applyOpsSignature: typeof applyOps;
declare const doc: Y.Doc;
// @ts-expect-error #17: the applier cannot be handed an op it always refuses.
applyOpsSignature([reset]);
applyOpsSignature(doc, [reset]);

const insertWorkflow: InsertWorkflowOp = {
op: "insert_workflow",
...env,
workflow: { nodes: [], links: [], definitions: { subgraphs: [] } },
};
const insertWorkflowAsOp: Op = insertWorkflow;
// Positive control: the real public arity plus a valid op must compile, so the
// reset assertion cannot pass merely because every call has the wrong arity.
applyOpsSignature(doc, [insertWorkflow]);

// The public vocabulary projection accepts every declared kind and no value
// outside the wire union. This is an external-consumer check through index.ts.
Expand Down
Loading