import { access, stat } from "node:fs/promises"; import { afterEach, describe, expect, it } from "vitest"; import { type ChildRuntime, createChildRuntime } from "../src/runtime/index"; const runtimes: ChildRuntime[] = []; const originalEnvironment = { ...process.env }; afterEach(async () => { await Promise.all(runtimes.splice(0).map((runtime) => runtime.cleanup())); for (const key of Object.keys(process.env)) { if (!(key in originalEnvironment)) delete process.env[key]; } Object.assign(process.env, originalEnvironment); }); describe("Klaus isolated Claude child runtime", () => { it("builds a query-private allowlisted environment and filters headers", async () => { process.env.ANTHROPIC_API_KEY = "blocked"; process.env.ANTHROPIC_CUSTOM_HEADERS = "x-ambient: blocked"; process.env.CLAUDE_CODE_OAUTH_TOKEN = "blocked"; process.env.AWS_SECRET_ACCESS_KEY = "blocked"; process.env.UNRELATED_KLAUS_TEST = "blocked"; process.env.KLAUS_E2E_BASE_URL = "http://127.0.0.1:43210"; const runtime = await createChildRuntime( "fake", { "X-Custom": "present", "X-Empty": "", "X-Null": null, Authorization: "Bearer hostile", "x-api-key": "hostile", Host: "hostile.example", "Content-Type": "application/hostile", "x-klaus-private": "must-not-pass", "X-KLAUS-UPPER": "must-not-pass", "bad header": "must-not-pass", "X-Bad-Value": "line one\nline two", "anthropic-beta": "unknown-beta, context-1m-2025-08-07, another-beta", }, { PATH: process.env.PATH ?? "", LANG: "klaus-test-locale", UNRELATED_KLAUS_TEST: "blocked", ANTHROPIC_API_KEY: "blocked", }, ); runtimes.push(runtime); expect(runtime.executable).toBeTypeOf("string"); if (!runtime.executable) throw new Error("Klaus runtime executable missing."); expect((await stat(runtime.executable)).isFile()).toBe(true); expect(runtime.env).toMatchObject({ PATH: process.env.PATH ?? "", LANG: "klaus-test-locale", HOME: runtime.configDir, CLAUDE_CONFIG_DIR: runtime.configDir, CLAUDE_CODE_OAUTH_TOKEN: "fake", CLAUDE_AGENT_SDK_CLIENT_APP: "klaus/1", CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: "0", ANTHROPIC_BASE_URL: "http://127.0.0.1:43210", ANTHROPIC_CUSTOM_HEADERS: "X-Custom: present\nX-Empty: ", }); for (const forbidden of [ "ANTHROPIC_API_KEY", "AWS_SECRET_ACCESS_KEY", "UNRELATED_KLAUS_TEST", ]) { expect(runtime.env).not.toHaveProperty(forbidden); } expect(runtime.betas).toEqual(["context-1m-2025-08-07"]); expect(runtime.env.ANTHROPIC_CUSTOM_HEADERS).not.toMatch( /auth|hostile|klaus|bad|ambient/i, ); }, 60_000); it("creates unique config directories and removes only its own directory", async () => { const first = await createChildRuntime("token-one", {}, {}); const second = await createChildRuntime("token-two", {}, {}); runtimes.push(second); expect(first.configDir).not.toBe(second.configDir); expect(first.env.HOME).toBe(first.configDir); expect(second.env.HOME).toBe(second.configDir); await first.cleanup(); await expect(access(first.configDir)).rejects.toMatchObject({ code: "ENOENT", }); await expect(access(second.configDir)).resolves.toBeUndefined(); }, 60_000); });