-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathglobal-config.test.ts
More file actions
147 lines (113 loc) · 5.05 KB
/
global-config.test.ts
File metadata and controls
147 lines (113 loc) · 5.05 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
import {
getOrCreateInstallationId,
readGlobalConfig,
readGlobalConfigSync,
updateGlobalConfig,
} from '../../lib/schemas/io/global-config';
import { createTempConfig } from './helpers/temp-config';
import { readFile, writeFile } from 'fs/promises';
import { afterAll, beforeEach, describe, expect, it } from 'vitest';
const tmp = createTempConfig('gc');
describe('global-config', () => {
beforeEach(() => tmp.setup());
afterAll(() => tmp.cleanup());
describe('readGlobalConfig', () => {
it('returns parsed config when file exists', async () => {
await writeFile(tmp.configFile, JSON.stringify({ telemetry: { enabled: false } }));
const config = await readGlobalConfig(tmp.configFile);
expect(config).toEqual({ telemetry: { enabled: false } });
});
it('returns empty object when file is missing or invalid', async () => {
expect(await readGlobalConfig(tmp.testDir + '/nonexistent.json')).toEqual({});
await writeFile(tmp.configFile, 'not json');
expect(await readGlobalConfig(tmp.configFile)).toEqual({});
});
it('drops invalid fields while preserving valid ones', async () => {
await writeFile(
tmp.configFile,
JSON.stringify({
transactionSearchIndexPercentage: 'not-a-number',
uvIndex: 'https://valid.url',
telemetry: { enabled: 'yes', endpoint: 'https://example.com' },
})
);
const config = await readGlobalConfig(tmp.configFile);
expect(config).toEqual({
transactionSearchIndexPercentage: undefined,
uvIndex: 'https://valid.url',
telemetry: { enabled: undefined, endpoint: 'https://example.com' },
});
});
it('preserves unknown fields via passthrough', async () => {
const full = {
installationId: 'abc-123',
telemetry: { enabled: true, endpoint: 'https://example.com', audit: false },
futureField: 'hello',
};
await writeFile(tmp.configFile, JSON.stringify(full));
const config = await readGlobalConfig(tmp.configFile);
expect(config).toEqual(full);
});
});
describe('readGlobalConfigSync', () => {
it('returns parsed config when file exists', async () => {
await writeFile(tmp.configFile, JSON.stringify({ telemetry: { enabled: false } }));
expect(readGlobalConfigSync(tmp.configFile)).toEqual({ telemetry: { enabled: false } });
});
it('returns empty object when file is missing or invalid', async () => {
expect(readGlobalConfigSync(tmp.testDir + '/nonexistent.json')).toEqual({});
await writeFile(tmp.configFile, 'not json');
expect(readGlobalConfigSync(tmp.configFile)).toEqual({});
});
});
describe('updateGlobalConfig', () => {
it('creates directory and writes config when none exists', async () => {
const fresh = createTempConfig('gc-fresh');
const ok = await updateGlobalConfig({ telemetry: { enabled: false } }, fresh.configDir, fresh.configFile);
expect(ok).toBe(true);
const written = JSON.parse(await readFile(fresh.configFile, 'utf-8'));
expect(written).toEqual({ telemetry: { enabled: false } });
await fresh.cleanup();
});
it('deep-merges telemetry sub-object with existing config', async () => {
await writeFile(
tmp.configFile,
JSON.stringify({ installationId: 'keep-me', telemetry: { enabled: true, endpoint: 'https://x.com' } })
);
await updateGlobalConfig({ telemetry: { enabled: false } }, tmp.configDir, tmp.configFile);
const written = JSON.parse(await readFile(tmp.configFile, 'utf-8'));
expect(written).toEqual({
installationId: 'keep-me',
telemetry: { enabled: false, endpoint: 'https://x.com' },
});
});
it('does not overwrite malformed config', async () => {
await writeFile(tmp.configFile, '{ invalid json');
const ok = await updateGlobalConfig({ telemetry: { enabled: true } }, tmp.configDir, tmp.configFile);
expect(ok).toBe(false);
expect(await readFile(tmp.configFile, 'utf-8')).toBe('{ invalid json');
});
it('returns false on write failures', async () => {
const ok = await updateGlobalConfig(
{ telemetry: { enabled: true } },
tmp.testDir + '/\0invalid',
tmp.testDir + '/\0invalid/config.json'
);
expect(ok).toBe(false);
});
});
describe('getOrCreateInstallationId', () => {
it('generates installationId on first run and returns created: true', async () => {
const result = await getOrCreateInstallationId(tmp.configDir, tmp.configFile);
expect(result.created).toBe(true);
expect(result.id).toMatch(/^[0-9a-f-]{36}$/);
const config = await readGlobalConfig(tmp.configFile);
expect(config.installationId).toBe(result.id);
});
it('returns existing id with created: false', async () => {
await writeFile(tmp.configFile, JSON.stringify({ installationId: 'existing-id' }));
const result = await getOrCreateInstallationId(tmp.configDir, tmp.configFile);
expect(result).toEqual({ id: 'existing-id', created: false });
});
});
});