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

workflowScriptQuery.test.ts

89 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 { OrchestrationWorkflowFileError } from "@t3tools/contracts";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import { it as effectIt } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import { afterAll, assert, describe } from "vite-plus/test";
11import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
12import { readWorkflowScript } from "./workflowScriptQuery.ts";
13​
14const root = NodePath.join(NodeOS.homedir(), ".claude", "projects", "__wf_script_test__");
15NodeFS.mkdirSync(root, { recursive: true });
16const scriptPath = NodePath.join(root, "run.js");
17NodeFS.writeFileSync(scriptPath, "export const meta = {};\n");
18const outside = NodePath.join(NodeOS.tmpdir(), "wf-outside.js");
19NodeFS.writeFileSync(outside, "evil\n");
20const link = NodePath.join(root, "sneaky.js");
21if (symlinksSupported) {
22 NodeFS.rmSync(link, { force: true });
23 NodeFS.symlinkSync(outside, link);
24 if (!NodeFS.lstatSync(link).isSymbolicLink()) {
25 throw new Error("test setup: sneaky.js must be a symlink");
26 }
28​
29afterAll(() => {
30 NodeFS.rmSync(root, { recursive: true, force: true });
31 NodeFS.rmSync(outside, { force: true });
32});
33​
34describe("workflow script containment", () => {
35 effectIt.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
36 "rejects a FIFO without waiting for a writer",
37 () =>
38 Effect.gen(function* () {
39 const fifo = NodePath.join(root, "pipe.js");
40 NodeChildProcess.execFileSync("mkfifo", [fifo]);
41 const error = yield* Effect.flip(readWorkflowScript({ scriptPath: fifo }));
42 assert.equal(error.reason, "not-regular-file");
43 }),
44 );
45​
46 effectIt.effect("serves a real script under the projects root", () =>
47 Effect.gen(function* () {
48 const result = yield* readWorkflowScript({ scriptPath });
49 assert.include(result.contents, "export const meta");
50 assert.equal(result.truncated, false);
51 }),
52 );
53​
54 effectIt.effect("rejects relative and non-js paths", () =>
55 Effect.gen(function* () {
56 const relative = yield* Effect.exit(readWorkflowScript({ scriptPath: "run.js" }));
57 assert.equal(relative._tag, "Failure");
58 const nonJs = yield* Effect.exit(
59 readWorkflowScript({ scriptPath: scriptPath.replace(".js", ".ts") }),
60 );
61 assert.equal(nonJs._tag, "Failure");
62 const error = yield* Effect.flip(readWorkflowScript({ scriptPath: "run.js" }));
63 assert.equal(error._tag, "OrchestrationGetWorkflowScriptError");
64 assert.equal(error.scriptPath, "run.js");
65 assert.equal(error.reason, "invalid-path");
66 assert.equal(error.message, "Workflow scripts must be absolute .js paths.");
67 assert.instanceOf(error.cause, OrchestrationWorkflowFileError);
68 }),
69 );
70​
71 effectIt.effect.skipIf(!symlinksSupported)(
72 "rejects paths outside the root and symlink escapes",
73 () =>
74 Effect.gen(function* () {
75 const escaped = yield* Effect.exit(readWorkflowScript({ scriptPath: outside }));
76 assert.equal(escaped._tag, "Failure");
77 const sneaky = yield* Effect.exit(
78 readWorkflowScript({ scriptPath: link }).pipe(
79 Effect.flip,
80 Effect.map((error) => error.reason),
81 ),
82 );
83 assert.equal(sneaky._tag, "Success");
84 if (sneaky._tag === "Success") {
85 assert.equal(sneaky.value, "outside-root");
86 }
87 }),
88 );
89});