|
| 1 | +import { Cline } from "../Cline" |
| 2 | +import { ClineProvider } from "../webview/ClineProvider" |
| 3 | +import { TemperatureOverrideService } from "../services/TemperatureOverrideService" |
| 4 | +import * as vscode from "vscode" |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +jest.mock("../webview/ClineProvider") |
| 8 | +jest.mock("../services/TemperatureOverrideService") |
| 9 | +jest.mock("../ignore/RooIgnoreController") |
| 10 | + |
| 11 | +// Mock storagePathManager to avoid file system errors |
| 12 | +jest.mock("../../shared/storagePathManager", () => ({ |
| 13 | + getStorageBasePath: jest.fn().mockReturnValue("/mock/storage/path"), |
| 14 | + getTaskDirectoryPath: jest.fn().mockReturnValue("/mock/task/path"), |
| 15 | + ensureDirectoryExists: jest.fn().mockResolvedValue(true), |
| 16 | +})) |
| 17 | + |
| 18 | +// More complete vscode mock |
| 19 | +jest.mock("vscode", () => ({ |
| 20 | + workspace: { |
| 21 | + getConfiguration: jest.fn().mockReturnValue({ |
| 22 | + get: jest.fn().mockReturnValue(true), |
| 23 | + }), |
| 24 | + createFileSystemWatcher: jest.fn().mockReturnValue({ |
| 25 | + onDidChange: jest.fn(), |
| 26 | + onDidCreate: jest.fn(), |
| 27 | + onDidDelete: jest.fn(), |
| 28 | + dispose: jest.fn(), |
| 29 | + }), |
| 30 | + }, |
| 31 | + window: { |
| 32 | + createTextEditorDecorationType: jest.fn().mockReturnValue({ |
| 33 | + dispose: jest.fn(), |
| 34 | + }), |
| 35 | + showErrorMessage: jest.fn(), |
| 36 | + tabGroups: { |
| 37 | + all: [], |
| 38 | + }, |
| 39 | + }, |
| 40 | + env: { |
| 41 | + language: "en", |
| 42 | + }, |
| 43 | + RelativePattern: jest.fn().mockImplementation(() => ({})), |
| 44 | +})) |
| 45 | + |
| 46 | +describe("Temperature Override Integration", () => { |
| 47 | + let cline: Cline |
| 48 | + let mockProvider: jest.Mocked<ClineProvider> |
| 49 | + let mockGetConfig: jest.Mock |
| 50 | + let mockTempOverrideService: jest.Mocked<TemperatureOverrideService> |
| 51 | + let mockApi: any |
| 52 | + const defaultTemp = 0 |
| 53 | + beforeEach(() => { |
| 54 | + // Reset mocks |
| 55 | + jest.clearAllMocks() |
| 56 | + |
| 57 | + mockProvider = { |
| 58 | + context: { |
| 59 | + globalStorageUri: { fsPath: "/test/storage" }, |
| 60 | + }, |
| 61 | + postStateToWebview: jest.fn(), |
| 62 | + postMessageToWebview: jest.fn(), |
| 63 | + log: jest.fn(), |
| 64 | + getState: jest.fn().mockReturnValue({ |
| 65 | + terminalOutputLineLimit: 100, |
| 66 | + maxWorkspaceFiles: 50, |
| 67 | + }), |
| 68 | + } as any |
| 69 | + |
| 70 | + mockGetConfig = jest.fn().mockReturnValue(true) |
| 71 | + ;(vscode.workspace.getConfiguration as jest.Mock).mockImplementation(() => ({ |
| 72 | + get: mockGetConfig, |
| 73 | + })) |
| 74 | + |
| 75 | + // Mock the TemperatureOverrideService |
| 76 | + mockTempOverrideService = { |
| 77 | + parseAndApplyOverride: jest.fn().mockImplementation((input, currentTemp) => { |
| 78 | + // Check configuration like the real service does |
| 79 | + const config = vscode.workspace.getConfiguration("roo-cline") |
| 80 | + if (!config.get<boolean>("enableTemperatureOverride", true)) { |
| 81 | + return null |
| 82 | + } |
| 83 | + |
| 84 | + if (input.startsWith("@customTemperature:")) { |
| 85 | + const match = input.match(/^@customTemperature:([^ ]*)/) |
| 86 | + if (match && match[1] === "0.9") { |
| 87 | + // Preserve leading whitespace after command like real service |
| 88 | + return { |
| 89 | + temperature: 0.9, |
| 90 | + originalTemp: currentTemp, |
| 91 | + cleanedInput: input.substring(match[0].length), |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return null |
| 96 | + }), |
| 97 | + } as any |
| 98 | + |
| 99 | + // Set up the mock behavior for the disabled test |
| 100 | + ;(TemperatureOverrideService as jest.Mock).mockImplementation(() => mockTempOverrideService) |
| 101 | + |
| 102 | + // Mock console.error to reduce test noise |
| 103 | + jest.spyOn(console, "error").mockImplementation(() => {}) |
| 104 | + |
| 105 | + // Create mock API with options property |
| 106 | + mockApi = { |
| 107 | + options: { |
| 108 | + modelTemperature: defaultTemp, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + cline = new Cline({ |
| 113 | + provider: mockProvider, |
| 114 | + apiConfiguration: { |
| 115 | + modelTemperature: defaultTemp, |
| 116 | + }, |
| 117 | + task: "test task", |
| 118 | + }) |
| 119 | + |
| 120 | + // Mock the API handler |
| 121 | + Object.defineProperty(cline, "api", { |
| 122 | + get: () => mockApi, |
| 123 | + }) |
| 124 | + |
| 125 | + // Mock providerRef.deref() to return our mockProvider with getState |
| 126 | + Object.defineProperty(cline, "providerRef", { |
| 127 | + get: () => ({ |
| 128 | + deref: () => mockProvider, |
| 129 | + }), |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + afterEach(() => { |
| 134 | + jest.restoreAllMocks() |
| 135 | + }) |
| 136 | + |
| 137 | + describe("initiateTaskLoop", () => { |
| 138 | + it("should not process temperature override when disabled", async () => { |
| 139 | + // Setup the mock to return null for this specific test |
| 140 | + jest.spyOn(mockTempOverrideService, "parseAndApplyOverride").mockReturnValue(null) |
| 141 | + |
| 142 | + const userContent = [ |
| 143 | + { |
| 144 | + type: "text" as const, |
| 145 | + text: "@customTemperature:0.9 Do something", |
| 146 | + }, |
| 147 | + ] |
| 148 | + |
| 149 | + // @ts-ignore - accessing private method for testing |
| 150 | + await cline.initiateTaskLoop(userContent) |
| 151 | + |
| 152 | + expect(cline.apiConfiguration.modelTemperature).toBe(defaultTemp) |
| 153 | + expect(userContent[0].text).toBe("@customTemperature:0.9 Do something") |
| 154 | + }) |
| 155 | + |
| 156 | + it("should apply temperature override when enabled", async () => { |
| 157 | + const userContent = [ |
| 158 | + { |
| 159 | + type: "text" as const, |
| 160 | + text: "@customTemperature:0.9 Do something", |
| 161 | + }, |
| 162 | + ] |
| 163 | + |
| 164 | + // @ts-ignore - accessing private method for testing |
| 165 | + await cline.initiateTaskLoop(userContent) |
| 166 | + |
| 167 | + expect(cline.apiConfiguration.modelTemperature).toBe(0.9) // Should be set to the override value |
| 168 | + expect(userContent[0].text).toBe(" Do something") // Should preserve leading space like real service |
| 169 | + expect(mockGetConfig).toHaveBeenCalledWith("enableTemperatureOverride", true) |
| 170 | + }) |
| 171 | + |
| 172 | + it("should handle invalid temperature override gracefully", async () => { |
| 173 | + const userContent = [ |
| 174 | + { |
| 175 | + type: "text" as const, |
| 176 | + text: "@customTemperature:3.0 Do something", |
| 177 | + }, |
| 178 | + ] |
| 179 | + |
| 180 | + // @ts-ignore - accessing private method for testing |
| 181 | + await cline.initiateTaskLoop(userContent) |
| 182 | + |
| 183 | + // Should not modify temperature when invalid |
| 184 | + expect(cline.apiConfiguration.modelTemperature).toBe(defaultTemp) |
| 185 | + expect(userContent[0].text).toBe("@customTemperature:3.0 Do something") |
| 186 | + }) |
| 187 | + |
| 188 | + it("should handle image blocks without error", async () => { |
| 189 | + const userContent = [ |
| 190 | + { |
| 191 | + type: "image" as const, |
| 192 | + source: "data:image/png;base64,...", |
| 193 | + }, |
| 194 | + ] |
| 195 | + |
| 196 | + // @ts-ignore - accessing private method for testing |
| 197 | + await cline.initiateTaskLoop(userContent) |
| 198 | + |
| 199 | + expect(cline.apiConfiguration.modelTemperature).toBe(defaultTemp) |
| 200 | + }) |
| 201 | + |
| 202 | + // Additional test for provider with direct options access |
| 203 | + it("should update and restore provider options for handlers with direct access", async () => { |
| 204 | + const userContent = [ |
| 205 | + { |
| 206 | + type: "text" as const, |
| 207 | + text: "@customTemperature:0.9 Do something", |
| 208 | + }, |
| 209 | + ] |
| 210 | + |
| 211 | + // @ts-ignore - accessing private method for testing |
| 212 | + await cline.initiateTaskLoop(userContent) |
| 213 | + |
| 214 | + // Check both apiConfiguration and provider options are updated |
| 215 | + expect(cline.apiConfiguration.modelTemperature).toBe(0.9) |
| 216 | + expect(mockApi.options.modelTemperature).toBe(0.9) |
| 217 | + |
| 218 | + // @ts-ignore - accessing private property for testing |
| 219 | + expect(cline.originalTemp).toBe(defaultTemp) |
| 220 | + |
| 221 | + // Call abortTask which restores temperature |
| 222 | + await cline.abortTask() |
| 223 | + |
| 224 | + // Check both are restored |
| 225 | + expect(cline.apiConfiguration.modelTemperature).toBe(defaultTemp) |
| 226 | + expect(mockApi.options.modelTemperature).toBe(defaultTemp) |
| 227 | + // @ts-ignore - accessing private property for testing |
| 228 | + expect(cline.originalTemp).toBeUndefined() |
| 229 | + }) |
| 230 | + }) |
| 231 | +}) |
0 commit comments