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

CheckpointStore.test.ts

201 lines6.8 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 * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts";
12import * as VcsProcess from "../../vcs/VcsProcess.ts";
13import type { VcsError } 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 VcsProcessTestLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
21const VcsDriverTestLayer = VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcessTestLayer));
22const CheckpointStoreTestLayer = CheckpointStoreLive.pipe(
23 Layer.provideMerge(VcsDriverTestLayer),
24 Layer.provideMerge(NodeServices.layer),
25);
26const TestLayer = CheckpointStoreTestLayer.pipe(
27 Layer.provideMerge(VcsProcessTestLayer),
28 Layer.provideMerge(VcsDriverTestLayer),
29 Layer.provideMerge(ServerConfigLayer),
30 Layer.provideMerge(NodeServices.layer),
31);
32​
33function makeTmpDir(
34 prefix = "checkpoint-store-test-",
35): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
36 return Effect.gen(function* () {
37 const fileSystem = yield* FileSystem.FileSystem;
38 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
39 });
41​
42function writeTextFile(
43 filePath: string,
44 contents: string,
45): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
46 return Effect.gen(function* () {
47 const fileSystem = yield* FileSystem.FileSystem;
48 yield* fileSystem.writeFileString(filePath, contents);
49 });
51​
52function git(
53 cwd: string,
54 args: ReadonlyArray<string>,
55): Effect.Effect<string, VcsError, VcsProcess.VcsProcess> {
56 return Effect.gen(function* () {
57 const process = yield* VcsProcess.VcsProcess;
58 const result = yield* process.run({
59 operation: "CheckpointStore.test.git",
60 command: "git",
61 cwd,
62 args,
63 timeoutMs: 10_000,
64 });
65 return result.stdout.trim();
66 });
68​
69function initRepoWithCommit(
70 cwd: string,
71): Effect.Effect<
72 void,
73 VcsError | PlatformError.PlatformError,
74 VcsProcess.VcsProcess | FileSystem.FileSystem
75> {
76 return Effect.gen(function* () {
77 yield* git(cwd, ["init"]);
78 yield* git(cwd, ["config", "user.email", "test@test.com"]);
79 yield* git(cwd, ["config", "user.name", "Test"]);
80 yield* writeTextFile(path.join(cwd, "README.md"), "# test\n");
81 yield* git(cwd, ["add", "."]);
82 yield* git(cwd, ["commit", "-m", "initial commit"]);
83 });
85​
86function buildLargeText(lineCount = 5_000): string {
87 return Array.from({ length: lineCount }, (_, index) => `line ${String(index).padStart(5, "0")}`)
88 .join("\n")
89 .concat("\n");
91​
92it.layer(TestLayer)("CheckpointStoreLive", (it) => {
93 describe("diffCheckpoints", () => {
94 it.effect("returns full oversized checkpoint diffs without truncation", () =>
95 Effect.gen(function* () {
96 const tmp = yield* makeTmpDir();
97 yield* initRepoWithCommit(tmp);
98 const checkpointStore = yield* CheckpointStore;
99 const threadId = ThreadId.make("thread-checkpoint-store");
100 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
101 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
102​
103 yield* checkpointStore.captureCheckpoint({
104 cwd: tmp,
105 checkpointRef: fromCheckpointRef,
106 });
107 yield* writeTextFile(path.join(tmp, "README.md"), buildLargeText());
108 yield* checkpointStore.captureCheckpoint({
109 cwd: tmp,
110 checkpointRef: toCheckpointRef,
111 });
112​
113 const diff = yield* checkpointStore.diffCheckpoints({
114 cwd: tmp,
115 fromCheckpointRef,
116 toCheckpointRef,
117 ignoreWhitespace: true,
118 });
119​
120 expect(diff).toContain("diff --git");
121 expect(diff).not.toContain("[truncated]");
122 expect(diff).toContain("+line 04999");
123 }),
124 );
125​
126 it.effect("can hide indentation churn when changes wrap existing lines", () =>
127 Effect.gen(function* () {
128 const tmp = yield* makeTmpDir();
129 yield* initRepoWithCommit(tmp);
130 const checkpointStore = yield* CheckpointStore;
131 const threadId = ThreadId.make("thread-checkpoint-store-whitespace");
132 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
133 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
134​
135 const componentPath = path.join(tmp, "Component.tsx");
136 yield* writeTextFile(
137 componentPath,
138 [
139 "export function View() {",
140 " return (",
141 " <section>",
142 " <h1>Title</h1>",
143 " <p>Body</p>",
144 " </section>",
145 " );",
146 "}",
147 "",
148 ].join("\n"),
149 );
150 yield* checkpointStore.captureCheckpoint({
151 cwd: tmp,
152 checkpointRef: fromCheckpointRef,
153 });
154 yield* writeTextFile(
155 componentPath,
156 [
157 "export function View() {",
158 " return (",
159 " <section>",
160 " {isReady ? (",
161 " <div>",
162 " <h1>Title</h1>",
163 " <p>Body</p>",
164 " </div>",
165 " ) : null}",
166 " </section>",
167 " );",
168 "}",
169 "",
170 ].join("\n"),
171 );
172 yield* checkpointStore.captureCheckpoint({
173 cwd: tmp,
174 checkpointRef: toCheckpointRef,
175 });
176​
177 const normalDiff = yield* checkpointStore.diffCheckpoints({
178 cwd: tmp,
179 fromCheckpointRef,
180 toCheckpointRef,
181 ignoreWhitespace: false,
182 });
183 const whitespaceIgnoredDiff = yield* checkpointStore.diffCheckpoints({
184 cwd: tmp,
185 fromCheckpointRef,
186 toCheckpointRef,
187 ignoreWhitespace: true,
188 });
189​
190 expect(normalDiff).toContain("diff --git");
191 expect(normalDiff).toContain("- <h1>Title</h1>");
192 expect(normalDiff).toContain("+ <h1>Title</h1>");
193 expect(whitespaceIgnoredDiff).toContain("diff --git");
194 expect(whitespaceIgnoredDiff).toContain("+ {isReady ? (");
195 expect(whitespaceIgnoredDiff).toContain("+ <div>");
196 expect(whitespaceIgnoredDiff).not.toContain("- <h1>Title</h1>");
197 expect(whitespaceIgnoredDiff).not.toContain("+ <h1>Title</h1>");
198 }),
199 );
200 });
201});