Skip to content

Commit 3cea125

Browse files
committed
fix(tests): adapt mode-switch specs to provider-owned task mode write
The CodeRabbit fixes moved the task-mode write into ClineProvider.handleModeSwitch (after validation and persistence) and made SwitchModeTool pass the explicit task. Update the pre-existing specs accordingly: - switchModeTool.spec.ts: handleModeSwitch is now asserted with (slug, task) - Task.spec.ts: the handleModeSwitch mock mirrors the provider's post-persistence mode write, and the test settles the task's initial mode before the user-selected mode switch
1 parent 68b0356 commit 3cea125

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type TaskTestAccess = {
4040
saveClineMessages: () => Promise<boolean>
4141
safeEnsureModelFetched: () => Promise<void>
4242
addToApiConversationHistory: (message: unknown, reasoning?: string) => Promise<void>
43+
// Private on Task; the provider-owned mode write (ClineProvider.handleModeSwitch)
44+
// sets it, and tests mirror that write through this helper.
45+
_taskMode: string | undefined
4346
}
4447

4548
type TaskAskResult = Awaited<ReturnType<Task["ask"]>>
@@ -1787,7 +1790,13 @@ describe("Cline", () => {
17871790
mode: "ask",
17881791
mcpEnabled: false,
17891792
} as unknown as ProviderState)
1790-
vi.spyOn(mockProvider, "handleModeSwitch").mockResolvedValue(undefined)
1793+
vi.spyOn(mockProvider, "handleModeSwitch").mockImplementation(async (mode, targetTask) => {
1794+
// Mirror ClineProvider.handleModeSwitch: after validation and persistence
1795+
// the provider owns the task's mode write.
1796+
if (targetTask) {
1797+
getTaskTestAccess(targetTask)._taskMode = mode
1798+
}
1799+
})
17911800
const task = new Task({
17921801
provider: mockProvider,
17931802
apiConfiguration: mockApiConfig,
@@ -1796,6 +1805,10 @@ describe("Cline", () => {
17961805
})
17971806
vi.spyOn(task, "handleWebviewAskResponse").mockImplementation(() => {})
17981807

1808+
// Let the task's initial mode ("ask", from provider state) settle first, so the
1809+
// mode selected with the user message is the task's final mode write.
1810+
await task.getTaskMode()
1811+
17991812
await task.submitUserMessage("switch modes", undefined, "code")
18001813
vi.spyOn(getTaskTestAccess(task), "getSystemPrompt").mockResolvedValue("mock system prompt")
18011814
const stream = (async function* () {

‎src/core/tools/__tests__/switchModeTool.spec.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ describe("SwitchModeTool", () => {
165165
}),
166166
)
167167

168-
// Should have called handleModeSwitch with the target slug
169-
expect(mockHandleModeSwitch).toHaveBeenCalledWith("architect")
168+
// Should have called handleModeSwitch with the target slug and the task
169+
expect(mockHandleModeSwitch).toHaveBeenCalledWith("architect", mockTask)
170170

171171
// Should have pushed success result
172172
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
@@ -184,7 +184,7 @@ describe("SwitchModeTool", () => {
184184
JSON.stringify({ tool: "switchMode", mode: "ask", reason: "" }),
185185
)
186186

187-
expect(mockHandleModeSwitch).toHaveBeenCalledWith("ask")
187+
expect(mockHandleModeSwitch).toHaveBeenCalledWith("ask", mockTask)
188188

189189
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith("Successfully switched from Code mode to Ask mode.")
190190
})
@@ -244,7 +244,7 @@ describe("SwitchModeTool", () => {
244244
// Should have asked for approval first
245245
expect(mockCallbacks.askApproval).toHaveBeenCalled()
246246
// Should have called handleModeSwitch (which throws)
247-
expect(mockHandleModeSwitch).toHaveBeenCalledWith("architect")
247+
expect(mockHandleModeSwitch).toHaveBeenCalledWith("architect", mockTask)
248248
// Error should be caught and reported
249249
expect(mockCallbacks.handleError).toHaveBeenCalledWith("switching mode", switchError)
250250
})
@@ -303,7 +303,7 @@ describe("SwitchModeTool", () => {
303303
await switchModeTool.handle(mockTask, block, mockCallbacks)
304304

305305
expect(mockCallbacks.askApproval).toHaveBeenCalled()
306-
expect(mockHandleModeSwitch).toHaveBeenCalledWith("custom-mode")
306+
expect(mockHandleModeSwitch).toHaveBeenCalledWith("custom-mode", mockTask)
307307
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
308308
"Successfully switched from Code mode to Custom Mode mode because: testing custom modes.",
309309
)
@@ -335,7 +335,7 @@ describe("SwitchModeTool", () => {
335335

336336
await switchModeTool.handle(mockTask, block, mockCallbacks)
337337

338-
expect(mockHandleModeSwitch).toHaveBeenCalledWith("code")
338+
expect(mockHandleModeSwitch).toHaveBeenCalledWith("code", mockTask)
339339
expect(mockCallbacks.pushToolResult).toHaveBeenCalledWith(
340340
"Successfully switched from Architect mode to Code mode because: switching back.",
341341
)

0 commit comments

Comments
 (0)