|
| 1 | +import { ConfigManager } from "../ConfigManager" |
| 2 | +import { ExtensionContext } from "vscode" |
| 3 | +import path from "path" |
| 4 | +import fs from "fs" |
| 5 | + |
| 6 | +// Mock class to simulate VSCode extension context |
| 7 | +class MockExtensionContext { |
| 8 | + private _globalState: Record<string, any> |
| 9 | + private _secrets: Record<string, any> |
| 10 | + private _storageUri: { fsPath: string } |
| 11 | + private _globalStorageUri: { fsPath: string } |
| 12 | + |
| 13 | + constructor(initialGlobalState = {}, initialSecrets = {}) { |
| 14 | + this._globalState = initialGlobalState |
| 15 | + this._secrets = initialSecrets |
| 16 | + this._storageUri = { fsPath: path.join(__dirname, "mock-storage") } |
| 17 | + this._globalStorageUri = { fsPath: path.join(__dirname, "mock-global-storage") } |
| 18 | + } |
| 19 | + |
| 20 | + get globalState() { |
| 21 | + return { |
| 22 | + get: jest.fn().mockImplementation((key) => Promise.resolve(this._globalState[key])), |
| 23 | + update: jest.fn().mockImplementation((key, value) => { |
| 24 | + this._globalState[key] = value |
| 25 | + return Promise.resolve() |
| 26 | + }), |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + get secrets() { |
| 31 | + return { |
| 32 | + get: jest.fn().mockImplementation((key) => Promise.resolve(this._secrets[key])), |
| 33 | + store: jest.fn().mockImplementation((key, value) => { |
| 34 | + this._secrets[key] = value |
| 35 | + return Promise.resolve() |
| 36 | + }), |
| 37 | + delete: jest.fn().mockImplementation((key) => { |
| 38 | + delete this._secrets[key] |
| 39 | + return Promise.resolve() |
| 40 | + }), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + get storageUri() { |
| 45 | + return this._storageUri |
| 46 | + } |
| 47 | + |
| 48 | + get globalStorageUri() { |
| 49 | + return this._globalStorageUri |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe("Rate Limit Migration Tests", () => { |
| 54 | + beforeEach(() => { |
| 55 | + jest.clearAllMocks() |
| 56 | + }) |
| 57 | + |
| 58 | + it("should migrate existing global rate limit to all profiles", async () => { |
| 59 | + // Case 1: Test migration with existing global rate limit |
| 60 | + const context1 = new MockExtensionContext( |
| 61 | + { rateLimitSeconds: 10 }, // Global state with rateLimitSeconds set to 10 |
| 62 | + { |
| 63 | + roo_cline_config_api_config: JSON.stringify({ |
| 64 | + currentApiConfigName: "default", |
| 65 | + apiConfigs: { |
| 66 | + default: { id: "abc123" }, |
| 67 | + profile1: { id: "def456", apiProvider: "anthropic" }, |
| 68 | + profile2: { id: "ghi789", apiProvider: "openrouter" }, |
| 69 | + }, |
| 70 | + }), |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + // Use a type assertion to bypass TypeScript's type checking |
| 75 | + const configManager1 = new ConfigManager(context1 as unknown as ExtensionContext) |
| 76 | + |
| 77 | + // Wait for initialization to complete |
| 78 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 79 | + |
| 80 | + // Check if the migration was applied |
| 81 | + const config1 = JSON.parse((await context1.secrets.get("roo_cline_config_api_config")) as string) |
| 82 | + |
| 83 | + // Verify migrations flag is set |
| 84 | + expect(config1.migrations.rateLimitMigrated).toBeTruthy() |
| 85 | + |
| 86 | + // Verify rate limits were applied to all profiles |
| 87 | + expect(config1.apiConfigs.default.rateLimitSeconds).toBe(10) |
| 88 | + expect(config1.apiConfigs.profile1.rateLimitSeconds).toBe(10) |
| 89 | + expect(config1.apiConfigs.profile2.rateLimitSeconds).toBe(10) |
| 90 | + }) |
| 91 | + |
| 92 | + it("should use default rate limit when no global rate limit exists", async () => { |
| 93 | + // Case 2: Test migration without global rate limit (should use default value) |
| 94 | + const context2 = new MockExtensionContext( |
| 95 | + {}, // No global state |
| 96 | + { |
| 97 | + roo_cline_config_api_config: JSON.stringify({ |
| 98 | + currentApiConfigName: "default", |
| 99 | + apiConfigs: { |
| 100 | + default: { id: "abc123" }, |
| 101 | + profile1: { id: "def456", apiProvider: "anthropic" }, |
| 102 | + }, |
| 103 | + }), |
| 104 | + }, |
| 105 | + ) |
| 106 | + |
| 107 | + // Use a type assertion to bypass TypeScript's type checking |
| 108 | + const configManager2 = new ConfigManager(context2 as unknown as ExtensionContext) |
| 109 | + |
| 110 | + // Wait for initialization to complete |
| 111 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 112 | + |
| 113 | + // Check if the migration was applied with default value |
| 114 | + const config2 = JSON.parse((await context2.secrets.get("roo_cline_config_api_config")) as string) |
| 115 | + |
| 116 | + // Verify migrations flag is set |
| 117 | + expect(config2.migrations.rateLimitMigrated).toBeTruthy() |
| 118 | + |
| 119 | + // Verify default rate limits were applied |
| 120 | + expect(config2.apiConfigs.default.rateLimitSeconds).toBe(5) // Default is 5 |
| 121 | + expect(config2.apiConfigs.profile1.rateLimitSeconds).toBe(5) // Default is 5 |
| 122 | + }) |
| 123 | + |
| 124 | + it("should apply rate limit to newly created profiles", async () => { |
| 125 | + // Case 3: Test creating new profile via saveConfig |
| 126 | + const context3 = new MockExtensionContext( |
| 127 | + { rateLimitSeconds: 15 }, // Global state with rateLimitSeconds set to 15 |
| 128 | + { |
| 129 | + roo_cline_config_api_config: JSON.stringify({ |
| 130 | + currentApiConfigName: "default", |
| 131 | + apiConfigs: { |
| 132 | + default: { id: "abc123", rateLimitSeconds: 8 }, |
| 133 | + }, |
| 134 | + migrations: { rateLimitMigrated: true }, |
| 135 | + }), |
| 136 | + }, |
| 137 | + ) |
| 138 | + |
| 139 | + // Use a type assertion to bypass TypeScript's type checking |
| 140 | + const configManager3 = new ConfigManager(context3 as unknown as ExtensionContext) |
| 141 | + |
| 142 | + // Wait for initialization to complete |
| 143 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
| 144 | + |
| 145 | + // Save a new profile |
| 146 | + await configManager3.saveConfig("newProfile", { apiProvider: "anthropic" }) |
| 147 | + |
| 148 | + // Check if the new profile got a rate limit from existing profiles |
| 149 | + const config3 = JSON.parse((await context3.secrets.get("roo_cline_config_api_config")) as string) |
| 150 | + |
| 151 | + // The new profile should inherit rate limit from existing profiles (8s) |
| 152 | + expect(config3.apiConfigs.newProfile.rateLimitSeconds).toBe(8) |
| 153 | + expect(config3.apiConfigs.newProfile.apiProvider).toBe("anthropic") |
| 154 | + }) |
| 155 | +}) |
0 commit comments