-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.utils.spec.ts
More file actions
78 lines (58 loc) · 1.95 KB
/
env.utils.spec.ts
File metadata and controls
78 lines (58 loc) · 1.95 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
import fs from 'node:fs';
import path from 'path';
import { DEFAULT_ENV, makeAppUtilsFake } from '../../tests/fakes/utils';
import { TEMP_DIRECTORY } from '../constants';
import { EnvUtils as EnvUtilsInterface, ProviderEnum } from '../interfaces';
import { EnvUtils } from './env.utils';
const ENV_FILE_PATH = path.join(TEMP_DIRECTORY, '.env');
export const makeFakeEnvUtils = () =>
({ update: vi.fn(), variables: vi.fn() } as EnvUtilsInterface);
const makeSut = () => {
const appUtils = makeAppUtilsFake();
const envUtils = new EnvUtils(appUtils);
return { sut: envUtils };
};
describe('EnvUtils', () => {
beforeEach(() => {
const fileContent = Object.entries({
PROVIDER: ProviderEnum.OpenAI,
OPENAI_API_KEY: '123456789',
OPENAI_N_COMMITS: '2',
})
.map(([key, value]) => `${key}=${value}`)
.join('\n');
fs.writeFileSync(ENV_FILE_PATH, fileContent, 'utf8');
});
describe('variables', () => {
it('should return an object with environment variables', () => {
const { sut } = makeSut();
const variables = sut.variables();
expect(variables).toBeDefined();
expect(typeof variables).toBe('object');
});
});
describe('update', () => {
it('should update the environment variables', () => {
const { sut } = makeSut();
const updates = {
PROVIDER: ProviderEnum.OpenAI,
OPENAI_API_KEY: '123456789',
OPENAI_N_COMMITS: '5',
};
sut.update(updates);
const variables = sut.variables();
expect(Object.keys(variables)).toEqual(
Object.keys({ ...DEFAULT_ENV, ...updates }),
);
});
it('should merge the updates with existing environment variables', () => {
const { sut } = makeSut();
const initialVariables = sut.variables();
const updates = {
OPENAI_N_COMMITS: '5',
};
sut.update(updates);
expect(sut.variables()).toEqual({ ...initialVariables, ...updates });
});
});
});