-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigService.test.ts
More file actions
248 lines (213 loc) · 8.62 KB
/
ConfigService.test.ts
File metadata and controls
248 lines (213 loc) · 8.62 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import 'reflect-metadata';
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('fs', () => ({
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
existsSync: vi.fn(),
}));
vi.mock('../logger.js', () => ({
logger: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
/**
* Mutable holder for the build-time embedded Terraform state.
* Tests that exercise the EMBEDDED_TFSTATE fallback path set this before
* calling `getTfOutputs()`; all other tests leave it as `null` so they
* don't accidentally exercise the fallback.
*/
let mockEmbeddedState: Record<string, unknown> | null = null;
vi.mock('../generated/tfstate.js', () => ({
get EMBEDDED_TFSTATE() {
return mockEmbeddedState;
},
}));
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { ConfigService } from './ConfigService.js';
/** Strongly-typed mock handles for the `fs` module. */
const mockExists = vi.mocked(existsSync);
const mockRead = vi.mocked(readFileSync);
const mockWrite = vi.mocked(writeFileSync);
/**
* Build a Terraform state file payload from an `outputs` map.
* Mirrors the shape that `terraform.tfstate` uses on disk.
*/
function makeState(outputs: Record<string, { value: unknown }>): string {
return JSON.stringify({ outputs });
}
describe('ConfigService', () => {
/** Fresh instance per test; each has its own in-memory tfstate cache. */
let service: ConfigService;
beforeEach(() => {
service = new ConfigService();
mockEmbeddedState = null;
});
describe('getTfOutputs', () => {
it('should return null when both the state file and embedded state are absent', () => {
mockExists.mockReturnValue(false);
expect(service.getTfOutputs()).toBeNull();
});
it('should use EMBEDDED_TFSTATE as fallback when the state file is absent', () => {
mockEmbeddedState = {
outputs: {
aws_region: { value: 'us-west-1' },
game_names: { value: ['minecraft'] },
},
};
mockExists.mockReturnValue(false);
const outputs = service.getTfOutputs();
expect(outputs).not.toBeNull();
expect(outputs!.aws_region).toBe('us-west-1');
expect(outputs!.game_names).toEqual(['minecraft']);
expect(outputs!.subnet_ids).toBe('');
});
it('should prefer the runtime state file over EMBEDDED_TFSTATE when both are present', () => {
mockEmbeddedState = { outputs: { aws_region: { value: 'embedded-region' } } };
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(makeState({ aws_region: { value: 'runtime-region' } }));
expect(service.getTfOutputs()!.aws_region).toBe('runtime-region');
});
it('should parse outputs and fill defaults for missing keys', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(
makeState({
aws_region: { value: 'us-west-2' },
ecs_cluster_name: { value: 'my-cluster' },
game_names: { value: ['minecraft', 'factorio'] },
}),
);
const outputs = service.getTfOutputs();
expect(outputs).not.toBeNull();
expect(outputs!.aws_region).toBe('us-west-2');
expect(outputs!.ecs_cluster_name).toBe('my-cluster');
expect(outputs!.game_names).toEqual(['minecraft', 'factorio']);
expect(outputs!.subnet_ids).toBe('');
expect(outputs!.alb_dns_name).toBeNull();
expect(outputs!.efs_access_points).toEqual({});
});
it('should apply the fallback aws_region when outputs omit it', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(makeState({}));
expect(service.getTfOutputs()!.aws_region).toBe('us-east-1');
});
it('should cache parsed outputs across calls', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(makeState({ aws_region: { value: 'eu-central-1' } }));
service.getTfOutputs();
service.getTfOutputs();
expect(mockRead).toHaveBeenCalledTimes(1);
});
it('should force a re-read after invalidateCache', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(makeState({ aws_region: { value: 'a' } }));
service.getTfOutputs();
service.invalidateCache();
mockRead.mockReturnValue(makeState({ aws_region: { value: 'b' } }));
expect(service.getTfOutputs()!.aws_region).toBe('b');
expect(mockRead).toHaveBeenCalledTimes(2);
});
it('should return null when the state file contains invalid JSON', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue('not-json{');
expect(service.getTfOutputs()).toBeNull();
});
});
describe('getRegion', () => {
it('should use aws_region from outputs when available', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(makeState({ aws_region: { value: 'ap-south-1' } }));
expect(service.getRegion()).toBe('ap-south-1');
});
it('should fall back to readEnvRegion when outputs unavailable', () => {
mockExists.mockReturnValue(false);
vi.spyOn(service, 'readEnvRegion').mockReturnValue('eu-west-3');
expect(service.getRegion()).toBe('eu-west-3');
});
it('should fall back to us-east-1 when no outputs and no env region', () => {
mockExists.mockReturnValue(false);
vi.spyOn(service, 'readEnvRegion').mockReturnValue(undefined);
expect(service.getRegion()).toBe('us-east-1');
});
});
describe('getApiToken', () => {
it('should return the token from API_TOKEN env when set', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue('env-tok');
expect(service.getApiToken()).toBe('env-tok');
});
it('should treat an explicitly-empty API_TOKEN env var as no token', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue('');
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(JSON.stringify({ api_token: 'file-tok' }));
// Env wins, even when empty — user intentionally disabled auth via env.
expect(service.getApiToken()).toBeNull();
});
it('should fall back to server_config.json.api_token when env is unset', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue(undefined);
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(JSON.stringify({ api_token: 'file-tok' }));
expect(service.getApiToken()).toBe('file-tok');
});
it('should return null when neither env nor file has a token', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue(undefined);
mockExists.mockReturnValue(false);
expect(service.getApiToken()).toBeNull();
});
it('should return null when the config file is malformed', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue(undefined);
mockExists.mockReturnValue(true);
mockRead.mockReturnValue('{bad');
expect(service.getApiToken()).toBeNull();
});
it('should return null when the api_token field is not a string', () => {
vi.spyOn(service, 'readEnvApiToken').mockReturnValue(undefined);
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(JSON.stringify({ api_token: 12345 }));
expect(service.getApiToken()).toBeNull();
});
});
describe('getConfig', () => {
it('should return defaults when the config file is missing', () => {
mockExists.mockReturnValue(false);
expect(service.getConfig()).toEqual({
watchdog_interval_minutes: 15,
watchdog_idle_checks: 4,
watchdog_min_packets: 100,
});
});
it('should merge saved config over defaults', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue(
JSON.stringify({ watchdog_idle_checks: 10, watchdog_min_packets: 250 }),
);
expect(service.getConfig()).toEqual({
watchdog_interval_minutes: 15,
watchdog_idle_checks: 10,
watchdog_min_packets: 250,
});
});
it('should return defaults when the config file is malformed', () => {
mockExists.mockReturnValue(true);
mockRead.mockReturnValue('{bad json');
const config = service.getConfig();
expect(config.watchdog_interval_minutes).toBe(15);
expect(config.watchdog_idle_checks).toBe(4);
expect(config.watchdog_min_packets).toBe(100);
});
});
describe('saveConfig', () => {
it('should write JSON-stringified config to disk', () => {
const config = {
watchdog_interval_minutes: 30,
watchdog_idle_checks: 6,
watchdog_min_packets: 500,
};
service.saveConfig(config);
expect(mockWrite).toHaveBeenCalledTimes(1);
const [, payload] = mockWrite.mock.calls[0]!;
expect(JSON.parse(payload as string)).toEqual(config);
});
});
});