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

workflowScriptQuery.test.ts

90 lines3.5 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeChildProcess from "node:child_process";
6import * as HostProcess from "@t3tools/shared/HostProcess";
7import { it as effectIt } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import { afterAll, assert, describe } from "vite-plus/test";
10import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
11import { readWorkflowScript } from "./workflowScriptQuery.ts";
12​
13const root = NodePath.join(NodeOS.homedir(), ".claude", "projects", "__wf_script_test__");
14NodeFS.mkdirSync(root, { recursive: true });
15const scriptPath = NodePath.join(root, "run.js");
16NodeFS.writeFileSync(scriptPath, "export const meta = {};\n");
17const outside = NodePath.join(NodeOS.tmpdir(), "wf-outside.js");
18NodeFS.writeFileSync(outside, "evil\n");
19const link = NodePath.join(root, "sneaky.js");
20if (symlinksSupported) {
21 NodeFS.rmSync(link, { force: true });
22 NodeFS.symlinkSync(outside, link);
23 if (!NodeFS.lstatSync(link).isSymbolicLink()) {
24 throw new Error("test setup: sneaky.js must be a symlink");
25 }
27​
28afterAll(() => {
29 NodeFS.rmSync(root, { recursive: true, force: true });
30 NodeFS.rmSync(outside, { force: true });
31});
32​
33describe("workflow script containment", () => {
34 effectIt.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
35 "rejects a FIFO without waiting for a writer",
36 () =>
37 Effect.gen(function* () {
38 const fifo = NodePath.join(root, "pipe.js");
39 NodeChildProcess.execFileSync("mkfifo", [fifo]);
40 const error = yield* Effect.flip(readWorkflowScript({ scriptPath: fifo }));
41 assert.equal(error.reason, "not-regular-file");
42 }),
43 );
44​
45 effectIt.effect("serves a real script under the projects root", () =>
46 Effect.gen(function* () {
47 const result = yield* readWorkflowScript({ scriptPath });
48 assert.include(result.contents, "export const meta");
49 assert.equal(result.truncated, false);
50 }),
51 );
52​
53 effectIt.effect("clips a truncated script at a whole UTF-8 character", () =>
54 Effect.gen(function* () {
55 const cap = 256 * 1024;
56 const split = NodePath.join(root, "split.js");
57 NodeFS.writeFileSync(split, `//${"x".repeat(cap - 4)}😀\n`);
58 const clipped = yield* readWorkflowScript({ scriptPath: split });
59 assert.equal(clipped.truncated, true);
60 assert.equal(clipped.contents.slice(-4), "xxxx");
61 assert.equal(clipped.contents.length, cap - 2);
62​
63 const whole = NodePath.join(root, "whole.js");
64 NodeFS.writeFileSync(whole, `\uFEFF//${"x".repeat(cap - 9)}😀\n`);
65 const kept = yield* readWorkflowScript({ scriptPath: whole });
66 assert.equal(kept.truncated, true);
67 assert.equal(kept.contents.slice(-4), "xx😀");
68 assert.equal(kept.contents[0], "\uFEFF");
69 }),
70 );
71​
72 effectIt.effect.skipIf(!symlinksSupported)(
73 "rejects paths outside the root and symlink escapes",
74 () =>
75 Effect.gen(function* () {
76 const escaped = yield* Effect.exit(readWorkflowScript({ scriptPath: outside }));
77 assert.equal(escaped._tag, "Failure");
78 const sneaky = yield* Effect.exit(
79 readWorkflowScript({ scriptPath: link }).pipe(
80 Effect.flip,
81 Effect.map((error) => error.reason),
82 ),
83 );
84 assert.equal(sneaky._tag, "Success");
85 if (sneaky._tag === "Success") {
86 assert.equal(sneaky.value, "outside-root");
87 }
88 }),
89 );
90});