fix tests

This commit is contained in:
uinstinct 2025-10-09 11:19:51 +05:30
parent 452da6eaf0
commit 55638464c5
3 changed files with 49 additions and 23 deletions

View file

@ -5,7 +5,7 @@ import * as path from "path";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import type { AuthConfig } from "./auth/workos.js";
import { runNormalFlow } from "./onboarding.js";
import { initializeWithOnboarding } from "./onboarding.js";
describe("onboarding config flag handling", () => {
let tempDir: string;
@ -40,7 +40,9 @@ describe("onboarding config flag handling", () => {
expect(fs.existsSync(configPath)).toBe(false);
// Should throw an error that mentions both the path and the failure
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(
/Failed to load config from ".*non-existent\.yaml": .*ENOENT/,
);
});
@ -64,9 +66,9 @@ models:
expect(fs.existsSync(configPath)).toBe(true);
// Should throw an error mentioning the path and failure to load
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
/Failed to load config from ".*malformed\.yaml": .+/,
);
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 () => {
@ -85,9 +87,9 @@ name: "Incomplete Config"
expect(fs.existsSync(configPath)).toBe(true);
// Should throw with our specific error format and include path
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
/^Failed to load config from ".*": .+/,
);
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(/^Failed to load config from ".*": .+/);
});
test("should handle different config path formats with proper error messages", async () => {
@ -99,16 +101,18 @@ name: "Incomplete Config"
];
for (const configPath of testPaths) {
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
/Failed to load config from ".*": .+/,
);
await expect(
initializeWithOnboarding(mockAuthConfig, configPath),
).rejects.toThrow(/Failed to load config from ".*": .+/);
}
});
test("should handle empty string config path", async () => {
// Empty string should be treated differently from undefined
// Note: empty string triggers onboarding flow, but should still fail in our error format
await expect(runNormalFlow(mockAuthConfig, "")).rejects.toThrow();
await expect(
initializeWithOnboarding(mockAuthConfig, ""),
).rejects.toThrow();
});
test("should not fall back to default config when explicit config fails", async () => {
@ -117,7 +121,7 @@ name: "Incomplete Config"
// Create a bad config file
fs.writeFileSync(configPath, "invalid: yaml: content: [");
const promise = runNormalFlow(mockAuthConfig, configPath);
const promise = initializeWithOnboarding(mockAuthConfig, configPath);
await expect(promise).rejects.toThrow();
@ -144,13 +148,13 @@ name: "Incomplete Config"
fs.writeFileSync(badConfigPath, "invalid yaml [");
// Case 1: Explicit --config that fails should throw our specific error
await expect(runNormalFlow(mockAuthConfig, badConfigPath)).rejects.toThrow(
/^Failed to load config from "/,
);
await expect(
initializeWithOnboarding(mockAuthConfig, badConfigPath),
).rejects.toThrow(/^Failed to load config from "/);
// Case 2: No explicit config should follow different logic
try {
await runNormalFlow(mockAuthConfig, undefined);
await initializeWithOnboarding(mockAuthConfig, undefined);
// If it succeeds, that's fine - the point is it's different behavior
} catch (error) {
const errorMessage =
@ -213,9 +217,9 @@ describe("CONTINUE_USE_BEDROCK environment variable", () => {
vi.resetModules();
const { runOnboardingFlow } = await import("./onboarding.js");
const result = await runOnboardingFlow(undefined, mockAuthConfig);
const result = await runOnboardingFlow(undefined);
expect(result.wasOnboarded).toBe(true);
expect(result).toBe(true);
expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining(
"✓ Using AWS Bedrock (CONTINUE_USE_BEDROCK detected)",
@ -235,7 +239,7 @@ describe("CONTINUE_USE_BEDROCK environment variable", () => {
process.stdin.isTTY = false;
try {
await runOnboardingFlow(undefined, mockAuthConfig);
await runOnboardingFlow(undefined);
// Verify the Bedrock message was NOT called by checking all calls
const allCalls = mockConsoleLog.mock.calls.flat();

View file

@ -3,7 +3,9 @@ import * as path from "path";
import chalk from "chalk";
import { login } from "./auth/workos.js";
import { AuthConfig, login } from "./auth/workos.js";
import { getApiClient } from "./config.js";
import { loadConfiguration } from "./configLoader.js";
import { env } from "./env.js";
import {
getApiKeyValidationError,
@ -131,9 +133,27 @@ export async function markOnboardingComplete(): Promise<void> {
fs.writeFileSync(flagPath, new Date().toISOString());
}
export async function initializeWithOnboarding(configPath: string | undefined) {
export async function initializeWithOnboarding(
authConfig: AuthConfig,
configPath: string | undefined,
) {
const firstTime = await isFirstTime();
if (configPath !== undefined) {
// throw an early error is configPath is invalid or has errors
try {
await loadConfiguration(
authConfig,
configPath,
getApiClient(authConfig?.accessToken),
);
} catch (errorMessage) {
throw new Error(
`Failed to load config from "${configPath}": ${errorMessage}`,
);
}
}
if (!firstTime) return;
const wasOnboarded = await runOnboardingFlow(configPath);

View file

@ -1,3 +1,4 @@
import { loadAuthConfig } from "../auth/workos.js";
import { initializeWithOnboarding } from "../onboarding.js";
import { logger } from "../util/logger.js";
@ -53,7 +54,8 @@ export async function initializeServices(initOptions: ServiceInitOptions = {}) {
// Handle onboarding for TUI mode (headless: false) unless explicitly skipped
if (!initOptions.headless && !initOptions.skipOnboarding) {
await initializeWithOnboarding(commandOptions.config);
const authConfig = loadAuthConfig();
await initializeWithOnboarding(authConfig, commandOptions.config);
}
// Handle ANTHROPIC_API_KEY in headless mode when no config path is provided