Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";
3import { Effect, FileSystem, Layer, Path } from "effect";
4
5import { ServerConfig } from "../../config.ts";
6import { GitCoreLive } from "../../git/Layers/GitCore.ts";
7import { WorkspaceEntries } from "../Services/WorkspaceEntries.ts";
8import { WorkspaceFileSystem } from "../Services/WorkspaceFileSystem.ts";
9import { WorkspaceEntriesLive } from "./WorkspaceEntries.ts";
10import { WorkspaceFileSystemLive } from "./WorkspaceFileSystem.ts";
11import { WorkspacePathsLive } from "./WorkspacePaths.ts";
12
13const ProjectLayer = WorkspaceFileSystemLive.pipe(
14 Layer.provide(WorkspacePathsLive),
15 Layer.provide(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
16);
17
18const TestLayer = Layer.empty.pipe(
19 Layer.provideMerge(ProjectLayer),
20 Layer.provideMerge(WorkspaceEntriesLive.pipe(Layer.provide(WorkspacePathsLive))),
21 Layer.provideMerge(WorkspacePathsLive),
22 Layer.provideMerge(GitCoreLive),
23 Layer.provide(
24 ServerConfig.layerTest(process.cwd(), {
25 prefix: "t3-workspace-files-test-",
26 }),
27 ),
28 Layer.provideMerge(NodeServices.layer),
29);
30
31const makeTempDir = Effect.gen(function* () {
32 const fileSystem = yield* FileSystem.FileSystem;
33 return yield* fileSystem.makeTempDirectoryScoped({
34 prefix: "t3code-workspace-files-",
35 });
36});
37
38const writeTextFile = Effect.fn("writeTextFile")(function* (
39 cwd: string,
40 relativePath: string,
41 contents = "",
42) {
43 const fileSystem = yield* FileSystem.FileSystem;
44 const path = yield* Path.Path;
45 const absolutePath = path.join(cwd, relativePath);
46 yield* fileSystem
47 .makeDirectory(path.dirname(absolutePath), { recursive: true })
48 .pipe(Effect.orDie);
49 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
50});
51
52it.layer(TestLayer)("WorkspaceFileSystemLive", (it) => {
53 describe("writeFile", () => {
54 it.effect("writes files relative to the workspace root", () =>
55 Effect.gen(function* () {
56 const workspaceFileSystem = yield* WorkspaceFileSystem;
57 const cwd = yield* makeTempDir;
58 const fileSystem = yield* FileSystem.FileSystem;
59 const path = yield* Path.Path;
60 const result = yield* workspaceFileSystem.writeFile({
61 cwd,
62 relativePath: "plans/effect-rpc.md",
63 contents: "# Plan\n",
64 });
65 const saved = yield* fileSystem
66 .readFileString(path.join(cwd, "plans/effect-rpc.md"))
67 .pipe(Effect.orDie);
68
69 expect(result).toEqual({ relativePath: "plans/effect-rpc.md" });
70 expect(saved).toBe("# Plan\n");
71 }),
72 );
73
74 it.effect("invalidates workspace entry search cache after writes", () =>
75 Effect.gen(function* () {
76 const workspaceEntries = yield* WorkspaceEntries;
77 const workspaceFileSystem = yield* WorkspaceFileSystem;
78 const cwd = yield* makeTempDir;
79 yield* writeTextFile(cwd, "src/existing.ts", "export {};\n");
80
81 const beforeWrite = yield* workspaceEntries.search({
82 cwd,
83 query: "rpc",
84 limit: 10,
85 });
86 expect(beforeWrite).toEqual({
87 entries: [],
88 truncated: false,
89 });
90
91 yield* workspaceFileSystem.writeFile({
92 cwd,
93 relativePath: "plans/effect-rpc.md",
94 contents: "# Plan\n",
95 });
96
97 const afterWrite = yield* workspaceEntries.search({
98 cwd,
99 query: "rpc",
100 limit: 10,
101 });
102 expect(afterWrite.entries).toEqual(
103 expect.arrayContaining([expect.objectContaining({ path: "plans/effect-rpc.md" })]),
104 );
105 expect(afterWrite.truncated).toBe(false);
106 }),
107 );
108
109 it.effect("rejects writes outside the workspace root", () =>
110 Effect.gen(function* () {
111 const workspaceFileSystem = yield* WorkspaceFileSystem;
112 const cwd = yield* makeTempDir;
113 const path = yield* Path.Path;
114 const fileSystem = yield* FileSystem.FileSystem;
115
116 const error = yield* workspaceFileSystem
117 .writeFile({
118 cwd,
119 relativePath: "../escape.md",
120 contents: "# nope\n",
121 })
122 .pipe(Effect.flip);
123
124 expect(error.message).toContain(
125 "Workspace file path must be relative to the project root: ../escape.md",
126 );
127
128 const escapedPath = path.resolve(cwd, "..", "escape.md");
129 const escapedStat = yield* fileSystem
130 .stat(escapedPath)
131 .pipe(Effect.catch(() => Effect.succeed(null)));
132 expect(escapedStat).toBeNull();
133 }),
134 );
135 });
136});