From b989d88c1fcc122aa087f0c82a1bb7e100dbee5c Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 17 Sep 2026 03:38:22 -0700 Subject: [PATCH] test: recover batch result helper contract --- README.md | 6 ++-- test/applier.test.ts | 12 +------- test/apply-result-helpers.test.ts | 50 +++++++++++++++++++++++++++++++ test/apply-result-helpers.ts | 5 +++- 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 test/apply-result-helpers.test.ts diff --git a/README.md b/README.md index 20d0faf7..2d88a56b 100644 --- a/README.md +++ b/README.md @@ -474,7 +474,7 @@ arrives on the update stream, not when the ack lists it as applied. **Batches abort the remainder.** If op *k* fails, ops 0..*k*-1 stay applied and ops *k*..*n* are not applied at all. Fix the failing op and resend the whole -batch with the **same** `op_id`s: the prefix comes back in `skipped`, the +batch with the **same** `op_id`s: the prefix comes back with `outcome: "no-op"`, the remainder applies. Rejected ops consume no `op_id`, so a batch is retryable. **The four `connect` rejections that used to break this are fixed (#34).** @@ -491,8 +491,8 @@ Rejection codes: `malformed_op`, `unknown_op`, `op_deferred`, `catalog_required`, `invalid_node_payload`, `unknown_widget`, `uncatalogued_widget_write`, `opaque_widgets`, `widget_out_of_range`, `input_slot_missing`, `output_slot_missing`, `not_a_subgraph`, -`interior_node_not_found`, `shared_definition_unforked`, and `apply_failed` for -anything unexpected. Match on `code`, never on `message`. +`interior_node_not_found`, `shared_definition_unforked`, `batch_aborted`, and +`apply_failed` for anything unexpected. Match on `code`, never on `message`. `uncatalogued_widget_write` means a NAME-KEYED widget write named a class the pinned catalog does not describe. `add_node` and `set_widget` (and `connect`'s diff --git a/test/applier.test.ts b/test/applier.test.ts index 86362306..ea711e58 100644 --- a/test/applier.test.ts +++ b/test/applier.test.ts @@ -32,6 +32,7 @@ import { type WorkflowJSON, type WorkflowNode, } from "../src/index.js"; +import { noOpIds, rejectedOutcome } from "./apply-result-helpers.js"; import { canonicalize, loadCatalog, loadLwwVectors, loadSession, sessionFiles } from "./helpers.js"; const catalog = loadCatalog(); @@ -47,21 +48,10 @@ function envelope(actor: string, baseVersion: number) { return { op_id: testOpId(), actor, base_version: baseVersion, stamp: [baseVersion, actor] as [number, string] }; } -function rejectedOutcome(result: ReturnType) { - return result.outcomes.find( - (outcome): outcome is Extract<(typeof result.outcomes)[number], { outcome: "rejected" }> => - outcome.outcome === "rejected" && outcome.reason.code !== "batch_aborted", - ); -} - function processedOpIds(result: ReturnType): string[] { return result.outcomes.filter((outcome) => outcome.outcome !== "rejected").map((outcome) => outcome.op_id); } -function noOpIds(result: ReturnType): string[] { - return result.outcomes.filter((outcome) => outcome.outcome === "no-op").map((outcome) => outcome.op_id); -} - describe("idempotency (byte-identical re-apply)", () => { for (const file of sessionFiles()) { it(`${file}: full stream twice + every op re-applied → byte-identical state`, () => { diff --git a/test/apply-result-helpers.test.ts b/test/apply-result-helpers.test.ts new file mode 100644 index 00000000..f83c56e2 --- /dev/null +++ b/test/apply-result-helpers.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from "vitest"; +import type { ApplyResult } from "../src/types.js"; +import { noOpIds, rejectedOutcome } from "./apply-result-helpers.js"; + +describe("ApplyResult helpers", () => { + it("returns the substantive rejection when an aborted outcome precedes it", () => { + const result: ApplyResult = { + outcomes: [ + { + op_id: "aborted", + outcome: "rejected", + reason: { code: "batch_aborted", message: "not processed" }, + }, + { + op_id: "failed", + outcome: "rejected", + reason: { code: "unknown_widget", message: "missing widget" }, + }, + ], + ops_seen: 0, + }; + + expect(rejectedOutcome(result)).toEqual(result.outcomes[1]); + }); + + it("returns no substantive rejection for an aborted-only result", () => { + const result: ApplyResult = { + outcomes: [ + { + op_id: "aborted", + outcome: "rejected", + reason: { code: "batch_aborted", message: "not processed" }, + }, + ], + ops_seen: 0, + }; + + expect(rejectedOutcome(result)).toBeUndefined(); + }); + + it("reports successful no-op ids without treating them as rejections", () => { + const result: ApplyResult = { + outcomes: [{ op_id: "duplicate", outcome: "no-op" }], + ops_seen: 1, + }; + + expect(rejectedOutcome(result)).toBeUndefined(); + expect(noOpIds(result)).toEqual(["duplicate"]); + }); +}); diff --git a/test/apply-result-helpers.ts b/test/apply-result-helpers.ts index b0279991..9425b584 100644 --- a/test/apply-result-helpers.ts +++ b/test/apply-result-helpers.ts @@ -1,7 +1,10 @@ import type { ApplyResult } from "../src/types.js"; export function rejectedOutcome(result: ApplyResult) { - return result.outcomes.find((outcome) => outcome.outcome === "rejected"); + return result.outcomes.find( + (outcome): outcome is Extract => + outcome.outcome === "rejected" && outcome.reason.code !== "batch_aborted", + ); } export function rejectedOutcomeWithIndex(result: ApplyResult) {