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

CheckpointService.test.ts

103 lines3.3 KB

1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointScopeId,
4 NodeId,
5 ProviderThreadId,
6 RunId,
7 ThreadId,
8 type OrchestrationV2CheckpointScope,
9 VcsProcessTimeoutError,
10} from "@t3tools/contracts";
11import * as DateTime from "effect/DateTime";
12import * as Effect from "effect/Effect";
13import * as Exit from "effect/Exit";
14import * as Layer from "effect/Layer";
15​
16import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
17import * as CheckpointService from "./CheckpointService.ts";
18import * as IdAllocator from "./IdAllocator.ts";
19​
20it.effect.each([false, true, "interrupt"] as const)(
21 "materializes baseline, lookup fails=%s",
22 (lookupFails) => {
23 const scope: OrchestrationV2CheckpointScope = {
24 id: CheckpointScopeId.make("checkpoint-scope:materialize-baseline"),
25 threadId: ThreadId.make("thread:materialize-baseline"),
26 runId: RunId.make("run:materialize-baseline:3"),
27 nodeId: NodeId.make("node:materialize-baseline:3"),
28 parentScopeId: null,
29 providerThreadId: ProviderThreadId.make("provider-thread:materialize-baseline"),
30 kind: "root_run",
31 ordinalWithinParent: 0,
32 advancesAppRunCount: true,
33 cwd: "/repo",
34 createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"),
35 };
36 const hasCheckpointRef = vi.fn((_input: CheckpointStore.RestoreCheckpointInput) =>
37 lookupFails === "interrupt"
38 ? Effect.interrupt
39 : lookupFails
40 ? Effect.fail(
41 new VcsProcessTimeoutError({
42 operation: "test.ref",
43 command: "git",
44 cwd: "/repo",
45 timeoutMs: 30000,
46 }),
47 )
48 : Effect.succeed(true),
49 );
50 const testLayer = CheckpointService.layer.pipe(
51 Layer.provide(
52 Layer.mergeAll(
53 IdAllocator.layer,
54 Layer.mock(CheckpointStore.CheckpointStore)({
55 isGitRepository: () => Effect.succeed(true),
56 hasCheckpointRef,
57 captureCheckpoint: () => Effect.void,
58 }),
59 ),
60 ),
61 );
62​
63 return Effect.gen(function* () {
64 const checkpoints = yield* CheckpointService.CheckpointServiceV2;
65 if (lookupFails === "interrupt") {
66 const exit = yield* Effect.exit(
67 checkpoints.materializeBaselineCheckpoint({ scope, ordinalWithinScope: 2 }),
68 );
69 assert.isTrue(Exit.hasInterrupts(exit));
70 const captureExit = yield* Effect.exit(
71 checkpoints.capture({
72 scope,
73 ordinalWithinScope: 1,
74 runId: scope.runId!,
75 nodeId: scope.nodeId!,
76 appRunOrdinal: 1,
77 capturedAt: scope.createdAt,
78 }),
79 );
80 assert.isTrue(Exit.hasInterrupts(captureExit));
81 return;
82 }
83 const baseline = yield* checkpoints.materializeBaselineCheckpoint({
84 scope,
85 ordinalWithinScope: 2,
86 });
87​
88 assert.equal(baseline.ordinalWithinScope, 2);
89 assert.equal(
90 baseline.ref,
91 CheckpointService.checkpointRefForScopeOrdinal({
92 scopeId: scope.id,
93 ordinalWithinScope: 2,
94 }),
95 );
96 assert.equal(baseline.status, lookupFails ? "missing" : "ready");
97 assert.deepEqual(hasCheckpointRef.mock.calls[0]?.[0], {
98 cwd: scope.cwd,
99 checkpointRef: baseline.ref,
100 });
101 }).pipe(Effect.provide(testLayer));
102 },
103);