The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Schema from "effect/Schema"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | chunkTerminalWrite, |
| 6 | DEFAULT_TERMINAL_ID, |
| 7 | TERMINAL_WRITE_MAX_LENGTH, |
| 8 | TerminalError, |
| 9 | TerminalOpenInput, |
| 10 | TerminalProviderEnvironmentError, |
| 11 | } from "./terminal.ts"; |
| 12 | import { ProviderInstanceId } from "./providerInstance.ts"; |
| 13 | |
| 14 | const encodeTerminalError = Schema.encodeUnknownSync(TerminalError); |
| 15 | const decodeTerminalError = Schema.decodeUnknownSync(TerminalError); |
| 16 | |
| 17 | function decodeSync<S extends Schema.Top>(schema: S, input: unknown): Schema.Schema.Type<S> { |
| 18 | return Schema.decodeUnknownSync(schema as never)(input) as Schema.Schema.Type<S>; |
| 19 | } |
| 20 | |
| 21 | describe("TerminalProviderEnvironmentError", () => { |
| 22 | it("round-trips its required cause without exposing it in the message", () => { |
| 23 | const cause = { operation: "read-secret", detail: "secret backend unavailable" }; |
| 24 | const error = new TerminalProviderEnvironmentError({ |
| 25 | providerInstanceId: ProviderInstanceId.make("codex_work"), |
| 26 | cause, |
| 27 | }); |
| 28 | const encoded = encodeTerminalError(error); |
| 29 | const decoded = decodeTerminalError(encoded); |
| 30 | |
| 31 | expect(decoded).toMatchObject({ |
| 32 | _tag: "TerminalProviderEnvironmentError", |
| 33 | providerInstanceId: "codex_work", |
| 34 | cause, |
| 35 | }); |
| 36 | expect(decoded.message).toBe( |
| 37 | "Could not prepare the terminal environment for provider instance: codex_work", |
| 38 | ); |
| 39 | expect(decoded.message).not.toContain("secret backend unavailable"); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | describe("TerminalOpenInput", () => { |
| 44 | it("drops invalid env keys while retaining valid overrides", () => { |
| 45 | const parsed = decodeSync(TerminalOpenInput, { |
| 46 | threadId: "thread-1", |
| 47 | terminalId: DEFAULT_TERMINAL_ID, |
| 48 | cwd: "/tmp/project", |
| 49 | env: { "bad-key": "1", GOOD_KEY: "2" }, |
| 50 | }); |
| 51 | expect(parsed.env).toEqual({ GOOD_KEY: "2" }); |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | describe("chunkTerminalWrite", () => { |
| 56 | it("splits oversized writes so every chunk fits the contract", () => { |
| 57 | const chunks = chunkTerminalWrite("y".repeat(TERMINAL_WRITE_MAX_LENGTH * 2 + 5)); |
| 58 | expect(chunks.map((chunk) => chunk.length)).toEqual([ |
| 59 | TERMINAL_WRITE_MAX_LENGTH, |
| 60 | TERMINAL_WRITE_MAX_LENGTH, |
| 61 | 5, |
| 62 | ]); |
| 63 | expect(chunks.join("")).toHaveLength(TERMINAL_WRITE_MAX_LENGTH * 2 + 5); |
| 64 | }); |
| 65 | |
| 66 | it("does not cut a surrogate pair in half at the boundary", () => { |
| 67 | const data = `${"z".repeat(TERMINAL_WRITE_MAX_LENGTH - 1)}😀tail`; |
| 68 | const chunks = chunkTerminalWrite(data); |
| 69 | expect(chunks[0]).toHaveLength(TERMINAL_WRITE_MAX_LENGTH - 1); |
| 70 | expect(chunks[1]).toBe("😀tail"); |
| 71 | expect(chunks.join("")).toBe(data); |
| 72 | }); |
| 73 | }); |