// Configuration tests import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { rm } from 'node:fs/promises' import { join } from 'node:path' import { tmpdir } from 'node:os' import { randomUUID } from 'node:crypto' import { loadConfig, saveConfig, setConfigValue, getConfigValue } from '../src/config.js' let testDir: string beforeEach(() => { testDir = join(tmpdir(), `ograph-test-${randomUUID()}`) process.env.OGRAPH_CONFIG_DIR = testDir }) afterEach(async () => { delete process.env.OGRAPH_CONFIG_DIR await rm(testDir, { recursive: true, force: true }) }) describe('config management', () => { it('should return empty config when file does not exist', async () => { const config = await loadConfig() expect(config).toEqual({}) }) it('should save and load config correctly', async () => { const testConfig = { endpoint: 'https://test.example.com', token: 'test-token-123', } await saveConfig(testConfig) const loadedConfig = await loadConfig() expect(loadedConfig).toEqual(testConfig) }) it('should set individual config values', async () => { await setConfigValue('endpoint', 'https://api.example.com') await setConfigValue('token', 'secret-token') const config = await loadConfig() expect(config.endpoint).toBe('https://api.example.com') expect(config.token).toBe('secret-token') }) it('should get individual config values', async () => { await saveConfig({ endpoint: 'https://get.example.com', token: 'get-token-456', }) const endpoint = await getConfigValue('endpoint') const token = await getConfigValue('token') const missing = await getConfigValue('nonexistent' as any) expect(endpoint).toBe('https://get.example.com') expect(token).toBe('get-token-456') expect(missing).toBeUndefined() }) it('should handle malformed config file gracefully', async () => { const { writeFile, mkdir } = await import('node:fs/promises') await mkdir(testDir, { recursive: true }) await writeFile(join(testDir, 'config.json'), '{bad json!!!}', 'utf-8') const config = await loadConfig() expect(config).toEqual({}) }) })