Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CheckpointService.test.ts

183 lines6.4 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { assert, it, vi } from "@effect/vitest";
3import {
4 CheckpointScopeId,
5 NodeId,
6 ProviderThreadId,
7 RunId,
8 ThreadId,
9 type OrchestrationV2CheckpointScope,
10 VcsProcessTimeoutError,
11 VcsUnsupportedOperationError,
12} from "@t3tools/contracts";
13import * as DateTime from "effect/DateTime";
14import * as Effect from "effect/Effect";
15import * as Exit from "effect/Exit";
16import * as Layer from "effect/Layer";
17​
18import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
19import * as CheckpointService from "./CheckpointService.ts";
20import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
21​
22it.effect.each([false, true, "interrupt"] as const)(
23 "materializes baseline, lookup fails=%s",
24 (lookupFails) => {
25 const scope: OrchestrationV2CheckpointScope = {
26 id: CheckpointScopeId.make("checkpoint-scope:materialize-baseline"),
27 threadId: ThreadId.make("thread:materialize-baseline"),
28 runId: RunId.make("run:materialize-baseline:3"),
29 nodeId: NodeId.make("node:materialize-baseline:3"),
30 parentScopeId: null,
31 providerThreadId: ProviderThreadId.make("provider-thread:materialize-baseline"),
32 kind: "root_run",
33 ordinalWithinParent: 0,
34 advancesAppRunCount: true,
35 cwd: "/repo",
36 createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
37 };
38 const hasCheckpointRef = vi.fn((_input: CheckpointStore.RestoreCheckpointInput) =>
39 lookupFails === "interrupt"
40 ? Effect.interrupt
41 : lookupFails
42 ? Effect.fail(
43 new VcsProcessTimeoutError({
44 operation: "test.ref",
45 command: "git",
46 cwd: "/repo",
47 timeoutMs: 30000,
48 }),
49 )
50 : Effect.succeed(true),
51 );
52 const layerTest = CheckpointService.layer.pipe(
53 Layer.provide(
54 Layer.mergeAll(
55 IdAllocator.layer,
56 Layer.mock(CheckpointStore.CheckpointStore)({
57 isGitRepository: () => Effect.succeed(true),
58 hasCheckpointRef,
59 captureCheckpoint: () => Effect.void,
60 }),
61 ),
62 ),
63 Layer.provideMerge(NodeCrypto.layer),
64 );
65​
66 return Effect.gen(function* () {
67 const checkpoints = yield* CheckpointService.CheckpointServiceV2;
68 if (lookupFails === "interrupt") {
69 const exit = yield* Effect.exit(
70 checkpoints.materializeBaselineCheckpoint({ scope, ordinalWithinScope: 2 }),
71 );
72 assert.isTrue(Exit.hasInterrupts(exit));
73 const captureExit = yield* Effect.exit(
74 checkpoints.capture({
75 scope,
76 ordinalWithinScope: 1,
77 runId: scope.runId!,
78 nodeId: scope.nodeId!,
79 appRunOrdinal: 1,
80 capturedAt: scope.createdAt,
81 }),
82 );
83 assert.isTrue(Exit.hasInterrupts(captureExit));
84 return;
85 }
86 const baseline = yield* checkpoints.materializeBaselineCheckpoint({
87 scope,
88 ordinalWithinScope: 2,
89 });
90​
91 assert.equal(baseline.ordinalWithinScope, 2);
92 assert.equal(
93 baseline.ref,
94 yield* CheckpointService.checkpointRefForScopeOrdinal({
95 scopeId: scope.id,
96 ordinalWithinScope: 2,
97 }),
98 );
99 assert.equal(baseline.status, lookupFails ? "missing" : "ready");
100 assert.deepEqual(hasCheckpointRef.mock.calls[0]?.[0], {
101 cwd: scope.cwd,
102 checkpointRef: baseline.ref,
103 });
104 }).pipe(Effect.provide(layerTest));
105 },
106);
107​
108it.effect(
109 "skips unsupported baseline and turn checkpoints without swallowing capture failures",
110 () =>
111 Effect.gen(function* () {
112 const scope: OrchestrationV2CheckpointScope = {
113 id: CheckpointScopeId.make("checkpoint-scope:capture-failure"),
114 threadId: ThreadId.make("thread:capture-failure"),
115 runId: RunId.make("run:capture-failure:1"),
116 nodeId: NodeId.make("node:capture-failure:1"),
117 parentScopeId: null,
118 providerThreadId: ProviderThreadId.make("provider-thread:capture-failure"),
119 kind: "root_run",
120 ordinalWithinParent: 0,
121 advancesAppRunCount: true,
122 cwd: "/repo/ignored",
123 createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
124 };
125 for (const captureError of [
126 new VcsProcessTimeoutError({
127 operation: "test.capture",
128 command: "git",
129 cwd: scope.cwd,
130 timeoutMs: 30000,
131 }),
132 new VcsUnsupportedOperationError({
133 operation: "test.capture",
134 kind: "git",
135 detail: "The workspace directory is ignored by its Git repository.",
136 }),
137 ]) {
138 const layerTest = CheckpointService.layer.pipe(
139 Layer.provide(
140 Layer.mergeAll(
141 IdAllocator.layer,
142 Layer.mock(CheckpointStore.CheckpointStore)({
143 isGitRepository: () => Effect.succeed(true),
144 hasCheckpointRef: () => Effect.succeed(false),
145 captureCheckpoint: () => Effect.fail(captureError),
146 }),
147 ),
148 ),
149 Layer.provideMerge(NodeCrypto.layer),
150 );
151 yield* Effect.gen(function* () {
152 const checkpoints = yield* CheckpointService.CheckpointServiceV2;
153 const baseline = yield* Effect.result(
154 checkpoints.captureBaseline({ scope, ordinalWithinScope: 0 }),
155 );
156 const checkpoint = yield* checkpoints.capture({
157 scope,
158 ordinalWithinScope: 1,
159 runId: scope.runId!,
160 nodeId: scope.nodeId!,
161 appRunOrdinal: 1,
162 capturedAt: scope.createdAt,
163 });
164​
165 assert.deepEqual(
166 { baseline: baseline._tag, turn: checkpoint.status, files: checkpoint.files },
167 captureError._tag === "VcsUnsupportedOperationError"
168 ? { baseline: "Success", turn: "missing", files: [] }
169 : { baseline: "Failure", turn: "error", files: [] },
170 );
171 if (baseline._tag === "Failure") {
172 assert.strictEqual(baseline.failure.cause, captureError);
173 } else {
174 const materialized = yield* checkpoints.materializeBaselineCheckpoint({
175 scope,
176 ordinalWithinScope: 0,
177 });
178 assert.strictEqual(materialized.status, "missing");
179 }
180 }).pipe(Effect.provide(layerTest));
181 }
182 }),
183);