mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +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
289 lines
10 KiB
TypeScript
289 lines
10 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
const sseMocks = vi.hoisted(() => ({
|
|
addClient: vi.fn(),
|
|
addDirectories: vi.fn(),
|
|
removeDirectories: vi.fn(),
|
|
setClientVisibility: vi.fn(),
|
|
getConnectionStatus: vi.fn(),
|
|
getClientCount: vi.fn(),
|
|
getActiveDirectories: vi.fn(),
|
|
getActiveSessions: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('../../src/services/sse-aggregator', () => ({
|
|
sseAggregator: sseMocks,
|
|
}))
|
|
|
|
vi.mock('../../src/utils/logger', () => ({
|
|
logger: {
|
|
info: vi.fn(),
|
|
error: vi.fn(),
|
|
warn: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
import { createSSERoutes } from '../../src/routes/sse'
|
|
import { encodeSSEFrame } from '../../src/utils/sse-frame'
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
async function readChunk(reader: { read(): Promise<{ value?: Uint8Array }> }): Promise<string> {
|
|
const { value } = await reader.read()
|
|
return decoder.decode(value)
|
|
}
|
|
|
|
describe('SSE Routes', () => {
|
|
let app: ReturnType<typeof createSSERoutes>
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
sseMocks.addClient.mockReturnValue(() => {})
|
|
sseMocks.addDirectories.mockReturnValue(true)
|
|
sseMocks.removeDirectories.mockReturnValue(true)
|
|
sseMocks.setClientVisibility.mockReturnValue(true)
|
|
sseMocks.getConnectionStatus.mockReturnValue({ connected: 1, total: 1 })
|
|
sseMocks.getClientCount.mockReturnValue(0)
|
|
sseMocks.getActiveDirectories.mockReturnValue([])
|
|
sseMocks.getActiveSessions.mockReturnValue({})
|
|
|
|
app = createSSERoutes()
|
|
})
|
|
|
|
describe('GET /stream', () => {
|
|
it('streams the connected frame and registers the client with parsed directories', async () => {
|
|
const cleanup = vi.fn()
|
|
sseMocks.addClient.mockReturnValue(cleanup)
|
|
sseMocks.getConnectionStatus.mockReturnValue({ connected: 1, total: 2 })
|
|
|
|
const res = await app.fetch(new Request('http://localhost/stream?directories=/one,/two,,/three'))
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('text/event-stream')
|
|
expect(res.headers.get('cache-control')).toBe('no-cache, no-store, no-transform')
|
|
expect(res.headers.get('connection')).toBe('keep-alive')
|
|
expect(res.headers.get('x-accel-buffering')).toBe('no')
|
|
|
|
const reader = res.body!.getReader()
|
|
try {
|
|
const first = await readChunk(reader)
|
|
expect(first).toContain('event: connected')
|
|
expect(first).toContain('"directories":["/one","/two","/three"]')
|
|
expect(first).toContain('"connected":1')
|
|
expect(first).toContain('"total":2')
|
|
|
|
const clientId = first.match(/"clientId":"([^"]+)"/)?.[1]
|
|
expect(clientId).toMatch(/^client_\d+_[a-z0-9]+$/)
|
|
|
|
expect(sseMocks.addClient).toHaveBeenCalledTimes(1)
|
|
const [idArg, callbackArg, frameWriterArg, directoriesArg] = sseMocks.addClient.mock.calls[0] as [
|
|
string,
|
|
(event: string, data: string) => void,
|
|
(frame: Uint8Array) => void,
|
|
string[],
|
|
]
|
|
expect(idArg).toBe(clientId)
|
|
expect(directoriesArg).toEqual(['/one', '/two', '/three'])
|
|
|
|
callbackArg('message', '{"from":"callback"}')
|
|
const second = await readChunk(reader)
|
|
expect(second).toBe('event: message\ndata: {"from":"callback"}\n\n')
|
|
|
|
frameWriterArg(encodeSSEFrame('message', '{"from":"frame"}'))
|
|
const third = await readChunk(reader)
|
|
expect(third).toBe('event: message\ndata: {"from":"frame"}\n\n')
|
|
} finally {
|
|
await reader.cancel()
|
|
}
|
|
|
|
expect(cleanup).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('defaults directories to an empty array when the query is absent', async () => {
|
|
const cleanup = vi.fn()
|
|
sseMocks.addClient.mockReturnValue(cleanup)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/stream'))
|
|
const reader = res.body!.getReader()
|
|
try {
|
|
const first = await readChunk(reader)
|
|
expect(first).toContain('event: connected')
|
|
expect(first).toContain('"directories":[]')
|
|
} finally {
|
|
await reader.cancel()
|
|
}
|
|
|
|
expect(sseMocks.addClient).toHaveBeenCalledWith(
|
|
expect.stringMatching(/^client_/),
|
|
expect.any(Function),
|
|
expect.any(Function),
|
|
[],
|
|
)
|
|
expect(cleanup).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
describe('POST /subscribe', () => {
|
|
it('subscribes directories for an existing client', async () => {
|
|
sseMocks.addDirectories.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'client-1', directories: ['/a', '/b'] }),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(sseMocks.addDirectories).toHaveBeenCalledWith('client-1', ['/a', '/b'])
|
|
})
|
|
|
|
it('returns 400 for an invalid subscribe body', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: '', directories: 'not-an-array' }),
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json() as { success: boolean; error: string }
|
|
expect(json.success).toBe(false)
|
|
expect(json.error).toBe('Invalid request')
|
|
expect(sseMocks.addDirectories).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 when the client is not found', async () => {
|
|
sseMocks.addDirectories.mockReturnValue(false)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/subscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'missing', directories: ['/a'] }),
|
|
}))
|
|
|
|
expect(res.status).toBe(404)
|
|
await expect(res.json()).resolves.toEqual({ success: false, error: 'Client not found' })
|
|
})
|
|
})
|
|
|
|
describe('POST /unsubscribe', () => {
|
|
it('unsubscribes directories for an existing client', async () => {
|
|
sseMocks.removeDirectories.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/unsubscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'client-1', directories: ['/a'] }),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(sseMocks.removeDirectories).toHaveBeenCalledWith('client-1', ['/a'])
|
|
})
|
|
|
|
it('returns 400 for an invalid unsubscribe body', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/unsubscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ directories: [] }),
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json() as { success: boolean; error: string }
|
|
expect(json.success).toBe(false)
|
|
expect(json.error).toBe('Invalid request')
|
|
expect(sseMocks.removeDirectories).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 when the client is not found', async () => {
|
|
sseMocks.removeDirectories.mockReturnValue(false)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/unsubscribe', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'missing', directories: ['/a'] }),
|
|
}))
|
|
|
|
expect(res.status).toBe(404)
|
|
await expect(res.json()).resolves.toEqual({ success: false, error: 'Client not found' })
|
|
})
|
|
})
|
|
|
|
describe('POST /visibility', () => {
|
|
it('sets visibility with an explicit active session id', async () => {
|
|
sseMocks.setClientVisibility.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/visibility', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'client-1', visible: true, activeSessionId: 'session-9' }),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(sseMocks.setClientVisibility).toHaveBeenCalledWith('client-1', true, 'session-9')
|
|
})
|
|
|
|
it('defaults the active session id to null when omitted', async () => {
|
|
sseMocks.setClientVisibility.mockReturnValue(true)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/visibility', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'client-1', visible: false }),
|
|
}))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({ success: true })
|
|
expect(sseMocks.setClientVisibility).toHaveBeenCalledWith('client-1', false, null)
|
|
})
|
|
|
|
it('returns 400 for an invalid visibility body', async () => {
|
|
const res = await app.fetch(new Request('http://localhost/visibility', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'client-1', visible: 'yes' }),
|
|
}))
|
|
|
|
expect(res.status).toBe(400)
|
|
const json = await res.json() as { success: boolean; error: string }
|
|
expect(json.success).toBe(false)
|
|
expect(json.error).toBe('Invalid request')
|
|
expect(sseMocks.setClientVisibility).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 when the client is not found', async () => {
|
|
sseMocks.setClientVisibility.mockReturnValue(false)
|
|
|
|
const res = await app.fetch(new Request('http://localhost/visibility', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ clientId: 'missing', visible: true }),
|
|
}))
|
|
|
|
expect(res.status).toBe(404)
|
|
await expect(res.json()).resolves.toEqual({ success: false, error: 'Client not found' })
|
|
})
|
|
})
|
|
|
|
describe('GET /status', () => {
|
|
it('returns the merged connection status and counts', async () => {
|
|
sseMocks.getConnectionStatus.mockReturnValue({ connected: 1, total: 2 })
|
|
sseMocks.getClientCount.mockReturnValue(4)
|
|
sseMocks.getActiveDirectories.mockReturnValue(['/a', '/b'])
|
|
sseMocks.getActiveSessions.mockReturnValue({ '/a': ['session-1'] })
|
|
|
|
const res = await app.fetch(new Request('http://localhost/status'))
|
|
|
|
expect(res.status).toBe(200)
|
|
await expect(res.json()).resolves.toEqual({
|
|
connected: 1,
|
|
total: 2,
|
|
clients: 4,
|
|
directories: ['/a', '/b'],
|
|
activeSessions: { '/a': ['session-1'] },
|
|
})
|
|
})
|
|
})
|
|
})
|