config.test.ts
4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import path from 'node:path';
describe('config utilities', () => {
const fakeHome = '/home/tester';
const expectedConfigPath = path.resolve(fakeHome, '.coderio', 'config.yaml');
beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();
});
afterEach(() => {
vi.unmock('fs');
vi.unmock('os');
vi.unmock('js-yaml');
});
async function importConfigModuleWithMocks({
exists = true,
fileContent = 'model: {}\nfigma: {}\n',
parsedYaml = { model: { provider: 'x', model: 'y', baseUrl: 'z', apiKey: 'k' }, figma: { token: 't' } },
yamlThrows,
}: {
exists?: boolean;
fileContent?: string;
parsedYaml?: any;
yamlThrows?: Error;
}) {
const existsSync = vi.fn(() => exists);
const readFileSync = vi.fn(() => fileContent);
const yamlLoad = vi.fn(() => parsedYaml);
if (yamlThrows) {
yamlLoad.mockImplementation(() => {
throw yamlThrows;
});
}
vi.doMock('os', () => ({
homedir: () => fakeHome,
}));
vi.doMock('fs', () => ({
existsSync,
readFileSync,
}));
vi.doMock('js-yaml', () => ({
default: {
load: yamlLoad,
},
}));
const mod = await import('../../src/utils/config');
return { mod, existsSync, readFileSync, yamlLoad };
}
it('should expose deterministic config path under homedir', async () => {
const { mod } = await importConfigModuleWithMocks({});
expect(mod.CONFIG_DIR).toBe(path.resolve(fakeHome, '.coderio'));
expect(mod.getConfigPath()).toBe(expectedConfigPath);
});
it('should throw when config file does not exist', async () => {
const { mod } = await importConfigModuleWithMocks({ exists: false });
expect(() => mod.loadConfig()).toThrowError(new RegExp(`Configuration file not found at: ${expectedConfigPath}`));
});
it('should throw when yaml parses to empty', async () => {
const { mod } = await importConfigModuleWithMocks({ parsedYaml: null });
expect(() => mod.loadConfig()).toThrowError(/Invalid config\.yaml structure: configuration is empty/);
});
it('should wrap yaml parsing errors with helpful message', async () => {
const { mod } = await importConfigModuleWithMocks({ yamlThrows: new Error('boom') });
expect(() => mod.loadConfig()).toThrowError(/Failed to load config\.yaml: boom/);
});
it('should cache loaded config', async () => {
const { mod, readFileSync, yamlLoad } = await importConfigModuleWithMocks({});
const first = mod.loadConfig();
const second = mod.loadConfig();
expect(first).toBe(second);
expect(readFileSync).toHaveBeenCalledTimes(1);
expect(yamlLoad).toHaveBeenCalledTimes(1);
});
it('should clear cache and reload after clearConfigCache', async () => {
const { mod, readFileSync, yamlLoad } = await importConfigModuleWithMocks({});
mod.loadConfig();
mod.clearConfigCache();
mod.loadConfig();
expect(readFileSync).toHaveBeenCalledTimes(2);
expect(yamlLoad).toHaveBeenCalledTimes(2);
});
it('should return model and figma config via helpers', async () => {
const parsedYaml = {
model: { provider: 'openai', model: 'gpt-4.1', baseUrl: 'https://example.com', apiKey: 'secret' },
figma: { token: 'figma-token' },
debug: { enabled: true, outputDir: '/tmp/debug' },
};
const { mod } = await importConfigModuleWithMocks({ parsedYaml });
expect(mod.getModelConfig()).toEqual(parsedYaml.model);
expect(mod.getFigmaConfig()).toEqual(parsedYaml.figma);
expect(mod.getDebugConfig()).toEqual(parsedYaml.debug);
});
it('should default debug config to disabled', async () => {
const parsedYaml = {
model: { provider: 'x', model: 'y', baseUrl: 'z', apiKey: 'k' },
figma: { token: 't' },
};
const { mod } = await importConfigModuleWithMocks({ parsedYaml });
expect(mod.getDebugConfig()).toEqual({ enabled: false });
});
});