-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathChatRow.diff-actions.spec.tsx
More file actions
248 lines (214 loc) · 7.55 KB
/
Copy pathChatRow.diff-actions.spec.tsx
File metadata and controls
248 lines (214 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React from "react"
import { fireEvent, render, screen } from "@/utils/test-utils"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import type { ClineMessage } from "@roo-code/types"
import { ExtensionStateContextProvider } from "@src/context/ExtensionStateContext"
import { ChatRowContent } from "../ChatRow"
const mockPostMessage = vi.fn()
vi.mock("@src/utils/vscode", () => ({
vscode: {
postMessage: (...args: unknown[]) => mockPostMessage(...args),
},
}))
// Mock i18n
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string) => {
const map: Record<string, string> = {
"chat:fileOperations.wantsToEdit": "Roo wants to edit this file",
"chat:fileOperations.wantsToEditProtected": "Roo wants to edit a protected file",
"chat:fileOperations.wantsToEditOutsideWorkspace": "Roo wants to edit outside workspace",
"chat:fileOperations.wantsToApplyBatchChanges": "Roo wants to apply batch changes",
}
return map[key] || key
},
}),
Trans: ({ children }: { children?: React.ReactNode }) => <>{children}</>,
initReactI18next: { type: "3rdParty", init: () => {} },
}))
// Mock CodeBlock (avoid ESM/highlighter costs)
vi.mock("@src/components/common/CodeBlock", () => ({
default: () => null,
}))
const queryClient = new QueryClient()
function createToolAskMessage(toolPayload: Record<string, unknown>): ClineMessage {
return {
type: "ask",
ask: "tool",
ts: Date.now(),
partial: false,
text: JSON.stringify(toolPayload),
}
}
function renderChatRow(message: ClineMessage, isExpanded = false) {
return render(
<ExtensionStateContextProvider>
<QueryClientProvider client={queryClient}>
<ChatRowContent
message={message}
isExpanded={isExpanded}
isLast={false}
isStreaming={false}
onToggleExpand={() => {}}
onSuggestionClick={() => {}}
onBatchFileResponse={() => {}}
onFollowUpUnmount={() => {}}
isFollowUpAnswered={false}
/>
</QueryClientProvider>
</ExtensionStateContextProvider>,
)
}
describe("ChatRow - inline diff stats and actions", () => {
beforeEach(() => {
vi.clearAllMocks()
mockPostMessage.mockClear()
})
it("uses appliedDiff edit treatment (header/icon/diff stats)", () => {
const diff = "@@ -1,1 +1,1 @@\n-old\n+new\n"
const message = createToolAskMessage({
tool: "appliedDiff",
path: "src/file.ts",
diff,
diffStats: { added: 1, removed: 1 },
})
const { container } = renderChatRow(message, false)
expect(screen.getByText("Roo wants to edit this file")).toBeInTheDocument()
expect(container.querySelector(".codicon-diff")).toBeInTheDocument()
expect(screen.getByText("+1")).toBeInTheDocument()
expect(screen.getByText("-1")).toBeInTheDocument()
})
it("uses same edit treatment for editedExistingFile", () => {
const diff = "@@ -1,1 +1,1 @@\n-old\n+new\n"
const message = createToolAskMessage({
tool: "editedExistingFile",
path: "src/file.ts",
diff,
diffStats: { added: 1, removed: 1 },
})
const { container } = renderChatRow(message)
expect(screen.getByText("Roo wants to edit this file")).toBeInTheDocument()
expect(container.querySelector(".codicon-diff")).toBeInTheDocument()
expect(screen.getByText("+1")).toBeInTheDocument()
expect(screen.getByText("-1")).toBeInTheDocument()
})
it("uses same edit treatment for searchAndReplace", () => {
const diff = "-a\n-b\n+c\n"
const message = createToolAskMessage({
tool: "searchAndReplace",
path: "src/file.ts",
diff,
diffStats: { added: 1, removed: 2 },
})
const { container } = renderChatRow(message)
expect(screen.getByText("Roo wants to edit this file")).toBeInTheDocument()
expect(container.querySelector(".codicon-diff")).toBeInTheDocument()
expect(screen.getByText("+1")).toBeInTheDocument()
expect(screen.getByText("-2")).toBeInTheDocument()
})
it("uses same edit treatment for newFileCreated", () => {
const content = "a\nb\nc"
const message = createToolAskMessage({
tool: "newFileCreated",
path: "src/new-file.ts",
content,
diffStats: { added: 3, removed: 0 },
})
const { container } = renderChatRow(message)
expect(screen.getByText("Roo wants to edit this file")).toBeInTheDocument()
expect(container.querySelector(".codicon-diff")).toBeInTheDocument()
expect(screen.getByText("+3")).toBeInTheDocument()
expect(screen.getByText("-0")).toBeInTheDocument()
})
it.each([
["appliedDiff", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["editedExistingFile", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["newFileCreated", "+new file"],
["searchAndReplace", "-a\n-b\n+c\n"],
["search_and_replace", "-a\n-b\n+c\n"],
["search_replace", "-a\n-b\n+c\n"],
["edit", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["edit_file", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["apply_patch", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["apply_diff", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
["insertContent", "@@ -1,1 +1,1 @@\n-old\n+new\n"],
])("shows jump-to-file affordance for %s", (tool, diff) => {
const message = createToolAskMessage({
tool,
path: "src/file.ts",
diff,
lineNumber: 0,
diffStats: { added: 1, removed: 1 },
})
const { container } = renderChatRow(message)
mockPostMessage.mockClear()
const openFileIcon = container.querySelector(".codicon-link-external") as HTMLElement | null
expect(openFileIcon).toBeInTheDocument()
if (!openFileIcon) {
throw new Error(`Expected external link icon for ${tool}`)
}
fireEvent.click(openFileIcon)
expect(mockPostMessage).toHaveBeenCalledTimes(1)
expect(mockPostMessage).toHaveBeenCalledWith({
type: "openFile",
text: "./src/file.ts",
})
})
it("does not show jump-to-file affordance when path is missing", () => {
const message = createToolAskMessage({
tool: "appliedDiff",
diff: "@@ -1,1 +1,1 @@\n-old\n+new\n",
diffStats: { added: 1, removed: 1 },
})
const { container } = renderChatRow(message)
expect(container.querySelector(".codicon-link-external")).not.toBeInTheDocument()
})
it("does not show jump-to-file affordance for non-file tools", () => {
const message = createToolAskMessage({
tool: "executeCommand",
path: "src/file.ts",
command: "echo hello",
content: "hello",
})
const { container } = renderChatRow(message)
expect(container.querySelector(".codicon-link-external")).not.toBeInTheDocument()
})
it("preserves protected and outside-workspace messaging in unified branch", () => {
const outsideWorkspaceMessage = createToolAskMessage({
tool: "searchAndReplace",
path: "../outside/file.ts",
diff: "-a\n+b\n",
isOutsideWorkspace: true,
diffStats: { added: 1, removed: 1 },
})
renderChatRow(outsideWorkspaceMessage)
expect(screen.getByText("Roo wants to edit outside workspace")).toBeInTheDocument()
const protectedMessage = createToolAskMessage({
tool: "appliedDiff",
path: "src/protected.ts",
diff: "-a\n+b\n",
isProtected: true,
diffStats: { added: 1, removed: 1 },
})
const { container } = renderChatRow(protectedMessage)
expect(screen.getByText("Roo wants to edit a protected file")).toBeInTheDocument()
expect(container.querySelector(".codicon-lock")).toBeInTheDocument()
})
it("keeps batch diff handling for unified edit tools", () => {
const message = createToolAskMessage({
tool: "searchAndReplace",
batchDiffs: [
{
path: "src/a.ts",
changeCount: 1,
key: "a",
content: "@@ -1,1 +1,1 @@\n-a\n+b\n",
diffStats: { added: 1, removed: 1 },
},
],
})
renderChatRow(message)
expect(screen.getByText("Roo wants to apply batch changes")).toBeInTheDocument()
expect(screen.getByText((text) => text.includes("src/a.ts"))).toBeInTheDocument()
})
})