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

VcsProcess.test.ts

138 lines4.4 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, VcsProcessTimeoutError } from "@t3tools/contracts";
10import * as VcsProcess from "./VcsProcess.ts";
11​
12const run = (input: VcsProcess.VcsProcessInput) =>
13 Effect.gen(function* () {
14 const process = yield* VcsProcess.VcsProcess;
15 return yield* process.run(input);
16 });
17​
18const liveLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
19​
20const provideLive = <A, E, R>(effect: Effect.Effect<A, E, R | VcsProcess.VcsProcess>) =>
21 effect.pipe(Effect.provide(liveLayer));
22​
23describe("VcsProcess.run", () => {
24 it.effect("collects stdout", () =>
25 Effect.gen(function* () {
26 const result = yield* run({
27 operation: "test.stdout",
28 command: "node",
29 args: ["-e", "process.stdout.write('hello')"],
30 cwd: process.cwd(),
31 });
32​
33 expect(result.stdout).toBe("hello");
34 expect(result.stderr).toBe("");
35 expect(result.stdoutTruncated).toBe(false);
36 expect(result.stderrTruncated).toBe(false);
37 }).pipe(provideLive),
38 );
39​
40 it.effect("writes stdin before waiting for exit", () =>
41 Effect.gen(function* () {
42 const result = yield* run({
43 operation: "test.stdin",
44 command: "node",
45 args: [
46 "-e",
47 [
48 "process.stdin.setEncoding('utf8');",
49 "let data='';",
50 "process.stdin.on('data', chunk => { data += chunk; });",
51 "process.stdin.on('end', () => { process.stdout.write(data); });",
52 ].join(""),
53 ],
54 cwd: process.cwd(),
55 stdin: "stdin payload",
56 });
57​
58 expect(result.stdout).toBe("stdin payload");
59 }).pipe(provideLive),
60 );
61​
62 it.effect("fails with VcsProcessExitError for non-zero exits by default", () =>
63 Effect.gen(function* () {
64 const error = yield* run({
65 operation: "test.exit",
66 command: "node",
67 args: ["-e", "process.stderr.write('boom'); process.exit(2)"],
68 cwd: process.cwd(),
69 }).pipe(Effect.flip);
70​
71 expect(error).toBeInstanceOf(VcsProcessExitError);
72 }).pipe(provideLive),
73 );
74​
75 it.effect("returns output when non-zero exits are allowed", () =>
76 Effect.gen(function* () {
77 const result = yield* run({
78 operation: "test.allowed-exit",
79 command: "node",
80 args: ["-e", "process.stderr.write('boom'); process.exit(2)"],
81 cwd: process.cwd(),
82 allowNonZeroExit: true,
83 });
84​
85 expect(result.exitCode).toBe(2);
86 expect(result.stderr).toBe("boom");
87 }).pipe(provideLive),
88 );
89​
90 it.effect("truncates output and appends the marker when requested", () =>
91 Effect.gen(function* () {
92 const result = yield* run({
93 operation: "test.truncate-marker",
94 command: "node",
95 args: ["-e", "process.stdout.write('x'.repeat(2048))"],
96 cwd: process.cwd(),
97 maxOutputBytes: 128,
98 appendTruncationMarker: true,
99 });
100​
101 expect(result.stdoutTruncated).toBe(true);
102 expect(result.stdout).toContain("[truncated]");
103 expect(result.stderrTruncated).toBe(false);
104 }).pipe(provideLive),
105 );
106​
107 it.effect("truncates without the marker when truncation markers are disabled", () =>
108 Effect.gen(function* () {
109 const result = yield* run({
110 operation: "test.truncate-silent",
111 command: "node",
112 args: ["-e", "process.stdout.write('x'.repeat(2048))"],
113 cwd: process.cwd(),
114 maxOutputBytes: 128,
115 });
116​
117 expect(result.stdoutTruncated).toBe(true);
118 expect(result.stdout).not.toContain("[truncated]");
119 }).pipe(provideLive),
120 );
121​
122 it.effect("fails with VcsProcessTimeoutError on timeout", () =>
123 Effect.gen(function* () {
124 const errorFiber = yield* run({
125 operation: "test.timeout",
126 command: "node",
127 args: ["-e", "setTimeout(() => {}, 5000)"],
128 cwd: process.cwd(),
129 timeoutMs: 50,
130 }).pipe(Effect.flip, Effect.forkScoped);
131 yield* Effect.yieldNow;
132 yield* TestClock.adjust(Duration.millis(50));
133 const error = yield* Fiber.join(errorFiber);
134​
135 expect(error).toBeInstanceOf(VcsProcessTimeoutError);
136 }).pipe(provideLive),
137 );
138});