forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.test.ts
52 lines (44 loc) · 1.84 KB
/
account.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
import { getUserEmail, setUserEmail, getAuthor } from '../src/globalConfig/accounts';
import { readGlobalConfig, writeGlobalConfigPartial } from '../src/globalConfig/globalConfig';
jest.mock('../src/globalConfig/globalConfig', () => ({
writeGlobalConfig: jest.fn(),
readGlobalConfig: jest.fn(),
writeGlobalConfigPartial: jest.fn(),
}));
describe('accounts module', () => {
beforeEach(() => {
delete process.env.PROMPTFOO_AUTHOR;
jest.resetModules();
});
describe('getUserEmail', () => {
it('should return the email from global config', () => {
jest.mocked(readGlobalConfig).mockReturnValue({ account: { email: 'test@example.com' } });
expect(getUserEmail()).toBe('test@example.com');
});
it('should return null if no email is set in global config', () => {
jest.mocked(readGlobalConfig).mockReturnValue({});
expect(getUserEmail()).toBeNull();
});
});
describe('setUserEmail', () => {
it('should write the email to global config', () => {
const writeGlobalConfigSpy = jest.mocked(writeGlobalConfigPartial);
setUserEmail('test@example.com');
expect(writeGlobalConfigSpy).toHaveBeenCalledWith({ account: { email: 'test@example.com' } });
});
});
describe('getAuthor', () => {
it('should return the author from environment variable', () => {
process.env.PROMPTFOO_AUTHOR = 'envAuthor';
expect(getAuthor()).toBe('envAuthor');
});
it('should return the email if environment variable is not set', () => {
jest.mocked(readGlobalConfig).mockReturnValue({ account: { email: 'test@example.com' } });
expect(getAuthor()).toBe('test@example.com');
});
it('should return null if neither environment variable nor email is set', () => {
jest.mocked(readGlobalConfig).mockReturnValue({});
expect(getAuthor()).toBeNull();
});
});
});