|
| 1 | +// npx vitest run __tests__/task-mode-switch.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from "vitest" |
| 4 | +import type { HistoryItem } from "@roo-code/types" |
| 5 | +import { RooCodeEventName } from "@roo-code/types" |
| 6 | +import { Task } from "../core/task/Task" |
| 7 | + |
| 8 | +const taskHistoryItem: HistoryItem = { |
| 9 | + id: "task-1", |
| 10 | + task: "Background work", |
| 11 | + tokensIn: 0, |
| 12 | + tokensOut: 0, |
| 13 | + totalCost: 0, |
| 14 | +} as unknown as HistoryItem |
| 15 | + |
| 16 | +/** Task-scoped providerSettingsManager stub; by default no mode-bound profile resolves. */ |
| 17 | +const makeProviderSettingsManager = ( |
| 18 | + overrides: Partial<{ |
| 19 | + getModeConfigId: ReturnType<typeof vi.fn> |
| 20 | + listConfig: ReturnType<typeof vi.fn> |
| 21 | + getProfile: ReturnType<typeof vi.fn> |
| 22 | + setModeConfig: ReturnType<typeof vi.fn> |
| 23 | + }> = {}, |
| 24 | +) => ({ |
| 25 | + getModeConfigId: vi.fn().mockResolvedValue(undefined), |
| 26 | + listConfig: vi.fn().mockResolvedValue([]), |
| 27 | + getProfile: vi.fn(), |
| 28 | + setModeConfig: vi.fn(), |
| 29 | + ...overrides, |
| 30 | +}) |
| 31 | + |
| 32 | +/** Provider double with everything switchTaskMode touches. */ |
| 33 | +function makeProvider(overrides: Record<string, unknown> = {}) { |
| 34 | + return { |
| 35 | + isTaskVisible: vi.fn().mockReturnValue(false), |
| 36 | + handleModeSwitch: vi.fn().mockResolvedValue(undefined), |
| 37 | + updateTaskHistory: vi.fn().mockResolvedValue(undefined), |
| 38 | + taskHistoryStore: { get: vi.fn().mockReturnValue(taskHistoryItem) }, |
| 39 | + context: { workspaceState: { get: vi.fn().mockReturnValue(false) } }, |
| 40 | + providerSettingsManager: makeProviderSettingsManager(), |
| 41 | + log: vi.fn(), |
| 42 | + ...overrides, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Task double with the methods/fields switchTaskMode reads from `this`. |
| 48 | + * The real prototype method is invoked with this double, mirroring the |
| 49 | + * provider-double pattern in provider-delegation.spec.ts. |
| 50 | + */ |
| 51 | +function makeTask(provider: unknown) { |
| 52 | + return { |
| 53 | + taskId: "task-1", |
| 54 | + providerRef: { deref: vi.fn(() => provider) }, |
| 55 | + setTaskMode: vi.fn(), |
| 56 | + emit: vi.fn(), |
| 57 | + updateApiConfiguration: vi.fn(), |
| 58 | + setTaskApiConfigName: vi.fn(), |
| 59 | + postTaskStateToWebview: vi.fn().mockResolvedValue(undefined), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +const invoke = (task: unknown, mode = "architect") => (Task.prototype as any).switchTaskMode.call(task, mode) |
| 64 | + |
| 65 | +describe("Task.switchTaskMode()", () => { |
| 66 | + it("routes a VISIBLE task through the global handleModeSwitch after task-scoped persistence", async () => { |
| 67 | + const provider = makeProvider({ isTaskVisible: vi.fn().mockReturnValue(true) }) |
| 68 | + const task = makeTask(provider) |
| 69 | + |
| 70 | + await invoke(task) |
| 71 | + |
| 72 | + expect(task.setTaskMode).toHaveBeenCalledWith("architect") |
| 73 | + // History persisted without broadcast before the global switch. |
| 74 | + expect(provider.updateTaskHistory).toHaveBeenCalledWith( |
| 75 | + { ...taskHistoryItem, mode: "architect" }, |
| 76 | + { broadcast: false }, |
| 77 | + ) |
| 78 | + expect(provider.handleModeSwitch).toHaveBeenCalledWith("architect") |
| 79 | + |
| 80 | + // The visible path delegates profile handling to the global handler: |
| 81 | + // no direct task-scoped profile resolution or API handler mutation. |
| 82 | + expect(provider.providerSettingsManager.getModeConfigId).not.toHaveBeenCalled() |
| 83 | + expect(task.updateApiConfiguration).not.toHaveBeenCalled() |
| 84 | + expect(task.setTaskApiConfigName).not.toHaveBeenCalled() |
| 85 | + }) |
| 86 | + |
| 87 | + it("never calls the global handleModeSwitch for a BACKGROUND task", async () => { |
| 88 | + const provider = makeProvider() |
| 89 | + const task = makeTask(provider) |
| 90 | + |
| 91 | + await invoke(task) |
| 92 | + |
| 93 | + expect(provider.handleModeSwitch).not.toHaveBeenCalled() |
| 94 | + expect(task.setTaskMode).toHaveBeenCalledWith("architect") |
| 95 | + expect(provider.updateTaskHistory).toHaveBeenCalledWith( |
| 96 | + { ...taskHistoryItem, mode: "architect" }, |
| 97 | + { broadcast: false }, |
| 98 | + ) |
| 99 | + // The per-task event still fires for API observers (the global path |
| 100 | + // emits it inside handleModeSwitch). |
| 101 | + expect(task.emit).toHaveBeenCalledWith(RooCodeEventName.TaskModeSwitched, "task-1", "architect") |
| 102 | + expect(task.postTaskStateToWebview).toHaveBeenCalledTimes(1) |
| 103 | + }) |
| 104 | + |
| 105 | + it("resolves the mode-bound profile task-scoped for a background task and applies it to the task only", async () => { |
| 106 | + const providerSettingsManager = makeProviderSettingsManager({ |
| 107 | + getModeConfigId: vi.fn().mockResolvedValue("cfg-arch"), |
| 108 | + listConfig: vi.fn().mockResolvedValue([ |
| 109 | + { id: "cfg-other", name: "other-profile" }, |
| 110 | + { id: "cfg-arch", name: "architect-profile" }, |
| 111 | + ]), |
| 112 | + getProfile: vi.fn().mockResolvedValue({ |
| 113 | + id: "cfg-arch", |
| 114 | + name: "architect-profile", |
| 115 | + apiProvider: "anthropic", |
| 116 | + apiModelId: "architect-model", |
| 117 | + }), |
| 118 | + }) |
| 119 | + const provider = makeProvider({ providerSettingsManager }) |
| 120 | + const task = makeTask(provider) |
| 121 | + |
| 122 | + await invoke(task) |
| 123 | + |
| 124 | + expect(providerSettingsManager.getModeConfigId).toHaveBeenCalledWith("architect") |
| 125 | + expect(providerSettingsManager.getProfile).toHaveBeenCalledWith({ name: "architect-profile" }) |
| 126 | + |
| 127 | + // The resolved profile (id/name stripped) is applied to THIS task's API handler. |
| 128 | + expect(task.updateApiConfiguration).toHaveBeenCalledWith({ |
| 129 | + apiProvider: "anthropic", |
| 130 | + apiModelId: "architect-model", |
| 131 | + }) |
| 132 | + expect(task.setTaskApiConfigName).toHaveBeenCalledWith("architect-profile") |
| 133 | + |
| 134 | + // Profile name persisted to this task's history without broadcast. |
| 135 | + expect(provider.updateTaskHistory).toHaveBeenCalledWith( |
| 136 | + { ...taskHistoryItem, apiConfigName: "architect-profile" }, |
| 137 | + { broadcast: false }, |
| 138 | + ) |
| 139 | + |
| 140 | + // Never routed through the global mode/profile switch. |
| 141 | + expect(provider.handleModeSwitch).not.toHaveBeenCalled() |
| 142 | + }) |
| 143 | + |
| 144 | + it("keeps the current config when the resolved profile has no apiProvider", async () => { |
| 145 | + const providerSettingsManager = makeProviderSettingsManager({ |
| 146 | + getModeConfigId: vi.fn().mockResolvedValue("cfg-empty"), |
| 147 | + listConfig: vi.fn().mockResolvedValue([{ id: "cfg-empty", name: "empty-profile" }]), |
| 148 | + getProfile: vi.fn().mockResolvedValue({ id: "cfg-empty", name: "empty-profile" }), |
| 149 | + }) |
| 150 | + const provider = makeProvider({ providerSettingsManager }) |
| 151 | + const task = makeTask(provider) |
| 152 | + |
| 153 | + await invoke(task) |
| 154 | + |
| 155 | + expect(task.updateApiConfiguration).not.toHaveBeenCalled() |
| 156 | + expect(task.setTaskApiConfigName).not.toHaveBeenCalled() |
| 157 | + // Mode itself still switches. |
| 158 | + expect(task.setTaskMode).toHaveBeenCalledWith("architect") |
| 159 | + expect(task.postTaskStateToWebview).toHaveBeenCalledTimes(1) |
| 160 | + }) |
| 161 | + |
| 162 | + it("does not write a mode→config binding when the mode has no saved config (background)", async () => { |
| 163 | + const provider = makeProvider() |
| 164 | + const task = makeTask(provider) |
| 165 | + |
| 166 | + await invoke(task) |
| 167 | + |
| 168 | + // handleModeSwitch's else-branch would call setModeConfig; a background |
| 169 | + // conversation must not mutate that user-level setting. |
| 170 | + expect(provider.providerSettingsManager.setModeConfig).not.toHaveBeenCalled() |
| 171 | + expect(task.updateApiConfiguration).not.toHaveBeenCalled() |
| 172 | + expect(task.setTaskApiConfigName).not.toHaveBeenCalled() |
| 173 | + }) |
| 174 | + |
| 175 | + it("skips mode-bound profile resolution entirely when lockApiConfigAcrossModes is set", async () => { |
| 176 | + const providerSettingsManager = makeProviderSettingsManager({ |
| 177 | + getModeConfigId: vi.fn().mockResolvedValue("cfg-arch"), |
| 178 | + }) |
| 179 | + const workspaceStateGet = vi.fn((key: string, defaultValue?: unknown) => |
| 180 | + key === "lockApiConfigAcrossModes" ? true : defaultValue, |
| 181 | + ) |
| 182 | + const provider = makeProvider({ |
| 183 | + providerSettingsManager, |
| 184 | + context: { workspaceState: { get: workspaceStateGet } }, |
| 185 | + }) |
| 186 | + const task = makeTask(provider) |
| 187 | + |
| 188 | + await invoke(task) |
| 189 | + |
| 190 | + expect(workspaceStateGet).toHaveBeenCalledWith("lockApiConfigAcrossModes", false) |
| 191 | + expect(providerSettingsManager.getModeConfigId).not.toHaveBeenCalled() |
| 192 | + expect(providerSettingsManager.listConfig).not.toHaveBeenCalled() |
| 193 | + expect(providerSettingsManager.getProfile).not.toHaveBeenCalled() |
| 194 | + expect(task.updateApiConfiguration).not.toHaveBeenCalled() |
| 195 | + // Mode itself still switches. |
| 196 | + expect(task.setTaskMode).toHaveBeenCalledWith("architect") |
| 197 | + }) |
| 198 | + |
| 199 | + it("treats a failed profile resolution as non-fatal: mode switches, state still posts", async () => { |
| 200 | + const resolutionError = new Error("settings store unavailable") |
| 201 | + const providerSettingsManager = makeProviderSettingsManager({ |
| 202 | + getModeConfigId: vi.fn().mockRejectedValue(resolutionError), |
| 203 | + }) |
| 204 | + const provider = makeProvider({ providerSettingsManager }) |
| 205 | + const task = makeTask(provider) |
| 206 | + |
| 207 | + await expect(invoke(task)).resolves.toBeUndefined() |
| 208 | + |
| 209 | + expect(task.setTaskMode).toHaveBeenCalledWith("architect") |
| 210 | + expect(task.updateApiConfiguration).not.toHaveBeenCalled() |
| 211 | + expect(provider.log).toHaveBeenCalledWith(expect.stringContaining("Task-scoped profile resolution failed")) |
| 212 | + expect(task.postTaskStateToWebview).toHaveBeenCalledTimes(1) |
| 213 | + }) |
| 214 | + |
| 215 | + it("is a no-op when the provider reference is gone", async () => { |
| 216 | + const task = makeTask(undefined) |
| 217 | + |
| 218 | + await invoke(task) |
| 219 | + |
| 220 | + expect(task.setTaskMode).not.toHaveBeenCalled() |
| 221 | + expect(task.emit).not.toHaveBeenCalled() |
| 222 | + expect(task.postTaskStateToWebview).not.toHaveBeenCalled() |
| 223 | + }) |
| 224 | +}) |
0 commit comments