Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import { createOrchestrationRecoveryCoordinator } from "./orchestrationRecovery";
4
5describe("createOrchestrationRecoveryCoordinator", () => {
6 it("defers live events until bootstrap completes and then requests replay", () => {
7 const coordinator = createOrchestrationRecoveryCoordinator();
8
9 expect(coordinator.beginSnapshotRecovery("bootstrap")).toBe(true);
10 expect(coordinator.classifyDomainEvent(4)).toBe("defer");
11
12 expect(coordinator.completeSnapshotRecovery(2)).toBe(true);
13 expect(coordinator.getState()).toMatchObject({
14 latestSequence: 2,
15 highestObservedSequence: 4,
16 bootstrapped: true,
17 pendingReplay: false,
18 inFlight: null,
19 });
20 });
21
22 it("classifies sequence gaps as recovery-only replay work", () => {
23 const coordinator = createOrchestrationRecoveryCoordinator();
24
25 coordinator.beginSnapshotRecovery("bootstrap");
26 coordinator.completeSnapshotRecovery(3);
27
28 expect(coordinator.classifyDomainEvent(5)).toBe("recover");
29 expect(coordinator.beginReplayRecovery("sequence-gap")).toBe(true);
30 expect(coordinator.getState().inFlight).toEqual({
31 kind: "replay",
32 reason: "sequence-gap",
33 });
34 });
35
36 it("tracks live event batches without entering recovery", () => {
37 const coordinator = createOrchestrationRecoveryCoordinator();
38
39 coordinator.beginSnapshotRecovery("bootstrap");
40 coordinator.completeSnapshotRecovery(3);
41
42 expect(coordinator.classifyDomainEvent(4)).toBe("apply");
43 expect(coordinator.markEventBatchApplied([{ sequence: 4 }])).toEqual([{ sequence: 4 }]);
44 expect(coordinator.getState()).toMatchObject({
45 latestSequence: 4,
46 highestObservedSequence: 4,
47 bootstrapped: true,
48 inFlight: null,
49 });
50 });
51
52 it("requests another replay when deferred events arrive during replay recovery", () => {
53 const coordinator = createOrchestrationRecoveryCoordinator();
54
55 coordinator.beginSnapshotRecovery("bootstrap");
56 coordinator.completeSnapshotRecovery(3);
57 coordinator.classifyDomainEvent(5);
58 coordinator.beginReplayRecovery("sequence-gap");
59 coordinator.classifyDomainEvent(7);
60 coordinator.markEventBatchApplied([{ sequence: 4 }, { sequence: 5 }, { sequence: 6 }]);
61
62 expect(coordinator.completeReplayRecovery()).toBe(true);
63 });
64
65 it("does not immediately replay again when replay returns no new events", () => {
66 const coordinator = createOrchestrationRecoveryCoordinator();
67
68 coordinator.beginSnapshotRecovery("bootstrap");
69 coordinator.completeSnapshotRecovery(3);
70 coordinator.classifyDomainEvent(5);
71 coordinator.beginReplayRecovery("sequence-gap");
72
73 expect(coordinator.completeReplayRecovery()).toBe(false);
74 expect(coordinator.getState()).toMatchObject({
75 latestSequence: 3,
76 highestObservedSequence: 5,
77 pendingReplay: false,
78 inFlight: null,
79 });
80 });
81
82 it("marks replay failure as unbootstrapped so snapshot fallback is recovery-only", () => {
83 const coordinator = createOrchestrationRecoveryCoordinator();
84
85 coordinator.beginSnapshotRecovery("bootstrap");
86 coordinator.completeSnapshotRecovery(3);
87 coordinator.beginReplayRecovery("sequence-gap");
88 coordinator.failReplayRecovery();
89
90 expect(coordinator.getState()).toMatchObject({
91 bootstrapped: false,
92 inFlight: null,
93 });
94 expect(coordinator.beginSnapshotRecovery("replay-failed")).toBe(true);
95 expect(coordinator.getState().inFlight).toEqual({
96 kind: "snapshot",
97 reason: "replay-failed",
98 });
99 });
100});