Files
opencode-manager/ocm-cli/test/repo-proxy.test.ts
Chris Scott dacde879d6 feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)
* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
2026-09-26 13:16:45 -04:00

130 lines
5.0 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import packageJson from '../package.json' with { type: 'json' }
import {
ManagerTokenInvalidError,
ManagerTooOldError,
MIN_MANAGER_VERSION,
OCM_VERSION,
RepoProxyNotFoundError,
RepoProxyResponseError,
RepoProxyUnreachableError,
repoProxyBaseUrl,
repoProxyUrl,
warmRepoProxy,
} from '../src/repo-proxy.js'
const managerUrl = 'https://manager.example.com'
function fetchReturning(...responses: Array<Response | Error>) {
const fetchMock = vi.fn(async () => {
const next = responses.shift()
if (!next) throw new Error('unexpected fetch')
if (next instanceof Error) throw next
return next
})
return fetchMock
}
const noWait = vi.fn(async () => undefined)
describe('repoProxyUrl', () => {
it('builds the repo-scoped proxy url', () => {
expect(repoProxyUrl(managerUrl, 42)).toBe('https://manager.example.com/api/opencode-proxy/repos/42')
})
})
describe('repoProxyBaseUrl', () => {
it('builds the manager proxy base url', () => {
expect(repoProxyBaseUrl(managerUrl)).toBe('https://manager.example.com/api/opencode-proxy')
})
})
describe('warmRepoProxy', () => {
it('reports the package version', () => {
expect(OCM_VERSION).toBe(packageJson.version)
})
it('resolves once the repo proxy route responds', async () => {
const fetchMock = fetchReturning(new Response('[]', { status: 200 }))
await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait })
expect(fetchMock).toHaveBeenCalledWith(
'https://manager.example.com/api/opencode-proxy/repos/42/api/session?limit=1',
{ headers: { Authorization: 'Bearer tok' } },
)
})
it('fails with an upgrade message when the Manager has no repo proxy route', async () => {
const fetchMock = fetchReturning(new Response('Not Found', { status: 404 }))
const error = await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait }).catch((err: unknown) => err)
expect(error).toBeInstanceOf(ManagerTooOldError)
expect((error as Error).message).toBe(
`OpenCode Manager at ${managerUrl} is too old for ocm 0.3.0; upgrade the Manager to >= ${MIN_MANAGER_VERSION}`,
)
expect(MIN_MANAGER_VERSION).toBe('0.19.0')
expect(fetchMock).toHaveBeenCalledTimes(1)
})
it('distinguishes a missing repo on a current Manager from an old Manager', async () => {
const fetchMock = fetchReturning(Response.json({ error: 'Repo not found' }, { status: 404 }))
await expect(warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait })).rejects.toBeInstanceOf(RepoProxyNotFoundError)
})
it('retries transient failures and reports the last response', async () => {
const fetchMock = fetchReturning(new Error('ECONNRESET'), new Response('', { status: 502 }), new Response('', { status: 503 }))
const wait = vi.fn(async () => undefined)
const error = await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait }).catch((err: unknown) => err)
expect(error).toBeInstanceOf(RepoProxyResponseError)
expect((error as Error).message).toContain('HTTP 503')
expect(fetchMock).toHaveBeenCalledTimes(3)
expect(wait.mock.calls).toEqual([[500], [1000]])
})
it('reports the status and body of a non-OK response', async () => {
const fetchMock = fetchReturning(new Response('proxy exploded', { status: 500 }))
const error = await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait, attempts: 1 }).catch((err: unknown) => err)
expect(error).toBeInstanceOf(RepoProxyResponseError)
expect((error as Error).message).toContain('HTTP 500')
expect((error as Error).message).toContain('proxy exploded')
})
it('reports an invalid manager token on 401 without retrying', async () => {
const fetchMock = fetchReturning(new Response('Unauthorized', { status: 401 }))
const error = await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait }).catch((err: unknown) => err)
expect(error).toBeInstanceOf(ManagerTokenInvalidError)
expect((error as Error).message).toContain('Settings -> Manager Internal Token')
expect((error as Error).message).toContain(`ocm login ${managerUrl}`)
expect(fetchMock).toHaveBeenCalledTimes(1)
})
it('reports the network cause when the manager is unreachable', async () => {
const cause = new Error('connect ECONNREFUSED')
const fetchMock = fetchReturning(cause, cause, cause)
const error = await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait }).catch((err: unknown) => err)
expect(error).toBeInstanceOf(RepoProxyUnreachableError)
expect((error as Error).message).toContain('connect ECONNREFUSED')
expect((error as Error).cause).toBe(cause)
expect(fetchMock).toHaveBeenCalledTimes(3)
})
it('succeeds after a transient failure', async () => {
const fetchMock = fetchReturning(new Response('', { status: 503 }), new Response('[]', { status: 200 }))
await warmRepoProxy(managerUrl, 'tok', 42, { fetch: fetchMock, wait: noWait })
expect(fetchMock).toHaveBeenCalledTimes(2)
})
})