|
1 | 1 | import { describe, expect, it, vi } from "vitest" |
2 | 2 | import type * as vscode from "vscode" |
3 | 3 |
|
4 | | -import { providerIdentifiers } from "@roo-code/types" |
| 4 | +import { providerIdentifiers, providerSettingsWithIdSchema } from "@roo-code/types" |
5 | 5 | import type { RooCodeSettings } from "@roo-code/types" |
6 | 6 |
|
7 | 7 | import { API } from "../api" |
@@ -31,9 +31,18 @@ const makeProfileStoreDouble = (seed: Record<string, { id: string; settings: Rec |
31 | 31 | } |
32 | 32 | return Promise.resolve({ name, id: entry.id, ...entry.settings }) |
33 | 33 | }) |
| 34 | + // The keys providerSettingsWithIdSchema keeps, which is the shape saveConfig stores. Used only to |
| 35 | + // mirror the key-stripping: the double does not re-run validation or the legacy rewrite, and the |
| 36 | + // payloads here are not full profiles. A double that stored the raw payload would let a rollback |
| 37 | + // compare against keys that never landed in the record. |
| 38 | + const profileRecordKeys = new Set<string>(Object.keys(providerSettingsWithIdSchema.shape)) |
| 39 | + |
34 | 40 | const saveConfig = vi.fn((name: string, settings: Record<string, unknown>) => { |
35 | 41 | const id = entries.get(name)?.id ?? "created-id" |
36 | | - entries.set(name, { id, settings }) |
| 42 | + const profileSettings = Object.fromEntries( |
| 43 | + Object.entries(settings).filter(([key]) => key !== "id" && profileRecordKeys.has(key)), |
| 44 | + ) |
| 45 | + entries.set(name, { id, settings: profileSettings }) |
37 | 46 | return Promise.resolve(id) |
38 | 47 | }) |
39 | 48 | const deleteConfig = vi.fn((name: string) => { |
@@ -66,6 +75,10 @@ describe("API - configuration", () => { |
66 | 75 | saveConfig, |
67 | 76 | setModeConfig, |
68 | 77 | hasConfig: vi.fn().mockResolvedValue(false), |
| 78 | + // The rollback capture reads the stored record back inside the same critical section. These |
| 79 | + // tests are about the mode fan-out and the payload carries no profile keys, so the record |
| 80 | + // this call saved is just the name plus the id the save returned. |
| 81 | + getProfile: vi.fn().mockResolvedValue({ id: "default-id", name: "default" }), |
69 | 82 | getModeConfigId: vi.fn().mockResolvedValue(undefined), |
70 | 83 | }, |
71 | 84 | postStateToWebview, |
@@ -170,6 +183,10 @@ describe("API - configuration", () => { |
170 | 183 | saveConfig, |
171 | 184 | setModeConfig, |
172 | 185 | hasConfig: vi.fn().mockResolvedValue(false), |
| 186 | + // The rollback capture reads the stored record back inside the same critical section. These |
| 187 | + // tests are about the mode fan-out and the payload carries no profile keys, so the record |
| 188 | + // this call saved is just the name plus the id the save returned. |
| 189 | + getProfile: vi.fn().mockResolvedValue({ id: "default-id", name: "default" }), |
173 | 190 | getModeConfigId: vi.fn().mockResolvedValue(undefined), |
174 | 191 | }, |
175 | 192 | postStateToWebview, |
@@ -276,6 +293,136 @@ describe("API - configuration", () => { |
276 | 293 | apiKey: "another-view", |
277 | 294 | }) |
278 | 295 | }) |
| 296 | + |
| 297 | + it("keeps a record another view rewrote under the id this call created", async () => { |
| 298 | + // This call creates the name, so the id check is the only guard the delete leg had. It is not |
| 299 | + // enough: saveConfig keeps the id of the record it overwrote, so a view that saved over the |
| 300 | + // name afterwards inherited this call id, and deleting on the id alone drops that record - |
| 301 | + // secrets included. Only the record comparison sees the write. |
| 302 | + const store = makeProfileStoreDouble() |
| 303 | + const { saveConfig, deleteConfig, hasConfig, getProfile } = store |
| 304 | + const setValues = vi.fn().mockResolvedValue(undefined) |
| 305 | + const setProviderSettings = vi.fn().mockResolvedValue(undefined) |
| 306 | + const postStateToWebview = vi.fn().mockResolvedValue(undefined) |
| 307 | + const sharedBefore = { apiProvider: providerIdentifiers.anthropic, apiModelId: "model-old" } |
| 308 | + // The other view writes through the same map while this call is still landing its mappings. |
| 309 | + const setModeConfig = vi.fn().mockImplementation(async () => { |
| 310 | + await saveConfig("pinned", { |
| 311 | + apiProvider: providerIdentifiers.openrouter, |
| 312 | + apiKey: "another-view", |
| 313 | + }) |
| 314 | + throw new Error("mode mapping down") |
| 315 | + }) |
| 316 | + const provider = { |
| 317 | + context: {}, |
| 318 | + on: vi.fn(), |
| 319 | + log: vi.fn(), |
| 320 | + getProviderProfileEntries: vi.fn().mockReturnValue([]), |
| 321 | + getValues: vi.fn().mockReturnValue({}), |
| 322 | + setValues, |
| 323 | + contextProxy: { |
| 324 | + getValue: vi.fn().mockReturnValue(undefined), |
| 325 | + setValues, |
| 326 | + getProviderSettings: vi.fn().mockReturnValue(sharedBefore), |
| 327 | + setProviderSettings, |
| 328 | + }, |
| 329 | + providerSettingsManager: { |
| 330 | + saveConfig, |
| 331 | + deleteConfig, |
| 332 | + hasConfig, |
| 333 | + getProfile, |
| 334 | + getModeConfigId: vi.fn().mockResolvedValue(undefined), |
| 335 | + setModeConfig, |
| 336 | + runExclusiveAcrossInstances: runManagerSectionInline, |
| 337 | + }, |
| 338 | + postStateToWebview, |
| 339 | + } as unknown as ClineProvider |
| 340 | + const outputChannel = { appendLine: vi.fn() } as unknown as vscode.OutputChannel |
| 341 | + // Logging is off by default, and the keep decision is reported through this.log; enable it |
| 342 | + // so the log line is observable. No socket path, so no IPC server is started. |
| 343 | + const api = new API(outputChannel, provider, makeClineProviderFactory(), undefined, true) |
| 344 | + |
| 345 | + await expect( |
| 346 | + api.setConfiguration({ currentApiConfigName: "pinned", modeApiConfigs: { code: "code-config" } }), |
| 347 | + ).rejects.toThrow("mode mapping down") |
| 348 | + |
| 349 | + expect(deleteConfig).not.toHaveBeenCalled() |
| 350 | + expect(store.entries.get("pinned")?.settings).toEqual({ |
| 351 | + apiProvider: providerIdentifiers.openrouter, |
| 352 | + apiKey: "another-view", |
| 353 | + }) |
| 354 | + expect(outputChannel.appendLine).toHaveBeenCalledWith(expect.stringContaining("under the id this call created")) |
| 355 | + }) |
| 356 | + |
| 357 | + it("restores the previous record when the payload carried keys the profile schema strips", async () => { |
| 358 | + // saveConfig drops the keys the profile schema does not know, so the record this call owns is |
| 359 | + // narrower than its payload. Comparing the re-read record against the payload instead of |
| 360 | + // against what the save wrote reads an untouched record as another view write, and the |
| 361 | + // rollback returns without putting the previous record back. |
| 362 | + const store = makeProfileStoreDouble({ |
| 363 | + pinned: { |
| 364 | + id: "pinned-id", |
| 365 | + settings: { apiProvider: providerIdentifiers.anthropic, apiKey: "before" }, |
| 366 | + }, |
| 367 | + }) |
| 368 | + const { saveConfig, deleteConfig, hasConfig, getProfile } = store |
| 369 | + // currentApiConfigName and mode are settings keys, not profile keys: the save strips both. |
| 370 | + const setValues = vi.fn().mockRejectedValue(new Error("refresh down")) |
| 371 | + const setModeConfig = vi.fn().mockResolvedValue(undefined) |
| 372 | + const setProviderSettings = vi.fn().mockResolvedValue(undefined) |
| 373 | + const postStateToWebview = vi.fn().mockResolvedValue(undefined) |
| 374 | + const sharedBefore = { apiProvider: providerIdentifiers.anthropic, apiModelId: "model-old" } |
| 375 | + const provider = { |
| 376 | + context: {}, |
| 377 | + on: vi.fn(), |
| 378 | + log: vi.fn(), |
| 379 | + getProviderProfileEntries: vi.fn().mockReturnValue([]), |
| 380 | + getValues: vi.fn().mockReturnValue({}), |
| 381 | + setValues, |
| 382 | + contextProxy: { |
| 383 | + getValue: vi.fn().mockReturnValue(undefined), |
| 384 | + setValues, |
| 385 | + getProviderSettings: vi.fn().mockReturnValue(sharedBefore), |
| 386 | + setProviderSettings, |
| 387 | + }, |
| 388 | + providerSettingsManager: { |
| 389 | + saveConfig, |
| 390 | + deleteConfig, |
| 391 | + hasConfig, |
| 392 | + getProfile, |
| 393 | + getModeConfigId: vi.fn().mockResolvedValue(undefined), |
| 394 | + setModeConfig, |
| 395 | + runExclusiveAcrossInstances: runManagerSectionInline, |
| 396 | + }, |
| 397 | + postStateToWebview, |
| 398 | + } as unknown as ClineProvider |
| 399 | + const outputChannel = { appendLine: vi.fn() } as unknown as vscode.OutputChannel |
| 400 | + const api = new API(outputChannel, provider, makeClineProviderFactory()) |
| 401 | + |
| 402 | + await expect( |
| 403 | + api.setConfiguration({ |
| 404 | + currentApiConfigName: "pinned", |
| 405 | + mode: "code", |
| 406 | + apiProvider: providerIdentifiers.openrouter, |
| 407 | + apiKey: "after", |
| 408 | + }), |
| 409 | + ).rejects.toThrow("refresh down") |
| 410 | + |
| 411 | + // Only the profile part of the payload landed, so the record is still this call to roll back: |
| 412 | + // the previous record comes back under the id it already had, and the delete leg is not |
| 413 | + // taken for a name that existed before the call. |
| 414 | + expect(store.entries.get("pinned")).toEqual({ |
| 415 | + id: "pinned-id", |
| 416 | + settings: { apiProvider: providerIdentifiers.anthropic, apiKey: "before" }, |
| 417 | + }) |
| 418 | + expect(deleteConfig).not.toHaveBeenCalled() |
| 419 | + expect(saveConfig).toHaveBeenLastCalledWith("pinned", { |
| 420 | + apiProvider: providerIdentifiers.anthropic, |
| 421 | + apiKey: "before", |
| 422 | + }) |
| 423 | + expect(postStateToWebview).not.toHaveBeenCalled() |
| 424 | + }) |
| 425 | + |
279 | 426 | it("restores the stored profile and shared settings when the refresh fails after the save", async () => { |
280 | 427 | const setValues = vi.fn().mockRejectedValue(new Error("refresh down")) |
281 | 428 | // The store double keeps the compensation re-read honest: this call creates the record, so the |
|
0 commit comments