import * as NodeServices from "@effect/platform-node/NodeServices"; import { it } from "@effect/vitest"; import { Effect, FileSystem, Layer, Path } from "effect"; import { expect } from "vitest"; import { CodexTextGenerationLive } from "./CodexTextGeneration.ts"; import { TextGenerationError } from "../Errors.ts"; import { TextGeneration } from "../Services/TextGeneration.ts"; function makeFakeCodexBinary(dir: string) { return Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const binDir = path.join(dir, "bin"); const codexPath = path.join(binDir, "codex"); yield* fs.makeDirectory(binDir, { recursive: true }); yield* fs.writeFileString( codexPath, [ "#!/bin/sh", 'output_path=""', "while [ $# -gt 0 ]; do", ' if [ "$1" = "--image" ]; then', " shift", ' if [ -n "$1" ]; then', ' seen_image="1"', " fi", " continue", " fi", ' if [ "$1" = "--output-last-message" ]; then', " shift", ' output_path="$1"', " fi", " shift", "done", 'if [ "$T3_FAKE_CODEX_REQUIRE_IMAGE" = "1" ] && [ "$seen_image" != "1" ]; then', ' printf "%s\\n" "missing --image input" >&2', " exit 2", "fi", 'stdin_content="$(cat)"', 'if [ -n "$T3_FAKE_CODEX_STDIN_MUST_CONTAIN" ]; then', ' printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CODEX_STDIN_MUST_CONTAIN" >/dev/null || {', ' printf "%s\\n" "stdin missing expected content" >&2', " exit 3", " }", "fi", 'if [ -n "$T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN" ]; then', ' if printf "%s" "$stdin_content" | grep -F -- "$T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN" >/dev/null; then', ' printf "%s\\n" "stdin contained forbidden content" >&2', " exit 4", " fi", "fi", 'if [ -n "$T3_FAKE_CODEX_STDERR" ]; then', ' printf "%s\\n" "$T3_FAKE_CODEX_STDERR" >&2', "fi", 'if [ -n "$output_path" ]; then', ' node -e \'const fs=require("node:fs"); const value=process.argv[2] ?? ""; fs.writeFileSync(process.argv[1], Buffer.from(value, "base64"));\' "$output_path" "${T3_FAKE_CODEX_OUTPUT_B64:-e30=}"', "fi", 'exit "${T3_FAKE_CODEX_EXIT_CODE:-0}"', "", ].join("\n"), ); yield* fs.chmod(codexPath, 0o755); return binDir; }); } function withFakeCodexEnv( input: { output: string; exitCode?: number; stderr?: string; requireImage?: boolean; stdinMustContain?: string; stdinMustNotContain?: string; }, effect: Effect.Effect, ) { return Effect.acquireUseRelease( Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-codex-text-" }); const binDir = yield* makeFakeCodexBinary(tempDir); const previousPath = process.env.PATH; const previousOutput = process.env.T3_FAKE_CODEX_OUTPUT_B64; const previousExitCode = process.env.T3_FAKE_CODEX_EXIT_CODE; const previousStderr = process.env.T3_FAKE_CODEX_STDERR; const previousRequireImage = process.env.T3_FAKE_CODEX_REQUIRE_IMAGE; const previousStdinMustContain = process.env.T3_FAKE_CODEX_STDIN_MUST_CONTAIN; const previousStdinMustNotContain = process.env.T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN; yield* Effect.sync(() => { process.env.PATH = `${binDir}:${previousPath ?? ""}`; process.env.T3_FAKE_CODEX_OUTPUT_B64 = Buffer.from(input.output, "utf8").toString("base64"); if (input.exitCode !== undefined) { process.env.T3_FAKE_CODEX_EXIT_CODE = String(input.exitCode); } else { delete process.env.T3_FAKE_CODEX_EXIT_CODE; } if (input.stderr !== undefined) { process.env.T3_FAKE_CODEX_STDERR = input.stderr; } else { delete process.env.T3_FAKE_CODEX_STDERR; } if (input.requireImage) { process.env.T3_FAKE_CODEX_REQUIRE_IMAGE = "1"; } else { delete process.env.T3_FAKE_CODEX_REQUIRE_IMAGE; } if (input.stdinMustContain !== undefined) { process.env.T3_FAKE_CODEX_STDIN_MUST_CONTAIN = input.stdinMustContain; } else { delete process.env.T3_FAKE_CODEX_STDIN_MUST_CONTAIN; } if (input.stdinMustNotContain !== undefined) { process.env.T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN = input.stdinMustNotContain; } else { delete process.env.T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN; } }); return { previousPath, previousOutput, previousExitCode, previousStderr, previousRequireImage, previousStdinMustContain, previousStdinMustNotContain, }; }), () => effect, (previous) => Effect.sync(() => { process.env.PATH = previous.previousPath; if (previous.previousOutput === undefined) { delete process.env.T3_FAKE_CODEX_OUTPUT_B64; } else { process.env.T3_FAKE_CODEX_OUTPUT_B64 = previous.previousOutput; } if (previous.previousExitCode === undefined) { delete process.env.T3_FAKE_CODEX_EXIT_CODE; } else { process.env.T3_FAKE_CODEX_EXIT_CODE = previous.previousExitCode; } if (previous.previousStderr === undefined) { delete process.env.T3_FAKE_CODEX_STDERR; } else { process.env.T3_FAKE_CODEX_STDERR = previous.previousStderr; } if (previous.previousRequireImage === undefined) { delete process.env.T3_FAKE_CODEX_REQUIRE_IMAGE; } else { process.env.T3_FAKE_CODEX_REQUIRE_IMAGE = previous.previousRequireImage; } if (previous.previousStdinMustContain === undefined) { delete process.env.T3_FAKE_CODEX_STDIN_MUST_CONTAIN; } else { process.env.T3_FAKE_CODEX_STDIN_MUST_CONTAIN = previous.previousStdinMustContain; } if (previous.previousStdinMustNotContain === undefined) { delete process.env.T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN; } else { process.env.T3_FAKE_CODEX_STDIN_MUST_NOT_CONTAIN = previous.previousStdinMustNotContain; } }), ); } const CodexTextGenerationTestLayer = Layer.provideMerge( CodexTextGenerationLive, NodeServices.layer, ); it.layer(CodexTextGenerationTestLayer)("CodexTextGenerationLive", (it) => { it.effect("generates and sanitizes commit messages", () => withFakeCodexEnv( { output: JSON.stringify({ subject: " Add important change to the system with too much detail and a trailing period.\nsecondary line", body: "\n- added migration\n- updated tests\n", }), }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generateCommitMessage({ cwd: process.cwd(), branch: "feature/codex-effect", stagedSummary: "M README.md", stagedPatch: "diff --git a/README.md b/README.md", }); expect(generated.subject.length).toBeLessThanOrEqual(72); expect(generated.subject.endsWith(".")).toBe(false); expect(generated.body).toBe("- added migration\n- updated tests"); }), ), ); it.effect("generates PR content and trims markdown body", () => withFakeCodexEnv( { output: JSON.stringify({ title: " Improve orchestration flow\nwith ignored suffix", body: "\n## Summary\n- improve flow\n\n## Testing\n- bun test\n\n", }), }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generatePrContent({ cwd: process.cwd(), baseBranch: "main", headBranch: "feature/codex-effect", commitSummary: "feat: improve orchestration flow", diffSummary: "2 files changed", diffPatch: "diff --git a/a.ts b/a.ts", }); expect(generated.title).toBe("Improve orchestration flow"); expect(generated.body.startsWith("## Summary")).toBe(true); expect(generated.body.endsWith("\n\n")).toBe(false); }), ), ); it.effect("generates branch names and normalizes branch fragments", () => withFakeCodexEnv( { output: JSON.stringify({ branch: " Feat/Session ", }), stdinMustNotContain: "Image attachments supplied to the model", }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generateBranchName({ cwd: process.cwd(), message: "Please update session handling.", }); expect(generated.branch).toBe("feat/session"); }), ), ); it.effect("omits attachment metadata section when no attachments are provided", () => withFakeCodexEnv( { output: JSON.stringify({ branch: "fix/session-timeout", }), stdinMustNotContain: "Attachment metadata:", }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generateBranchName({ cwd: process.cwd(), message: "Fix timeout behavior.", }); expect(generated.branch).toBe("fix/session-timeout"); }), ), ); it.effect("passes image attachments through as codex image inputs", () => withFakeCodexEnv( { output: JSON.stringify({ branch: "fix/ui-regression", }), requireImage: true, stdinMustContain: "Attachment metadata:", }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generateBranchName({ cwd: process.cwd(), message: "Fix layout bug from screenshot.", attachments: [ { type: "image", name: "bug.png", mimeType: "image/png", sizeBytes: 5, dataUrl: "data:image/png;base64,SGVsbG8=", }, ], }); expect(generated.branch).toBe("fix/ui-regression"); }), ), ); it.effect("returns null branch when codex returns the wrong object shape", () => withFakeCodexEnv( { output: JSON.stringify({ title: "This is not a branch payload", }), }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const generated = yield* textGeneration.generateBranchName({ cwd: process.cwd(), message: "Fix websocket reconnect flake", }); expect(generated.branch).toBeNull(); }), ), ); it.effect("returns typed TextGenerationError when codex exits non-zero", () => withFakeCodexEnv( { output: JSON.stringify({ subject: "ignored", body: "" }), exitCode: 1, stderr: "codex execution failed", }, Effect.gen(function* () { const textGeneration = yield* TextGeneration; const result = yield* textGeneration .generateCommitMessage({ cwd: process.cwd(), branch: "feature/codex-error", stagedSummary: "M README.md", stagedPatch: "diff --git a/README.md b/README.md", }) .pipe( Effect.match({ onFailure: (error) => ({ _tag: "Left" as const, left: error }), onSuccess: (value) => ({ _tag: "Right" as const, right: value }), }), ); expect(result._tag).toBe("Left"); if (result._tag === "Left") { expect(result.left).toBeInstanceOf(TextGenerationError); expect(result.left.message).toContain("Codex CLI command failed: codex execution failed"); } }), ), ); });