|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { extractEditDiff, isEditToolUpdate } from "./editDiff"; |
| 3 | + |
| 4 | +describe("isEditToolUpdate", () => { |
| 5 | + it("detects Edit kind and edit-class tool names", () => { |
| 6 | + // ACP ToolKind serializes lowercase on the wire ("kind":"edit"). |
| 7 | + expect(isEditToolUpdate({ kind: "edit", title: "Edit `src/a.ts`" })).toBe(true); |
| 8 | + expect(isEditToolUpdate({ kind: "Edit", title: "Edit `src/a.ts`" })).toBe(true); |
| 9 | + expect(isEditToolUpdate({ kind: "read", title: "Read `src/a.ts`" })).toBe(false); |
| 10 | + expect( |
| 11 | + isEditToolUpdate({ |
| 12 | + kind: "Other", |
| 13 | + _meta: { "x.ai/tool": { name: "search_replace" } }, |
| 14 | + }), |
| 15 | + ).toBe(true); |
| 16 | + expect( |
| 17 | + isEditToolUpdate({ |
| 18 | + kind: "Other", |
| 19 | + _meta: { "x.ai/tool": { name: "apply_patch" } }, |
| 20 | + }), |
| 21 | + ).toBe(true); |
| 22 | + expect( |
| 23 | + isEditToolUpdate({ |
| 24 | + kind: "Other", |
| 25 | + _meta: { "x.ai/tool": { name: "run_terminal_command" } }, |
| 26 | + }), |
| 27 | + ).toBe(false); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("extractEditDiff", () => { |
| 32 | + it("builds a unified diff from SearchReplace rawOutput details", () => { |
| 33 | + const diff = extractEditDiff({ |
| 34 | + kind: "Edit", |
| 35 | + title: "Edit `src/main.ts`", |
| 36 | + rawOutput: { |
| 37 | + type: "SearchReplace", |
| 38 | + EditsApplied: { |
| 39 | + old_string: "let x = 1;", |
| 40 | + new_string: "let x = 2;", |
| 41 | + absolute_path: "/tmp/main.ts", |
| 42 | + edits: { |
| 43 | + details: [ |
| 44 | + { |
| 45 | + old_string: "let x = 1;", |
| 46 | + new_string: "let x = 2;", |
| 47 | + old_line: 5, |
| 48 | + new_line: 5, |
| 49 | + context_before: "fn main() {\n", |
| 50 | + context_after: "}", |
| 51 | + line_prefix: "", |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }); |
| 58 | + // Hunk starts at the first shown old line — the context line at 4. |
| 59 | + expect(diff).toContain("@@ -4,3 +4,3 @@"); |
| 60 | + expect(diff).toContain("-let x = 1;"); |
| 61 | + expect(diff).toContain("+let x = 2;"); |
| 62 | + expect(diff).toContain(" fn main() {"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("accepts snake_case raw_output too (disk hydrate tolerance)", () => { |
| 66 | + const diff = extractEditDiff({ |
| 67 | + kind: "Edit", |
| 68 | + raw_output: { |
| 69 | + type: "SearchReplace", |
| 70 | + EditsApplied: { |
| 71 | + edits: { |
| 72 | + details: [ |
| 73 | + { |
| 74 | + old_string: "a", |
| 75 | + new_string: "b", |
| 76 | + old_line: 1, |
| 77 | + new_line: 1, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }); |
| 84 | + expect(diff).toContain("-a"); |
| 85 | + expect(diff).toContain("+b"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("falls back to content diff blocks (write tool full content)", () => { |
| 89 | + const diff = extractEditDiff({ |
| 90 | + kind: "Edit", |
| 91 | + title: "Write `src/out.txt`", |
| 92 | + content: [ |
| 93 | + { |
| 94 | + type: "diff", |
| 95 | + path: "src/out.txt", |
| 96 | + new_text: "hello\nworld\n", |
| 97 | + }, |
| 98 | + ], |
| 99 | + }); |
| 100 | + // Pure insertion: zero old lines ("-1,0"), two new lines. |
| 101 | + expect(diff).toContain("@@ -1,0 +1,2 @@"); |
| 102 | + expect(diff).toContain("+hello"); |
| 103 | + expect(diff).toContain("+world"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("renders replacements against the old text via LCS alignment", () => { |
| 107 | + const diff = extractEditDiff({ |
| 108 | + kind: "Edit", |
| 109 | + content: [ |
| 110 | + { |
| 111 | + type: "diff", |
| 112 | + path: "x.txt", |
| 113 | + old_text: "keep\nremove\nkeep2\n", |
| 114 | + new_text: "keep\nadded\nkeep2\n", |
| 115 | + }, |
| 116 | + ], |
| 117 | + }); |
| 118 | + expect(diff).toContain("-remove"); |
| 119 | + expect(diff).toContain("+added"); |
| 120 | + expect(diff).toContain(" keep"); |
| 121 | + }); |
| 122 | + |
| 123 | + it("returns undefined for non-edit output", () => { |
| 124 | + expect( |
| 125 | + extractEditDiff({ kind: "Read", rawOutput: { type: "ReadFile" } }), |
| 126 | + ).toBeUndefined(); |
| 127 | + expect( |
| 128 | + extractEditDiff({ |
| 129 | + kind: "Edit", |
| 130 | + rawOutput: { type: "Bash", output: [], exit_code: 0 }, |
| 131 | + }), |
| 132 | + ).toBeUndefined(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments