86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
|
|
import { describe, it, expect } from "@jest/globals";
|
|||
|
|
import {
|
|||
|
|
ALLOWED_TRANSITIONS,
|
|||
|
|
ROLE_FOR_TRANSITION,
|
|||
|
|
SOD_REQUIRED_TRANSITIONS,
|
|||
|
|
TRANSACTION_STATES,
|
|||
|
|
canTransition,
|
|||
|
|
} from "../../src/types/transactionState";
|
|||
|
|
|
|||
|
|
describe("Transaction state machine (architecture note §8–§9)", () => {
|
|||
|
|
it("declares the 12 states from §8.1", () => {
|
|||
|
|
expect(TRANSACTION_STATES).toEqual([
|
|||
|
|
"DRAFT",
|
|||
|
|
"INITIATED",
|
|||
|
|
"PRECONDITIONS_PENDING",
|
|||
|
|
"READY_FOR_PREPARE",
|
|||
|
|
"PREPARED",
|
|||
|
|
"EXECUTING",
|
|||
|
|
"PARTIALLY_EXECUTED",
|
|||
|
|
"VALIDATING",
|
|||
|
|
"COMMITTED",
|
|||
|
|
"ABORTED",
|
|||
|
|
"UNWIND_PENDING",
|
|||
|
|
"CLOSED",
|
|||
|
|
]);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("§9.1 permitted high-level transitions", () => {
|
|||
|
|
// Each of these is listed in the note; canTransition must accept them.
|
|||
|
|
const legal: Array<[string, string]> = [
|
|||
|
|
["DRAFT", "INITIATED"],
|
|||
|
|
["INITIATED", "PRECONDITIONS_PENDING"],
|
|||
|
|
["PRECONDITIONS_PENDING", "READY_FOR_PREPARE"],
|
|||
|
|
["READY_FOR_PREPARE", "PREPARED"],
|
|||
|
|
["PREPARED", "EXECUTING"],
|
|||
|
|
["EXECUTING", "PARTIALLY_EXECUTED"],
|
|||
|
|
["EXECUTING", "VALIDATING"],
|
|||
|
|
["PARTIALLY_EXECUTED", "VALIDATING"],
|
|||
|
|
["VALIDATING", "COMMITTED"],
|
|||
|
|
["VALIDATING", "ABORTED"],
|
|||
|
|
["ABORTED", "UNWIND_PENDING"],
|
|||
|
|
["COMMITTED", "CLOSED"],
|
|||
|
|
["UNWIND_PENDING", "CLOSED"],
|
|||
|
|
];
|
|||
|
|
it.each(legal)("allows %s -> %s", (from, to) => {
|
|||
|
|
expect(canTransition(from as any, to as any)).toBe(true);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// A few illegal edges — explicitly not in §9.1.
|
|||
|
|
const illegal: Array<[string, string]> = [
|
|||
|
|
["DRAFT", "COMMITTED"],
|
|||
|
|
["INITIATED", "EXECUTING"],
|
|||
|
|
["CLOSED", "INITIATED"],
|
|||
|
|
["PREPARED", "COMMITTED"],
|
|||
|
|
["COMMITTED", "ABORTED"],
|
|||
|
|
["ABORTED", "COMMITTED"],
|
|||
|
|
];
|
|||
|
|
it.each(illegal)("rejects %s -> %s", (from, to) => {
|
|||
|
|
expect(canTransition(from as any, to as any)).toBe(false);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("CLOSED is a terminal state", () => {
|
|||
|
|
expect(ALLOWED_TRANSITIONS.CLOSED).toEqual([]);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("segregation-of-duties checkpoints (§13)", () => {
|
|||
|
|
it("flags the four SoD-gated transitions", () => {
|
|||
|
|
expect([...SOD_REQUIRED_TRANSITIONS].sort()).toEqual(
|
|||
|
|
[
|
|||
|
|
"ABORTED->UNWIND_PENDING",
|
|||
|
|
"PREPARED->EXECUTING",
|
|||
|
|
"READY_FOR_PREPARE->PREPARED",
|
|||
|
|
"VALIDATING->COMMITTED",
|
|||
|
|
].sort(),
|
|||
|
|
);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("assigns a role to every SoD-gated transition", () => {
|
|||
|
|
for (const key of SOD_REQUIRED_TRANSITIONS) {
|
|||
|
|
expect(ROLE_FOR_TRANSITION[key]).toBeDefined();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
});
|