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

CheckpointStore.test.ts

206 lines6.9 KB

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