mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* refactor(config): make on-disk opencode.json the single source of truth Make <workspace>/.config/opencode/opencode.json the only store for the active OpenCode configuration. One owner (opencode-config-file.ts) reads, validates, writes, archives, and seeds the file; one owner (opencode-config-apply.ts) decides how an edit is applied: restart pending, or a live patch with recovery. Settings routes, the internal assistant API, the health-watch supervisor, boot, and host import all go through them, so an edit that lands on the file without going through the database is visible immediately instead of at the next Manager boot. Remove the named-config profiles. Migration 019 archives every opencode_configs row to .config/opencode-configs-archive/<name>.json, restores the default row to opencode.json when no file exists, then drops the table, its indexes, and repos.opencode_config_name. The /opencode-configs* and config/switch routes and the CreateConfigDialog/SwitchConfigDialog dialogs are gone, and the ocm tool gains GET/PUT /opencode-config backed by the same apply owner. Harden the write path: serialize and atomically write config updates while preserving the file mode, keep withFileLock usable after a rejection, route reloadConfig's cleaned-config write through the file owner, stop boot from rewriting an existing valid config when importing state, prune the health-watch directory to its newest 20 entries, resolve archive paths with realpath, and share one frontend config query across dialogs. * fix(mirror): canonicalize worktree paths before branch ownership check * refactor(config): unify opencode config apply and rollback paths - share one config route factory between public and internal mounts - capture last-known-good before every config write and restore via one helper - stop auto-restarting on save; flag restart-required fields and live-patch the rest - add CONFIG_PATCH_TIMEOUT_MS for config patch requests - extract canonicalPath helpers, health-watch artifact writer, and useProvidersWithModels hook * refactor(model-state): use OpenCode state file as model state source of truth
142 lines
4.6 KiB
TypeScript
142 lines
4.6 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import * as http from 'http'
|
|
import * as fs from 'fs/promises'
|
|
import { randomUUID } from 'crypto'
|
|
import { createIPCServer } from '../../src/ipc/ipcServer'
|
|
import type { IPCServer } from '../../src/ipc/ipcServer'
|
|
|
|
interface IPCResponse {
|
|
status: number
|
|
body: string
|
|
}
|
|
|
|
function sendRequest(socketPath: string, path: string, body?: string): Promise<IPCResponse> {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request({ socketPath, path, method: 'POST' }, (res) => {
|
|
const chunks: Buffer[] = []
|
|
res.on('data', (chunk: Buffer) => chunks.push(chunk))
|
|
res.on('end', () => {
|
|
resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })
|
|
})
|
|
})
|
|
req.on('error', reject)
|
|
if (body !== undefined) {
|
|
req.write(body)
|
|
}
|
|
req.end()
|
|
})
|
|
}
|
|
|
|
describe('IPCServer', () => {
|
|
const originalXdgRuntimeDir = process.env['XDG_RUNTIME_DIR']
|
|
const servers: IPCServer[] = []
|
|
|
|
const startServer = async (context: string = randomUUID()): Promise<IPCServer> => {
|
|
const server = await createIPCServer(context)
|
|
servers.push(server)
|
|
return server
|
|
}
|
|
|
|
beforeEach(() => {
|
|
delete process.env['XDG_RUNTIME_DIR']
|
|
})
|
|
|
|
afterEach(async () => {
|
|
for (const server of servers.splice(0)) {
|
|
await server.dispose()
|
|
await fs.unlink(server.ipcHandlePath).catch(() => {})
|
|
}
|
|
if (originalXdgRuntimeDir === undefined) {
|
|
delete process.env['XDG_RUNTIME_DIR']
|
|
} else {
|
|
process.env['XDG_RUNTIME_DIR'] = originalXdgRuntimeDir
|
|
}
|
|
})
|
|
|
|
it('registers and retrieves handlers by name', async () => {
|
|
const server = await startServer()
|
|
const handler = { handle: vi.fn(async () => 'ok') }
|
|
|
|
server.registerHandler('askpass', handler)
|
|
|
|
expect(server.getHandler('askpass')).toBe(handler)
|
|
expect(server.getHandler('missing')).toBeUndefined()
|
|
})
|
|
|
|
it('exposes the socket path through getEnv', async () => {
|
|
const server = await startServer()
|
|
|
|
expect(server.getEnv()).toEqual({ VSCODE_GIT_IPC_HANDLE: server.ipcHandlePath })
|
|
})
|
|
|
|
it('creates a random socket path when no context is given', async () => {
|
|
const server = await startServer(randomUUID())
|
|
const randomServer = await createIPCServer()
|
|
servers.push(randomServer)
|
|
|
|
expect(randomServer.ipcHandlePath).toContain('opencode-git-')
|
|
expect(randomServer.ipcHandlePath).not.toBe(server.ipcHandlePath)
|
|
})
|
|
|
|
it('returns the handler result as JSON with status 200', async () => {
|
|
const server = await startServer()
|
|
server.registerHandler('askpass', { handle: async (request) => ({ request, value: 7 }) })
|
|
|
|
const response = await sendRequest(server.ipcHandlePath, '/askpass', JSON.stringify({ user: 'alice' }))
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(JSON.parse(response.body)).toEqual({ request: { user: 'alice' }, value: 7 })
|
|
})
|
|
|
|
it('returns 404 with an error body for an unknown path', async () => {
|
|
const server = await startServer()
|
|
|
|
const response = await sendRequest(server.ipcHandlePath, '/unknown', '{}')
|
|
|
|
expect(response.status).toBe(404)
|
|
expect(JSON.parse(response.body)).toEqual({ error: 'Handler not found' })
|
|
})
|
|
|
|
it('returns 400 when the request body is empty', async () => {
|
|
const server = await startServer()
|
|
server.registerHandler('askpass', { handle: async () => 'ok' })
|
|
|
|
const response = await sendRequest(server.ipcHandlePath, '/askpass')
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(JSON.parse(response.body)).toEqual({ error: 'Empty request body' })
|
|
})
|
|
|
|
it('returns 500 when the handler throws', async () => {
|
|
const server = await startServer()
|
|
server.registerHandler('askpass', {
|
|
handle: async () => {
|
|
throw new Error('handler exploded')
|
|
},
|
|
})
|
|
|
|
const response = await sendRequest(server.ipcHandlePath, '/askpass', '{}')
|
|
|
|
expect(response.status).toBe(500)
|
|
expect(JSON.parse(response.body)).toEqual({ error: 'Internal server error' })
|
|
})
|
|
|
|
it('returns 500 when the request body is not valid JSON', async () => {
|
|
const server = await startServer()
|
|
server.registerHandler('askpass', { handle: async () => 'ok' })
|
|
|
|
const response = await sendRequest(server.ipcHandlePath, '/askpass', 'not-json')
|
|
|
|
expect(response.status).toBe(500)
|
|
expect(JSON.parse(response.body)).toEqual({ error: 'Internal server error' })
|
|
})
|
|
|
|
it('closes the server on dispose', async () => {
|
|
const server = await startServer()
|
|
|
|
await server.dispose()
|
|
|
|
await expect(sendRequest(server.ipcHandlePath, '/askpass', '{}')).rejects.toBeDefined()
|
|
})
|
|
})
|