Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6
7import { acpReadTextFile, acpWriteTextFile } from "./AcpClientFs.ts";
8
9const withTempDir = <A, E>(
10 use: (input: {
11 readonly fileSystem: FileSystem.FileSystem;
12 readonly path: Path.Path;
13 readonly directory: string;
14 }) => Effect.Effect<A, E>,
15) =>
16 Effect.gen(function* () {
17 const fileSystem = yield* FileSystem.FileSystem;
18 const path = yield* Path.Path;
19 const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "acp-client-fs-" });
20 return yield* use({ fileSystem, path, directory });
21 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped);
22
23describe("AcpClientFs", () => {
24 it.effect("reads whole files and 1-based line windows", () =>
25 withTempDir(({ fileSystem, path, directory }) =>
26 Effect.gen(function* () {
27 const filePath = path.join(directory, "notes.txt");
28 yield* fileSystem.writeFileString(filePath, "one\ntwo\nthree\nfour");
29
30 const whole = yield* acpReadTextFile(fileSystem, {
31 sessionId: "session",
32 path: filePath,
33 });
34 expect(whole.content).toBe("one\ntwo\nthree\nfour");
35
36 const windowed = yield* acpReadTextFile(fileSystem, {
37 sessionId: "session",
38 path: filePath,
39 line: 2,
40 limit: 2,
41 });
42 expect(windowed.content).toBe("two\nthree");
43 }),
44 ),
45 );
46
47 it.effect("rejects relative paths and missing files with typed request errors", () =>
48 withTempDir(({ fileSystem, path, directory }) =>
49 Effect.gen(function* () {
50 const relative = yield* Effect.flip(
51 acpReadTextFile(fileSystem, { sessionId: "session", path: "notes.txt" }),
52 );
53 expect(relative.message).toContain("absolute path");
54
55 const missing = yield* Effect.flip(
56 acpReadTextFile(fileSystem, {
57 sessionId: "session",
58 path: path.join(directory, "missing.txt"),
59 }),
60 );
61 expect(missing.message).toContain("Could not read text file");
62 }),
63 ),
64 );
65
66 it.effect("writes files, creating parent directories", () =>
67 withTempDir(({ fileSystem, path, directory }) =>
68 Effect.gen(function* () {
69 const filePath = path.join(directory, "nested", "deep", "config.json");
70 yield* acpWriteTextFile(fileSystem, {
71 sessionId: "session",
72 path: filePath,
73 content: '{"ok":true}',
74 });
75 expect(yield* fileSystem.readFileString(filePath)).toBe('{"ok":true}');
76 }),
77 ),
78 );
79});