Skip to content

Commit 5563846

Browse files
committed
fix tests
1 parent 452da6e commit 5563846

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

extensions/cli/src/onboarding.test.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from "path";
55
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
66

77
import type { AuthConfig } from "./auth/workos.js";
8-
import { runNormalFlow } from "./onboarding.js";
8+
import { initializeWithOnboarding } from "./onboarding.js";
99

1010
describe("onboarding config flag handling", () => {
1111
let tempDir: string;
@@ -40,7 +40,9 @@ describe("onboarding config flag handling", () => {
4040
expect(fs.existsSync(configPath)).toBe(false);
4141

4242
// Should throw an error that mentions both the path and the failure
43-
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
43+
await expect(
44+
initializeWithOnboarding(mockAuthConfig, configPath),
45+
).rejects.toThrow(
4446
/Failed to load config from ".*non-existent\.yaml": .*ENOENT/,
4547
);
4648
});
@@ -64,9 +66,9 @@ models:
6466
expect(fs.existsSync(configPath)).toBe(true);
6567

6668
// Should throw an error mentioning the path and failure to load
67-
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
68-
/Failed to load config from ".*malformed\.yaml": .+/,
69-
);
69+
await expect(
70+
initializeWithOnboarding(mockAuthConfig, configPath),
71+
).rejects.toThrow(/Failed to load config from ".*malformed\.yaml": .+/);
7072
});
7173

7274
test("should fail loudly when --config points to file with missing required fields", async () => {
@@ -85,9 +87,9 @@ name: "Incomplete Config"
8587
expect(fs.existsSync(configPath)).toBe(true);
8688

8789
// Should throw with our specific error format and include path
88-
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
89-
/^Failed to load config from ".*": .+/,
90-
);
90+
await expect(
91+
initializeWithOnboarding(mockAuthConfig, configPath),
92+
).rejects.toThrow(/^Failed to load config from ".*": .+/);
9193
});
9294

9395
test("should handle different config path formats with proper error messages", async () => {
@@ -99,16 +101,18 @@ name: "Incomplete Config"
99101
];
100102

101103
for (const configPath of testPaths) {
102-
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
103-
/Failed to load config from ".*": .+/,
104-
);
104+
await expect(
105+
initializeWithOnboarding(mockAuthConfig, configPath),
106+
).rejects.toThrow(/Failed to load config from ".*": .+/);
105107
}
106108
});
107109

108110
test("should handle empty string config path", async () => {
109111
// Empty string should be treated differently from undefined
110112
// Note: empty string triggers onboarding flow, but should still fail in our error format
111-
await expect(runNormalFlow(mockAuthConfig, "")).rejects.toThrow();
113+
await expect(
114+
initializeWithOnboarding(mockAuthConfig, ""),
115+
).rejects.toThrow();
112116
});
113117

114118
test("should not fall back to default config when explicit config fails", async () => {
@@ -117,7 +121,7 @@ name: "Incomplete Config"
117121
// Create a bad config file
118122
fs.writeFileSync(configPath, "invalid: yaml: content: [");
119123

120-
const promise = runNormalFlow(mockAuthConfig, configPath);
124+
const promise = initializeWithOnboarding(mockAuthConfig, configPath);
121125

122126
await expect(promise).rejects.toThrow();
123127

@@ -144,13 +148,13 @@ name: "Incomplete Config"
144148
fs.writeFileSync(badConfigPath, "invalid yaml [");
145149

146150
// Case 1: Explicit --config that fails should throw our specific error
147-
await expect(runNormalFlow(mockAuthConfig, badConfigPath)).rejects.toThrow(
148-
/^Failed to load config from "/,
149-
);
151+
await expect(
152+
initializeWithOnboarding(mockAuthConfig, badConfigPath),
153+
).rejects.toThrow(/^Failed to load config from "/);
150154

151155
// Case 2: No explicit config should follow different logic
152156
try {
153-
await runNormalFlow(mockAuthConfig, undefined);
157+
await initializeWithOnboarding(mockAuthConfig, undefined);
154158
// If it succeeds, that's fine - the point is it's different behavior
155159
} catch (error) {
156160
const errorMessage =
@@ -213,9 +217,9 @@ describe("CONTINUE_USE_BEDROCK environment variable", () => {
213217
vi.resetModules();
214218
const { runOnboardingFlow } = await import("./onboarding.js");
215219

216-
const result = await runOnboardingFlow(undefined, mockAuthConfig);
220+
const result = await runOnboardingFlow(undefined);
217221

218-
expect(result.wasOnboarded).toBe(true);
222+
expect(result).toBe(true);
219223
expect(mockConsoleLog).toHaveBeenCalledWith(
220224
expect.stringContaining(
221225
"✓ Using AWS Bedrock (CONTINUE_USE_BEDROCK detected)",
@@ -235,7 +239,7 @@ describe("CONTINUE_USE_BEDROCK environment variable", () => {
235239
process.stdin.isTTY = false;
236240

237241
try {
238-
await runOnboardingFlow(undefined, mockAuthConfig);
242+
await runOnboardingFlow(undefined);
239243

240244
// Verify the Bedrock message was NOT called by checking all calls
241245
const allCalls = mockConsoleLog.mock.calls.flat();

extensions/cli/src/onboarding.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import * as path from "path";
33

44
import chalk from "chalk";
55

6-
import { login } from "./auth/workos.js";
6+
import { AuthConfig, login } from "./auth/workos.js";
7+
import { getApiClient } from "./config.js";
8+
import { loadConfiguration } from "./configLoader.js";
79
import { env } from "./env.js";
810
import {
911
getApiKeyValidationError,
@@ -131,9 +133,27 @@ export async function markOnboardingComplete(): Promise<void> {
131133
fs.writeFileSync(flagPath, new Date().toISOString());
132134
}
133135

134-
export async function initializeWithOnboarding(configPath: string | undefined) {
136+
export async function initializeWithOnboarding(
137+
authConfig: AuthConfig,
138+
configPath: string | undefined,
139+
) {
135140
const firstTime = await isFirstTime();
136141

142+
if (configPath !== undefined) {
143+
// throw an early error is configPath is invalid or has errors
144+
try {
145+
await loadConfiguration(
146+
authConfig,
147+
configPath,
148+
getApiClient(authConfig?.accessToken),
149+
);
150+
} catch (errorMessage) {
151+
throw new Error(
152+
`Failed to load config from "${configPath}": ${errorMessage}`,
153+
);
154+
}
155+
}
156+
137157
if (!firstTime) return;
138158

139159
const wasOnboarded = await runOnboardingFlow(configPath);

extensions/cli/src/services/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { loadAuthConfig } from "../auth/workos.js";
12
import { initializeWithOnboarding } from "../onboarding.js";
23
import { logger } from "../util/logger.js";
34

@@ -53,7 +54,8 @@ export async function initializeServices(initOptions: ServiceInitOptions = {}) {
5354

5455
// Handle onboarding for TUI mode (headless: false) unless explicitly skipped
5556
if (!initOptions.headless && !initOptions.skipOnboarding) {
56-
await initializeWithOnboarding(commandOptions.config);
57+
const authConfig = loadAuthConfig();
58+
await initializeWithOnboarding(authConfig, commandOptions.config);
5759
}
5860

5961
// Handle ANTHROPIC_API_KEY in headless mode when no config path is provided

0 commit comments

Comments
 (0)