The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { isWindowsCommandNotFound } from "@t3tools/provider-core/server/snapshotProbe"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Deferred from "effect/Deferred"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as PlatformError from "effect/PlatformError"; |
| 9 | import * as Sink from "effect/Sink"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { ChildProcessSpawner } from "effect/process"; |
| 12 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 13 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; |
| 14 | |
| 15 | import * as ProcessRunner from "./processRunner.ts"; |
| 16 | |
| 17 | type ChildProcessCommand = { |
| 18 | readonly command: string; |
| 19 | readonly args: ReadonlyArray<string>; |
| 20 | readonly options: { |
| 21 | readonly shell?: boolean | string; |
| 22 | }; |
| 23 | }; |
| 24 | |
| 25 | function asChildProcessCommand(command: unknown): ChildProcessCommand { |
| 26 | return command as ChildProcessCommand; |
| 27 | } |
| 28 | |
| 29 | function makeHandle(input: { |
| 30 | readonly stdout?: string | Stream.Stream<Uint8Array>; |
| 31 | readonly stderr?: string | Stream.Stream<Uint8Array>; |
| 32 | readonly code?: number; |
| 33 | readonly stdin?: ChildProcessSpawner.ChildProcessHandle["stdin"]; |
| 34 | readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>; |
| 35 | }) { |
| 36 | return ChildProcessSpawner.makeHandle({ |
| 37 | pid: ChildProcessSpawner.ProcessId(1), |
| 38 | exitCode: input.exitCode ?? Effect.succeed(ChildProcessSpawner.ExitCode(input.code ?? 0)), |
| 39 | isRunning: Effect.succeed(false), |
| 40 | kill: () => Effect.void, |
| 41 | unref: Effect.succeed(Effect.void), |
| 42 | stdin: input.stdin ?? Sink.drain, |
| 43 | stdout: |
| 44 | typeof input.stdout === "string" |
| 45 | ? Stream.encodeText(Stream.make(input.stdout)) |
| 46 | : (input.stdout ?? Stream.empty), |
| 47 | stderr: |
| 48 | typeof input.stderr === "string" |
| 49 | ? Stream.encodeText(Stream.make(input.stderr)) |
| 50 | : (input.stderr ?? Stream.empty), |
| 51 | all: Stream.empty, |
| 52 | getInputFd: () => Sink.drain, |
| 53 | getOutputFd: () => Stream.empty, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function makeSpawner( |
| 58 | f: ( |
| 59 | command: ChildProcessCommand, |
| 60 | ) => Effect.Effect<ChildProcessSpawner.ChildProcessHandle, PlatformError.PlatformError>, |
| 61 | ) { |
| 62 | return ChildProcessSpawner.make((command) => f(asChildProcessCommand(command))); |
| 63 | } |
| 64 | |
| 65 | const runWith = |
| 66 | (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) => |
| 67 | (input: ProcessRunner.ProcessRunInput) => |
| 68 | Effect.service(ProcessRunner.ProcessRunner).pipe( |
| 69 | Effect.flatMap((runner) => |
| 70 | runner.run({ |
| 71 | ...input, |
| 72 | }), |
| 73 | ), |
| 74 | Effect.provide( |
| 75 | ProcessRunner.layer.pipe( |
| 76 | Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)), |
| 77 | ), |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | describe("runProcess", () => { |
| 82 | it.effect("resolves and escapes Windows command shims before spawning", () => { |
| 83 | const spawner = makeSpawner((command) => |
| 84 | Effect.sync(() => { |
| 85 | expect(command.command).toBe( |
| 86 | [ |
| 87 | '^"C:\\Users\\tester\\AppData\\Roaming\\npm\\az.cmd^"', |
| 88 | '^"repos^"', |
| 89 | '^"pr^"', |
| 90 | '^"list^"', |
| 91 | '^"--source-branch^"', |
| 92 | '^"feature^ ^&^ release^"', |
| 93 | ].join(" "), |
| 94 | ); |
| 95 | expect(command.args).toEqual([]); |
| 96 | expect(command.options.shell).toBe(true); |
| 97 | return makeHandle({ stdout: "[]" }); |
| 98 | }), |
| 99 | ); |
| 100 | |
| 101 | return runWith(spawner)({ |
| 102 | command: "az", |
| 103 | args: ["repos", "pr", "list", "--source-branch", "feature & release"], |
| 104 | env: { AZURE_CONFIG_DIR: "C:\\Users\\tester\\.azure" }, |
| 105 | }).pipe( |
| 106 | Effect.provideService(HostProcess.Platform, "win32"), |
| 107 | Effect.provideService(HostProcess.Environment, { |
| 108 | PATH: "C:\\Users\\tester\\AppData\\Roaming\\npm", |
| 109 | PATHEXT: ".COM;.EXE;.BAT;.CMD", |
| 110 | }), |
| 111 | Effect.provideService(SpawnExecutableResolution, (_command, _platform, env) => |
| 112 | env.PATH === "C:\\Users\\tester\\AppData\\Roaming\\npm" && |
| 113 | env.AZURE_CONFIG_DIR === "C:\\Users\\tester\\.azure" |
| 114 | ? "C:\\Users\\tester\\AppData\\Roaming\\npm\\az.cmd" |
| 115 | : undefined, |
| 116 | ), |
| 117 | Effect.map((result) => { |
| 118 | expect(result.stdout).toBe("[]"); |
| 119 | }), |
| 120 | ); |
| 121 | }); |
| 122 | |
| 123 | it.effect("types spawn failures that the platform spawner throws synchronously", () => |
| 124 | Effect.gen(function* () { |
| 125 | // File-valued PATH entries make spawn throw ENOTDIR synchronously, arriving through Effect.callback as a defect. |
| 126 | const defect = Object.assign(new Error("spawn jj ENOTDIR"), { code: "ENOTDIR" }); |
| 127 | const spawner = makeSpawner(() => |
| 128 | Effect.callback<never, never>(() => { |
| 129 | throw defect; |
| 130 | }), |
| 131 | ); |
| 132 | |
| 133 | const error = yield* runWith(spawner)({ |
| 134 | command: "jj", |
| 135 | args: ["--version"], |
| 136 | cwd: "/repo", |
| 137 | }).pipe(Effect.flip); |
| 138 | |
| 139 | expect(error._tag).toBe("ProcessSpawnError"); |
| 140 | if (error._tag !== "ProcessSpawnError") { |
| 141 | return expect.fail("Expected ProcessSpawnError"); |
| 142 | } |
| 143 | expect(error).toMatchObject({ command: "jj", argumentCount: 1, cwd: "/repo" }); |
| 144 | expect(error.cause).toBe(defect); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | it.effect("reports child startup before delivering cancellation during acquisition", () => |
| 149 | Effect.gen(function* () { |
| 150 | let started = false; |
| 151 | const spawner = makeSpawner(() => |
| 152 | Effect.withFiber((fiber) => |
| 153 | Effect.sync(() => { |
| 154 | fiber.interruptUnsafe(); |
| 155 | return makeHandle({}); |
| 156 | }), |
| 157 | ), |
| 158 | ); |
| 159 | const result = yield* runWith(spawner)({ |
| 160 | command: "git", |
| 161 | args: ["restore"], |
| 162 | onSpawn: () => { |
| 163 | started = true; |
| 164 | }, |
| 165 | }).pipe(Effect.exit); |
| 166 | expect(Exit.isFailure(result) && Cause.hasInterruptsOnly(result.cause)).toBe(true); |
| 167 | expect(started).toBe(true); |
| 168 | }).pipe(Effect.scoped), |
| 169 | ); |
| 170 | |
| 171 | it.effect("fails when output exceeds max buffer in default mode", () => |
| 172 | Effect.gen(function* () { |
| 173 | const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) }))); |
| 174 | |
| 175 | const error = yield* runWith(spawner)({ |
| 176 | command: "fake", |
| 177 | args: ["stdout-bytes", "2048"], |
| 178 | maxOutputBytes: 128, |
| 179 | }).pipe(Effect.flip); |
| 180 | |
| 181 | expect(error._tag).toBe("ProcessOutputLimitError"); |
| 182 | if (error._tag !== "ProcessOutputLimitError") { |
| 183 | return expect.fail("Expected ProcessOutputLimitError"); |
| 184 | } |
| 185 | expect(error).toMatchObject({ |
| 186 | stream: "stdout", |
| 187 | maxBytes: 128, |
| 188 | observedBytes: 2048, |
| 189 | }); |
| 190 | expect(error.message).toBe( |
| 191 | "Process 'fake' stdout produced 2048 bytes, exceeding the 128 byte limit", |
| 192 | ); |
| 193 | }), |
| 194 | ); |
| 195 | |
| 196 | it.effect("accepts output at the byte limit followed by an empty chunk", () => |
| 197 | Effect.gen(function* () { |
| 198 | const output = new TextEncoder().encode("exactly"); |
| 199 | const spawner = makeSpawner(() => |
| 200 | Effect.succeed( |
| 201 | makeHandle({ |
| 202 | stdout: Stream.make(output, new Uint8Array()), |
| 203 | }), |
| 204 | ), |
| 205 | ); |
| 206 | |
| 207 | const result = yield* runWith(spawner)({ |
| 208 | command: "fake", |
| 209 | args: ["exact-limit"], |
| 210 | maxOutputBytes: output.byteLength, |
| 211 | }); |
| 212 | |
| 213 | expect(result.stdout).toBe("exactly"); |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | it.effect("fails fast on output limit before timeout for long-running output", () => |
| 218 | Effect.gen(function* () { |
| 219 | const textChunk = "x".repeat(64); |
| 220 | const spawner = makeSpawner(() => |
| 221 | Effect.succeed( |
| 222 | makeHandle({ |
| 223 | stdout: Stream.fromIterable(Array.from({ length: 10 }, () => textChunk)).pipe( |
| 224 | Stream.encodeText, |
| 225 | ), |
| 226 | exitCode: Effect.never, |
| 227 | }), |
| 228 | ), |
| 229 | ); |
| 230 | |
| 231 | const error = yield* runWith(spawner)({ |
| 232 | command: "fake", |
| 233 | args: ["spam-stdout"], |
| 234 | maxOutputBytes: 128, |
| 235 | timeout: "2 seconds", |
| 236 | }).pipe(Effect.flip); |
| 237 | |
| 238 | expect(error).toBeInstanceOf(ProcessRunner.ProcessOutputLimitError); |
| 239 | }), |
| 240 | ); |
| 241 | |
| 242 | it.effect("truncates output when outputMode is truncate", () => |
| 243 | Effect.gen(function* () { |
| 244 | const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) }))); |
| 245 | |
| 246 | const result = yield* runWith(spawner)({ |
| 247 | command: "fake", |
| 248 | args: ["stdout-bytes", "2048"], |
| 249 | maxOutputBytes: 128, |
| 250 | outputMode: "truncate", |
| 251 | }); |
| 252 | |
| 253 | expect(result.code).toBe(0); |
| 254 | expect(result.stdout.length).toBeLessThanOrEqual(128); |
| 255 | expect(result.stdoutTruncated).toBe(true); |
| 256 | expect(result.stderrTruncated).toBe(false); |
| 257 | }), |
| 258 | ); |
| 259 | |
| 260 | it.effect("writes stdin before waiting for exit", () => |
| 261 | Effect.gen(function* () { |
| 262 | const stdinWritten = yield* Deferred.make<void>(); |
| 263 | const decoder = new TextDecoder(); |
| 264 | const spawner = makeSpawner(() => |
| 265 | Effect.succeed( |
| 266 | makeHandle({ |
| 267 | stdout: "stdin payload", |
| 268 | stdin: Sink.forEach((chunk: Uint8Array) => { |
| 269 | const text = decoder.decode(chunk, { stream: true }); |
| 270 | return text.includes("stdin payload") |
| 271 | ? Deferred.succeed(stdinWritten, undefined) |
| 272 | : Effect.void; |
| 273 | }), |
| 274 | exitCode: Deferred.await(stdinWritten).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))), |
| 275 | }), |
| 276 | ), |
| 277 | ); |
| 278 | |
| 279 | const result = yield* runWith(spawner)({ |
| 280 | command: "fake", |
| 281 | args: ["stdin-echo"], |
| 282 | stdin: "stdin payload", |
| 283 | }); |
| 284 | |
| 285 | expect(result.stdout).toBe("stdin payload"); |
| 286 | expect(result.code).toBe(0); |
| 287 | }), |
| 288 | ); |
| 289 | }); |
| 290 | |
| 291 | describe("isWindowsCommandNotFound", () => { |
| 292 | it.effect("matches the localized German cmd.exe error text", () => |
| 293 | Effect.gen(function* () { |
| 294 | const isCommandNotFound = yield* isWindowsCommandNotFound( |
| 295 | 1, |
| 296 | "wird nicht als interner oder externer Befehl, betriebsfahiges Programm oder Batch-Datei erkannt", |
| 297 | ).pipe(Effect.provideService(HostProcess.Platform, "win32")); |
| 298 | expect(isCommandNotFound).toBe(true); |
| 299 | }), |
| 300 | ); |
| 301 | }); |