import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'fs/promises' import { tmpdir } from 'os' import path from 'path' import { Database } from 'bun:sqlite' const paths = vi.hoisted(() => ({ config: '', configDir: '', configHome: '', healthWatch: '' })) vi.mock('@opencode-manager/shared/config/env', () => ({ getConfigPath: () => paths.configDir, getOpenCodeConfigFilePath: () => paths.config, getOpenCodeConfigHome: () => paths.configHome, getOpenCodeHealthWatchPath: () => paths.healthWatch, getWorkspacePath: () => paths.configDir, OPENCODE_CONFIG_SOURCE_NAMES: ['opencode.json', 'opencode.jsonc'], })) vi.mock('../../src/utils/logger', () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), }, })) const markRestartPendingMock = vi.hoisted(() => vi.fn()) const clearStartupErrorMock = vi.hoisted(() => vi.fn()) vi.mock('../../src/services/opencode-single-server', () => ({ opencodeServerManager: { markRestartPending: markRestartPendingMock, clearStartupError: clearStartupErrorMock, }, ConfigReloadError: class ConfigReloadError extends Error {}, })) import { migrate } from '../../src/db/migration-runner' import type { OpenCodeClient } from '../../src/services/opencode/client' import { allMigrations } from '../../src/db/migrations' import { SettingsService } from '../../src/services/settings' import { applyOpenCodeConfigUpdate, captureLastKnownGoodOpenCodeConfig, restoreLastKnownGoodOpenCodeConfig, seedOpenCodeConfigFile, type ApplyOpenCodeConfigResult, } from '../../src/services/opencode-config-apply' import { OPENCODE_CONFIG_SEED, OpenCodeConfigConflictError, OpenCodeConfigSourceInvalidError, OpenCodeConfigValidationError, readOpenCodeConfigFile, writeOpenCodeConfigFile, } from '../../src/services/opencode-config-file' function expectStatus( result: ApplyOpenCodeConfigResult, status: T, ): Extract { expect(result.status).toBe(status) return result as Extract } function parseSnapshot(snapshot: string): { version: number; sources: Array<{ name: string; rawContent: string }> } { return JSON.parse(snapshot) as { version: number; sources: Array<{ name: string; rawContent: string }> } } const locationReloadMock = vi.hoisted(() => vi.fn<() => Promise>()) const mcpAddMock = vi.hoisted(() => vi.fn<() => Promise>()) const mcpRemoveMock = vi.hoisted(() => vi.fn<() => Promise>()) const configGetMock = vi.hoisted(() => vi.fn<() => Promise>()) const openCodeClient = { api: { config: { get: configGetMock }, location: { reload: locationReloadMock }, mcp: { add: mcpAddMock, remove: mcpRemoveMock }, }, } as unknown as OpenCodeClient describe('opencode-config-apply', () => { let workDir: string let db: Database let settingsService: SettingsService function sourcePath(name: string): string { return path.join(workDir, name) } beforeEach(async () => { vi.clearAllMocks() locationReloadMock.mockReset().mockResolvedValue(undefined) mcpAddMock.mockReset().mockResolvedValue(undefined) mcpRemoveMock.mockReset().mockResolvedValue(undefined) configGetMock.mockReset().mockRejectedValue(new Error('OpenCode unavailable')) workDir = await mkdtemp(path.join(tmpdir(), 'opencode-config-apply-')) paths.config = path.join(workDir, 'opencode.json') paths.configDir = workDir paths.configHome = path.join(workDir, '..', `${path.basename(workDir)}-home`) paths.healthWatch = path.join(workDir, 'health-watch') await mkdir(paths.configHome, { recursive: true }) db = new Database(':memory:') migrate(db, allMigrations) settingsService = new SettingsService(db) }) afterEach(async () => { db.close() await rm(workDir, { recursive: true, force: true }) await rm(paths.configHome, { recursive: true, force: true }) }) it.each([ ['theme', { theme: 'light' }], ['model', { model: 'a/b' }], ['agents', { agents: { reviewer: { description: 'Reviews' } } }], ['permissions', { permissions: [{ action: 'webfetch', resource: '*', effect: 'deny' }] }], ['plugins', { plugins: ['/plugins/probe'] }], ['providers', { providers: { example: { package: 'y' } } }], ])('reloads OpenCode without a restart for a semantic %s change', async (_field, change) => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') const result = expectStatus(await applyOpenCodeConfigUpdate({ content: change, settingsService, openCodeClient, }), 'reloaded') expect(locationReloadMock).toHaveBeenCalledTimes(1) expect(markRestartPendingMock).not.toHaveBeenCalled() expect(result.config.content).toEqual(change) }) it('keeps an unchanged {env:} reference when a merge patch resends the resolved value', async () => { await writeFile( sourcePath('opencode.json'), '{\n // keep me\n "providers": { "acme": { "settings": { "apiKey": "{env:ACME_KEY}" } } }\n}\n', 'utf8', ) configGetMock.mockResolvedValue([ { type: 'document', path: sourcePath('opencode.json'), info: { providers: { acme: { settings: { apiKey: 'secret' } } } }, }, ]) await applyOpenCodeConfigUpdate({ content: { providers: { acme: { name: 'Acme', settings: { apiKey: 'secret' } } } }, mode: 'merge', settingsService, openCodeClient, }) const written = await readFile(sourcePath('opencode.json'), 'utf8') expect(written).toContain('"apiKey": "{env:ACME_KEY}"') expect(written).toContain('"name": "Acme"') expect(written).toContain('// keep me') expect(written).not.toContain('secret') }) it('marks a restart pending when the reload fails', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') locationReloadMock.mockRejectedValue(new Error('OpenCode server unavailable')) const result = expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'light' }, settingsService, openCodeClient, }), 'restart_pending') expect(result.config.content).toEqual({ theme: 'light' }) expect(markRestartPendingMock).toHaveBeenCalledTimes(1) }) it('applies a comment-only edit without marking a restart pending', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') const commented = '{\n // keep this comment\n "theme": "dark"\n}\n' const result = expectStatus(await applyOpenCodeConfigUpdate({ content: commented, source: 'opencode.json', settingsService, openCodeClient, }), 'applied') expect(result.config.rawContent).toBe(commented) await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(commented) expect(markRestartPendingMock).not.toHaveBeenCalled() expect(locationReloadMock).not.toHaveBeenCalled() }) it('writes a raw string to the explicitly requested source verbatim', async () => { await writeFile(sourcePath('opencode.json'), '{"model":"a/b"}', 'utf8') const submitted = '{\n // config source\n "theme": "light"\n}\n' const result = expectStatus(await applyOpenCodeConfigUpdate({ content: submitted, source: 'opencode.jsonc', settingsService, openCodeClient, }), 'reloaded') await expect(readFile(sourcePath('opencode.jsonc'), 'utf8')).resolves.toBe(submitted) await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe('{"model":"a/b"}') expect(result.config.content).toEqual({ model: 'a/b', theme: 'light' }) }) it('throws a conflict and preserves last known good when the expected revision is stale', async () => { const initial = await writeOpenCodeConfigFile('{"theme":"dark"}', 'opencode.jsonc') settingsService.saveLastKnownGoodConfig('sentinel') await writeFile(sourcePath('opencode.json'), '{"theme":"light"}', 'utf8') await expect(applyOpenCodeConfigUpdate({ content: { theme: 'system' }, expectedRevision: initial.revision, settingsService, openCodeClient, })).rejects.toBeInstanceOf(OpenCodeConfigConflictError) expect(settingsService.getLastKnownGoodConfig()).toBe('sentinel') await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe('{"theme":"light"}') expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('saves the valid prior full snapshot as last known good after a successful write', async () => { const previous = '{"theme":"dark"}' await writeFile(sourcePath('opencode.json'), previous, 'utf8') await applyOpenCodeConfigUpdate({ content: { theme: 'light' }, settingsService, openCodeClient, }) const lastGood = settingsService.getLastKnownGoodConfig() expect(lastGood).not.toBeNull() const parsed = parseSnapshot(lastGood as string) expect(parsed.version).toBe(1) expect(parsed.sources).toEqual([{ name: 'opencode.json', rawContent: previous }]) }) it('rejects an object update when a source is invalid and keeps last known good', async () => { await writeFile(sourcePath('opencode.json'), '{"model": 5}', 'utf8') settingsService.saveLastKnownGoodConfig('sentinel') await expect(applyOpenCodeConfigUpdate({ content: { theme: 'light' }, settingsService, openCodeClient, })).rejects.toBeInstanceOf(OpenCodeConfigSourceInvalidError) expect(settingsService.getLastKnownGoodConfig()).toBe('sentinel') await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe('{"model": 5}') expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('rejects invalid raw content and writes nothing', async () => { const previous = '{"theme":"dark"}' await writeFile(sourcePath('opencode.json'), previous, 'utf8') await expect(applyOpenCodeConfigUpdate({ content: '{"model": 5}', source: 'opencode.json', settingsService, openCodeClient, })).rejects.toBeInstanceOf(OpenCodeConfigValidationError) await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe(previous) expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('restores the prior sources if persisting last known good fails', async () => { const lower = '{"model":"a/b"}' const higher = '{\n // keep\n "theme":"dark"\n}' await writeFile(sourcePath('opencode.json'), lower) await writeFile(sourcePath('opencode.jsonc'), higher) vi.spyOn(settingsService, 'saveLastKnownGoodConfig').mockImplementation(() => { throw new Error('database write failed') }) await expect(applyOpenCodeConfigUpdate({ content: { model: 'a/b', theme: 'light' }, settingsService, openCodeClient, })).rejects.toThrow('database write failed') expect(await readFile(sourcePath('opencode.json'), 'utf8')).toBe(lower) expect(await readFile(sourcePath('opencode.jsonc'), 'utf8')).toBe(higher) expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('reloads after repairing an invalid source even when merged values are unchanged', async () => { await writeFile(sourcePath('opencode.json'), '{"model":123}') await writeFile(sourcePath('opencode.jsonc'), '{"model":"a/b"}') const result = await applyOpenCodeConfigUpdate({ content: '{}', source: 'opencode.json', settingsService, openCodeClient, }) expect(result.status).toBe('reloaded') expect(locationReloadMock).toHaveBeenCalledOnce() expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('preserves unknown fields sent back with the merged content', async () => { const raw = JSON.stringify({ theme: 'dark', customTool: { enabled: true } }) await writeFile(sourcePath('opencode.json'), raw, 'utf8') const current = await readOpenCodeConfigFile() const result = expectStatus(await applyOpenCodeConfigUpdate({ content: { ...current!.content, theme: 'light' }, settingsService, openCodeClient, }), 'reloaded') expect(result.config.content).toEqual({ theme: 'light', customTool: { enabled: true } }) const onDisk = JSON.parse(await readFile(sourcePath('opencode.json'), 'utf8')) as Record expect(onDisk).toEqual({ theme: 'light', customTool: { enabled: true } }) }) it('captures every source as last known good and restores them together', async () => { await writeFile(sourcePath('opencode.json'), '{"model":"a/b"}', 'utf8') await writeFile(sourcePath('opencode.jsonc'), '{"theme":"dark"}', 'utf8') await captureLastKnownGoodOpenCodeConfig(settingsService) await rm(sourcePath('opencode.json')) await rm(sourcePath('opencode.jsonc')) const restored = await restoreLastKnownGoodOpenCodeConfig(settingsService) expect(restored?.content).toEqual({ model: 'a/b', theme: 'dark' }) await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe('{"model":"a/b"}') await expect(readFile(sourcePath('opencode.jsonc'), 'utf8')).resolves.toBe('{"theme":"dark"}') expect(clearStartupErrorMock).toHaveBeenCalledTimes(1) }) it('does not capture an invalid prior snapshot as last known good', async () => { await writeFile(sourcePath('opencode.json'), '{"model": 5}', 'utf8') settingsService.saveLastKnownGoodConfig('sentinel') const captured = await captureLastKnownGoodOpenCodeConfig(settingsService) expect(captured?.isValid).toBe(false) expect(settingsService.getLastKnownGoodConfig()).toBe('sentinel') }) it('restores a legacy raw last known good snapshot as an opencode.json source', async () => { const service = { getLastKnownGoodConfig: () => '{"theme":"dark"}' } as unknown as SettingsService const restored = await restoreLastKnownGoodOpenCodeConfig(service) expect(restored?.content).toEqual({ theme: 'dark' }) await expect(readFile(sourcePath('opencode.json'), 'utf8')).resolves.toBe('{"theme":"dark"}') expect(clearStartupErrorMock).toHaveBeenCalledTimes(1) }) it('returns null from restore when no last known good config exists', async () => { const service = { getLastKnownGoodConfig: () => null } as unknown as SettingsService expect(await restoreLastKnownGoodOpenCodeConfig(service)).toBeNull() expect(clearStartupErrorMock).not.toHaveBeenCalled() }) it('reloads an mcp-only change without touching the MCP API', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') const server = { type: 'local' as const, command: ['npx', 'local'] } const result = expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'dark', mcp: { servers: { local: server } } }, settingsService, openCodeClient, }), 'reloaded') expect(result.config.content).toEqual({ theme: 'dark', mcp: { servers: { local: server } } }) expect(locationReloadMock).toHaveBeenCalledOnce() expect(mcpAddMock).not.toHaveBeenCalled() expect(mcpRemoveMock).not.toHaveBeenCalled() expect(markRestartPendingMock).not.toHaveBeenCalled() }) it('reloads an mcp-only removal without touching the MCP API', async () => { const server = { type: 'local' as const, command: ['npx', 'local'] } await writeFile(sourcePath('opencode.json'), JSON.stringify({ theme: 'dark', mcp: { servers: { local: server } } }), 'utf8') expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'dark', mcp: { servers: {} } }, settingsService, openCodeClient, }), 'reloaded') expect(locationReloadMock).toHaveBeenCalledOnce() expect(mcpRemoveMock).not.toHaveBeenCalled() expect(mcpAddMock).not.toHaveBeenCalled() }) it('reloads an mcp-only change that touches a legacy flat entry', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'dark', mcp: { legacy: { type: 'local', command: ['npx', 'legacy'] } } }, settingsService, openCodeClient, }), 'reloaded') expect(locationReloadMock).toHaveBeenCalledOnce() expect(mcpAddMock).not.toHaveBeenCalled() expect(mcpRemoveMock).not.toHaveBeenCalled() }) it('reloads when mcp changes alongside another key', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') const result = expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'light', mcp: { local: { type: 'local' } } }, settingsService, openCodeClient, }), 'reloaded') expect(result.config.content).toEqual({ theme: 'light', mcp: { local: { type: 'local' } } }) expect(locationReloadMock).toHaveBeenCalledTimes(1) }) it('applies an unchanged content edit without reloading or marking a restart pending', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') expectStatus(await applyOpenCodeConfigUpdate({ content: { theme: 'dark' }, settingsService, openCodeClient, }), 'applied') expect(markRestartPendingMock).not.toHaveBeenCalled() expect(locationReloadMock).not.toHaveBeenCalled() }) it('seeds a minimal opencode.jsonc snapshot and removes every other source', async () => { await writeFile(sourcePath('opencode.json'), '{"theme":"dark"}', 'utf8') const seeded = await seedOpenCodeConfigFile() expect(seeded.path).toBe(sourcePath('opencode.jsonc')) expect(seeded.rawContent).toBe(OPENCODE_CONFIG_SEED) expect(seeded.content).toEqual({ $schema: 'https://opencode.ai/config.json' }) expect(seeded.isValid).toBe(true) await expect(readdir(workDir)).resolves.toEqual(['opencode.jsonc']) }) })