Open History to see the last commit
1import { assert, it, describe } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Logger from "effect/Logger";
7import * as Path from "effect/Path";
8
9import * as VcsProjectConfig from "./VcsProjectConfig.ts";
10
11const layerTest = VcsProjectConfig.layer.pipe(
12 Layer.provide(NodeServices.layer),
13 Layer.provideMerge(NodeServices.layer),
14);
15
16describe("VcsProjectConfig", () => {
17 it.layer(layerTest)("uses an explicit requested VCS kind before config", (it) => {
18 it.effect("returns the requested kind", () =>
19 Effect.gen(function* () {
20 const config = yield* VcsProjectConfig.VcsProjectConfig;
21 const kind = yield* config.resolveKind({
22 cwd: "/repo",
23 requestedKind: "jj",
24 });
25
26 assert.equal(kind, "jj");
27 }),
28 );
29 });
30
31 it.layer(layerTest)("discovers .t3code/vcs.json from nested workspaces", (it) => {
32 it.effect("returns the configured kind", () =>
33 Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;
35 const path = yield* Path.Path;
36 const root = yield* fileSystem.makeTempDirectoryScoped({
37 prefix: "t3-vcs-config-test-",
38 });
39 const configDir = path.join(root, ".t3code");
40 const nested = path.join(root, "packages", "app");
41 yield* fileSystem.makeDirectory(configDir, { recursive: true });
42 yield* fileSystem.makeDirectory(nested, { recursive: true });
43 yield* fileSystem.writeFileString(
44 path.join(configDir, "vcs.json"),
45 // @effect-diagnostics-next-line preferSchemaOverJson:off
46 JSON.stringify({ vcs: { kind: "jj" } }),
47 );
48
49 const config = yield* VcsProjectConfig.VcsProjectConfig;
50 const kind = yield* config.resolveKind({ cwd: nested });
51
52 assert.equal(kind, "jj");
53 }),
54 );
55 });
56
57 it.layer(layerTest)("continues to parent configs after a candidate inspect failure", (it) => {
58 it.effect("logs the failed candidate and returns the parent config", () => {
59 const messages: unknown[] = [];
60 const logger = Logger.make<unknown, void>(({ message }) => {
61 messages.push(message);
62 });
63
64 return Effect.gen(function* () {
65 const fileSystem = yield* FileSystem.FileSystem;
66 const path = yield* Path.Path;
67 const root = yield* fileSystem.makeTempDirectoryScoped({
68 prefix: "t3-vcs-config-test-",
69 });
70 const configDir = path.join(root, ".t3code");
71 const cwd = path.join(root, "invalid\0child");
72 yield* fileSystem.makeDirectory(configDir, { recursive: true });
73 yield* fileSystem.writeFileString(
74 path.join(configDir, "vcs.json"),
75 // @effect-diagnostics-next-line preferSchemaOverJson:off
76 JSON.stringify({ vcs: { kind: "jj" } }),
77 );
78
79 const config = yield* VcsProjectConfig.VcsProjectConfig;
80 const kind = yield* config.resolveKind({ cwd });
81
82 assert.equal(kind, "jj");
83 const failedCandidate = path.join(cwd, ".t3code", "vcs.json");
84 const [error] = messages[0] as ReadonlyArray<unknown>;
85 assert.instanceOf(error, VcsProjectConfig.VcsProjectConfigError);
86 assert.equal(
87 error.message,
88 "Failed to inspect VCS project config at " + failedCandidate + ".",
89 );
90 assert.deepInclude(error, {
91 operation: "inspect",
92 cwd,
93 configPath: failedCandidate,
94 _tag: "VcsProjectConfigError",
95 });
96 }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
97 });
98 });
99
100 it.layer(layerTest)("falls back to auto when no config exists", (it) => {
101 it.effect("returns auto", () =>
102 Effect.gen(function* () {
103 const fileSystem = yield* FileSystem.FileSystem;
104 const root = yield* fileSystem.makeTempDirectoryScoped({
105 prefix: "t3-vcs-config-test-",
106 });
107 const config = yield* VcsProjectConfig.VcsProjectConfig;
108 const kind = yield* config.resolveKind({ cwd: root });
109
110 assert.equal(kind, "auto");
111 }),
112 );
113 });
114
115 it.layer(layerTest)("falls back to auto when config JSON is malformed", (it) => {
116 it.effect("returns auto and logs the failed operation and path", () => {
117 const messages: unknown[] = [];
118 const logger = Logger.make<unknown, void>(({ message }) => {
119 messages.push(message);
120 });
121
122 return Effect.gen(function* () {
123 const fileSystem = yield* FileSystem.FileSystem;
124 const path = yield* Path.Path;
125 const root = yield* fileSystem.makeTempDirectoryScoped({
126 prefix: "t3-vcs-config-test-",
127 });
128 const configDir = path.join(root, ".t3code");
129 yield* fileSystem.makeDirectory(configDir, { recursive: true });
130 yield* fileSystem.writeFileString(path.join(configDir, "vcs.json"), "{not json");
131
132 const config = yield* VcsProjectConfig.VcsProjectConfig;
133 const kind = yield* config.resolveKind({ cwd: root });
134
135 assert.equal(kind, "auto");
136 const [error] = messages[0] as ReadonlyArray<unknown>;
137 assert.instanceOf(error, VcsProjectConfig.VcsProjectConfigError);
138 assert.equal(
139 error.message,
140 "Failed to decode VCS project config at " + path.join(configDir, "vcs.json") + ".",
141 );
142 assert.deepInclude(error.cause, { _tag: "SchemaError" });
143 assert.deepInclude(error, {
144 operation: "decode",
145 cwd: root,
146 configPath: path.join(configDir, "vcs.json"),
147 _tag: "VcsProjectConfigError",
148 });
149 }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
150 });
151 });
152
153 it.layer(layerTest)("falls back to auto when the config path cannot be read", (it) => {
154 it.effect("retains the read failure context", () => {
155 const messages: unknown[] = [];
156 const logger = Logger.make<unknown, void>(({ message }) => {
157 messages.push(message);
158 });
159
160 return Effect.gen(function* () {
161 const fileSystem = yield* FileSystem.FileSystem;
162 const path = yield* Path.Path;
163 const root = yield* fileSystem.makeTempDirectoryScoped({
164 prefix: "t3-vcs-config-test-",
165 });
166 const configPath = path.join(root, ".t3code", "vcs.json");
167 yield* fileSystem.makeDirectory(configPath, { recursive: true });
168
169 const config = yield* VcsProjectConfig.VcsProjectConfig;
170 const kind = yield* config.resolveKind({ cwd: root });
171
172 assert.equal(kind, "auto");
173 const [error] = messages[0] as ReadonlyArray<unknown>;
174 assert.instanceOf(error, VcsProjectConfig.VcsProjectConfigError);
175 assert.equal(error.message, "Failed to read VCS project config at " + configPath + ".");
176 assert.deepInclude(error.cause, { _tag: "PlatformError" });
177 assert.deepInclude(error, {
178 operation: "read",
179 cwd: root,
180 configPath,
181 _tag: "VcsProjectConfigError",
182 });
183 }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
184 });
185 });
186
187 it.layer(layerTest)("falls back to auto when config kind is invalid", (it) => {
188 it.effect("returns auto", () =>
189 Effect.gen(function* () {
190 const fileSystem = yield* FileSystem.FileSystem;
191 const path = yield* Path.Path;
192 const root = yield* fileSystem.makeTempDirectoryScoped({
193 prefix: "t3-vcs-config-test-",
194 });
195 const configDir = path.join(root, ".t3code");
196 yield* fileSystem.makeDirectory(configDir, { recursive: true });
197 yield* fileSystem.writeFileString(
198 path.join(configDir, "vcs.json"),
199 `{"vcs":{"kind":"svn"}}`,
200 );
201
202 const config = yield* VcsProjectConfig.VcsProjectConfig;
203 const kind = yield* config.resolveKind({ cwd: root });
204
205 assert.equal(kind, "auto");
206 }),
207 );
208 });
209});