mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* feat(sandbox): add KVM-backed agent sandboxing via microsandbox * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * feat(sandbox): route host-shell surfaces into microVM via shell wrapper * fix(sandbox): harden enforcement validation and backup handling * fix(sandbox): bound lifecycle queue and harden install/backup paths Bound the OpenCode supervisor lifecycle queue to one running plus one pending operation so repeated restarts cannot stack unbounded HTTP requests, while exempting stop() so shutdown is never dropped. Reject enforcement backups that carry no recoverable removed sections, matching the existing fail-closed handling of unparseable backups. Install OpenCode through a private mktemp staging directory instead of the predictable /tmp/opencode path. Use named path/os imports in the files added by this branch. * refactor(sandbox): remove OpenCode version allowlist gating under enforcement * fix(settings): use resolved opencode binary and verify installed version * refactor(sandbox): scope enforcement to agent bash tool only * fix(sandbox): plan bash against factory directory and attest runtime tmpfs; align permission reply API * fix(sandbox): pin neutral entrypoint and persist opencode upgrades across rebuilds * fix(sandbox): align canonical spec with msb 0.6.8 runtime defaults * feat(sandbox): show unwrapped sandboxed bash commands in message UI * refactor(sandbox): pin shell shim to sandbox agent bash without rewriting commands * feat(sandbox): route notifications through a host-side manager tool * feat(sandbox): route all assistant capabilities through the ocm tool Add an allow-listed request action to the generated ocm plugin so agents reach settings, assistant reload, repos, git-info, workspaces, and schedules through the tool instead of curling the internal API with a bearer token, which is impossible under sandboxing. Rewrite the curl-based skills to use the ocm request action and drop the apiBaseUrl plumbing. Remove the now-unread .opencode/internal-token file and the sandbox tmpfs mask that hid it; attestation now rejects any tmpfs other than the runtime /tmp mount. * fix(sidebar): wire new-session and new-schedule actions to every page AssistantRedirect never registered a listener for the sidebar's new-session event, so the desktop sidebar CTA on /assistant was a no-op; the Schedules pages had the same gap for new-schedule. Make useSidebarAction the single source of truth for action keys and event dispatch, and warn when an action is dispatched with no mounted handler so an unhandled CTA fails loudly instead of silently. Wire the missing listeners to the existing per-page handlers. * feat(sandbox): forward git credentials into the microVM and add sandbox guest image * feat(sandbox): add git credential forwarding toggle to settings
719 lines
27 KiB
TypeScript
719 lines
27 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
import { Hono } from 'hono'
|
|
import type { Database } from 'bun:sqlite'
|
|
import { createOpenCodeProxyRoutes } from '../../src/routes/opencode-proxy'
|
|
import type { SettingsService } from '../../src/services/settings'
|
|
import { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
|
|
|
vi.mock('bun:sqlite', () => ({
|
|
Database: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('../../src/services/internal-token', () => ({
|
|
getOrCreateInternalToken: vi.fn().mockReturnValue('test-internal-token'),
|
|
}))
|
|
|
|
const isLifecycleInitializedMock = vi.hoisted(() => vi.fn().mockReturnValue(true))
|
|
|
|
vi.mock('../../src/services/opencode-single-server', () => ({
|
|
opencodeServerManager: { isLifecycleInitialized: isLifecycleInitializedMock },
|
|
}))
|
|
|
|
const mockSettingsService = {
|
|
getOpenCodeServerPassword: vi.fn().mockReturnValue('test-password'),
|
|
} as unknown as SettingsService
|
|
|
|
const mockDb = {} as Database
|
|
|
|
describe('opencode-proxy routes', () => {
|
|
let app: Hono
|
|
let originalFetch: typeof globalThis.fetch
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
isLifecycleInitializedMock.mockReturnValue(true)
|
|
originalFetch = globalThis.fetch
|
|
app = new Hono()
|
|
app.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService))
|
|
})
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = originalFetch
|
|
})
|
|
|
|
it('returns 401 without authorization header', async () => {
|
|
const res = await app.request('/api/opencode-proxy/doc')
|
|
expect(res.status).toBe(401)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toBe('Unauthorized')
|
|
})
|
|
|
|
it('returns 503 and never forwards when the OpenCode lifecycle is not initialized', async () => {
|
|
isLifecycleInitializedMock.mockReturnValue(false)
|
|
const upstreamFetch = vi.fn().mockResolvedValue(new Response('should not be reached'))
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/session/ses_1/message', {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(503)
|
|
expect(upstreamFetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 401 with invalid bearer token', async () => {
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer wrong-token' },
|
|
})
|
|
expect(res.status).toBe(401)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toBe('Unauthorized')
|
|
})
|
|
|
|
it('returns 401 with invalid basic auth password', async () => {
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Basic ' + Buffer.from('opencode:wrong-password').toString('base64') },
|
|
})
|
|
expect(res.status).toBe(401)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toBe('Unauthorized')
|
|
})
|
|
|
|
it('returns 200 with valid bearer and injected Basic auth', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchUrl = fetchCall[0]
|
|
expect(fetchUrl).toContain('http://127.0.0.1:')
|
|
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
expect(fetchHeaders['Authorization']).toMatch(/^Basic /)
|
|
expect(fetchHeaders['Authorization']).not.toContain('Bearer')
|
|
expect(fetchHeaders['Authorization']).toContain(
|
|
Buffer.from('opencode:test-password').toString('base64')
|
|
)
|
|
})
|
|
|
|
it('returns 200 with valid basic auth (opencode attach) and injected Basic auth', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const basicAuthHeader = 'Basic ' + Buffer.from('opencode:test-internal-token').toString('base64')
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: basicAuthHeader },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
|
|
expect(fetchHeaders['Authorization']).toMatch(/^Basic /)
|
|
expect(fetchHeaders['Authorization']).not.toContain('Bearer')
|
|
expect(fetchHeaders['Authorization']).toContain(
|
|
Buffer.from('opencode:test-password').toString('base64')
|
|
)
|
|
})
|
|
|
|
it('strips caller Bearer and injects Basic auth', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
await app.request('/api/opencode-proxy/doc', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'x-opencode-directory': '/some/dir',
|
|
},
|
|
})
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
|
|
expect(fetchHeaders['Authorization']).not.toContain('Bearer')
|
|
expect(fetchHeaders['Authorization']).toMatch(/^Basic /)
|
|
expect(fetchHeaders['x-opencode-directory']).toBe('/some/dir')
|
|
})
|
|
|
|
it('forwards x-opencode-directory header unchanged', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
await app.request('/api/opencode-proxy/doc', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'x-opencode-directory': '/home/user/project',
|
|
'x-opencode-workspace': 'my-workspace',
|
|
},
|
|
})
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
|
|
expect(fetchHeaders['x-opencode-directory']).toBe('/home/user/project')
|
|
expect(fetchHeaders['x-opencode-workspace']).toBe('my-workspace')
|
|
})
|
|
|
|
it('returns 501 for WebSocket upgrade requests', async () => {
|
|
const res = await app.request('/api/opencode-proxy/ws', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
Connection: 'Upgrade',
|
|
Upgrade: 'websocket',
|
|
},
|
|
})
|
|
expect(res.status).toBe(501)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toContain('WebSocket')
|
|
})
|
|
|
|
it('preserves SSE content-type header from upstream', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('event: message\ndata: hello\n\n', {
|
|
status: 200,
|
|
headers: { 'content-type': 'text/event-stream' },
|
|
})
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/events', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get('content-type')).toBe('text/event-stream')
|
|
})
|
|
|
|
it('does not buffer SSE response body', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('data: chunk1\n\ndata: chunk2\n\n', {
|
|
status: 200,
|
|
headers: { 'content-type': 'text/event-stream' },
|
|
})
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/events', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(res.body).toBeDefined()
|
|
})
|
|
|
|
it('returns 502 when upstream fetch fails', async () => {
|
|
const upstreamFetch = vi.fn().mockRejectedValue(new Error('Connection refused'))
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(502)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toBe('Proxy request failed')
|
|
})
|
|
|
|
it('preserves query string in upstream URL', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
await app.request('/api/opencode-proxy/doc?foo=bar&baz=qux', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchUrl = fetchCall[0]
|
|
expect(fetchUrl).toContain('?foo=bar&baz=qux')
|
|
})
|
|
|
|
it('strips hop-by-hop headers from request', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
await app.request('/api/opencode-proxy/doc', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
Host: 'localhost:5003',
|
|
Connection: 'keep-alive',
|
|
'Transfer-Encoding': 'chunked',
|
|
},
|
|
})
|
|
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
|
|
expect(fetchHeaders['Host']).toBeUndefined()
|
|
expect(fetchHeaders['host']).toBeUndefined()
|
|
expect(fetchHeaders['Connection']).toBeUndefined()
|
|
expect(fetchHeaders['connection']).toBeUndefined()
|
|
expect(fetchHeaders['Transfer-Encoding']).toBeUndefined()
|
|
expect(fetchHeaders['transfer-encoding']).toBeUndefined()
|
|
})
|
|
|
|
it('strips hop-by-hop headers from response', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', {
|
|
status: 200,
|
|
headers: {
|
|
'content-type': 'text/plain',
|
|
connection: 'keep-alive',
|
|
'transfer-encoding': 'chunked',
|
|
},
|
|
})
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.headers.get('connection')).toBeNull()
|
|
expect(res.headers.get('transfer-encoding')).toBeNull()
|
|
expect(res.headers.get('content-type')).toBe('text/plain')
|
|
})
|
|
|
|
it('forwards the session shell endpoint', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/session/ses_1/shell', {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
})
|
|
|
|
it('forwards percent-encoded PTY paths when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/%70ty', {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
})
|
|
|
|
it('forwards custom slash command execution when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/session/ses_1/command', {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
})
|
|
|
|
it('forwards a local MCP server add when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'evil',
|
|
config: { type: 'local', command: ['node', 'server.js'] },
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual({
|
|
name: 'evil',
|
|
config: { type: 'local', command: ['node', 'server.js'] },
|
|
})
|
|
})
|
|
|
|
it('forwards a command-bearing MCP add without an explicit local type when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'evil',
|
|
config: { command: ['npx', 'evil-server'] },
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual({
|
|
name: 'evil',
|
|
config: { command: ['npx', 'evil-server'] },
|
|
})
|
|
})
|
|
|
|
it('forwards a remote MCP server add when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'remote-server',
|
|
config: { type: 'remote', url: 'https://example.com/mcp' },
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual({
|
|
name: 'remote-server',
|
|
config: { type: 'remote', url: 'https://example.com/mcp' },
|
|
})
|
|
})
|
|
|
|
it('forwards MCP server adds raw when enforcement is off', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/mcp', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
name: 'local-server',
|
|
config: { type: 'local', command: ['node', 'server.js'] },
|
|
}),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as { config: { type: string } }
|
|
expect(forwarded.config.type).toBe('local')
|
|
})
|
|
|
|
it('forwards a PATCH /config mutation with LSP servers and experimental hooks exactly when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const body = JSON.stringify({
|
|
lsp: { typescript: { command: ['typescript-language-server'] } },
|
|
experimental: {
|
|
hook: { file_edited: [{ command: ['chmod', '+x', 'x'] }] },
|
|
chatMaxRetries: 4,
|
|
},
|
|
})
|
|
const res = await app.request('/api/opencode-proxy/config', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual(JSON.parse(body))
|
|
})
|
|
|
|
it('forwards ordinary agent endpoints when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/session/ses_1/prompt_async', {
|
|
method: 'POST',
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalled()
|
|
})
|
|
|
|
it('forwards a PATCH /config mutation with plugins exactly when the OpenCode child is enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const body = JSON.stringify({ theme: 'dark', plugin: ['opencode-plugin-npm'] })
|
|
const res = await app.request('/api/opencode-proxy/config', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual(JSON.parse(body))
|
|
})
|
|
|
|
it('forwards a PATCH /config mutation with local MCP servers and formatter config exactly when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const body = JSON.stringify({
|
|
formatter: { command: 'prettier' },
|
|
mcp: { local: { type: 'local', command: ['node', 'server.js'] } },
|
|
})
|
|
const res = await app.request('/api/opencode-proxy/config', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body,
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual(JSON.parse(body))
|
|
})
|
|
|
|
it('forwards a malformed PATCH /config body exactly when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/config', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: '{not json',
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(await new Response(fetchCall[1].body as ReadableStream).text()).toBe('{not json')
|
|
})
|
|
|
|
it('forwards PATCH /config mutations raw when enforcement is off', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/config', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ theme: 'dark', plugin: ['opencode-plugin-npm'] }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded.plugin).toEqual(['opencode-plugin-npm'])
|
|
})
|
|
|
|
it('forwards non-config mutations raw when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/session/ses_1/message', {
|
|
method: 'PATCH',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ content: 'hello' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded.content).toBe('hello')
|
|
})
|
|
|
|
it('forwards a well-known auth write when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/auth/sso.example.com', {
|
|
method: 'PUT',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ type: 'wellknown', key: 'SSO_TOKEN', token: 't' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual({ type: 'wellknown', key: 'SSO_TOKEN', token: 't' })
|
|
})
|
|
|
|
it('forwards api and oauth auth writes when enforced', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/auth/anthropic', {
|
|
method: 'PUT',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ type: 'api', key: 'sk-test' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded).toEqual({ type: 'api', key: 'sk-test' })
|
|
})
|
|
|
|
it('forwards auth writes raw when enforcement is off', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'application/json' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const res = await app.request('/api/opencode-proxy/auth/sso.example.com', {
|
|
method: 'PUT',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ type: 'wellknown', key: 'SSO_TOKEN', token: 't' }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(await new Response(fetchCall[1].body as ReadableStream).text()) as Record<string, unknown>
|
|
expect(forwarded.type).toBe('wellknown')
|
|
})
|
|
|
|
it('returns 503 through the proxy gate on a below-threshold health failure and reopens once the supervisor recovers', async () => {
|
|
const lifecycle = { initialized: true }
|
|
isLifecycleInitializedMock.mockImplementation(() => lifecycle.initialized)
|
|
const manager = {
|
|
start: vi.fn().mockResolvedValue(undefined),
|
|
stop: vi.fn().mockResolvedValue(undefined),
|
|
isOperationInProgress: vi.fn(() => false),
|
|
checkHealth: vi.fn().mockResolvedValue(true),
|
|
restart: vi.fn().mockResolvedValue(undefined),
|
|
reloadConfig: vi.fn().mockResolvedValue(undefined),
|
|
clearStartupError: vi.fn(),
|
|
getLastStartupError: vi.fn(() => null),
|
|
isLastStartupErrorNonRecoverable: vi.fn(() => false),
|
|
setLifecycleInitialized: vi.fn((value: boolean) => { lifecycle.initialized = value }),
|
|
getPort: vi.fn(() => 5551),
|
|
getVersion: vi.fn(() => '1.0.137'),
|
|
getMinVersion: vi.fn(() => '1.0.137'),
|
|
isVersionSupported: vi.fn(() => true),
|
|
}
|
|
const supervisor = new OpenCodeSupervisor(manager as unknown as never, {} as SettingsService, {
|
|
failureThreshold: 2,
|
|
watchEnabled: false,
|
|
})
|
|
await supervisor.start()
|
|
|
|
const upstreamFetch = vi.fn().mockResolvedValue(new Response('ok', { status: 200 }))
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
|
|
const healthyRes = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
expect(healthyRes.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalledTimes(1)
|
|
|
|
manager.checkHealth.mockResolvedValueOnce(false)
|
|
const status = await supervisor.checkNow('manual')
|
|
expect(status.state).toBe('unhealthy')
|
|
expect(lifecycle.initialized).toBe(false)
|
|
|
|
const blockedRes = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
expect(blockedRes.status).toBe(503)
|
|
expect(upstreamFetch).toHaveBeenCalledTimes(1)
|
|
|
|
manager.checkHealth.mockResolvedValueOnce(true)
|
|
const recovered = await supervisor.checkNow('manual')
|
|
expect(recovered.healthy).toBe(true)
|
|
expect(lifecycle.initialized).toBe(true)
|
|
|
|
const reopenedRes = await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
expect(reopenedRes.status).toBe(200)
|
|
expect(upstreamFetch).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|