-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathconfig.test.ts
More file actions
179 lines (158 loc) · 6.69 KB
/
Copy pathconfig.test.ts
File metadata and controls
179 lines (158 loc) · 6.69 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
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
175
176
177
178
179
import { mkdtempSync, writeFileSync } from 'node:fs';
import { homedir, tmpdir } from 'node:os';
import { join } from 'node:path';
import { beforeEach, describe, expect, it } from 'vitest';
import { defaultConfigPath, loadConfig, readConfigFileSettings } from './config.js';
import { writeProfile } from './credentials.js';
let tmpRoot: string;
let credentialsPath: string;
beforeEach(() => {
tmpRoot = mkdtempSync(join(tmpdir(), 'testsprite-config-'));
credentialsPath = join(tmpRoot, 'credentials');
});
describe('loadConfig', () => {
it('returns built-in defaults when nothing is provided', () => {
const config = loadConfig({ env: {}, credentialsPath });
expect(config.apiUrl).toBe('https://api.testsprite.com');
expect(config.apiKey).toBeUndefined();
expect(config.profile).toBe('default');
});
it('honors TESTSPRITE_API_URL over the file', () => {
writeProfile('default', { apiUrl: 'https://from-file.example.com' }, { path: credentialsPath });
const config = loadConfig({
env: { TESTSPRITE_API_URL: 'https://from-env.example.com' },
credentialsPath,
});
expect(config.apiUrl).toBe('https://from-env.example.com');
});
it('honors TESTSPRITE_API_KEY over the file', () => {
writeProfile('default', { apiKey: 'sk-from-file' }, { path: credentialsPath });
const config = loadConfig({
env: { TESTSPRITE_API_KEY: 'sk-from-env' },
credentialsPath,
});
expect(config.apiKey).toBe('sk-from-env');
});
it('honors TESTSPRITE_PROFILE', () => {
expect(loadConfig({ env: { TESTSPRITE_PROFILE: 'staging' }, credentialsPath }).profile).toBe(
'staging',
);
});
it('option.profile overrides env var', () => {
expect(
loadConfig({
profile: 'option-profile',
env: { TESTSPRITE_PROFILE: 'env-profile' },
credentialsPath,
}).profile,
).toBe('option-profile');
});
it('treats empty TESTSPRITE_PROFILE as unset (falls back to default)', () => {
expect(loadConfig({ env: { TESTSPRITE_PROFILE: '' }, credentialsPath }).profile).toBe(
'default',
);
});
it('treats whitespace-only TESTSPRITE_PROFILE as unset (falls back to default)', () => {
const config = loadConfig({ env: { TESTSPRITE_PROFILE: ' ' }, credentialsPath });
expect(config.profile).toBe('default');
});
it('reads credentials from the default profile when TESTSPRITE_PROFILE is blank', () => {
writeProfile('default', { apiKey: 'sk-default' }, { path: credentialsPath });
const config = loadConfig({ env: { TESTSPRITE_PROFILE: ' ' }, credentialsPath });
expect(config.apiKey).toBe('sk-default');
});
it('option.endpointUrl overrides everything', () => {
writeProfile('default', { apiUrl: 'https://file' }, { path: credentialsPath });
const config = loadConfig({
endpointUrl: 'https://flag',
env: { TESTSPRITE_API_URL: 'https://env' },
credentialsPath,
});
expect(config.apiUrl).toBe('https://flag');
});
it('falls back to credentials file when env is unset', () => {
writeProfile(
'default',
{ apiKey: 'sk-file', apiUrl: 'https://from-file.example.com' },
{ path: credentialsPath },
);
const config = loadConfig({ env: {}, credentialsPath });
expect(config.apiKey).toBe('sk-file');
expect(config.apiUrl).toBe('https://from-file.example.com');
});
it('reads the requested profile, not just default', () => {
writeProfile('dev', { apiKey: 'sk-dev' }, { path: credentialsPath });
const config = loadConfig({ profile: 'dev', env: {}, credentialsPath });
expect(config.apiKey).toBe('sk-dev');
});
it('treats empty / whitespace TESTSPRITE_API_URL as unset (falls through to profile)', () => {
writeProfile(
'default',
{ apiKey: 'sk-file', apiUrl: 'https://api.example.com:8443' },
{ path: credentialsPath },
);
const config = loadConfig({
env: { TESTSPRITE_API_URL: ' ' },
credentialsPath,
});
expect(config.apiUrl).toBe('https://api.example.com:8443');
});
it('treats empty / whitespace TESTSPRITE_API_KEY as unset (falls through to profile)', () => {
writeProfile('default', { apiKey: 'sk-file' }, { path: credentialsPath });
const config = loadConfig({
env: { TESTSPRITE_API_KEY: '' },
credentialsPath,
});
expect(config.apiKey).toBe('sk-file');
});
});
describe('defaultConfigPath', () => {
it('points to ~/.testsprite/config', () => {
expect(defaultConfigPath()).toBe(join(homedir(), '.testsprite', 'config'));
});
});
describe('readConfigFileSettings + the config-file layer of loadConfig', () => {
function writeConfigFile(content: string): string {
const path = join(mkdtempSync(join(tmpdir(), 'testsprite-config-')), 'config');
writeFileSync(path, content, 'utf8');
return path;
}
it('reads endpoint_url/output/project_id from the profile section', () => {
const path = writeConfigFile(
'[default]\nendpoint_url = https://selfhosted.example.com\noutput = json\nproject_id = project_cfg\n',
);
expect(readConfigFileSettings('default', { path })).toEqual({
endpointUrl: 'https://selfhosted.example.com',
output: 'json',
projectId: 'project_cfg',
});
// A different profile section is not leaked into.
expect(readConfigFileSettings('other', { path })).toEqual({});
});
it('missing file and unknown keys are non-fatal', () => {
expect(readConfigFileSettings('default', { path: '/nope/definitely/missing' })).toEqual({});
const path = writeConfigFile('[default]\nmystery_key = x\n');
expect(readConfigFileSettings('default', { path })).toEqual({});
});
it('resolves the path from TESTSPRITE_CONFIG_FILE when no explicit path is given', () => {
const path = writeConfigFile('[default]\nproject_id = project_from_env_path\n');
const settings = readConfigFileSettings('default', {
env: { TESTSPRITE_CONFIG_FILE: path },
});
expect(settings.projectId).toBe('project_from_env_path');
});
it('loadConfig: config-file endpoint_url sits above the built-in default and below env', () => {
const path = writeConfigFile('[default]\nendpoint_url = https://cfg.example.com\n');
const missingCreds = join(mkdtempSync(join(tmpdir(), 'testsprite-creds-')), 'credentials');
// Only the config file supplies a URL -> it wins over the built-in default.
const fromConfig = loadConfig({ env: {}, credentialsPath: missingCreds, configPath: path });
expect(fromConfig.apiUrl).toBe('https://cfg.example.com');
// Env still outranks the config file.
const fromEnv = loadConfig({
env: { TESTSPRITE_API_URL: 'https://env.example.com' },
credentialsPath: missingCreds,
configPath: path,
});
expect(fromEnv.apiUrl).toBe('https://env.example.com');
});
});