| 1 | import * as NodeFS from "node:fs"; |
| 2 | |
| 3 | const argv = process.argv.slice(2); |
| 4 | const args = argv.join(" "); |
| 5 | |
| 6 | function fail(message, code) { |
| 7 | process.stderr.write(message + "\n"); |
| 8 | process.exit(code); |
| 9 | } |
| 10 | |
| 11 | const permissionIndex = argv.indexOf("--permission-mode"); |
| 12 | if (permissionIndex === -1 || argv[permissionIndex + 1] !== "dontAsk") { |
| 13 | fail("text generation must deny permission prompts", 12); |
| 14 | } |
| 15 | const toolsIndex = argv.indexOf("--tools"); |
| 16 | if (toolsIndex === -1 || argv[toolsIndex + 1] !== "") { |
| 17 | fail("text generation must receive an explicit empty tool set", 6); |
| 18 | } |
| 19 | if (argv.includes("--dangerously-skip-permissions")) { |
| 20 | fail("text generation must not bypass permissions", 7); |
| 21 | } |
| 22 | if (!argv.includes("--disable-slash-commands")) { |
| 23 | fail("text generation must disable skills", 8); |
| 24 | } |
| 25 | if (!argv.includes("--strict-mcp-config")) { |
| 26 | fail("text generation must not load configured MCP servers", 9); |
| 27 | } |
| 28 | const settingsIndex = argv.indexOf("--settings"); |
| 29 | if (settingsIndex === -1 || JSON.parse(argv[settingsIndex + 1]).disableAllHooks !== true) { |
| 30 | fail("text generation must disable hooks", 10); |
| 31 | } |
| 32 | const cwdMustNotBe = process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE; |
| 33 | if (cwdMustNotBe && NodeFS.realpathSync(process.cwd()) === NodeFS.realpathSync(cwdMustNotBe)) { |
| 34 | fail("text generation ran in the project directory", 11); |
| 35 | } |
| 36 | |
| 37 | let stdinContent = ""; |
| 38 | if (!process.stdin.isTTY) { |
| 39 | const chunks = []; |
| 40 | for await (const chunk of process.stdin) { |
| 41 | chunks.push(chunk); |
| 42 | } |
| 43 | stdinContent = Buffer.concat(chunks).toString("utf8"); |
| 44 | } |
| 45 | |
| 46 | const argsMustContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN; |
| 47 | if (argsMustContain && !args.includes(argsMustContain)) { |
| 48 | fail("args missing expected content", 2); |
| 49 | } |
| 50 | |
| 51 | const argsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN; |
| 52 | if (argsMustNotContain && args.includes(argsMustNotContain)) { |
| 53 | fail("args contained forbidden content", 3); |
| 54 | } |
| 55 | |
| 56 | const stdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN; |
| 57 | if (stdinMustContain && !stdinContent.includes(stdinMustContain)) { |
| 58 | fail("stdin missing expected content", 4); |
| 59 | } |
| 60 | |
| 61 | const configDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE; |
| 62 | if (configDirMustBe && process.env.CLAUDE_CONFIG_DIR !== configDirMustBe) { |
| 63 | fail("CLAUDE_CONFIG_DIR was " + (process.env.CLAUDE_CONFIG_DIR ?? ""), 5); |
| 64 | } |
| 65 | |
| 66 | const stderrText = process.env.T3_FAKE_CLAUDE_STDERR; |
| 67 | if (stderrText) { |
| 68 | process.stderr.write(stderrText + "\n"); |
| 69 | } |
| 70 | |
| 71 | process.stdout.write(process.env.T3_FAKE_CLAUDE_OUTPUT ?? ""); |
| 72 | process.exitCode = Number(process.env.T3_FAKE_CLAUDE_EXIT_CODE ?? 0); |