|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + extractCloudFileContent, |
| 5 | + type ParsedToolCall, |
| 6 | +} from "./cloudToolChanges"; |
| 7 | + |
| 8 | +function toolCall(overrides: Partial<ParsedToolCall>): ParsedToolCall { |
| 9 | + return { |
| 10 | + toolCallId: overrides.toolCallId ?? "tc-1", |
| 11 | + kind: overrides.kind ?? null, |
| 12 | + title: overrides.title, |
| 13 | + status: overrides.status ?? "completed", |
| 14 | + locations: overrides.locations, |
| 15 | + content: overrides.content, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function textContent(text: string): ParsedToolCall["content"] { |
| 20 | + return [{ type: "content", content: { type: "text", text } }]; |
| 21 | +} |
| 22 | + |
| 23 | +function diffContent( |
| 24 | + path: string, |
| 25 | + newText: string, |
| 26 | + oldText?: string, |
| 27 | +): ParsedToolCall["content"] { |
| 28 | + return [{ type: "diff", path, newText, oldText: oldText ?? null }]; |
| 29 | +} |
| 30 | + |
| 31 | +function makeToolCalls( |
| 32 | + ...calls: ParsedToolCall[] |
| 33 | +): Map<string, ParsedToolCall> { |
| 34 | + return new Map(calls.map((tc, i) => [tc.toolCallId || `tc-${i}`, tc])); |
| 35 | +} |
| 36 | + |
| 37 | +describe("extractCloudFileContent", () => { |
| 38 | + it("returns untouched for an empty tool calls map", () => { |
| 39 | + const result = extractCloudFileContent(new Map(), "src/app.ts"); |
| 40 | + expect(result).toEqual({ content: null, touched: false }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns untouched when no tool call matches the file", () => { |
| 44 | + const calls = makeToolCalls( |
| 45 | + toolCall({ |
| 46 | + kind: "read", |
| 47 | + locations: [{ path: "src/other.ts" }], |
| 48 | + content: textContent("other content"), |
| 49 | + }), |
| 50 | + ); |
| 51 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 52 | + expect(result).toEqual({ content: null, touched: false }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("extracts content from a read tool call", () => { |
| 56 | + const calls = makeToolCalls( |
| 57 | + toolCall({ |
| 58 | + kind: "read", |
| 59 | + locations: [{ path: "src/app.ts" }], |
| 60 | + content: textContent("file content"), |
| 61 | + }), |
| 62 | + ); |
| 63 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 64 | + expect(result).toEqual({ content: "file content", touched: true }); |
| 65 | + }); |
| 66 | + |
| 67 | + it("extracts content from a write tool call", () => { |
| 68 | + const calls = makeToolCalls( |
| 69 | + toolCall({ |
| 70 | + kind: "write", |
| 71 | + locations: [{ path: "src/app.ts" }], |
| 72 | + content: diffContent("src/app.ts", "new content"), |
| 73 | + }), |
| 74 | + ); |
| 75 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 76 | + expect(result).toEqual({ content: "new content", touched: true }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("extracts content from an edit tool call", () => { |
| 80 | + const calls = makeToolCalls( |
| 81 | + toolCall({ |
| 82 | + kind: "edit", |
| 83 | + locations: [{ path: "src/app.ts" }], |
| 84 | + content: diffContent("src/app.ts", "edited content", "old content"), |
| 85 | + }), |
| 86 | + ); |
| 87 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 88 | + expect(result).toEqual({ content: "edited content", touched: true }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("marks deleted files as touched with null content", () => { |
| 92 | + const calls = makeToolCalls( |
| 93 | + toolCall({ |
| 94 | + toolCallId: "tc-read", |
| 95 | + kind: "read", |
| 96 | + locations: [{ path: "src/app.ts" }], |
| 97 | + content: textContent("original"), |
| 98 | + }), |
| 99 | + toolCall({ |
| 100 | + toolCallId: "tc-delete", |
| 101 | + kind: "delete", |
| 102 | + locations: [{ path: "src/app.ts" }], |
| 103 | + }), |
| 104 | + ); |
| 105 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 106 | + expect(result).toEqual({ content: null, touched: true }); |
| 107 | + }); |
| 108 | + |
| 109 | + it("uses the latest content when multiple tool calls touch the same file", () => { |
| 110 | + const calls = makeToolCalls( |
| 111 | + toolCall({ |
| 112 | + toolCallId: "tc-read", |
| 113 | + kind: "read", |
| 114 | + locations: [{ path: "src/app.ts" }], |
| 115 | + content: textContent("v1"), |
| 116 | + }), |
| 117 | + toolCall({ |
| 118 | + toolCallId: "tc-edit", |
| 119 | + kind: "edit", |
| 120 | + locations: [{ path: "src/app.ts" }], |
| 121 | + content: diffContent("src/app.ts", "v2", "v1"), |
| 122 | + }), |
| 123 | + ); |
| 124 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 125 | + expect(result).toEqual({ content: "v2", touched: true }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("skips failed tool calls", () => { |
| 129 | + const calls = makeToolCalls( |
| 130 | + toolCall({ |
| 131 | + kind: "write", |
| 132 | + status: "failed", |
| 133 | + locations: [{ path: "src/app.ts" }], |
| 134 | + content: diffContent("src/app.ts", "bad content"), |
| 135 | + }), |
| 136 | + ); |
| 137 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 138 | + expect(result).toEqual({ content: null, touched: false }); |
| 139 | + }); |
| 140 | + |
| 141 | + it("matches absolute paths against relative paths", () => { |
| 142 | + const calls = makeToolCalls( |
| 143 | + toolCall({ |
| 144 | + kind: "read", |
| 145 | + locations: [{ path: "/home/user/project/src/app.ts" }], |
| 146 | + content: textContent("absolute match"), |
| 147 | + }), |
| 148 | + ); |
| 149 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 150 | + expect(result).toEqual({ content: "absolute match", touched: true }); |
| 151 | + }); |
| 152 | + |
| 153 | + it("infers kind from title when kind is not set", () => { |
| 154 | + const calls = makeToolCalls( |
| 155 | + toolCall({ |
| 156 | + kind: null, |
| 157 | + title: "Write src/app.ts", |
| 158 | + locations: [{ path: "src/app.ts" }], |
| 159 | + content: diffContent("src/app.ts", "inferred write"), |
| 160 | + }), |
| 161 | + ); |
| 162 | + const result = extractCloudFileContent(calls, "src/app.ts"); |
| 163 | + expect(result).toEqual({ content: "inferred write", touched: true }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe("move operations", () => { |
| 167 | + it("marks file as touched when looking up the source path", () => { |
| 168 | + const calls = makeToolCalls( |
| 169 | + toolCall({ |
| 170 | + kind: "move", |
| 171 | + locations: [{ path: "src/old.ts" }, { path: "src/new.ts" }], |
| 172 | + }), |
| 173 | + ); |
| 174 | + const result = extractCloudFileContent(calls, "src/old.ts"); |
| 175 | + expect(result).toEqual({ content: null, touched: true }); |
| 176 | + }); |
| 177 | + |
| 178 | + it("marks file as touched when looking up the destination path", () => { |
| 179 | + const calls = makeToolCalls( |
| 180 | + toolCall({ |
| 181 | + kind: "move", |
| 182 | + locations: [{ path: "src/old.ts" }, { path: "src/new.ts" }], |
| 183 | + }), |
| 184 | + ); |
| 185 | + const result = extractCloudFileContent(calls, "src/new.ts"); |
| 186 | + expect(result).toEqual({ content: null, touched: true }); |
| 187 | + }); |
| 188 | + |
| 189 | + it("extracts content from move with diff", () => { |
| 190 | + const calls = makeToolCalls( |
| 191 | + toolCall({ |
| 192 | + kind: "move", |
| 193 | + locations: [{ path: "src/old.ts" }, { path: "src/new.ts" }], |
| 194 | + content: diffContent("src/new.ts", "moved content"), |
| 195 | + }), |
| 196 | + ); |
| 197 | + const result = extractCloudFileContent(calls, "src/new.ts"); |
| 198 | + expect(result).toEqual({ content: "moved content", touched: true }); |
| 199 | + }); |
| 200 | + |
| 201 | + it("does not match unrelated paths for move", () => { |
| 202 | + const calls = makeToolCalls( |
| 203 | + toolCall({ |
| 204 | + kind: "move", |
| 205 | + locations: [{ path: "src/old.ts" }, { path: "src/new.ts" }], |
| 206 | + }), |
| 207 | + ); |
| 208 | + const result = extractCloudFileContent(calls, "src/other.ts"); |
| 209 | + expect(result).toEqual({ content: null, touched: false }); |
| 210 | + }); |
| 211 | + }); |
| 212 | +}); |
0 commit comments