|
| 1 | +import { |
| 2 | + chmodSync, |
| 3 | + mkdirSync, |
| 4 | + mkdtempSync, |
| 5 | + readdirSync, |
| 6 | + readFileSync, |
| 7 | + statSync, |
| 8 | + writeFileSync, |
| 9 | +} from 'node:fs'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | +import { join } from 'node:path'; |
| 12 | +import type { Account } from '@linkcode/schema'; |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 14 | +import { createProviderConfigStore } from '../provider-store'; |
| 15 | +import { createInMemoryVault } from './fixtures/in-memory-vault'; |
| 16 | + |
| 17 | +const fsMocks = vi.hoisted(() => ({ |
| 18 | + openTargets: new Map<number, string>(), |
| 19 | + renameTarget: null as string | null, |
| 20 | + renameTargets: [] as string[], |
| 21 | + syncTargets: [] as string[], |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('node:fs', async (importOriginal) => { |
| 25 | + const actual = await importOriginal<typeof import('node:fs')>(); |
| 26 | + return { |
| 27 | + ...actual, |
| 28 | + closeSync(descriptor: number) { |
| 29 | + try { |
| 30 | + actual.closeSync(descriptor); |
| 31 | + } finally { |
| 32 | + fsMocks.openTargets.delete(descriptor); |
| 33 | + } |
| 34 | + }, |
| 35 | + fsyncSync(descriptor: number) { |
| 36 | + fsMocks.syncTargets.push(fsMocks.openTargets.get(descriptor) ?? ''); |
| 37 | + actual.fsyncSync(descriptor); |
| 38 | + }, |
| 39 | + openSync( |
| 40 | + path: Parameters<typeof actual.openSync>[0], |
| 41 | + flags: Parameters<typeof actual.openSync>[1], |
| 42 | + mode?: Parameters<typeof actual.openSync>[2], |
| 43 | + ) { |
| 44 | + const descriptor = actual.openSync(path, flags, mode); |
| 45 | + fsMocks.openTargets.set(descriptor, String(path)); |
| 46 | + return descriptor; |
| 47 | + }, |
| 48 | + renameSync( |
| 49 | + oldPath: Parameters<typeof actual.renameSync>[0], |
| 50 | + newPath: Parameters<typeof actual.renameSync>[1], |
| 51 | + ) { |
| 52 | + const target = String(newPath); |
| 53 | + fsMocks.renameTargets.push(target); |
| 54 | + if (target === fsMocks.renameTarget) throw new Error('simulated rename failure'); |
| 55 | + actual.renameSync(oldPath, newPath); |
| 56 | + }, |
| 57 | + }; |
| 58 | +}); |
| 59 | + |
| 60 | +const oauthAccount: Account = { |
| 61 | + id: 'acc_oauth', |
| 62 | + label: 'Subscription', |
| 63 | + credential: { type: 'oauth', agent: 'claude-code' }, |
| 64 | + createdAt: 0, |
| 65 | +}; |
| 66 | + |
| 67 | +let savedHome: string | undefined; |
| 68 | + |
| 69 | +beforeEach(() => { |
| 70 | + savedHome = process.env.HOME; |
| 71 | + process.env.HOME = mkdtempSync(join(tmpdir(), 'linkcode-config-persistence-')); |
| 72 | + process.env.LINKCODE_CHANNEL = 'release'; |
| 73 | +}); |
| 74 | + |
| 75 | +afterEach(() => { |
| 76 | + process.env.HOME = savedHome; |
| 77 | + delete process.env.LINKCODE_CHANNEL; |
| 78 | + fsMocks.renameTarget = null; |
| 79 | + fsMocks.renameTargets = []; |
| 80 | + fsMocks.openTargets.clear(); |
| 81 | + fsMocks.syncTargets = []; |
| 82 | + vi.restoreAllMocks(); |
| 83 | +}); |
| 84 | + |
| 85 | +function paths(): { dir: string; config: string } { |
| 86 | + const dir = join(process.env.HOME ?? '', '.linkcode'); |
| 87 | + return { dir, config: join(dir, 'config.json') }; |
| 88 | +} |
| 89 | + |
| 90 | +function readConfig(): Record<string, unknown> { |
| 91 | + return JSON.parse(readFileSync(paths().config, 'utf8')) as Record<string, unknown>; |
| 92 | +} |
| 93 | + |
| 94 | +describe('provider config persistence', () => { |
| 95 | + it('atomically replaces providers and accounts, preserves other fields, and tightens the mode', () => { |
| 96 | + const { dir, config } = paths(); |
| 97 | + mkdirSync(dir, { recursive: true }); |
| 98 | + writeFileSync(config, JSON.stringify({ hostname: '127.0.0.2' })); |
| 99 | + chmodSync(config, 0o644); |
| 100 | + const store = createProviderConfigStore(createInMemoryVault(), {}, []); |
| 101 | + |
| 102 | + store.update({ |
| 103 | + providers: { codex: { enabled: true, activeAccountId: oauthAccount.id } }, |
| 104 | + accounts: [oauthAccount], |
| 105 | + }); |
| 106 | + |
| 107 | + expect(readConfig()).toEqual({ |
| 108 | + hostname: '127.0.0.2', |
| 109 | + providers: { codex: { enabled: true, activeAccountId: oauthAccount.id } }, |
| 110 | + accounts: [oauthAccount], |
| 111 | + }); |
| 112 | + expect(store.get()).toEqual({ |
| 113 | + codex: { enabled: true, activeAccountId: oauthAccount.id }, |
| 114 | + }); |
| 115 | + expect(store.getAccounts()).toEqual([oauthAccount]); |
| 116 | + expect(statSync(config).mode & 0o777).toBe(0o600); |
| 117 | + expect(fsMocks.renameTargets).toEqual([config]); |
| 118 | + expect(fsMocks.syncTargets[0]).toContain(join(dir, '.config.')); |
| 119 | + expect(fsMocks.syncTargets[0]?.endsWith('.tmp')).toBe(true); |
| 120 | + expect(fsMocks.syncTargets.slice(1)).toEqual(process.platform === 'win32' ? [] : [dir]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('rejects corrupt JSON without replacing the file or publishing memory', () => { |
| 124 | + const { dir, config } = paths(); |
| 125 | + mkdirSync(dir, { recursive: true }); |
| 126 | + const corrupt = '{ definitely not JSON'; |
| 127 | + writeFileSync(config, corrupt); |
| 128 | + const store = createProviderConfigStore(createInMemoryVault(), {}, []); |
| 129 | + |
| 130 | + expect(() => |
| 131 | + store.update({ |
| 132 | + providers: { codex: { enabled: true } }, |
| 133 | + accounts: [oauthAccount], |
| 134 | + }), |
| 135 | + ).toThrow(`Invalid JSON in daemon config at ${config}`); |
| 136 | + |
| 137 | + expect(readFileSync(config, 'utf8')).toBe(corrupt); |
| 138 | + expect(store.get()).toEqual({}); |
| 139 | + expect(store.getAccounts()).toEqual([]); |
| 140 | + }); |
| 141 | + |
| 142 | + it('leaves the previous file and memory unchanged when rename fails', () => { |
| 143 | + const { dir, config } = paths(); |
| 144 | + mkdirSync(dir, { recursive: true }); |
| 145 | + const previous = `${JSON.stringify({ providers: {}, accounts: [] }, null, 2)}\n`; |
| 146 | + writeFileSync(config, previous); |
| 147 | + const store = createProviderConfigStore(createInMemoryVault(), {}, []); |
| 148 | + fsMocks.renameTarget = config; |
| 149 | + |
| 150 | + expect(() => |
| 151 | + store.update({ |
| 152 | + providers: { codex: { enabled: true } }, |
| 153 | + accounts: [oauthAccount], |
| 154 | + }), |
| 155 | + ).toThrow('simulated rename failure'); |
| 156 | + |
| 157 | + expect(readFileSync(config, 'utf8')).toBe(previous); |
| 158 | + expect(store.get()).toEqual({}); |
| 159 | + expect(store.getAccounts()).toEqual([]); |
| 160 | + expect(readdirSync(dir).filter((entry) => entry.endsWith('.tmp'))).toEqual([]); |
| 161 | + }); |
| 162 | +}); |
0 commit comments