| ... | ... | @@ -1,12 +1,13 @@ |
| 1 | | import { ClaudeSettings, ProviderInstanceId } from "@t3tools/contracts"; |
| 2 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | 2 | import { it } from "@effect/vitest"; |
| 3 | import { ClaudeSettings, ProviderInstanceId } from "@t3tools/contracts"; |
| 4 | import { isHostWindows } from "@t3tools/shared/hostProcess"; |
| 5 | import { createModelSelection } from "@t3tools/shared/model"; |
| 4 | 6 | import * as Effect from "effect/Effect"; |
| 5 | 7 | import * as FileSystem from "effect/FileSystem"; |
| 6 | 8 | import * as Layer from "effect/Layer"; |
| 7 | 9 | import * as Path from "effect/Path"; |
| 8 | 10 | import * as Schema from "effect/Schema"; |
| 9 | | import { createModelSelection } from "@t3tools/shared/model"; |
| 10 | 11 | import { expect } from "vite-plus/test"; |
| 11 | 12 | |
| 12 | 13 | import * as ServerConfig from "../config.ts"; |
| ... | ... | @@ -23,47 +24,80 @@ function makeFakeClaudeBinary(dir: string) { |
| 23 | 24 | return Effect.gen(function* () { |
| 24 | 25 | const fs = yield* FileSystem.FileSystem; |
| 25 | 26 | const path = yield* Path.Path; |
| 27 | const isWindows = yield* isHostWindows; |
| 26 | 28 | const binDir = path.join(dir, "bin"); |
| 27 | | const claudePath = path.join(binDir, "claude"); |
| 29 | const stubPath = path.join(binDir, "claude-stub.mjs"); |
| 28 | 30 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 29 | 31 | |
| 32 | // The stub behaviour lives in Node rather than a `#!/bin/sh` script so the |
| 33 | // same implementation is usable on Windows, where a shebang file is not |
| 34 | // executable and would fall through to the real Claude CLI on PATH. |
| 30 | 35 | yield* fs.writeFileString( |
| 31 | | claudePath, |
| 36 | stubPath, |
| 32 | 37 | [ |
| 33 | | "#!/bin/sh", |
| 34 | | 'args="$*"', |
| 35 | | 'stdin_content="$(cat)"', |
| 36 | | 'if [ -n "$T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN" ]; then', |
| 37 | | ' printf "%s" "$args" | grep -F -- "$T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN" >/dev/null || {', |
| 38 | | ' printf "%s\\n" "args missing expected content" >&2', |
| 39 | | " exit 2", |
| 40 | | " }", |
| 41 | | "fi", |
| 42 | | 'if [ -n "$T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN" ]; then', |
| 43 | | ' if printf "%s" "$args" | grep -F -- "$T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN" >/dev/null; then', |
| 44 | | ' printf "%s\\n" "args contained forbidden content" >&2', |
| 45 | | " exit 3", |
| 46 | | " fi", |
| 47 | | "fi", |
| 48 | | 'if [ -n "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" ]; then', |
| 49 | | ' printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN" >/dev/null || {', |
| 50 | | ' printf "%s\\n" "stdin missing expected content" >&2', |
| 51 | | " exit 4", |
| 38 | 'const args = process.argv.slice(2).join(" ");', |
| 39 | "", |
| 40 | "function fail(message, code) {", |
| 41 | ' process.stderr.write(message + "\\n");', |
| 42 | " process.exit(code);", |
| 43 | "}", |
| 44 | "", |
| 45 | 'let stdinContent = "";', |
| 46 | "if (!process.stdin.isTTY) {", |
| 47 | " const chunks = [];", |
| 48 | " for await (const chunk of process.stdin) {", |
| 49 | " chunks.push(chunk);", |
| 52 | 50 | " }", |
| 53 | | "fi", |
| 54 | | 'if [ -n "$T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE" ] && [ "$CLAUDE_CONFIG_DIR" != "$T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE" ]; then', |
| 55 | | ' printf "%s\\n" "CLAUDE_CONFIG_DIR was $CLAUDE_CONFIG_DIR" >&2', |
| 56 | | " exit 5", |
| 57 | | "fi", |
| 58 | | 'if [ -n "$T3_FAKE_CLAUDE_STDERR" ]; then', |
| 59 | | ' printf "%s\\n" "$T3_FAKE_CLAUDE_STDERR" >&2', |
| 60 | | "fi", |
| 61 | | 'printf "%s" "$T3_FAKE_CLAUDE_OUTPUT"', |
| 62 | | 'exit "${T3_FAKE_CLAUDE_EXIT_CODE:-0}"', |
| 51 | ' stdinContent = Buffer.concat(chunks).toString("utf8");', |
| 52 | "}", |
| 53 | "", |
| 54 | "const argsMustContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_CONTAIN;", |
| 55 | "if (argsMustContain && !args.includes(argsMustContain)) {", |
| 56 | ' fail("args missing expected content", 2);', |
| 57 | "}", |
| 58 | "", |
| 59 | "const argsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;", |
| 60 | "if (argsMustNotContain && args.includes(argsMustNotContain)) {", |
| 61 | ' fail("args contained forbidden content", 3);', |
| 62 | "}", |
| 63 | "", |
| 64 | "const stdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;", |
| 65 | "if (stdinMustContain && !stdinContent.includes(stdinMustContain)) {", |
| 66 | ' fail("stdin missing expected content", 4);', |
| 67 | "}", |
| 68 | "", |
| 69 | "const configDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;", |
| 70 | "if (configDirMustBe && process.env.CLAUDE_CONFIG_DIR !== configDirMustBe) {", |
| 71 | ' fail("CLAUDE_CONFIG_DIR was " + (process.env.CLAUDE_CONFIG_DIR ?? ""), 5);', |
| 72 | "}", |
| 73 | "", |
| 74 | "const stderrText = process.env.T3_FAKE_CLAUDE_STDERR;", |
| 75 | "if (stderrText) {", |
| 76 | ' process.stderr.write(stderrText + "\\n");', |
| 77 | "}", |
| 78 | "", |
| 79 | 'process.stdout.write(process.env.T3_FAKE_CLAUDE_OUTPUT ?? "");', |
| 80 | "process.exitCode = Number(process.env.T3_FAKE_CLAUDE_EXIT_CODE ?? 0);", |
| 63 | 81 | "", |
| 64 | 82 | ].join("\n"), |
| 65 | 83 | ); |
| 66 | | yield* fs.chmod(claudePath, 0o755); |
| 84 | |
| 85 | if (isWindows) { |
| 86 | // Windows resolves executables through PATHEXT, so the entry point has to |
| 87 | // carry a real extension. `resolveSpawnCommand` spawns `.cmd` via a shell. |
| 88 | yield* fs.writeFileString( |
| 89 | path.join(binDir, "claude.cmd"), |
| 90 | ["@echo off", 'node "%~dp0claude-stub.mjs" %*', "exit /b %ERRORLEVEL%", ""].join("\r\n"), |
| 91 | ); |
| 92 | } else { |
| 93 | const claudePath = path.join(binDir, "claude"); |
| 94 | yield* fs.writeFileString( |
| 95 | claudePath, |
| 96 | ["#!/bin/sh", 'exec node "$(dirname "$0")/claude-stub.mjs" "$@"', ""].join("\n"), |
| 97 | ); |
| 98 | yield* fs.chmod(claudePath, 0o755); |
| 99 | } |
| 100 | |
| 67 | 101 | return binDir; |
| 68 | 102 | }); |
| 69 | 103 | } |
| ... | ... | @@ -85,6 +119,7 @@ function withFakeClaudeEnv<A, E, R>( |
| 85 | 119 | const fs = yield* FileSystem.FileSystem; |
| 86 | 120 | const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-claude-text-" }); |
| 87 | 121 | const binDir = yield* makeFakeClaudeBinary(tempDir); |
| 122 | const pathDelimiter = (yield* isHostWindows) ? ";" : ":"; |
| 88 | 123 | const previousPath = process.env.PATH; |
| 89 | 124 | const previousOutput = process.env.T3_FAKE_CLAUDE_OUTPUT; |
| 90 | 125 | const previousExitCode = process.env.T3_FAKE_CLAUDE_EXIT_CODE; |
| ... | ... | @@ -96,7 +131,7 @@ function withFakeClaudeEnv<A, E, R>( |
| 96 | 131 | |
| 97 | 132 | yield* Effect.acquireRelease( |
| 98 | 133 | Effect.sync(() => { |
| 99 | | process.env.PATH = `${binDir}:${previousPath ?? ""}`; |
| 134 | process.env.PATH = `${binDir}${pathDelimiter}${previousPath ?? ""}`; |
| 100 | 135 | process.env.T3_FAKE_CLAUDE_OUTPUT = input.output; |
| 101 | 136 | |
| 102 | 137 | if (input.exitCode !== undefined) { |