Elffuss BraidHub

kikocis/continue public

94488b344dec · 8.6 KiBRawPermalink
1import { SecretType } from "@continuedev/config-yaml";
2import { BrowserSerializedContinueConfig } from "core";
3import { usesFreeTrialApiKey } from "./freeTrialHelpers";
4
5// Mock the decodeSecretLocation function
6vi.mock("@continuedev/config-yaml", async () => {
7 const actual = await vi.importActual("@continuedev/config-yaml");
8 return {
9 ...actual,
10 decodeSecretLocation: vi.fn(),
11 };
12});
13
14const { decodeSecretLocation } = await import("@continuedev/config-yaml");
15
16describe("usesFreeTrialApiKey", () => {
17 beforeEach(() => {
18 vi.clearAllMocks();
19 });
20
21 it("should return false when config is null", () => {
22 const result = usesFreeTrialApiKey(null);
23 expect(result).toBe(false);
24 });
25
26 it("should return false when config is undefined", () => {
27 const result = usesFreeTrialApiKey(undefined as any);
28 expect(result).toBe(false);
29 });
30
31 it("should return false when no models have apiKeyLocation", () => {
32 const config: BrowserSerializedContinueConfig = {
33 modelsByRole: {
34 chat: [
35 {
36 title: "Model 1",
37 provider: "test",
38 model: "test-model",
39 underlyingProviderName: "test",
40 },
41 {
42 title: "Model 2",
43 provider: "test",
44 model: "test-model-2",
45 underlyingProviderName: "test",
46 },
47 ],
48 edit: [],
49 apply: [],
50 summarize: [],
51 autocomplete: [],
52 rerank: [],
53 embed: [],
54 },
55 selectedModelByRole: {
56 chat: null,
57 edit: null,
58 apply: null,
59 summarize: null,
60 autocomplete: null,
61 rerank: null,
62 embed: null,
63 },
64 contextProviders: [],
65 slashCommands: [],
66 tools: [],
67 mcpServerStatuses: [],
68 usePlatform: false,
69 rules: [],
70 };
71
72 const result = usesFreeTrialApiKey(config);
73 expect(result).toBe(false);
74 });
75
76 it("should return false when models have apiKeyLocation but none are free trial", () => {
77 const config: BrowserSerializedContinueConfig = {
78 modelsByRole: {
79 chat: [
80 {
81 title: "Model 1",
82 provider: "test",
83 model: "test-model",
84 apiKeyLocation: "user:testuser/api-key",
85 underlyingProviderName: "test",
86 },
87 ],
88 edit: [
89 {
90 title: "Model 2",
91 provider: "test",
92 model: "test-model-2",
93 apiKeyLocation: "organization:testorg/api-key",
94 underlyingProviderName: "test",
95 },
96 ],
97 apply: [],
98 summarize: [],
99 autocomplete: [],
100 rerank: [],
101 embed: [],
102 },
103 selectedModelByRole: {
104 chat: null,
105 edit: null,
106 apply: null,
107 summarize: null,
108 autocomplete: null,
109 rerank: null,
110 embed: null,
111 },
112 contextProviders: [],
113 slashCommands: [],
114 tools: [],
115 mcpServerStatuses: [],
116 usePlatform: false,
117 rules: [],
118 };
119
120 vi.mocked(decodeSecretLocation)
121 .mockReturnValueOnce({
122 secretType: SecretType.User,
123 userSlug: "testuser",
124 secretName: "api-key",
125 })
126 .mockReturnValueOnce({
127 secretType: SecretType.Organization,
128 orgSlug: "testorg",
129 secretName: "api-key",
130 });
131
132 const result = usesFreeTrialApiKey(config);
133 expect(result).toBe(false);
134 expect(decodeSecretLocation).toHaveBeenCalledTimes(2);
135 });
136
137 it("should return true when at least one model uses free trial API key", () => {
138 const config: BrowserSerializedContinueConfig = {
139 modelsByRole: {
140 chat: [
141 {
142 title: "Model 1",
143 provider: "test",
144 model: "test-model",
145 apiKeyLocation: "user:testuser/api-key",
146 underlyingProviderName: "test",
147 },
148 {
149 title: "Free Trial Model",
150 provider: "test",
151 model: "free-trial-model",
152 apiKeyLocation: "free_trial:owner/package/api-key",
153 underlyingProviderName: "test",
154 },
155 ],
156 edit: [],
157 apply: [],
158 summarize: [],
159 autocomplete: [],
160 rerank: [],
161 embed: [],
162 },
163 selectedModelByRole: {
164 chat: null,
165 edit: null,
166 apply: null,
167 summarize: null,
168 autocomplete: null,
169 rerank: null,
170 embed: null,
171 },
172 contextProviders: [],
173 slashCommands: [],
174 tools: [],
175 mcpServerStatuses: [],
176 usePlatform: false,
177 rules: [],
178 };
179
180 vi.mocked(decodeSecretLocation)
181 .mockReturnValueOnce({
182 secretType: SecretType.User,
183 userSlug: "testuser",
184 secretName: "api-key",
185 })
186 .mockReturnValueOnce({
187 secretType: SecretType.FreeTrial,
188 blockSlug: { ownerSlug: "owner", packageSlug: "package" },
189 secretName: "api-key",
190 });
191
192 const result = usesFreeTrialApiKey(config);
193 expect(result).toBe(true);
194 expect(decodeSecretLocation).toHaveBeenCalledTimes(2);
195 });
196
197 it("should return true when free trial model is in a different role", () => {
198 const config: BrowserSerializedContinueConfig = {
199 modelsByRole: {
200 chat: [
201 {
202 title: "Model 1",
203 provider: "test",
204 model: "test-model",
205 apiKeyLocation: "user:testuser/api-key",
206 underlyingProviderName: "test",
207 },
208 ],
209 edit: [
210 {
211 title: "Free Trial Edit Model",
212 provider: "test",
213 model: "free-trial-edit-model",
214 apiKeyLocation: "free_trial:owner/package/api-key",
215 underlyingProviderName: "test",
216 },
217 ],
218 apply: [],
219 summarize: [],
220 autocomplete: [],
221 rerank: [],
222 embed: [],
223 },
224 selectedModelByRole: {
225 chat: null,
226 edit: null,
227 apply: null,
228 summarize: null,
229 autocomplete: null,
230 rerank: null,
231 embed: null,
232 },
233 contextProviders: [],
234 slashCommands: [],
235 tools: [],
236 mcpServerStatuses: [],
237 usePlatform: false,
238 rules: [],
239 };
240
241 vi.mocked(decodeSecretLocation)
242 .mockReturnValueOnce({
243 secretType: SecretType.User,
244 userSlug: "testuser",
245 secretName: "api-key",
246 })
247 .mockReturnValueOnce({
248 secretType: SecretType.FreeTrial,
249 blockSlug: { ownerSlug: "owner", packageSlug: "package" },
250 secretName: "api-key",
251 });
252
253 const result = usesFreeTrialApiKey(config);
254 expect(result).toBe(true);
255 });
256
257 it("should return false and log error when decodeSecretLocation throws", () => {
258 const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
259
260 const config: BrowserSerializedContinueConfig = {
261 modelsByRole: {
262 chat: [
263 {
264 title: "Model 1",
265 provider: "test",
266 model: "test-model",
267 apiKeyLocation: "invalid-secret-location",
268 underlyingProviderName: "test",
269 },
270 ],
271 edit: [],
272 apply: [],
273 summarize: [],
274 autocomplete: [],
275 rerank: [],
276 embed: [],
277 },
278 selectedModelByRole: {
279 chat: null,
280 edit: null,
281 apply: null,
282 summarize: null,
283 autocomplete: null,
284 rerank: null,
285 embed: null,
286 },
287 contextProviders: [],
288 slashCommands: [],
289 tools: [],
290 mcpServerStatuses: [],
291 usePlatform: false,
292 rules: [],
293 };
294
295 vi.mocked(decodeSecretLocation).mockImplementation(() => {
296 throw new Error("Invalid secret location format");
297 });
298
299 const result = usesFreeTrialApiKey(config);
300 expect(result).toBe(false);
301 expect(consoleSpy).toHaveBeenCalledWith(
302 "Error checking for free trial API key:",
303 expect.any(Error),
304 );
305
306 consoleSpy.mockRestore();
307 });
308
309 it("should handle empty modelsByRole object", () => {
310 const config: BrowserSerializedContinueConfig = {
311 modelsByRole: {
312 chat: [],
313 edit: [],
314 apply: [],
315 summarize: [],
316 autocomplete: [],
317 rerank: [],
318 embed: [],
319 },
320 selectedModelByRole: {
321 chat: null,
322 edit: null,
323 apply: null,
324 summarize: null,
325 autocomplete: null,
326 rerank: null,
327 embed: null,
328 },
329 contextProviders: [],
330 slashCommands: [],
331 tools: [],
332 mcpServerStatuses: [],
333 usePlatform: false,
334 rules: [],
335 };
336
337 const result = usesFreeTrialApiKey(config);
338 expect(result).toBe(false);
339 expect(decodeSecretLocation).not.toHaveBeenCalled();
340 });
341});