Skip to content

Commit 5ee6bcb

Browse files
committed
test(webview): exercise the real activation for the committed-switch guard
1 parent 8d33d13 commit 5ee6bcb

1 file changed

Lines changed: 72 additions & 8 deletions

File tree

‎src/core/webview/__tests__/ClineProvider.sticky-mode.spec.ts‎

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,23 +1108,22 @@ describe("ClineProvider - Sticky Mode", () => {
11081108
await provider.saveViewState("mode", "code")
11091109
await provider.contextProxy.setValue("mode", "code")
11101110

1111-
// The mode's saved profile was deleted: the lookup inside the activation
1112-
// throws before any shared write lands. The committed-switch guard must still
1113-
// be off, so the reconcile restores every mode write.
1111+
// The REAL activation is exercised: its profile lookup throws before the
1112+
// activated profile is written, so the committed-switch guard must still be
1113+
// off (the callback fires only after activateProfile resolves) and the
1114+
// reconcile must restore every mode write.
11141115
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockResolvedValue("architect-profile-id")
11151116
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([
11161117
{ id: "architect-profile-id", name: "architect-profile", apiProvider: providerIdentifiers.openrouter },
1117-
{ id: "architect-profile-id", name: "architect-profile", apiProvider: providerIdentifiers.openrouter },
11181118
])
11191119
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
11201120
name: "architect-profile",
11211121
id: "architect-profile-id",
11221122
apiProvider: providerIdentifiers.openrouter,
11231123
})
1124-
// The activation's own profile lookup throws before any shared write lands
1125-
// (e.g. the profile was deleted): the committed-switch guard must still be
1126-
// off, so the reconcile restores every mode write.
1127-
provider["activateProviderProfileUnlocked"] = vi.fn().mockRejectedValue(new Error("profile not found"))
1124+
vi.spyOn(provider.providerSettingsManager, "activateProfile").mockRejectedValue(
1125+
new Error("profile not found"),
1126+
)
11281127
await provider.addClineToStack(mockTask)
11291128

11301129
await expect(provider["handleModeSwitchUnlocked"]("architect", mockTask)).rejects.toThrow(
@@ -1137,6 +1136,71 @@ describe("ClineProvider - Sticky Mode", () => {
11371136
expect(updateTaskHistorySpy).toHaveBeenCalledWith(expect.objectContaining({ mode: "code" }))
11381137
})
11391138

1139+
it("keeps the switch when the activation fails after the profile write", async () => {
1140+
// Same typed double as the activation-failure rollback test above.
1141+
const mockTask = Object.assign(
1142+
{} as Task,
1143+
{
1144+
taskId: "test-task-id",
1145+
taskMode: "code",
1146+
_taskMode: "code",
1147+
emit: vi.fn(),
1148+
saveClineMessages: vi.fn(),
1149+
clineMessages: [],
1150+
apiConversationHistory: [],
1151+
updateApiConfiguration: vi.fn(),
1152+
} as Partial<Task>,
1153+
)
1154+
const historyItem: HistoryItem = {
1155+
id: "test-task-id",
1156+
ts: Date.now(),
1157+
task: "Test task",
1158+
mode: "code",
1159+
number: 1,
1160+
tokensIn: 0,
1161+
tokensOut: 0,
1162+
cacheWrites: 0,
1163+
cacheReads: 0,
1164+
totalCost: 0,
1165+
}
1166+
vi.spyOn(provider.taskHistoryStore, "get").mockReturnValue(historyItem)
1167+
const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
1168+
await provider.saveViewState("mode", "code")
1169+
await provider.contextProxy.setValue("mode", "code")
1170+
1171+
// The real activation resolves its profile lookup - flipping the
1172+
// committed-switch guard at that point - and only a later shared write
1173+
// fails: the activated profile is already in place, so the switch must
1174+
// be kept rather than reconciled.
1175+
vi.spyOn(provider.providerSettingsManager, "getModeConfigId").mockResolvedValue("architect-profile-id")
1176+
vi.spyOn(provider.providerSettingsManager, "listConfig").mockResolvedValue([
1177+
{ id: "architect-profile-id", name: "architect-profile", apiProvider: providerIdentifiers.openrouter },
1178+
])
1179+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
1180+
name: "architect-profile",
1181+
id: "architect-profile-id",
1182+
apiProvider: providerIdentifiers.openrouter,
1183+
})
1184+
vi.spyOn(provider.providerSettingsManager, "activateProfile").mockResolvedValue({
1185+
id: "architect-profile-id",
1186+
name: "architect-profile",
1187+
apiProvider: providerIdentifiers.openrouter,
1188+
})
1189+
vi.spyOn(provider.providerSettingsManager, "setModeConfig").mockRejectedValue(
1190+
new Error("set-mode-config failed"),
1191+
)
1192+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
1193+
await provider.addClineToStack(mockTask)
1194+
1195+
await expect(provider["handleModeSwitchUnlocked"]("architect", mockTask)).rejects.toThrow(
1196+
"set-mode-config failed",
1197+
)
1198+
1199+
expect(provider.getValue("mode")).toBe("architect")
1200+
expect(provider["viewLocalState"].mode).toBe("architect")
1201+
expect(provider.contextProxy.getValue("currentApiConfigName")).toBe("architect-profile")
1202+
})
1203+
11401204
it("reports ModeChanged for a switch the catch keeps after activation wrote", async () => {
11411205
// Same typed double as the activation-failure rollback test above.
11421206
const mockTask = Object.assign(

0 commit comments

Comments
 (0)