|
| 1 | +import { getOrCreateInstallationId, readGlobalConfig, updateGlobalConfig } from '../global-config'; |
| 2 | +import { createTempConfig } from './helpers/temp-config'; |
| 3 | +import { readFile, writeFile } from 'fs/promises'; |
| 4 | +import { afterAll, beforeEach, describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +const tmp = createTempConfig('gc'); |
| 7 | + |
| 8 | +describe('global-config', () => { |
| 9 | + beforeEach(() => tmp.setup()); |
| 10 | + afterAll(() => tmp.cleanup()); |
| 11 | + |
| 12 | + describe('readGlobalConfig', () => { |
| 13 | + it('returns parsed config when file exists', async () => { |
| 14 | + await writeFile(tmp.configFile, JSON.stringify({ telemetry: { enabled: false } })); |
| 15 | + |
| 16 | + const config = await readGlobalConfig(tmp.configFile); |
| 17 | + |
| 18 | + expect(config).toEqual({ telemetry: { enabled: false } }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns empty object when file is missing or invalid', async () => { |
| 22 | + expect(await readGlobalConfig(tmp.testDir + '/nonexistent.json')).toEqual({}); |
| 23 | + |
| 24 | + await writeFile(tmp.configFile, JSON.stringify({ telemetry: { enabled: 'false' } })); |
| 25 | + expect(await readGlobalConfig(tmp.configFile)).toEqual({}); |
| 26 | + }); |
| 27 | + |
| 28 | + it('preserves unknown fields via passthrough', async () => { |
| 29 | + const full = { |
| 30 | + installationId: 'abc-123', |
| 31 | + telemetry: { enabled: true, endpoint: 'https://example.com', audit: false }, |
| 32 | + futureField: 'hello', |
| 33 | + }; |
| 34 | + await writeFile(tmp.configFile, JSON.stringify(full)); |
| 35 | + |
| 36 | + const config = await readGlobalConfig(tmp.configFile); |
| 37 | + |
| 38 | + expect(config).toEqual(full); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('updateGlobalConfig', () => { |
| 43 | + it('creates directory and writes config when none exists', async () => { |
| 44 | + const fresh = createTempConfig('gc-fresh'); |
| 45 | + |
| 46 | + const ok = await updateGlobalConfig({ telemetry: { enabled: false } }, fresh.configDir, fresh.configFile); |
| 47 | + |
| 48 | + expect(ok).toBe(true); |
| 49 | + const written = JSON.parse(await readFile(fresh.configFile, 'utf-8')); |
| 50 | + expect(written).toEqual({ telemetry: { enabled: false } }); |
| 51 | + |
| 52 | + await fresh.cleanup(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('deep-merges telemetry sub-object with existing config', async () => { |
| 56 | + await writeFile( |
| 57 | + tmp.configFile, |
| 58 | + JSON.stringify({ installationId: 'keep-me', telemetry: { enabled: true, endpoint: 'https://x.com' } }) |
| 59 | + ); |
| 60 | + |
| 61 | + await updateGlobalConfig({ telemetry: { enabled: false } }, tmp.configDir, tmp.configFile); |
| 62 | + |
| 63 | + const written = JSON.parse(await readFile(tmp.configFile, 'utf-8')); |
| 64 | + expect(written).toEqual({ |
| 65 | + installationId: 'keep-me', |
| 66 | + telemetry: { enabled: false, endpoint: 'https://x.com' }, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns false on write failures', async () => { |
| 71 | + const ok = await updateGlobalConfig( |
| 72 | + { telemetry: { enabled: true } }, |
| 73 | + tmp.testDir + '/\0invalid', |
| 74 | + tmp.testDir + '/\0invalid/config.json' |
| 75 | + ); |
| 76 | + |
| 77 | + expect(ok).toBe(false); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('getOrCreateInstallationId', () => { |
| 82 | + it('generates installationId on first run and returns created: true', async () => { |
| 83 | + const result = await getOrCreateInstallationId(tmp.configDir, tmp.configFile); |
| 84 | + |
| 85 | + expect(result.created).toBe(true); |
| 86 | + expect(result.id).toMatch(/^[0-9a-f-]{36}$/); |
| 87 | + const config = await readGlobalConfig(tmp.configFile); |
| 88 | + expect(config.installationId).toBe(result.id); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns existing id with created: false', async () => { |
| 92 | + await writeFile(tmp.configFile, JSON.stringify({ installationId: 'existing-id' })); |
| 93 | + |
| 94 | + const result = await getOrCreateInstallationId(tmp.configDir, tmp.configFile); |
| 95 | + |
| 96 | + expect(result).toEqual({ id: 'existing-id', created: false }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments