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

VcsProcess.test.ts

268 lines8.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import { TestClock } from "effect/testing";
8​
9import { VcsProcessExitError, VcsProcessSpawnError, VcsProcessTimeoutError } from "@v12/contracts";
10import * as ProcessRunner from "../processRunner.ts";
11import * as VcsProcess from "./VcsProcess.ts";
12​
13const run = (input: VcsProcess.VcsProcessInput) =>
14 Effect.gen(function* () {
15 const process = yield* VcsProcess.VcsProcess;
16 return yield* process.run(input);
17 });
18​
19const liveLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
20​
21const provideLive = <A, E, R>(effect: Effect.Effect<A, E, R | VcsProcess.VcsProcess>) =>
22 effect.pipe(Effect.provide(liveLayer));
23​
24const baseInput = {
25 operation: "test.process-boundary",
26 command: "git",
27 args: ["status", "--short"],
28 cwd: "/workspace",
29} satisfies VcsProcess.VcsProcessInput;
30​
31const captureProcessResult = (
32 result: Effect.Effect<ProcessRunner.ProcessRunOutput, ProcessRunner.ProcessRunError>,
33) =>
34 VcsProcess.make.pipe(
35 Effect.provideService(
36 ProcessRunner.ProcessRunner,
37 ProcessRunner.ProcessRunner.of({ run: () => result }),
38 ),
39 Effect.flatMap((service) => service.run(baseInput)),
40 Effect.flip,
41 );
42​
43describe("VcsProcess.run", () => {
44 it.effect("collects stdout", () =>
45 Effect.gen(function* () {
46 const result = yield* run({
47 operation: "test.stdout",
48 command: "node",
49 args: ["-e", "process.stdout.write('hello')"],
50 cwd: process.cwd(),
51 });
52​
53 expect(result.stdout).toBe("hello");
54 expect(result.stderr).toBe("");
55 expect(result.stdoutTruncated).toBe(false);
56 expect(result.stderrTruncated).toBe(false);
57 }).pipe(provideLive),
58 );
59​
60 it.effect("writes stdin before waiting for exit", () =>
61 Effect.gen(function* () {
62 const result = yield* run({
63 operation: "test.stdin",
64 command: "node",
65 args: [
66 "-e",
67 [
68 "process.stdin.setEncoding('utf8');",
69 "let data='';",
70 "process.stdin.on('data', chunk => { data += chunk; });",
71 "process.stdin.on('end', () => { process.stdout.write(data); });",
72 ].join(""),
73 ],
74 cwd: process.cwd(),
75 stdin: "stdin payload",
76 });
77​
78 expect(result.stdout).toBe("stdin payload");
79 }).pipe(provideLive),
80 );
81​
82 it.effect("fails with VcsProcessExitError for non-zero exits by default", () =>
83 Effect.gen(function* () {
84 const secretArgument = "--token=super-secret-token";
85 const secretStderr = "remote rejected super-secret-token";
86 const error = yield* run({
87 operation: "test.exit",
88 command: "node",
89 args: [
90 "-e",
91 "process.stderr.write(process.argv[1]); process.exit(2)",
92 secretStderr,
93 secretArgument,
94 ],
95 cwd: process.cwd(),
96 }).pipe(Effect.flip);
97​
98 expect(error).toBeInstanceOf(VcsProcessExitError);
99 expect(error).toMatchObject({
100 operation: "test.exit",
101 command: "node",
102 argumentCount: 4,
103 exitCode: 2,
104 detail: "Process exited with a non-zero status.",
105 failureKind: "command-failed",
106 stderrLength: secretStderr.length,
107 stderrTruncated: false,
108 });
109 expect(error.message).not.toContain(secretArgument);
110 expect(error.message).not.toContain(secretStderr);
111 }).pipe(provideLive),
112 );
113​
114 it.effect("classifies authentication failures without retaining stderr", () =>
115 Effect.gen(function* () {
116 const secretStderr = "authentication failed for token super-secret-token";
117 const error = yield* run({
118 operation: "test.authentication",
119 command: "node",
120 args: ["-e", "process.stderr.write(process.argv[1]); process.exit(1)", secretStderr],
121 cwd: process.cwd(),
122 }).pipe(Effect.flip);
123​
124 expect(error).toBeInstanceOf(VcsProcessExitError);
125 expect(error).toMatchObject({
126 operation: "test.authentication",
127 command: "node",
128 exitCode: 1,
129 detail: "Authentication failed.",
130 failureKind: "authentication",
131 stderrLength: secretStderr.length,
132 stderrTruncated: false,
133 });
134 expect(error.message).not.toContain(secretStderr);
135 expect(error.message).not.toContain("super-secret-token");
136 }).pipe(provideLive),
137 );
138​
139 it.effect("retains spawn causes without exposing process arguments in the error message", () =>
140 Effect.gen(function* () {
141 const secretArgument = "--token=super-secret-token";
142 const error = yield* run({
143 operation: "test.spawn",
144 command: "definitely-not-a-v12-executable",
145 args: [secretArgument],
146 cwd: process.cwd(),
147 }).pipe(Effect.flip);
148​
149 expect(error).toBeInstanceOf(VcsProcessSpawnError);
150 expect(error).toMatchObject({
151 operation: "test.spawn",
152 command: "definitely-not-a-v12-executable",
153 argumentCount: 1,
154 });
155 expect(error).toHaveProperty("cause");
156 expect(error.message).not.toContain(secretArgument);
157 }).pipe(provideLive),
158 );
159​
160 it.effect("preserves real boundary causes without manufacturing structural ones", () =>
161 Effect.gen(function* () {
162 const cause = new Error("secret stdin failure");
163 const error = yield* captureProcessResult(
164 Effect.fail(
165 new ProcessRunner.ProcessStdinError({
166 command: baseInput.command,
167 argumentCount: baseInput.args.length,
168 cwd: baseInput.cwd,
169 stdinBytes: 47,
170 cause,
171 }),
172 ),
173 );
174​
175 expect(error).toMatchObject({
176 _tag: "VcsProcessStdinWriteError",
177 operation: baseInput.operation,
178 stdinBytes: 47,
179 cause,
180 });
181 expect(error.message).not.toContain(cause.message);
182​
183 const missingExitCodeError = yield* captureProcessResult(
184 Effect.succeed({
185 stdout: "",
186 stderr: "",
187 code: null,
188 timedOut: false,
189 stdoutTruncated: false,
190 stderrTruncated: false,
191 }),
192 );
193​
194 expect(missingExitCodeError).toMatchObject({
195 _tag: "VcsProcessMissingExitCodeError",
196 operation: baseInput.operation,
197 command: baseInput.command,
198 cwd: baseInput.cwd,
199 argumentCount: baseInput.args.length,
200 });
201 expect(missingExitCodeError).not.toHaveProperty("cause");
202 }),
203 );
204​
205 it.effect("returns output when non-zero exits are allowed", () =>
206 Effect.gen(function* () {
207 const result = yield* run({
208 operation: "test.allowed-exit",
209 command: "node",
210 args: ["-e", "process.stderr.write('boom'); process.exit(2)"],
211 cwd: process.cwd(),
212 allowNonZeroExit: true,
213 });
214​
215 expect(result.exitCode).toBe(2);
216 expect(result.stderr).toBe("boom");
217 }).pipe(provideLive),
218 );
219​
220 it.effect("truncates output and appends the marker when requested", () =>
221 Effect.gen(function* () {
222 const result = yield* run({
223 operation: "test.truncate-marker",
224 command: "node",
225 args: ["-e", "process.stdout.write('x'.repeat(2048))"],
226 cwd: process.cwd(),
227 maxOutputBytes: 128,
228 appendTruncationMarker: true,
229 });
230​
231 expect(result.stdoutTruncated).toBe(true);
232 expect(result.stdout).toContain("[truncated]");
233 expect(result.stderrTruncated).toBe(false);
234 }).pipe(provideLive),
235 );
236​
237 it.effect("truncates without the marker when truncation markers are disabled", () =>
238 Effect.gen(function* () {
239 const result = yield* run({
240 operation: "test.truncate-silent",
241 command: "node",
242 args: ["-e", "process.stdout.write('x'.repeat(2048))"],
243 cwd: process.cwd(),
244 maxOutputBytes: 128,
245 });
246​
247 expect(result.stdoutTruncated).toBe(true);
248 expect(result.stdout).not.toContain("[truncated]");
249 }).pipe(provideLive),
250 );
251​
252 it.effect("fails with VcsProcessTimeoutError on timeout", () =>
253 Effect.gen(function* () {
254 const errorFiber = yield* run({
255 operation: "test.timeout",
256 command: "node",
257 args: ["-e", "setTimeout(() => {}, 5000)"],
258 cwd: process.cwd(),
259 timeoutMs: 50,
260 }).pipe(Effect.flip, Effect.forkScoped);
261 yield* Effect.yieldNow;
262 yield* TestClock.adjust(Duration.millis(50));
263 const error = yield* Fiber.join(errorFiber);
264​
265 expect(error).toBeInstanceOf(VcsProcessTimeoutError);
266 }).pipe(provideLive),
267 );
268});