-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathonboarding.test.ts
More file actions
377 lines (309 loc) · 11.4 KB
/
Copy pathonboarding.test.ts
File metadata and controls
377 lines (309 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import type { AuthConfig } from "./auth/workos.js";
import { initializeWithOnboarding } from "./onboarding.js";
describe("onboarding config flag handling", () => {
let tempDir: string;
let mockAuthConfig: AuthConfig;
beforeEach(() => {
// Create a temporary directory for test config files
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "continue-test-"));
// Create a minimal auth config for testing
mockAuthConfig = {
userId: "test-user",
userEmail: "test@example.com",
accessToken: "test-token",
refreshToken: "test-refresh",
expiresAt: Date.now() + 3600000,
organizationId: "test-org",
};
});
afterEach(() => {
// Clean up temporary directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
test("should fail loudly when --config points to non-existent file", async () => {
const configPath = path.join(tempDir, "non-existent.yaml");
// Verify the file doesn't exist
expect(fs.existsSync(configPath)).toBe(false);
// Should throw an error that mentions both the path and the failure
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(
/Failed to load config from ".*non-existent\.yaml": .*ENOENT/,
);
});
test("should fail loudly when --config points to malformed YAML file", async () => {
const configPath = path.join(tempDir, "malformed.yaml");
// Create a malformed YAML file
fs.writeFileSync(
configPath,
`
name: "Test Config"
models:
- name: "GPT-4"
provider: "openai"
invalid_yaml_syntax: [unclosed array
`,
);
// Verify the file exists
expect(fs.existsSync(configPath)).toBe(true);
// Should throw an error mentioning the path and failure to load
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(/Failed to load config from ".*malformed\.yaml": .+/);
});
test("should fail loudly when --config points to file with missing required fields", async () => {
const configPath = path.join(tempDir, "incomplete.yaml");
// Create a config file missing required fields
fs.writeFileSync(
configPath,
`
name: "Incomplete Config"
# Missing models array and other required fields
`,
);
// Verify the file exists
expect(fs.existsSync(configPath)).toBe(true);
// Should throw with our specific error format and include path
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(/^Failed to load config from ".*": .+/);
});
test("should handle different config path formats with proper error messages", async () => {
const testPaths = [
"./non-existent.yaml",
"/absolute/path/config.yaml",
"../relative/config.yaml",
"simple-name.yaml",
];
for (const configPath of testPaths) {
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(/Failed to load config from ".*": .+/);
}
});
test("should handle empty string config path", async () => {
// Loads default agent with no error
await initializeWithOnboarding(mockAuthConfig, "");
});
test("should not fall back to default config when explicit config fails", async () => {
const configPath = path.join(tempDir, "bad-config.yaml");
// Create a bad config file
fs.writeFileSync(configPath, "invalid: yaml: content: [");
const promise = initializeWithOnboarding(mockAuthConfig, configPath);
await expect(promise).rejects.toThrow();
try {
await promise;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
// CRITICAL: Must have our specific error format from the fix
expect(message).toMatch(/^Failed to load config from ".*": .+/);
// Error should be about the specific config file we provided
expect(message).toContain(configPath);
// Should NOT mention falling back to default config (this was the bug!)
expect(message).not.toContain("~/.continue/config.yaml");
expect(message).not.toContain("default config");
expect(message).not.toContain("fallback");
}
});
test("demonstrates the fix: explicit config failure vs no config provided", async () => {
const badConfigPath = path.join(tempDir, "bad.yaml");
fs.writeFileSync(badConfigPath, "invalid yaml [");
// Case 1: Explicit --config that fails should throw our specific error
await expect(
initializeWithOnboarding(mockAuthConfig, badConfigPath),
).rejects.toThrow(/^Failed to load config from "/);
// Case 2: No explicit config should follow different logic
try {
await initializeWithOnboarding(mockAuthConfig, undefined);
// If it succeeds, that's fine - the point is it's different behavior
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
// This should NOT have our "Failed to load config from" prefix
expect(errorMessage).not.toMatch(/^Failed to load config from "/);
}
});
});
describe("onboarding local config handling", () => {
let tempDir: string;
let originalContinueGlobalDir: string | undefined;
let originalNodeEnv: string | undefined;
let originalCi: string | undefined;
let originalVitest: string | undefined;
let originalGithubActions: string | undefined;
let originalIsTTY: boolean;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "continue-home-"));
originalContinueGlobalDir = process.env.CONTINUE_GLOBAL_DIR;
originalNodeEnv = process.env.NODE_ENV;
originalCi = process.env.CI;
originalVitest = process.env.VITEST;
originalGithubActions = process.env.GITHUB_ACTIONS;
originalIsTTY = process.stdin.isTTY;
process.env.CONTINUE_GLOBAL_DIR = tempDir;
vi.resetModules();
vi.doMock("./auth/workos.js", () => ({
login: vi.fn(),
}));
vi.doMock("./config.js", () => ({
getApiClient: vi.fn(() => ({})),
}));
vi.doMock("./configLoader.js", () => ({
loadConfiguration: vi.fn(),
}));
});
afterEach(() => {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, {
force: true,
maxRetries: 3,
recursive: true,
retryDelay: 100,
});
}
if (originalContinueGlobalDir === undefined) {
delete process.env.CONTINUE_GLOBAL_DIR;
} else {
process.env.CONTINUE_GLOBAL_DIR = originalContinueGlobalDir;
}
if (originalNodeEnv === undefined) {
delete process.env.NODE_ENV;
} else {
process.env.NODE_ENV = originalNodeEnv;
}
if (originalCi === undefined) {
delete process.env.CI;
} else {
process.env.CI = originalCi;
}
if (originalVitest === undefined) {
delete process.env.VITEST;
} else {
process.env.VITEST = originalVitest;
}
if (originalGithubActions === undefined) {
delete process.env.GITHUB_ACTIONS;
} else {
process.env.GITHUB_ACTIONS = originalGithubActions;
}
process.stdin.isTTY = originalIsTTY;
vi.doUnmock("./auth/workos.js");
vi.doUnmock("./config.js");
vi.doUnmock("./configLoader.js");
vi.doUnmock("./util/prompt.js");
vi.resetModules();
});
test("should skip interactive onboarding when default config.yaml exists", async () => {
fs.writeFileSync(path.join(tempDir, "config.yaml"), "name: Local Config\n");
delete process.env.NODE_ENV;
delete process.env.CI;
delete process.env.VITEST;
delete process.env.GITHUB_ACTIONS;
process.stdin.isTTY = true;
const questionWithChoices = vi
.fn()
.mockRejectedValue(new Error("prompted"));
vi.doMock("./util/prompt.js", () => ({
question: vi.fn(),
questionWithChoices,
}));
const { runOnboardingFlow } = await import("./onboarding.js");
await expect(runOnboardingFlow(undefined)).resolves.toBe(false);
expect(questionWithChoices).not.toHaveBeenCalled();
});
test("should mark onboarding complete after a successful --config load", async () => {
const configPath = path.join(tempDir, "custom-config.yaml");
const flagPath = path.join(tempDir, ".onboarding_complete");
const loadConfiguration = vi.fn().mockResolvedValue({
config: { name: "Custom Config" },
source: { path: configPath, type: "cli-flag" },
});
vi.doMock("./configLoader.js", () => ({
loadConfiguration,
}));
const { initializeWithOnboarding } = await import("./onboarding.js");
await initializeWithOnboarding(null, configPath);
expect(loadConfiguration).toHaveBeenCalledOnce();
expect(fs.existsSync(flagPath)).toBe(true);
});
});
// Separate describe block with its own mocking for BEDROCK tests
describe("CONTINUE_USE_BEDROCK environment variable", () => {
const mockConsoleLog = vi.fn();
let mockAuthConfig: AuthConfig;
const originalEnv = process.env.CONTINUE_USE_BEDROCK;
// Mock initialize for these tests only
const mockInitialize = vi.fn().mockResolvedValue({
config: { name: "test-config", models: [], rules: [] },
llmApi: {},
model: { name: "test-model" },
mcpService: {},
apiClient: {},
});
beforeEach(() => {
mockConsoleLog.mockClear();
mockInitialize.mockClear();
// Spy on console.log for these tests
vi.spyOn(console, "log").mockImplementation(mockConsoleLog);
// Mock the config module
vi.doMock("./config.js", () => ({ initialize: mockInitialize }));
mockAuthConfig = {
userId: "test-user",
userEmail: "test@example.com",
accessToken: "test-token",
refreshToken: "test-refresh",
expiresAt: Date.now() + 3600000,
organizationId: "test-org",
};
});
afterEach(() => {
if (originalEnv) {
process.env.CONTINUE_USE_BEDROCK = originalEnv;
} else {
delete process.env.CONTINUE_USE_BEDROCK;
}
vi.restoreAllMocks();
vi.doUnmock("./config.js");
});
test("should bypass interactive options when CONTINUE_USE_BEDROCK=1", async () => {
process.env.CONTINUE_USE_BEDROCK = "1";
// Re-import to get the mocked version
vi.resetModules();
const { runOnboardingFlow } = await import("./onboarding.js");
const result = await runOnboardingFlow(undefined);
expect(result).toBe(true);
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining(
"✓ Using AWS Bedrock (CONTINUE_USE_BEDROCK detected)",
),
);
});
test("should not bypass when CONTINUE_USE_BEDROCK is not '1'", async () => {
process.env.CONTINUE_USE_BEDROCK = "0";
// Re-import to get the mocked version
vi.resetModules();
const { runOnboardingFlow } = await import("./onboarding.js");
// Mock non-interactive environment to avoid hanging
const originalIsTTY = process.stdin.isTTY;
process.stdin.isTTY = false;
try {
await runOnboardingFlow(undefined);
// Verify the Bedrock message was NOT called by checking all calls
const allCalls = mockConsoleLog.mock.calls.flat();
const hasBedrockMessage = allCalls.some((call) =>
String(call).includes(
"✓ Using AWS Bedrock (CONTINUE_USE_BEDROCK detected)",
),
);
expect(hasBedrockMessage).toBe(false);
} finally {
process.stdin.isTTY = originalIsTTY;
}
});
});