|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import { CloudService } from "@roo-code/cloud" |
| 4 | +import { webviewMessageHandler } from "../webviewMessageHandler" |
| 5 | +import { ClineProvider } from "../ClineProvider" |
| 6 | +import { ProviderSettings } from "@roo-code/types" |
| 7 | + |
| 8 | +// Mock CloudService |
| 9 | +vi.mock("@roo-code/cloud", () => ({ |
| 10 | + CloudService: { |
| 11 | + instance: { |
| 12 | + getOrganizationSettings: vi.fn(), |
| 13 | + }, |
| 14 | + }, |
| 15 | +})) |
| 16 | + |
| 17 | +describe("webviewMessageHandler - Organization Defaults", () => { |
| 18 | + let mockProvider: any |
| 19 | + let mockMarketplaceManager: any |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + // Reset mocks |
| 23 | + vi.clearAllMocks() |
| 24 | + |
| 25 | + // Create mock provider |
| 26 | + mockProvider = { |
| 27 | + log: vi.fn(), |
| 28 | + upsertProviderProfile: vi.fn(), |
| 29 | + postMessageToWebview: vi.fn(), |
| 30 | + getState: vi.fn().mockResolvedValue({ |
| 31 | + apiConfiguration: {}, |
| 32 | + currentApiConfigName: "test-config", |
| 33 | + }), |
| 34 | + } |
| 35 | + |
| 36 | + // Create mock marketplace manager |
| 37 | + mockMarketplaceManager = {} |
| 38 | + }) |
| 39 | + |
| 40 | + it("should apply organization default settings when creating a new profile", async () => { |
| 41 | + // Mock organization settings with defaults |
| 42 | + const orgDefaults = { |
| 43 | + anthropic: { |
| 44 | + apiProvider: "anthropic" as const, |
| 45 | + anthropicApiKey: "org-default-key", |
| 46 | + apiModelId: "claude-3-opus-20240229", |
| 47 | + temperature: 0.7, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + vi.mocked(CloudService.instance.getOrganizationSettings).mockResolvedValue({ |
| 52 | + version: 1, |
| 53 | + defaultSettings: {}, |
| 54 | + allowList: { allowAll: true, providers: {} }, |
| 55 | + defaultProviderSettings: orgDefaults, |
| 56 | + }) |
| 57 | + |
| 58 | + // Send upsertApiConfiguration message |
| 59 | + const message = { |
| 60 | + type: "upsertApiConfiguration" as const, |
| 61 | + text: "new-profile", |
| 62 | + apiConfiguration: { |
| 63 | + apiProvider: "anthropic", |
| 64 | + anthropicApiKey: "user-key", // User-provided key should take precedence |
| 65 | + // temperature is not provided, so org default should be used |
| 66 | + } as ProviderSettings, |
| 67 | + } |
| 68 | + |
| 69 | + await webviewMessageHandler(mockProvider, message, mockMarketplaceManager) |
| 70 | + |
| 71 | + // Verify that upsertProviderProfile was called with merged settings |
| 72 | + expect(mockProvider.upsertProviderProfile).toHaveBeenCalledWith("new-profile", { |
| 73 | + apiProvider: "anthropic", |
| 74 | + anthropicApiKey: "user-key", // User value takes precedence |
| 75 | + apiModelId: "claude-3-opus-20240229", // From org defaults |
| 76 | + temperature: 0.7, // From org defaults |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it("should handle missing organization settings gracefully", async () => { |
| 81 | + // Mock CloudService to throw an error |
| 82 | + vi.mocked(CloudService.instance.getOrganizationSettings).mockRejectedValue(new Error("Not authenticated")) |
| 83 | + |
| 84 | + // Send upsertApiConfiguration message |
| 85 | + const message = { |
| 86 | + type: "upsertApiConfiguration" as const, |
| 87 | + text: "new-profile", |
| 88 | + apiConfiguration: { |
| 89 | + apiProvider: "anthropic", |
| 90 | + anthropicApiKey: "user-key", |
| 91 | + } as ProviderSettings, |
| 92 | + } |
| 93 | + |
| 94 | + await webviewMessageHandler(mockProvider, message, mockMarketplaceManager) |
| 95 | + |
| 96 | + // Verify that error was logged |
| 97 | + expect(mockProvider.log).toHaveBeenCalledWith(expect.stringContaining("Failed to get organization defaults")) |
| 98 | + |
| 99 | + // Verify that upsertProviderProfile was still called with original settings |
| 100 | + expect(mockProvider.upsertProviderProfile).toHaveBeenCalledWith("new-profile", { |
| 101 | + apiProvider: "anthropic", |
| 102 | + anthropicApiKey: "user-key", |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it("should not apply defaults for a different provider", async () => { |
| 107 | + // Mock organization settings with defaults for anthropic |
| 108 | + const orgDefaults = { |
| 109 | + anthropic: { |
| 110 | + apiProvider: "anthropic" as const, |
| 111 | + anthropicApiKey: "org-default-key", |
| 112 | + apiModelId: "claude-3-opus-20240229", |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + vi.mocked(CloudService.instance.getOrganizationSettings).mockResolvedValue({ |
| 117 | + version: 1, |
| 118 | + defaultSettings: {}, |
| 119 | + allowList: { allowAll: true, providers: {} }, |
| 120 | + defaultProviderSettings: orgDefaults, |
| 121 | + }) |
| 122 | + |
| 123 | + // Send upsertApiConfiguration message for openai provider |
| 124 | + const message = { |
| 125 | + type: "upsertApiConfiguration" as const, |
| 126 | + text: "new-profile", |
| 127 | + apiConfiguration: { |
| 128 | + apiProvider: "openai", |
| 129 | + openAiApiKey: "user-key", |
| 130 | + } as ProviderSettings, |
| 131 | + } |
| 132 | + |
| 133 | + await webviewMessageHandler(mockProvider, message, mockMarketplaceManager) |
| 134 | + |
| 135 | + // Verify that only the user-provided settings were used |
| 136 | + expect(mockProvider.upsertProviderProfile).toHaveBeenCalledWith("new-profile", { |
| 137 | + apiProvider: "openai", |
| 138 | + openAiApiKey: "user-key", |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments