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

CheckpointStore.test.ts

122 lines4.0 KB

1import path from "node:path";
2​
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { it } from "@effect/vitest";
5import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect";
6import { describe, expect } from "vitest";
7​
8import { checkpointRefForThreadTurn } from "../Utils.ts";
9import { CheckpointStoreLive } from "./CheckpointStore.ts";
10import { CheckpointStore } from "../Services/CheckpointStore.ts";
11import { GitCoreLive } from "../../git/Layers/GitCore.ts";
12import { GitCore } from "../../git/Services/GitCore.ts";
13import { GitCommandError } from "@t3tools/contracts";
14import { ServerConfig } from "../../config.ts";
15import { ThreadId } from "@t3tools/contracts";
16​
17const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
18 prefix: "t3-checkpoint-store-test-",
19});
20const GitCoreTestLayer = GitCoreLive.pipe(
21 Layer.provide(ServerConfigLayer),
22 Layer.provide(NodeServices.layer),
23);
24const CheckpointStoreTestLayer = CheckpointStoreLive.pipe(
25 Layer.provide(GitCoreTestLayer),
26 Layer.provide(NodeServices.layer),
27);
28const TestLayer = Layer.mergeAll(NodeServices.layer, GitCoreTestLayer, CheckpointStoreTestLayer);
29​
30function makeTmpDir(
31 prefix = "checkpoint-store-test-",
32): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
33 return Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;
35 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
36 });
38​
39function writeTextFile(
40 filePath: string,
41 contents: string,
42): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
43 return Effect.gen(function* () {
44 const fileSystem = yield* FileSystem.FileSystem;
45 yield* fileSystem.writeFileString(filePath, contents);
46 });
48​
49function git(
50 cwd: string,
51 args: ReadonlyArray<string>,
52): Effect.Effect<string, GitCommandError, GitCore> {
53 return Effect.gen(function* () {
54 const gitCore = yield* GitCore;
55 const result = yield* gitCore.execute({
56 operation: "CheckpointStore.test.git",
57 cwd,
58 args,
59 timeoutMs: 10_000,
60 });
61 return result.stdout.trim();
62 });
64​
65function initRepoWithCommit(
66 cwd: string,
67): Effect.Effect<
68 void,
69 GitCommandError | PlatformError.PlatformError,
70 GitCore | FileSystem.FileSystem
71> {
72 return Effect.gen(function* () {
73 const core = yield* GitCore;
74 yield* core.initRepo({ cwd });
75 yield* git(cwd, ["config", "user.email", "test@test.com"]);
76 yield* git(cwd, ["config", "user.name", "Test"]);
77 yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
78 yield* git(cwd, ["add", "."]);
79 yield* git(cwd, ["commit", "-m", "initial commit"]);
80 });
82​
83function buildLargeText(lineCount = 5_000): string {
84 return Array.from({ length: lineCount }, (_, index) => `line ${String(index).padStart(5, "0")}`)
85 .join("\n")
86 .concat("\n");
88​
89it.layer(TestLayer)("CheckpointStoreLive", (it) => {
90 describe("diffCheckpoints", () => {
91 it.effect("returns full oversized checkpoint diffs without truncation", () =>
92 Effect.gen(function* () {
93 const tmp = yield* makeTmpDir();
94 yield* initRepoWithCommit(tmp);
95 const checkpointStore = yield* CheckpointStore;
96 const threadId = ThreadId.makeUnsafe("thread-checkpoint-store");
97 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
98 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
99​
100 yield* checkpointStore.captureCheckpoint({
101 cwd: tmp,
102 checkpointRef: fromCheckpointRef,
103 });
104 yield* writeTextFile(path.join(tmp, "README.md"), buildLargeText());
105 yield* checkpointStore.captureCheckpoint({
106 cwd: tmp,
107 checkpointRef: toCheckpointRef,
108 });
109​
110 const diff = yield* checkpointStore.diffCheckpoints({
111 cwd: tmp,
112 fromCheckpointRef,
113 toCheckpointRef,
114 });
115​
116 expect(diff).toContain("diff --git");
117 expect(diff).not.toContain("[truncated]");
118 expect(diff).toContain("+line 04999");
119 }),
120 );
121 });
122});