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

workflowScriptQuery.test.ts

74 lines2.9 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 { it as effectIt } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import { afterAll, assert, describe } from "vite-plus/test";
8import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
9import { readWorkflowScript } from "./workflowScriptQuery.ts";
10​
11const root = NodePath.join(NodeOS.homedir(), ".claude", "projects", "__wf_script_test__");
12NodeFS.mkdirSync(root, { recursive: true });
13const scriptPath = NodePath.join(root, "run.js");
14NodeFS.writeFileSync(scriptPath, "export const meta = {};\n");
15const outside = NodePath.join(NodeOS.tmpdir(), "wf-outside.js");
16NodeFS.writeFileSync(outside, "evil\n");
17const link = NodePath.join(root, "sneaky.js");
18// Planted only where the host allows it; the escape test is skipped
19// otherwise rather than passing vacuously on "not-found".
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("readWorkflowScript containment", () => {
34 effectIt.effect("serves a real script under the projects root", () =>
35 Effect.gen(function* () {
36 const result = yield* readWorkflowScript({ scriptPath });
37 assert.include(result.contents, "export const meta");
38 assert.equal(result.truncated, false);
39 }),
40 );
41​
42 effectIt.effect("rejects relative and non-js paths", () =>
43 Effect.gen(function* () {
44 const relative = yield* Effect.exit(readWorkflowScript({ scriptPath: "run.js" }));
45 assert.equal(relative._tag, "Failure");
46 const nonJs = yield* Effect.exit(
47 readWorkflowScript({ scriptPath: scriptPath.replace(".js", ".ts") }),
48 );
49 assert.equal(nonJs._tag, "Failure");
50 }),
51 );
52​
53 effectIt.effect.skipIf(!symlinksSupported)(
54 "rejects paths outside the root and symlink escapes",
55 () =>
56 Effect.gen(function* () {
57 const escaped = yield* Effect.exit(readWorkflowScript({ scriptPath: outside }));
58 assert.equal(escaped._tag, "Failure");
59 // A symlink INSIDE the root pointing outside must fail specifically on
60 // realpath re-containment — a "not-found" would mean the link was
61 // never exercised and the assertion proves nothing.
62 const sneaky = yield* Effect.exit(
63 readWorkflowScript({ scriptPath: link }).pipe(
64 Effect.flip,
65 Effect.map((error) => error.reason),
66 ),
67 );
68 assert.equal(sneaky._tag, "Success");
69 if (sneaky._tag === "Success") {
70 assert.equal(sneaky.value, "outside-root");
71 }
72 }),
73 );
74});