forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalConfig.test.ts
174 lines (142 loc) · 5.21 KB
/
globalConfig.test.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as fs from 'fs';
import yaml from 'js-yaml';
import type {
readGlobalConfig,
writeGlobalConfig,
writeGlobalConfigPartial,
} from '../src/globalConfig/globalConfig';
// Clear any existing mocks
jest.unmock('../src/globalConfig/globalConfig');
jest.mock('fs', () => ({
readFileSync: jest.fn(),
writeFileSync: jest.fn(),
statSync: jest.fn(),
readdirSync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn(),
}));
jest.mock('proxy-agent', () => ({
ProxyAgent: jest.fn().mockImplementation(() => ({})),
}));
jest.mock('glob', () => ({
globSync: jest.fn(),
}));
jest.mock('../src/database');
describe('Global Config', () => {
let globalConfig: {
readGlobalConfig: typeof readGlobalConfig;
writeGlobalConfig: typeof writeGlobalConfig;
writeGlobalConfigPartial: typeof writeGlobalConfigPartial;
};
beforeEach(async () => {
jest.clearAllMocks();
await jest.isolateModules(async () => {
globalConfig = await import('../src/globalConfig/globalConfig');
});
});
const mockConfig = { account: { email: 'test@example.com' } };
describe('readGlobalConfig', () => {
describe('when config file exists', () => {
beforeEach(() => {
jest
.mocked(fs.existsSync)
.mockImplementation(
(path) =>
path.toString().includes('promptfoo.yaml') || path.toString().includes('.promptfoo'),
);
jest.mocked(fs.readFileSync).mockReturnValue(yaml.dump(mockConfig));
});
it('should read and parse the existing config file', () => {
const result = globalConfig.readGlobalConfig();
expect(fs.existsSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toHaveBeenCalledTimes(1);
expect(fs.readFileSync).toHaveBeenCalledWith(
expect.stringContaining('promptfoo.yaml'),
'utf-8',
);
expect(result).toEqual(mockConfig);
});
it('should handle empty config file by returning empty object', () => {
jest.mocked(fs.readFileSync).mockReturnValue('');
const result = globalConfig.readGlobalConfig();
expect(result).toEqual({});
});
});
describe('when config file does not exist', () => {
beforeEach(() => {
jest.mocked(fs.existsSync).mockReturnValue(false);
jest.mocked(fs.writeFileSync).mockImplementation();
jest.mocked(fs.mkdirSync).mockImplementation();
});
it('should create new config directory and file with empty config', () => {
const result = globalConfig.readGlobalConfig();
expect(fs.existsSync).toHaveBeenCalledTimes(2);
expect(fs.mkdirSync).toHaveBeenCalledWith(expect.any(String), { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('promptfoo.yaml'),
expect.any(String),
);
expect(result).toEqual({});
});
});
describe('error handling', () => {
it('should throw error if config file is invalid YAML', () => {
jest.mocked(fs.existsSync).mockReturnValue(true);
jest.mocked(fs.readFileSync).mockReturnValue('invalid: yaml: content:');
expect(() => globalConfig.readGlobalConfig()).toThrow(/bad indentation of a mapping entry/);
});
});
});
describe('writeGlobalConfig', () => {
it('should write config to file in YAML format', () => {
globalConfig.writeGlobalConfig(mockConfig);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('promptfoo.yaml'),
expect.stringContaining('account:'),
);
});
});
describe('writeGlobalConfigPartial', () => {
beforeEach(() => {
// Setup initial config
jest.mocked(fs.existsSync).mockReturnValue(true);
jest.mocked(fs.readFileSync).mockReturnValue(
yaml.dump({
account: { email: 'old@example.com' },
cloud: { apiKey: 'old-key', apiHost: 'old-host' },
}),
);
});
it('should merge new config with existing config', () => {
const partialConfig = {
account: { email: 'new@example.com' },
};
globalConfig.writeGlobalConfigPartial(partialConfig);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('promptfoo.yaml'),
expect.stringMatching(/email: new@example\.com.*apiKey: old-key/s),
);
});
it('should remove keys when value is falsy', () => {
const partialConfig = {
cloud: undefined,
};
globalConfig.writeGlobalConfigPartial(partialConfig);
expect(fs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('promptfoo.yaml'),
expect.not.stringContaining('apiKey: old-key'),
);
});
it('should update specific keys while preserving others', () => {
const partialConfig = {
cloud: { apiKey: 'new-key' },
};
globalConfig.writeGlobalConfigPartial(partialConfig);
const writeCall = jest.mocked(fs.writeFileSync).mock.calls[0][1] as string;
const writtenConfig = yaml.load(writeCall) as any;
expect(writtenConfig.cloud.apiKey).toBe('new-key');
expect(writtenConfig.account.email).toBe('old@example.com');
});
});
});