mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
1378 lines
52 KiB
TypeScript
1378 lines
52 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach, beforeAll, afterAll } from 'vitest'
|
|
import { execFileSync, spawn } from 'child_process'
|
|
import { createServer, request } from 'http'
|
|
import type { Server } from 'http'
|
|
import type { AddressInfo } from 'net'
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
|
|
import { tmpdir } from 'os'
|
|
import { join } from 'path'
|
|
import { Hono } from 'hono'
|
|
import { createNodeWebSocket } from '@hono/node-ws'
|
|
import type { Database } from 'bun:sqlite'
|
|
import { buildOpenCodeBasicAuth } from '@opencode-manager/shared/opencode'
|
|
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
|
|
import { createOpenCodeProxyRoutes } from '../../src/routes/opencode-proxy'
|
|
import type { SettingsService } from '../../src/services/settings'
|
|
import { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
|
|
import { resolveOpenCode2Binary, startOpenCodeServe, type OpenCodeServe } from '../helpers/opencode-binary'
|
|
import { startWebSocketServer } from '../helpers/websocket-server'
|
|
|
|
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 getRepoByIdMock = vi.hoisted(() => vi.fn())
|
|
|
|
vi.mock('../../src/db/queries', () => ({
|
|
getRepoById: getRepoByIdMock,
|
|
}))
|
|
|
|
const upstreamBaseUrl = vi.hoisted(() => ({ value: 'http://127.0.0.1:5551' }))
|
|
|
|
vi.mock('../../src/services/opencode/upstream', async (importOriginal) => ({
|
|
...await importOriginal<typeof import('../../src/services/opencode/upstream')>(),
|
|
getOpenCodeUpstreamBaseUrl: () => upstreamBaseUrl.value,
|
|
}))
|
|
|
|
const getOpenCodeServerPasswordMock = vi.hoisted(() => vi.fn().mockReturnValue('test-password'))
|
|
|
|
const readyRepo = { id: 7, fullPath: '/srv/repos/my-repo', cloneStatus: 'ready' }
|
|
|
|
const mockSettingsService = {
|
|
getOpenCodeServerPassword: getOpenCodeServerPasswordMock,
|
|
} as unknown as SettingsService
|
|
|
|
const mockDb = {} as Database
|
|
|
|
function createProxyApp(): Hono {
|
|
const proxyApp = new Hono()
|
|
proxyApp.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService, createNodeWebSocket({ app: proxyApp }).upgradeWebSocket))
|
|
return proxyApp
|
|
}
|
|
|
|
function upstreamOk() {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(
|
|
new Response('ok', { status: 200, headers: { 'content-type': 'text/plain' } })
|
|
)
|
|
globalThis.fetch = upstreamFetch as unknown as typeof fetch
|
|
return upstreamFetch
|
|
}
|
|
|
|
describe('opencode-proxy routes', () => {
|
|
let app: Hono
|
|
let originalFetch: typeof globalThis.fetch
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
isLifecycleInitializedMock.mockReturnValue(true)
|
|
originalFetch = globalThis.fetch
|
|
app = createProxyApp()
|
|
})
|
|
|
|
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('defaults x-opencode-directory to the workspace when the caller sends no location', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
await app.request('/api/opencode-proxy/doc', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
const fetchHeaders = (upstreamFetch.mock.calls[0] as [string, RequestInit])[1].headers as Record<string, string>
|
|
expect(fetchHeaders['x-opencode-directory']).toBe(encodeURIComponent(getWorkspacePath()))
|
|
})
|
|
|
|
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('still requires authorization for WebSocket upgrades without a PTY ticket', async () => {
|
|
const res = await app.request('/api/opencode-proxy/api/experimental/persistent-pty/pty_1/connect', {
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket' },
|
|
})
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('requires authorization for a ticketed /api/pty connect', async () => {
|
|
const res = await app.request('/api/opencode-proxy/api/pty/pty_1/connect?ticket=abc', {
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket' },
|
|
})
|
|
expect(res.status).toBe(401)
|
|
})
|
|
|
|
it('returns 403 for a ticketed persistent PTY connect from an untrusted origin', async () => {
|
|
const res = await app.request('/api/opencode-proxy/api/experimental/persistent-pty/pty_1/connect?ticket=abc', {
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket', Origin: 'http://evil.example' },
|
|
})
|
|
expect(res.status).toBe(403)
|
|
const body = await res.json() as { error: string }
|
|
expect(body.error).toBe('Origin not allowed')
|
|
})
|
|
|
|
it('returns 503 for a ticketed persistent PTY connect before lifecycle initialization', async () => {
|
|
isLifecycleInitializedMock.mockReturnValue(false)
|
|
const res = await app.request('/api/opencode-proxy/api/experimental/persistent-pty/pty_1/connect?ticket=abc', {
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket' },
|
|
})
|
|
expect(res.status).toBe(503)
|
|
})
|
|
|
|
it('does not resolve a repo for a ticketed persistent PTY connect through the repo mount', async () => {
|
|
const res = await app.request('/api/opencode-proxy/repos/999/api/experimental/persistent-pty/pty_1/connect?ticket=abc', {
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket', Origin: 'http://evil.example' },
|
|
})
|
|
expect(res.status).toBe(403)
|
|
expect(getRepoByIdMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
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('returns a null body for bodyless upstream statuses', async () => {
|
|
const upstreamFetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 }))
|
|
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(204)
|
|
expect(await res.text()).toBe('')
|
|
})
|
|
|
|
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),
|
|
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(() => '2.0.15'),
|
|
getMinVersion: vi.fn(() => '2.0.15'),
|
|
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)
|
|
})
|
|
})
|
|
|
|
describe('opencode-proxy repo-scoped mount', () => {
|
|
let app: Hono
|
|
let originalFetch: typeof globalThis.fetch
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
isLifecycleInitializedMock.mockReturnValue(true)
|
|
originalFetch = globalThis.fetch
|
|
app = createProxyApp()
|
|
})
|
|
|
|
afterEach(() => {
|
|
globalThis.fetch = originalFetch
|
|
})
|
|
|
|
it('returns 404 for an unknown repo', async () => {
|
|
getRepoByIdMock.mockReturnValue(null)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/999/api/session', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(404)
|
|
expect(upstreamFetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 404 for a non-ready repo', async () => {
|
|
getRepoByIdMock.mockReturnValue({ ...readyRepo, cloneStatus: 'cloning' })
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(404)
|
|
expect(upstreamFetch).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('pins x-opencode-directory to the repo path', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session?limit=1', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'x-opencode-directory': encodeURIComponent('/some/other/dir'),
|
|
},
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchHeaders = fetchCall[1].headers as Record<string, string>
|
|
expect(fetchHeaders['x-opencode-directory']).toBe(encodeURIComponent('/srv/repos/my-repo'))
|
|
expect(fetchCall[0]).toBe('http://127.0.0.1:5551/api/session?limit=1')
|
|
})
|
|
|
|
it('rewrites the location[directory] query value', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request(
|
|
`/api/opencode-proxy/repos/7/api/agent?location%5Bdirectory%5D=${encodeURIComponent('/local/dir')}`,
|
|
{ headers: { Authorization: 'Bearer test-internal-token' } }
|
|
)
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchUrl = new URL(fetchCall[0])
|
|
expect(fetchUrl.searchParams.get('location[directory]')).toBe('/srv/repos/my-repo')
|
|
})
|
|
|
|
it('rewrites the directory query on GET /api/session', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session?directory=/local/dir&limit=5', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const fetchUrl = new URL(fetchCall[0])
|
|
expect(fetchUrl.searchParams.get('directory')).toBe('/srv/repos/my-repo')
|
|
expect(fetchUrl.searchParams.get('limit')).toBe('5')
|
|
})
|
|
|
|
it('rewrites a JSON body top-level location.directory', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ title: 'x', location: { directory: '/local/dir' } }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
const forwarded = JSON.parse(fetchCall[1].body as string) as Record<string, unknown>
|
|
expect(forwarded).toEqual({ title: 'x', location: { directory: '/srv/repos/my-repo' } })
|
|
})
|
|
|
|
it('streams a non-JSON body through untouched', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'text/plain',
|
|
},
|
|
body: 'location=/local/dir',
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(fetchCall[1].body).toBeInstanceOf(ReadableStream)
|
|
expect(await new Response(fetchCall[1].body as ReadableStream).text()).toBe('location=/local/dir')
|
|
})
|
|
|
|
it('forwards a JSON body without a location unchanged', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
const body = JSON.stringify({ title: 'x' })
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
method: 'POST',
|
|
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]
|
|
expect(new TextDecoder().decode(fetchCall[1].body as ArrayBuffer)).toBe(body)
|
|
})
|
|
|
|
it('forwards a malformed JSON body unchanged', async () => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
method: 'POST',
|
|
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(new TextDecoder().decode(fetchCall[1].body as ArrayBuffer)).toBe('{not json')
|
|
})
|
|
|
|
describe('workspace directories', () => {
|
|
const worktreePath = join(getWorkspacePath(), '.opencode', 'state', 'opencode', 'worktree', 'abc123', 'feature')
|
|
const otherRepoPath = join(getWorkspacePath(), 'repos', 'other-repo')
|
|
|
|
beforeEach(() => {
|
|
getRepoByIdMock.mockReturnValue(readyRepo)
|
|
})
|
|
|
|
it('keeps a workspace x-opencode-directory', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/agent', {
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'x-opencode-directory': encodeURIComponent(worktreePath),
|
|
},
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect((fetchCall[1].headers as Record<string, string>)['x-opencode-directory']).toBe(encodeURIComponent(worktreePath))
|
|
})
|
|
|
|
it('keeps another project location[directory] from the workspace', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request(
|
|
`/api/opencode-proxy/repos/7/api/agent?location%5Bdirectory%5D=${encodeURIComponent(otherRepoPath)}`,
|
|
{ headers: { Authorization: 'Bearer test-internal-token' } }
|
|
)
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(new URL(fetchCall[0]).searchParams.get('location[directory]')).toBe(otherRepoPath)
|
|
expect((fetchCall[1].headers as Record<string, string>)['x-opencode-directory']).toBe(encodeURIComponent(otherRepoPath))
|
|
})
|
|
|
|
it('keeps a workspace body location.directory', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session', {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: 'Bearer test-internal-token',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ title: 'x', location: { directory: worktreePath } }),
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(JSON.parse(fetchCall[1].body as string)).toEqual({ title: 'x', location: { directory: worktreePath } })
|
|
})
|
|
|
|
it('keeps a workspace directory filter on the session list', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request(`/api/opencode-proxy/repos/7/api/session?directory=${encodeURIComponent(worktreePath)}`, {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(new URL(fetchCall[0]).searchParams.get('directory')).toBe(worktreePath)
|
|
})
|
|
|
|
it('does not add a directory filter to an unfiltered session list', async () => {
|
|
const upstreamFetch = upstreamOk()
|
|
|
|
const res = await app.request('/api/opencode-proxy/repos/7/api/session?limit=50&parentID=null', {
|
|
headers: { Authorization: 'Bearer test-internal-token' },
|
|
})
|
|
|
|
expect(res.status).toBe(200)
|
|
const fetchCall = upstreamFetch.mock.calls[0] as [string, RequestInit]
|
|
expect(new URL(fetchCall[0]).searchParams.has('directory')).toBe(false)
|
|
})
|
|
})
|
|
})
|
|
|
|
const SHIPPED_OPENCODE_BIN = resolveOpenCode2Binary()
|
|
|
|
function startMockLlm(): Promise<{ port: number; close: () => void }> {
|
|
const server = createServer((req, res) => {
|
|
if (req.method === 'GET' && req.url?.endsWith('/models')) {
|
|
res.writeHead(200, { 'content-type': 'application/json' })
|
|
res.end(JSON.stringify({ object: 'list', data: [{ id: 'mock-model', object: 'model' }] }))
|
|
return
|
|
}
|
|
if (req.method !== 'POST' || !req.url?.endsWith('/chat/completions')) {
|
|
res.writeHead(404)
|
|
res.end()
|
|
return
|
|
}
|
|
req.on('data', () => undefined)
|
|
req.on('end', () => {
|
|
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' })
|
|
const base = { id: 'chatcmpl-e2e', object: 'chat.completion.chunk', created: 1, model: 'mock-model' }
|
|
res.write(`data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] })}\n\n`)
|
|
res.write(`data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta: { content: 'hi' }, finish_reason: null }] })}\n\n`)
|
|
res.write(`data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`)
|
|
res.write('data: [DONE]\n\n')
|
|
res.end()
|
|
})
|
|
})
|
|
return new Promise((resolve) => {
|
|
server.listen(0, '127.0.0.1', () => resolve({ port: (server.address() as AddressInfo).port, close: () => server.close() }))
|
|
})
|
|
}
|
|
|
|
function runOpenCodeAgainstProxy(
|
|
serverUrl: string,
|
|
cwd: string,
|
|
homeDirectory: string,
|
|
configHome: string,
|
|
): Promise<{ status: number | null; stdout: string; stderr: string }> {
|
|
const binary = resolveOpenCode2Binary()
|
|
if (!binary) throw new Error('no OpenCode 2 binary is available for the binary-backed test')
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(binary, ['run', '--server', serverUrl, '--format', 'json', 'hi'], {
|
|
cwd,
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
env: {
|
|
...process.env,
|
|
HOME: homeDirectory,
|
|
XDG_CONFIG_HOME: configHome,
|
|
XDG_DATA_HOME: join(configHome, '..', 'data'),
|
|
XDG_STATE_HOME: join(configHome, '..', 'state'),
|
|
XDG_CACHE_HOME: join(configHome, '..', 'cache'),
|
|
OPENCODE_DISABLE_MODELS_FETCH: '1',
|
|
OPENCODE_PASSWORD: 'test-internal-token',
|
|
},
|
|
})
|
|
let stdout = ''
|
|
let stderr = ''
|
|
child.stdout?.on('data', (chunk: Buffer) => {
|
|
stdout += chunk.toString()
|
|
})
|
|
child.stderr?.on('data', (chunk: Buffer) => {
|
|
stderr += chunk.toString()
|
|
})
|
|
const timer = setTimeout(() => {
|
|
child.kill('SIGKILL')
|
|
resolve({ status: null, stdout, stderr })
|
|
}, 90000)
|
|
child.on('close', (code) => {
|
|
clearTimeout(timer)
|
|
resolve({ status: code, stdout, stderr })
|
|
})
|
|
child.on('error', (error) => {
|
|
clearTimeout(timer)
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
|
|
describe.skipIf(SHIPPED_OPENCODE_BIN === null)('opencode-proxy repo-scoped mount against the shipped OpenCode 2 binary', () => {
|
|
let instanceRoot: string
|
|
let configHome: string
|
|
let homeDirectory: string
|
|
let instance: OpenCodeServe
|
|
let llm: { port: number; close: () => void }
|
|
|
|
beforeAll(async () => {
|
|
instanceRoot = mkdtempSync(join(tmpdir(), 'ocm-proxy-e2e-'))
|
|
configHome = join(instanceRoot, 'config')
|
|
homeDirectory = join(instanceRoot, 'home')
|
|
for (const directory of [join(configHome, 'opencode'), homeDirectory, join(instanceRoot, 'data'), join(instanceRoot, 'state'), join(instanceRoot, 'cache')]) {
|
|
mkdirSync(directory, { recursive: true })
|
|
}
|
|
|
|
llm = await startMockLlm()
|
|
writeFileSync(
|
|
join(configHome, 'opencode', 'opencode.json'),
|
|
JSON.stringify({
|
|
providers: {
|
|
mock: {
|
|
package: '@opencode/ai/providers/openai-compatible',
|
|
name: 'Mock',
|
|
settings: { baseURL: `http://127.0.0.1:${llm.port}/v1`, apiKey: 'mock-key' },
|
|
models: { 'mock-model': { name: 'Mock Model' } },
|
|
},
|
|
},
|
|
model: 'mock/mock-model',
|
|
permissions: [{ action: '*', resource: '*', effect: 'allow' }],
|
|
}),
|
|
)
|
|
|
|
instance = await startOpenCodeServe({ env: { HOME: homeDirectory, XDG_CONFIG_HOME: configHome } })
|
|
})
|
|
|
|
afterEach(() => {
|
|
getOpenCodeServerPasswordMock.mockReturnValue('test-password')
|
|
upstreamBaseUrl.value = 'http://127.0.0.1:5551'
|
|
})
|
|
|
|
afterAll(async () => {
|
|
await instance.stop()
|
|
llm.close()
|
|
rmSync(instanceRoot, { recursive: true, force: true })
|
|
})
|
|
|
|
it('pins the repo directory for a client started from an unrelated cwd', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'ocm-proxy-e2e-'))
|
|
const clientCwd = join(root, 'client-cwd')
|
|
const repoPath = join(root, 'repo')
|
|
for (const directory of [clientCwd, repoPath]) {
|
|
mkdirSync(directory, { recursive: true })
|
|
}
|
|
|
|
upstreamBaseUrl.value = instance.baseUrl
|
|
getOpenCodeServerPasswordMock.mockReturnValue(instance.password)
|
|
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: repoPath, cloneStatus: 'ready' })
|
|
|
|
const { proxyServer, proxyPort } = await startProxyServer()
|
|
|
|
try {
|
|
const result = await runOpenCodeAgainstProxy(
|
|
`http://127.0.0.1:${proxyPort}/api/opencode-proxy/repos/1`,
|
|
clientCwd,
|
|
homeDirectory,
|
|
configHome,
|
|
)
|
|
expect(result.status, result.stderr || result.stdout).toBe(0)
|
|
|
|
const response = await fetch(`${instance.baseUrl}/api/session?directory=${encodeURIComponent(repoPath)}`, {
|
|
headers: { Authorization: buildOpenCodeBasicAuth(instance.password) },
|
|
})
|
|
const body = (await response.json()) as { data: { location: { directory: string } }[] }
|
|
expect(body.data.some((session) => session.location.directory === repoPath)).toBe(true)
|
|
} finally {
|
|
await new Promise<void>((resolve) => proxyServer.close(() => resolve()))
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
}, 120000)
|
|
|
|
it('bridges a ticketed persistent PTY WebSocket for a remote client without an Authorization header', async () => {
|
|
const ptyDaemonsBefore = listPtyDaemonPids()
|
|
const root = mkdtempSync(join(tmpdir(), 'ocm-proxy-pty-'))
|
|
const repoPath = join(root, 'repo')
|
|
mkdirSync(repoPath, { recursive: true })
|
|
|
|
upstreamBaseUrl.value = instance.baseUrl
|
|
getOpenCodeServerPasswordMock.mockReturnValue(instance.password)
|
|
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: repoPath, cloneStatus: 'ready' })
|
|
|
|
const { proxyServer, proxyPort } = await startProxyServer()
|
|
const repoProxy = `127.0.0.1:${proxyPort}/api/opencode-proxy/repos/1`
|
|
const clientAuth = 'Basic ' + Buffer.from('opencode:test-internal-token').toString('base64')
|
|
let leakedPtyDaemons: number[] = []
|
|
|
|
try {
|
|
const created = await fetch(`http://${repoProxy}/api/session`, {
|
|
method: 'POST',
|
|
headers: { Authorization: clientAuth, 'content-type': 'application/json' },
|
|
body: JSON.stringify({ title: 'pty-bridge' }),
|
|
})
|
|
expect(created.status, await created.clone().text()).toBe(200)
|
|
const sessionID = ((await created.json()) as { data: { id: string } }).data.id
|
|
|
|
const terminal = await fetch(`http://${repoProxy}/api/experimental/session/${sessionID}/terminal`, {
|
|
method: 'POST',
|
|
headers: { Authorization: clientAuth, 'content-type': 'application/json' },
|
|
body: JSON.stringify({ command: '/bin/sh', args: [], title: 'pty-bridge', env: {}, cwd: repoPath }),
|
|
})
|
|
expect(terminal.status, await terminal.clone().text()).toBe(200)
|
|
const ptyID = ((await terminal.json()) as { data: { id: string } }).data.id
|
|
|
|
const token = await fetch(`http://${repoProxy}/api/experimental/persistent-pty/${ptyID}/connect-token`, {
|
|
method: 'POST',
|
|
headers: { Authorization: clientAuth, 'x-opencode-ticket': '1' },
|
|
})
|
|
expect(token.status, await token.clone().text()).toBe(200)
|
|
const ticket = ((await token.json()) as { data: { ticket: string } }).data.ticket
|
|
|
|
const socket = new WebSocket(`ws://${repoProxy}/api/experimental/persistent-pty/${ptyID}/connect?ticket=${encodeURIComponent(ticket)}`)
|
|
socket.binaryType = 'arraybuffer'
|
|
const output = await new Promise<string>((resolve, reject) => {
|
|
let received = ''
|
|
let sent = false
|
|
const timer = setTimeout(() => reject(new Error(`timed out; received: ${received}`)), 20000)
|
|
socket.addEventListener('message', (event) => {
|
|
received += typeof event.data === 'string' ? event.data : new TextDecoder().decode(event.data as ArrayBuffer)
|
|
if (!sent && received.includes('replay_complete')) {
|
|
sent = true
|
|
socket.send('echo proxied-$((40+2))\n')
|
|
}
|
|
if (received.includes('proxied-42')) {
|
|
clearTimeout(timer)
|
|
resolve(received)
|
|
}
|
|
})
|
|
socket.addEventListener('close', (event) => {
|
|
clearTimeout(timer)
|
|
reject(new Error(`socket closed ${event.code}; received: ${received}`))
|
|
})
|
|
})
|
|
socket.close()
|
|
|
|
expect(output).toContain('proxied-42')
|
|
} finally {
|
|
await shutdownPersistentPty(repoProxy, clientAuth)
|
|
await new Promise<void>((resolve) => proxyServer.close(() => resolve()))
|
|
leakedPtyDaemons = await waitForNoNewPtyDaemons(ptyDaemonsBefore)
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
|
|
expect(leakedPtyDaemons).toEqual([])
|
|
}, 120000)
|
|
|
|
it('rejects a persistent PTY connect with a bogus ticket as HTTP 403', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'ocm-proxy-pty-'))
|
|
const repoPath = join(root, 'repo')
|
|
mkdirSync(repoPath, { recursive: true })
|
|
|
|
upstreamBaseUrl.value = instance.baseUrl
|
|
getOpenCodeServerPasswordMock.mockReturnValue(instance.password)
|
|
getRepoByIdMock.mockReturnValue({ id: 1, fullPath: repoPath, cloneStatus: 'ready' })
|
|
|
|
const { proxyServer, proxyPort } = await startProxyServer()
|
|
|
|
try {
|
|
const status = await readUpgradeRejectionStatus(
|
|
proxyPort,
|
|
'/api/opencode-proxy/repos/1/api/experimental/persistent-pty/pty_bogus/connect?ticket=bogus',
|
|
)
|
|
expect(status).toBe(403)
|
|
} finally {
|
|
await new Promise<void>((resolve) => proxyServer.close(() => resolve()))
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
}, 120000)
|
|
})
|
|
|
|
function listPtyDaemonPids(): number[] {
|
|
try {
|
|
return execFileSync('pgrep', ['-f', 'opencode-pty'], { encoding: 'utf8' })
|
|
.split('\n')
|
|
.map((line) => Number(line.trim()))
|
|
.filter((pid) => Number.isInteger(pid) && pid > 0)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
async function waitForNoNewPtyDaemons(baseline: number[]): Promise<number[]> {
|
|
const deadline = Date.now() + 10000
|
|
let leaked: number[] = []
|
|
while (Date.now() < deadline) {
|
|
leaked = listPtyDaemonPids().filter((pid) => !baseline.includes(pid))
|
|
if (leaked.length === 0) return []
|
|
await new Promise((resolve) => setTimeout(resolve, 250))
|
|
}
|
|
return leaked
|
|
}
|
|
|
|
async function shutdownPersistentPty(repoProxy: string, clientAuth: string): Promise<void> {
|
|
await fetch(`http://${repoProxy}/api/experimental/persistent-pty/shutdown`, {
|
|
method: 'POST',
|
|
headers: { Authorization: clientAuth },
|
|
}).catch(() => undefined)
|
|
}
|
|
|
|
function readUpgradeRejectionStatus(port: number, path: string): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
const req = request({
|
|
host: '127.0.0.1',
|
|
port,
|
|
path,
|
|
headers: { Connection: 'Upgrade', Upgrade: 'websocket' },
|
|
})
|
|
req.on('response', (res) => {
|
|
res.resume()
|
|
resolve(res.statusCode ?? 0)
|
|
})
|
|
req.on('upgrade', (res) => {
|
|
res.destroy()
|
|
resolve(101)
|
|
})
|
|
req.on('error', reject)
|
|
req.end()
|
|
})
|
|
}
|
|
|
|
async function startProxyServer(): Promise<{ proxyServer: Server; proxyPort: number }> {
|
|
const { server, port } = await startWebSocketServer({
|
|
app: new Hono(),
|
|
register: (proxyApp, upgradeWebSocket) => {
|
|
proxyApp.route('/api/opencode-proxy', createOpenCodeProxyRoutes(mockDb, mockSettingsService, upgradeWebSocket))
|
|
},
|
|
})
|
|
return { proxyServer: server, proxyPort: port }
|
|
}
|