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