feat(auth): add anthropic api key storage and cli auth
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { mkdtempSync, statSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
describe('auth/anthropic', () => {
|
||||
const originalHome = process.env.HOME;
|
||||
const originalEnvKey = process.env.ANTHROPIC_API_KEY;
|
||||
|
||||
let homeDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
homeDir = mkdtempSync(join(tmpdir(), 'flynn-auth-anthropic-'));
|
||||
process.env.HOME = homeDir;
|
||||
delete process.env.ANTHROPIC_API_KEY;
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
process.env.HOME = originalHome;
|
||||
if (originalEnvKey) {
|
||||
process.env.ANTHROPIC_API_KEY = originalEnvKey;
|
||||
} else {
|
||||
delete process.env.ANTHROPIC_API_KEY;
|
||||
}
|
||||
});
|
||||
|
||||
it('stores, loads, and clears Anthropic API key', async () => {
|
||||
const mod = await import('./anthropic.js');
|
||||
|
||||
expect(mod.loadStoredAnthropicAuth()).toBeNull();
|
||||
|
||||
mod.storeAnthropicAuth('sk-test');
|
||||
expect(mod.loadStoredAnthropicAuth()?.api_key).toBe('sk-test');
|
||||
|
||||
const authFile = join(homeDir, '.config/flynn/auth.json');
|
||||
const mode = statSync(authFile).mode & 0o777;
|
||||
expect(mode).toBe(0o600);
|
||||
|
||||
mod.clearAnthropicAuth();
|
||||
expect(mod.loadStoredAnthropicAuth()).toBeNull();
|
||||
});
|
||||
|
||||
it('getAnthropicApiKey prefers environment variable', async () => {
|
||||
process.env.ANTHROPIC_API_KEY = 'sk-env';
|
||||
const mod = await import('./anthropic.js');
|
||||
expect(mod.getAnthropicApiKey()).toBe('sk-env');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user