79 lines
3.4 KiB
TypeScript
79 lines
3.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
operationFingerprint,
|
|
operationIdentity,
|
|
runIdempotentOperation,
|
|
type IdempotencyStore,
|
|
} from "../../packages/siax-access/src/idempotency";
|
|
|
|
describe("@siax/access universal idempotency", () => {
|
|
it("binds every result-affecting input into the fingerprint", () => {
|
|
const a = operationFingerprint(["t", "p", "u", "instruction-extrahera"]);
|
|
const b = operationFingerprint(["t", "p", "u", "instruction-extrahera-belopp"]);
|
|
expect(a).not.toBe(b);
|
|
});
|
|
it("rejects malformed identity (same shape 0pt/DOC0 both enforce)", () => {
|
|
expect(() => operationIdentity({ tenantId: "t", projectId: "p", actorId: "a", idempotencyKey: "../bad", payload: [] })).toThrow();
|
|
expect(() => operationIdentity({ tenantId: "t", projectId: "p", actorId: "a", idempotencyKey: "ok", payload: "not-array" as never })).toThrow();
|
|
});
|
|
it("conflicts when a reused key carries a changed payload", () => {
|
|
const fp1 = operationFingerprint(["t", "p", "u", "A"]);
|
|
const fp2 = operationFingerprint(["t", "p", "u", "B"]);
|
|
expect(fp1).not.toBe(fp2);
|
|
});
|
|
it("replays a stored receipt verbatim on the exact fingerprint", async () => {
|
|
const stored = { x: 1 };
|
|
const store: IdempotencyStore<unknown, { x: number }> = {
|
|
start: async () => false,
|
|
save: async () => {},
|
|
receipt: async () => stored,
|
|
};
|
|
const outcome = await runIdempotentOperation({
|
|
store, identity: {}, fingerprint: "fp",
|
|
dispatch: async () => { throw new Error("must not rerun"); },
|
|
});
|
|
expect(outcome.kind).toBe("replayed");
|
|
if (outcome.kind === "replayed") expect(outcome.receipt).toEqual(stored);
|
|
});
|
|
it("claims once and saves only after the dispatch", async () => {
|
|
const order: string[] = [];
|
|
const store: IdempotencyStore<unknown, { x: number }> = {
|
|
start: async () => { order.push("start"); return true; },
|
|
save: async () => { order.push("save"); },
|
|
receipt: async () => { order.push("receipt"); return undefined; },
|
|
};
|
|
let dispatched = 0;
|
|
const outcome = await runIdempotentOperation({
|
|
store, identity: {}, fingerprint: "fp",
|
|
dispatch: async () => { order.push("dispatch"); return { x: ++dispatched }; },
|
|
});
|
|
expect(outcome.kind).toBe("claimed");
|
|
expect(dispatched).toBe(1);
|
|
expect(order).toEqual(["start", "dispatch", "save"]);
|
|
});
|
|
it("reports a store error as conflict, never as a fresh start", async () => {
|
|
const store: IdempotencyStore<unknown, { x: number }> = {
|
|
start: async () => { throw new Error("db down"); },
|
|
save: async () => {},
|
|
receipt: async () => { throw new Error("db down"); },
|
|
};
|
|
let dispatched = 0;
|
|
const outcome = await runIdempotentOperation({
|
|
store, identity: {}, fingerprint: "fp",
|
|
dispatch: async () => { dispatched++; return { x: 1 }; },
|
|
});
|
|
expect(outcome.kind).toBe("conflict");
|
|
expect(dispatched).toBe(0);
|
|
});
|
|
it("does not rerun the dispatch when start succeeds but save later fails", async () => {
|
|
const store: IdempotencyStore<unknown, { x: number }> = { start: async () => true, save: async () => { throw new Error("journal write failed"); }, receipt: async () => undefined };
|
|
let dispatched = 0;
|
|
const outcome = await runIdempotentOperation({
|
|
store, identity: {}, fingerprint: "fp",
|
|
dispatch: async () => { dispatched++; return { x: 1 }; },
|
|
});
|
|
expect(outcome.kind).toBe("claimed");
|
|
expect(dispatched).toBe(1);
|
|
});
|
|
});
|