Files
opencode-manager/backend/test/services/opencode-manager-tool-plugin.test.ts
T
Chris Scott 5e2452172c fix(artifacts): render artifacts from the checked path and serve raw files once
- show_artifact returns the validated path as tool metadata; the chat card
  renders from it and maps repo paths with getWorkspaceFilePath, so local
  repos and moved sessions load the right file
- serve .htm as text/html
- raw file route stats and reads once and adds ETag/Last-Modified with 304
- reuse isPathWithinRoot from utils/fs-safe
- move readShowArtifactCall to frontend/src/lib/artifactTool
2026-10-12 16:43:04 -04:00

728 lines
31 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { promises as fs } from 'fs'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
import http from 'http'
import type { AddressInfo } from 'net'
import path from 'path'
import os from 'os'
import { ASSISTANT_NOTIFICATION_LIMITS, AssistantNotificationRequestSchema, MANAGER_TOOL_NAME } from '@opencode-manager/shared/schemas'
import { MANAGER_TOOL_ALLOWED_ROUTES, parseAllowedRoute } from '../../src/services/opencode-manager-tool-plugin'
import { installManagedPlugins, getOpenCodePluginDir } from '../../src/services/opencode/plugin-registry'
import { loadGeneratedPlugin, type GeneratedTool } from '../helpers/opencode-plugin-context'
import { resolveOpenCode2Binary, runOpenCodeStandalone } from '../helpers/opencode-binary'
type JsonSchema = {
type?: string
properties?: Record<string, JsonSchema>
required?: string[]
additionalProperties?: boolean
anyOf?: JsonSchema[]
oneOf?: JsonSchema[]
enum?: string[]
const?: string
maxLength?: number
minLength?: number
}
async function loadTool(configHome: string): Promise<GeneratedTool> {
const plugin = await loadGeneratedPlugin(path.join(getOpenCodePluginDir(configHome), 'ocm-manager.js'))
const tool = plugin.registeredTools().find((candidate) => candidate.name === MANAGER_TOOL_NAME)
if (!tool?.execute) throw new Error(`The generated plugin does not register a "${MANAGER_TOOL_NAME}" tool`)
return tool
}
function toolInputSchema(tool: GeneratedTool): JsonSchema {
return tool.input as JsonSchema
}
function notificationParamsSchema(tool: GeneratedTool): JsonSchema {
return toolInputSchema(tool).properties?.params?.anyOf?.[0] ?? {}
}
function requestParamsSchema(tool: GeneratedTool): JsonSchema {
return toolInputSchema(tool).properties?.params?.anyOf?.[1] ?? {}
}
function walkthroughParamsSchema(tool: GeneratedTool): JsonSchema {
return toolInputSchema(tool).properties?.params?.anyOf?.[2] ?? {}
}
function artifactParamsSchema(tool: GeneratedTool): JsonSchema {
return toolInputSchema(tool).properties?.params?.anyOf?.[3] ?? {}
}
async function runToolResult(
tool: GeneratedTool,
input: unknown,
context: { sessionID?: string } = {},
): Promise<{ content: string; metadata?: Record<string, unknown> }> {
return (await tool.execute!(input, { signal: new AbortController().signal, sessionID: context.sessionID })) as {
content: string
metadata?: Record<string, unknown>
}
}
async function runTool(tool: GeneratedTool, input: unknown, context: { sessionID?: string } = {}): Promise<string> {
return (await runToolResult(tool, input, context)).content
}
function jsonResponse(body: unknown, { ok = true, status = 200 } = {}) {
return vi.fn().mockResolvedValue({
ok,
status,
text: async () => JSON.stringify(body),
})
}
describe('ocm-manager plugin', () => {
let configHome: string
beforeEach(async () => {
configHome = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-manager-'))
await installManagedPlugins(configHome)
process.env.OCM_INTERNAL_API_URL = 'http://localhost:5003/api/internal'
process.env.OCM_INTERNAL_TOKEN = 'secret-token'
})
afterEach(async () => {
vi.unstubAllGlobals()
delete process.env.OCM_INTERNAL_API_URL
delete process.env.OCM_INTERNAL_TOKEN
await fs.rm(configHome, { recursive: true, force: true })
})
it('default-exports the V2 plugin definition', async () => {
const plugin = await loadGeneratedPlugin(path.join(getOpenCodePluginDir(configHome), 'ocm-manager.js'))
expect(plugin.id).toBe('ocm.manager')
})
it('writes a plugin file with no zod import and no import statement', async () => {
const source = await fs.readFile(path.join(getOpenCodePluginDir(configHome), 'ocm-manager.js'), 'utf-8')
expect(source).not.toContain('zod')
expect(source).not.toMatch(/(^|\n)\s*import\b/)
expect(source).toContain("id: 'ocm.manager'")
})
it('registers the manager tool with the documented JSON Schema', async () => {
const tool = await loadTool(configHome)
const schema = toolInputSchema(tool)
expect(schema.type).toBe('object')
expect(schema.required).toEqual(['action', 'params'])
expect(schema.additionalProperties).toBe(false)
expect(schema.properties?.action?.enum).toEqual(['send_notification', 'request', 'open_walkthrough', 'show_artifact'])
expect(schema.properties?.params?.anyOf).toHaveLength(4)
expect(notificationParamsSchema(tool).required).toEqual(['title', 'body'])
expect(requestParamsSchema(tool).required).toEqual(['method', 'path'])
expect(walkthroughParamsSchema(tool).required).toBeUndefined()
expect(artifactParamsSchema(tool).required).toEqual(['title', 'path'])
expect(notificationParamsSchema(tool).additionalProperties).toBe(false)
expect(requestParamsSchema(tool).additionalProperties).toBe(false)
expect(walkthroughParamsSchema(tool).additionalProperties).toBe(false)
expect(artifactParamsSchema(tool).additionalProperties).toBe(false)
expect(notificationParamsSchema(tool).properties?.priority?.enum).toEqual(['normal', 'high'])
expect(requestParamsSchema(tool).properties?.method?.enum).toEqual(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
expect(walkthroughParamsSchema(tool).properties?.source?.oneOf?.map((variant) => variant.properties?.kind?.const)).toEqual([
'session',
'uncommitted',
'staged',
'unstaged',
'branch',
'pullRequest',
])
})
it('lists the open_walkthrough action in the description', async () => {
const tool = await loadTool(configHome)
expect(tool.description).toContain('open_walkthrough')
expect(tool.description).toContain('request the change Walkthrough for the current session')
expect(tool.description).toContain('when this session is open on desktop')
})
it('lists the show_artifact action in the description', async () => {
const tool = await loadTool(configHome)
expect(tool.description).toContain('show_artifact')
expect(tool.description).toContain('write a self-contained .html file with your file tools')
expect(tool.description).toContain('rendered inline in this session')
})
it('enforces the notification limits the internal API enforces', async () => {
const tool = await loadTool(configHome)
const notification = notificationParamsSchema(tool)
const withinLimits = { title: 'x'.repeat(ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX), body: 'b'.repeat(ASSISTANT_NOTIFICATION_LIMITS.BODY_MAX) }
const overLimit = { title: 'x'.repeat(ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX + 1), body: 'b' }
expect(notification.properties?.title?.maxLength).toBe(ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX)
expect(notification.properties?.body?.maxLength).toBe(ASSISTANT_NOTIFICATION_LIMITS.BODY_MAX)
expect(notification.properties?.url?.maxLength).toBe(ASSISTANT_NOTIFICATION_LIMITS.URL_MAX)
expect(notification.properties?.tag?.maxLength).toBe(ASSISTANT_NOTIFICATION_LIMITS.TAG_MAX)
expect(notification.properties?.title?.minLength).toBe(1)
expect(notification.properties?.body?.minLength).toBe(1)
expect(notification.properties?.url?.minLength).toBe(1)
expect(requestParamsSchema(tool).properties?.path?.minLength).toBe(1)
expect(requestParamsSchema(tool).properties?.path?.maxLength).toBe(500)
expect(notification.required).toEqual(['title', 'body'])
expect(AssistantNotificationRequestSchema.safeParse(withinLimits).success).toBe(true)
expect(AssistantNotificationRequestSchema.safeParse(overLimit).success).toBe(false)
})
it('sends a notification through the internal API with the host token', async () => {
const fetchMock = jsonResponse({ delivered: 2, expired: 0, failed: 0, noSubscriptions: false })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
const result = await runTool(tool, {
action: 'send_notification',
params: { title: 'Storm watch', body: 'Formation odds crossed 40%', priority: 'high' },
})
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/notifications/send')
expect(init.method).toBe('POST')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(JSON.parse(init.body)).toEqual({ title: 'Storm watch', body: 'Formation odds crossed 40%', priority: 'high' })
expect(result).toBe('Notification sent: 2 delivered, 0 failed.')
})
it('reports when the user has no registered devices', async () => {
vi.stubGlobal('fetch', jsonResponse({ delivered: 0, expired: 0, failed: 0, noSubscriptions: true }))
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'send_notification', params: { title: 't', body: 'b' } }))
.resolves.toBe('No devices are registered for push notifications, so nothing was delivered.')
})
it('surfaces the API status and body when the request is rejected', async () => {
vi.stubGlobal('fetch', jsonResponse({ error: 'Rate limit exceeded' }, { ok: false, status: 429 }))
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'send_notification', params: { title: 't', body: 'b' } }))
.rejects.toThrow(/429.*Rate limit exceeded/)
})
it('fails when the internal API is not configured', async () => {
delete process.env.OCM_INTERNAL_TOKEN
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'send_notification', params: { title: 't', body: 'b' } }))
.rejects.toThrow('The OpenCode Manager internal API is not configured for this OpenCode server.')
expect(fetchMock).not.toHaveBeenCalled()
})
it('rejects unknown actions and invalid params without calling the API', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'constructor', params: {} })).rejects.toThrow(/Unknown OpenCode Manager action/)
await expect(runTool(tool, { action: 'send_notification', params: { title: 't' } })).rejects.toThrow(/Invalid parameters/)
await expect(runTool(tool, { action: 'request', params: null })).rejects.toThrow(/Invalid parameters/)
expect(fetchMock).not.toHaveBeenCalled()
})
it('rejects params belonging to the other action without calling the API', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'request', params: { title: 't', body: 'b' } }))
.rejects.toThrow(/Invalid parameters for OpenCode Manager action: request/)
await expect(runTool(tool, { action: 'send_notification', params: { method: 'GET', path: '/settings' } }))
.rejects.toThrow(/Invalid parameters for OpenCode Manager action: send_notification/)
expect(fetchMock).not.toHaveBeenCalled()
})
it('opens the walkthrough for the current session with the requested source', async () => {
const fetchMock = jsonResponse({ walkthrough: { stops: [{}, {}] }, currentDiffHash: 'h', stale: false, generating: false, error: null })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
const result = await runToolResult(
tool,
{ action: 'open_walkthrough', params: { source: { kind: 'staged' } } },
{ sessionID: 'ses_abc' },
)
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/change-walkthroughs/ses_abc')
expect(init.method).toBe('POST')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(init.headers['content-type']).toBe('application/json')
expect(JSON.parse(init.body)).toEqual({ source: { kind: 'staged' } })
expect(result.content).toBe(
'Requested the walkthrough (2 stops). It opens in the Manager window when this session is open on desktop; otherwise the user can open it from this tool call.',
)
expect(result.metadata).toBeUndefined()
})
it('reports that the walkthrough is generating', async () => {
vi.stubGlobal('fetch', jsonResponse({ walkthrough: null, currentDiffHash: 'h', stale: false, generating: true, error: null }))
const tool = await loadTool(configHome)
const result = await runToolResult(tool, { action: 'open_walkthrough', params: {} }, { sessionID: 'ses_abc' })
expect(result.content).toBe(
'Requested the walkthrough; it is generating. It opens in the Manager window when this session is open on desktop; otherwise the user can open it from this tool call.',
)
expect(result.metadata).toBeUndefined()
})
it('opens the walkthrough without a body when no source is given', async () => {
const fetchMock = jsonResponse({ walkthrough: null, currentDiffHash: 'h', stale: false, generating: true, error: null })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await runTool(tool, { action: 'open_walkthrough', params: {} }, { sessionID: 'ses_abc' })
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/change-walkthroughs/ses_abc')
expect(init.headers['content-type']).toBeUndefined()
expect(init.body).toBeUndefined()
})
it('resolves the artifact file through the internal API and reports the resolved path', async () => {
const fetchMock = jsonResponse({ path: '/workspace/repos/app/report.html' })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
const result = await runToolResult(
tool,
{ action: 'show_artifact', params: { title: 'Formation odds', path: 'reports/report.html' } },
{ sessionID: 'ses_abc' },
)
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/session-artifacts/ses_abc/resolve')
expect(init.method).toBe('POST')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(init.headers['content-type']).toBe('application/json')
expect(JSON.parse(init.body)).toEqual({ path: 'reports/report.html' })
expect(result.content).toBe(
'Showing the artifact "Formation odds" from /workspace/repos/app/report.html in the Manager chat. It renders the current file contents, so after editing the file the user can reload the view; call show_artifact again only to show a different file.',
)
expect(result.metadata).toEqual({ path: '/workspace/repos/app/report.html' })
})
it('surfaces the resolve error when the artifact path is rejected', async () => {
vi.stubGlobal('fetch', jsonResponse({ error: 'Artifact file not found: reports/report.html' }, { ok: false, status: 404 }))
const tool = await loadTool(configHome)
await expect(
runTool(tool, { action: 'show_artifact', params: { title: 'Formation odds', path: 'reports/report.html' } }, { sessionID: 'ses_abc' }),
).rejects.toThrow(/404.*Artifact file not found: reports\/report\.html/)
})
it('rejects a show_artifact call missing path without calling the API', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'show_artifact', params: { title: 'Formation odds' } }, { sessionID: 'ses_abc' }))
.rejects.toThrow('Invalid parameters for OpenCode Manager action: show_artifact. Missing required parameter: path.')
expect(fetchMock).not.toHaveBeenCalled()
})
it('sends an allow-listed GET request with a query string and no body', async () => {
const fetchMock = jsonResponse({ userId: 'default', theme: 'dark' })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
const result = await runTool(tool, { action: 'request', params: { method: 'GET', path: '/settings?userId=default' } })
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/settings?userId=default')
expect(init.method).toBe('GET')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(init.headers['content-type']).toBeUndefined()
expect(init.body).toBeUndefined()
expect(result).toBe('{"userId":"default","theme":"dark"}')
})
it('sends a PATCH request with a JSON body', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await runTool(tool, { action: 'request', params: { method: 'PATCH', path: '/settings', body: { theme: 'dark' } } })
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/settings')
expect(init.method).toBe('PATCH')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(init.headers['content-type']).toBe('application/json')
expect(JSON.parse(init.body)).toEqual({ theme: 'dark' })
})
it('sends a PATCH request to the config route with a JSON body', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await runTool(tool, { action: 'request', params: { method: 'PATCH', path: '/opencode-config', body: { patch: { theme: 'dark' } } } })
expect(fetchMock).toHaveBeenCalledTimes(1)
const [url, init] = fetchMock.mock.calls[0] ?? []
expect(url).toBe('http://localhost:5003/api/internal/opencode-config')
expect(init.method).toBe('PATCH')
expect(init.headers.Authorization).toBe('Bearer secret-token')
expect(init.headers['content-type']).toBe('application/json')
expect(JSON.parse(init.body)).toEqual({ patch: { theme: 'dark' } })
})
it('allows every route in the exported allow list', async () => {
const tool = await loadTool(configHome)
for (const route of MANAGER_TOOL_ALLOWED_ROUTES) {
const { method, path: pattern } = parseAllowedRoute(route)
const path = pattern.split('/').map((segment) => (segment === '*' ? 'x' : segment)).join('/')
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
await runTool(tool, { action: 'request', params: { method, path } })
expect(fetchMock).toHaveBeenCalledTimes(1)
}
})
it('allows session routes and rejects session deletion', async () => {
const tool = await loadTool(configHome)
const allowedRoutes = [
['POST', '/sessions'],
['GET', '/sessions/abc/reply'],
] as const
for (const [method, path] of allowedRoutes) {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
await runTool(tool, { action: 'request', params: { method, path } })
expect(fetchMock).toHaveBeenCalledTimes(1)
}
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
await expect(runTool(tool, { action: 'request', params: { method: 'DELETE', path: '/sessions/abc' } }))
.rejects.toThrow(/is not an allowed OpenCode Manager route/)
expect(fetchMock).not.toHaveBeenCalled()
})
it('rejects non-allow-listed routes without calling the API', async () => {
const tool = await loadTool(configHome)
const deniedRoutes = [
['GET', '/git-credentials/gh-env'],
['POST', '/sandbox/shell'],
['GET', '/repos/0/mirror/head'],
['POST', '/notifications/send'],
['DELETE', '/settings'],
['PUT', '/opencode-config'],
['DELETE', '/sessions/abc'],
['GET', '/session-goals'],
['POST', '/session-goals'],
['POST', '/session-goals/1/pause'],
['POST', '/session-goals/1/cancel'],
['GET', '/multi-runs'],
['POST', '/multi-runs'],
['POST', '/multi-runs/1/fusions'],
['POST', '/multi-runs/1/entries/2/discard'],
['GET', '/change-walkthroughs/x'],
['POST', '/change-walkthroughs/x'],
] as const
for (const [method, path] of deniedRoutes) {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
await expect(runTool(tool, { action: 'request', params: { method, path } }))
.rejects.toThrow(/is not an allowed OpenCode Manager route/)
expect(fetchMock).not.toHaveBeenCalled()
}
})
it('allows listing schedule worktrees but rejects deleting them', async () => {
const tool = await loadTool(configHome)
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
await runTool(tool, { action: 'request', params: { method: 'GET', path: '/repos/1/schedules/2/worktrees' } })
expect(fetchMock).toHaveBeenCalledTimes(1)
const deleteMock = jsonResponse({})
vi.stubGlobal('fetch', deleteMock)
await expect(runTool(tool, { action: 'request', params: { method: 'DELETE', path: '/repos/1/schedules/2/worktrees' } }))
.rejects.toThrow(/is not an allowed OpenCode Manager route/)
expect(deleteMock).not.toHaveBeenCalled()
})
it('normalizes path traversal and rejects the resolved route without calling the API', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(
runTool(tool, { action: 'request', params: { method: 'GET', path: '/repos/x/../../git-credentials/gh-env' } }),
).rejects.toThrow(/is not an allowed OpenCode Manager route/)
expect(fetchMock).not.toHaveBeenCalled()
})
it('rejects an absolute URL that resolves outside the internal API', async () => {
const fetchMock = jsonResponse({})
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'request', params: { method: 'GET', path: 'http://evil.com/steal' } }))
.rejects.toThrow(/resolves outside/)
expect(fetchMock).not.toHaveBeenCalled()
})
it('returns the empty-body message when the response body is empty', async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: true, status: 200, text: async () => '' })
vi.stubGlobal('fetch', fetchMock)
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'request', params: { method: 'GET', path: '/settings' } }))
.resolves.toBe('The request succeeded with an empty response body.')
})
it('surfaces the API status and body when the request action is rejected', async () => {
vi.stubGlobal('fetch', jsonResponse({ error: 'Rate limit exceeded' }, { ok: false, status: 429 }))
const tool = await loadTool(configHome)
await expect(runTool(tool, { action: 'request', params: { method: 'GET', path: '/settings' } }))
.rejects.toThrow(/429.*Rate limit exceeded/)
})
})
const SHIPPED_OPENCODE_BIN = resolveOpenCode2Binary()
type ChatRequest = { messages?: unknown[]; tools?: { function?: { name?: string; parameters?: unknown } }[] }
describe.skipIf(SHIPPED_OPENCODE_BIN === null)('ocm-manager plugin against the shipped OpenCode 2 binary', () => {
let root: string
function startInternalApiServer(requests: string[]) {
const server = http.createServer((req, res) => {
let body = ''
req.on('data', (chunk: Buffer) => {
body += chunk.toString()
})
req.on('end', () => {
if (req.method === 'POST' && req.url?.endsWith('/notifications/send')) {
requests.push(JSON.stringify({ auth: req.headers.authorization, body }))
}
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ delivered: 1, expired: 0, failed: 0, noSubscriptions: false }))
})
})
return new Promise<{ server: http.Server; port: number }>((resolve) => {
server.listen(0, '127.0.0.1', () => resolve({ server, port: (server.address() as AddressInfo).port }))
})
}
function startLlmServer(offeredTools: ChatRequest['tools'][], toolResults: string[]) {
const server = http.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
}
let body = ''
req.on('data', (chunk: Buffer) => {
body += chunk.toString()
})
req.on('end', () => {
const parsed = JSON.parse(body || '{}') as ChatRequest
offeredTools.push(parsed.tools ?? [])
const messages = parsed.messages ?? []
const toolMessages = messages.filter((m) => (m as { role?: string }).role === 'tool')
for (const message of toolMessages) {
toolResults.push(String((message as { content?: unknown }).content ?? ''))
}
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' })
const writeChunk = (obj: unknown) => res.write(`data: ${JSON.stringify(obj)}\n\n`)
const base = { id: 'chatcmpl-e2e', object: 'chat.completion.chunk', created: 1, model: 'mock-model' }
const hasTools = Array.isArray(parsed.tools) && parsed.tools.length > 0
if (hasTools && toolMessages.length === 0) {
const args = JSON.stringify({
action: 'send_notification',
params: { title: 'Storm watch', body: 'Formation odds crossed 40%', priority: 'high' },
})
writeChunk({
...base,
choices: [
{
index: 0,
delta: {
role: 'assistant',
content: null,
tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: MANAGER_TOOL_NAME, arguments: '' } }],
},
finish_reason: null,
},
],
})
writeChunk({
...base,
choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: args } }] }, finish_reason: null }],
})
writeChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: 'tool_calls' }] })
} else {
writeChunk({ ...base, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] })
writeChunk({ ...base, choices: [{ index: 0, delta: { content: 'FINAL' }, finish_reason: null }] })
writeChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })
}
res.write('data: [DONE]\n\n')
res.end()
})
})
return new Promise<{ server: http.Server; port: number }>((resolve) => {
server.listen(0, '127.0.0.1', () => resolve({ server, port: (server.address() as AddressInfo).port }))
})
}
function writeOpenCodeConfig(configHome: string, llmPort: number) {
writeFileSync(
path.join(configHome, 'opencode', 'opencode.json'),
JSON.stringify({
providers: {
mock: {
package: '@opencode/ai/providers/openai-compatible',
name: 'Mock',
settings: { baseURL: `http://127.0.0.1:${llmPort}/v1`, apiKey: 'mock-key' },
models: { 'mock-model': { name: 'Mock Model' } },
},
},
model: 'mock/mock-model',
permissions: [{ action: '*', resource: '*', effect: 'allow' }],
}),
)
}
beforeEach(() => {
root = mkdtempSync(path.join(os.tmpdir(), 'ocm-manager-e2e-'))
})
afterEach(() => {
rmSync(root, { recursive: true, force: true })
})
it('offers the tool to the model and sends the notification from the Manager process', async () => {
const configHome = path.join(root, 'config')
const workDir = path.join(root, 'work')
mkdirSync(path.join(configHome, 'opencode'), { recursive: true })
mkdirSync(workDir, { recursive: true })
const apiRequests: string[] = []
const offeredTools: ChatRequest['tools'][] = []
const toolResults: string[] = []
const api = await startInternalApiServer(apiRequests)
const llm = await startLlmServer(offeredTools, toolResults)
writeOpenCodeConfig(configHome, llm.port)
await installManagedPlugins(configHome)
try {
const result = await runOpenCodeStandalone({
cwd: workDir,
env: {
...process.env,
HOME: root,
XDG_CONFIG_HOME: configHome,
XDG_DATA_HOME: path.join(root, 'data'),
XDG_STATE_HOME: path.join(root, 'state'),
XDG_CACHE_HOME: path.join(root, 'cache'),
OPENCODE_DISABLE_MODELS_FETCH: '1',
PWD: workDir,
OCM_SANDBOX_ENFORCED: 'false',
OCM_INTERNAL_API_URL: `http://127.0.0.1:${api.port}/api/internal`,
OCM_INTERNAL_TOKEN: 'test-token',
},
message: 'notify the user',
})
expect(result.status).toBe(0)
const managerTool = offeredTools.flatMap((tools) => tools ?? []).find((t) => t.function?.name === MANAGER_TOOL_NAME)
expect(managerTool?.function?.parameters).toMatchObject({
type: 'object',
properties: {
action: { type: 'string', enum: ['send_notification', 'request', 'open_walkthrough', 'show_artifact'] },
params: {
anyOf: [
{
type: 'object',
required: ['title', 'body'],
properties: {
title: { type: 'string', minLength: 1, maxLength: ASSISTANT_NOTIFICATION_LIMITS.TITLE_MAX },
body: { type: 'string', minLength: 1, maxLength: ASSISTANT_NOTIFICATION_LIMITS.BODY_MAX },
url: { type: 'string', maxLength: ASSISTANT_NOTIFICATION_LIMITS.URL_MAX },
tag: { type: 'string', maxLength: ASSISTANT_NOTIFICATION_LIMITS.TAG_MAX },
priority: { type: 'string', enum: ['normal', 'high'] },
},
},
{
type: 'object',
required: ['method', 'path'],
properties: {
method: { type: 'string', enum: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] },
path: { type: 'string', minLength: 1, maxLength: 500 },
body: { type: 'object' },
},
},
{
type: 'object',
additionalProperties: false,
properties: {
source: expect.anything(),
},
},
{
type: 'object',
required: ['title', 'path'],
properties: {
title: { type: 'string', minLength: 1, maxLength: 120 },
path: { type: 'string', minLength: 1, maxLength: 1000 },
},
},
],
},
},
required: ['action', 'params'],
})
expect(apiRequests).toHaveLength(1)
const request = JSON.parse(apiRequests[0] as string) as { auth: string; body: string }
expect(request.auth).toBe('Bearer test-token')
expect(JSON.parse(request.body)).toEqual({
title: 'Storm watch',
body: 'Formation odds crossed 40%',
priority: 'high',
sessionId: expect.stringMatching(/^ses_/),
})
expect(toolResults.some((output) => output.includes('Notification sent: 1 delivered, 0 failed.'))).toBe(true)
} finally {
api.server.close()
llm.server.close()
}
}, 120000)
})