|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createPosixTools } from "@intx/tools-posix"; |
| 6 | +import { createToolRunner } from "@intx/agent"; |
| 7 | +import type { AgentTool } from "@intx/agent"; |
| 8 | + |
| 9 | +import { createCodexToolProxies, type CodexRunTool } from "./codex-tool-proxies.js"; |
| 10 | +import { buildCorePosixToolPlugins } from "./posix-tool-plugins.js"; |
| 11 | +import { createPermissionGate } from "../permission/gate.js"; |
| 12 | + |
| 13 | +/** |
| 14 | + * apply_patch forwards each op through the same posixTools.run chain the rest |
| 15 | + * of the agent uses (see tools.ts), so verify-plugin's and delete-file-plugin's |
| 16 | + * diffs surface here too without any apply_patch-specific plumbing. |
| 17 | + */ |
| 18 | +async function invokeApplyPatch(tools: AgentTool[], input: string) { |
| 19 | + const runner = createToolRunner(tools); |
| 20 | + return runner.run( |
| 21 | + { id: "call-1", name: "apply_patch", arguments: { input } }, |
| 22 | + new AbortController().signal, |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +async function makeApplyPatch(cwd: string): Promise<AgentTool[]> { |
| 27 | + const gate = createPermissionGate({ |
| 28 | + approvals: [], |
| 29 | + interactive: false, |
| 30 | + skipPermissions: true, |
| 31 | + auto: false, |
| 32 | + cwd, |
| 33 | + }); |
| 34 | + const posixTools = createPosixTools({ |
| 35 | + cwd, |
| 36 | + plugins: buildCorePosixToolPlugins({ cwd, permissionGate: gate }), |
| 37 | + }); |
| 38 | + const runTool: CodexRunTool = async (name, args) => { |
| 39 | + // read_file's real tool output is cat -n formatted (line numbers), which |
| 40 | + // is not the raw content applyUpdateHunks needs — in production this |
| 41 | + // means Update File hunks generally fail to match context (filed as |
| 42 | + // CL-6966, Urgent; not this issue's bug to fix). This stub bypasses that |
| 43 | + // known defect by returning raw content, so the "Update File shows the |
| 44 | + // diff" test below is NOT proof that apply_patch Update works end to end |
| 45 | + // — it only proves the diff-surfacing added here is correct once the op |
| 46 | + // succeeds. Add File / Delete File below do not depend on read_file and |
| 47 | + // are real, unstubbed coverage. |
| 48 | + if (name === "read_file") { |
| 49 | + const path = String((args as { path?: unknown }).path ?? ""); |
| 50 | + try { |
| 51 | + return { content: await readFile(join(cwd, path), "utf8") }; |
| 52 | + } catch (err) { |
| 53 | + return { content: err instanceof Error ? err.message : String(err), isError: true }; |
| 54 | + } |
| 55 | + } |
| 56 | + const result = await posixTools.run( |
| 57 | + { id: "codex-proxy", name, arguments: args }, |
| 58 | + new AbortController().signal, |
| 59 | + ); |
| 60 | + return { |
| 61 | + content: typeof result.content === "string" ? result.content : JSON.stringify(result.content), |
| 62 | + ...(result.isError === true ? { isError: true } : {}), |
| 63 | + }; |
| 64 | + }; |
| 65 | + return createCodexToolProxies({ |
| 66 | + isCodex: true, |
| 67 | + runTool, |
| 68 | + runManageTasks: async () => ({ content: "ok" }), |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +describe("apply_patch surfaces the changed region", () => { |
| 73 | + test("Update File shows the diff", async () => { |
| 74 | + const cwd = await mkdtemp(join(tmpdir(), "apply-patch-diff-")); |
| 75 | + try { |
| 76 | + await writeFile(join(cwd, "a.txt"), "line1\nworld\nline3\n"); |
| 77 | + const tools = await makeApplyPatch(cwd); |
| 78 | + const input = [ |
| 79 | + "*** Begin Patch", |
| 80 | + "*** Update File: a.txt", |
| 81 | + "@@", |
| 82 | + " line1", |
| 83 | + "-world", |
| 84 | + "+universe", |
| 85 | + " line3", |
| 86 | + "*** End Patch", |
| 87 | + ].join("\n"); |
| 88 | + |
| 89 | + const result = await invokeApplyPatch(tools, input); |
| 90 | + |
| 91 | + expect(result.isError).not.toBe(true); |
| 92 | + expect(String(result.content)).toContain("-world"); |
| 93 | + expect(String(result.content)).toContain("+universe"); |
| 94 | + } finally { |
| 95 | + await rm(cwd, { recursive: true, force: true }); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + test("Delete File shows the removed content", async () => { |
| 100 | + const cwd = await mkdtemp(join(tmpdir(), "apply-patch-diff-")); |
| 101 | + try { |
| 102 | + await writeFile(join(cwd, "gone.txt"), "bye\n"); |
| 103 | + const tools = await makeApplyPatch(cwd); |
| 104 | + const input = ["*** Begin Patch", "*** Delete File: gone.txt", "*** End Patch"].join("\n"); |
| 105 | + |
| 106 | + const result = await invokeApplyPatch(tools, input); |
| 107 | + |
| 108 | + expect(result.isError).not.toBe(true); |
| 109 | + expect(String(result.content)).toContain("-bye"); |
| 110 | + } finally { |
| 111 | + await rm(cwd, { recursive: true, force: true }); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + test("Add File shows the added content", async () => { |
| 116 | + const cwd = await mkdtemp(join(tmpdir(), "apply-patch-diff-")); |
| 117 | + try { |
| 118 | + const tools = await makeApplyPatch(cwd); |
| 119 | + const input = ["*** Begin Patch", "*** Add File: new.txt", "+hello", "*** End Patch"].join( |
| 120 | + "\n", |
| 121 | + ); |
| 122 | + |
| 123 | + const result = await invokeApplyPatch(tools, input); |
| 124 | + |
| 125 | + expect(result.isError).not.toBe(true); |
| 126 | + expect(String(result.content)).toContain("+hello"); |
| 127 | + } finally { |
| 128 | + await rm(cwd, { recursive: true, force: true }); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments