test: clean up onboarding tests per code review feedback

- Replace fail() calls with expect().rejects.toThrow() pattern
- Fix test syntax error with toThrow callback
- Consolidate redundant error handling in tests
This commit is contained in:
Chad Metcalf 2025-08-03 15:50:02 -07:00
parent aa06fd90e0
commit b589f8f2c2
No known key found for this signature in database

View file

@ -16,7 +16,7 @@ describe('onboarding config flag handling', () => {
// Create a minimal auth config for testing
mockAuthConfig = {
userId: 'test-user',
userEmail: 'test@example.com',
userEmail: 'test@example.com',
accessToken: 'test-token',
refreshToken: 'test-refresh',
expiresAt: Date.now() + 3600000,
@ -76,42 +76,13 @@ name: "Incomplete Config"
// Verify the file exists
expect(fs.existsSync(configPath)).toBe(true);
try {
await runNormalFlow(mockAuthConfig, configPath);
fail('Expected runNormalFlow to throw an error');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Must have our specific error format that shows we caught and re-threw
expect(errorMessage).toMatch(/^Failed to load config from ".*": .+/);
// Must mention the specific config file path
expect(errorMessage).toContain(configPath);
// Must NOT be a generic fallback error - should be about our specific file
expect(errorMessage).not.toContain('Unable to find');
expect(errorMessage).not.toContain('No configuration found');
}
// Should throw with our specific error format and include path
await expect(runNormalFlow(mockAuthConfig, configPath)).rejects.toThrow(
/^Failed to load config from ".*": .+/
);
});
test('error message should include the exact config path that was provided', async () => {
const configPath = './some/relative/path/config.yaml';
try {
await runNormalFlow(mockAuthConfig, configPath);
fail('Expected runNormalFlow to throw an error');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Should include the exact path that was provided
expect(errorMessage).toContain(configPath);
// Should have the expected error message format
expect(errorMessage).toMatch(/Failed to load config from ".*": .+/);
}
});
test('should handle different config path formats', async () => {
test('should handle different config path formats with proper error messages', async () => {
const testPaths = [
'./non-existent.yaml',
'/absolute/path/config.yaml',
@ -120,46 +91,43 @@ name: "Incomplete Config"
];
for (const configPath of testPaths) {
try {
await runNormalFlow(mockAuthConfig, configPath);
fail(`Expected runNormalFlow to throw an error for path: ${configPath}`);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// Each error should mention the specific path
expect(errorMessage).toContain(configPath);
expect(errorMessage).toContain('Failed to load config from');
}
await expect(runNormalFlow(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();
});
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 = runNormalFlow(mockAuthConfig, configPath);
await expect(promise).rejects.toThrow();
try {
await runNormalFlow(mockAuthConfig, configPath);
fail('Expected runNormalFlow to throw an error');
await promise;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
const message = error instanceof Error ? error.message : String(error);
// CRITICAL: Must have our specific error format from the fix
expect(errorMessage).toMatch(/^Failed to load config from ".*": .+/);
expect(message).toMatch(/^Failed to load config from ".*": .+/);
// Error should be about the specific config file we provided
expect(errorMessage).toContain(configPath);
expect(message).toContain(configPath);
// Should NOT mention falling back to default config (this was the bug!)
expect(errorMessage).not.toContain('~/.continue/config.yaml');
expect(errorMessage).not.toContain('default config');
expect(errorMessage).not.toContain('fallback');
// Should NOT be a generic "config not found" error that would indicate
// the system tried to fall back to defaults
expect(errorMessage).not.toContain('Unable to find');
expect(errorMessage).not.toContain('No configuration found');
expect(message).not.toContain('~/.continue/config.yaml');
expect(message).not.toContain('default config');
expect(message).not.toContain('fallback');
}
});
@ -168,24 +136,17 @@ name: "Incomplete Config"
fs.writeFileSync(badConfigPath, 'invalid yaml [');
// Case 1: Explicit --config that fails should throw our specific error
try {
await runNormalFlow(mockAuthConfig, badConfigPath);
fail('Expected explicit config to fail');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
// This should have our "Failed to load config from" prefix
expect(errorMessage).toMatch(/^Failed to load config from "/);
expect(errorMessage).toContain(badConfigPath);
}
await expect(runNormalFlow(mockAuthConfig, badConfigPath)).rejects.toThrow(
/^Failed to load config from "/
);
// Case 2: No explicit config should follow different logic (might succeed or fail differently)
// Case 2: No explicit config should follow different logic
try {
await runNormalFlow(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
// because no explicit config was provided
expect(errorMessage).not.toMatch(/^Failed to load config from "/);
}
});