Files
opencode-manager/ocm-cli/test/remote-session.test.ts
T
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

89 lines
3.7 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from 'vitest'
import { createManagerSessionTransfer } from '../src/remote-session.js'
import type { SessionTransferData } from '@opencode-manager/shared/opencode'
afterEach(() => {
vi.unstubAllGlobals()
})
const transferData = {
info: { id: 'ses_a' },
messages: [{ id: 'msg_1' }],
} as unknown as SessionTransferData
describe('createManagerSessionTransfer', () => {
it('imports through the manager proxy with basic auth and the remote location', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ data: { id: 'ses_new' } }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
)
vi.stubGlobal('fetch', fetchMock)
const transfer = createManagerSessionTransfer('https://manager.example', 'tok_123')
const result = await transfer.importSession('/workspace/repo', transferData)
expect(result).toEqual({ sessionID: 'ses_new' })
expect(fetchMock).toHaveBeenCalledOnce()
const [url, init] = fetchMock.mock.calls[0]!
expect(String(url)).toBe('https://manager.example/api/opencode-proxy/api/experimental/session/import')
expect(init.method).toBe('POST')
const headers = init.headers as Headers
expect(headers.get('authorization')).toBe(`Basic ${Buffer.from('opencode:tok_123').toString('base64')}`)
const body = JSON.parse(init.body as string) as Record<string, unknown>
expect(body.location).toEqual({ directory: '/workspace/repo' })
expect(body.info).toEqual(transferData.info)
expect(body.messages).toEqual(transferData.messages)
})
it('sends a synthetic reminder through the manager proxy', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ data: {} }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
)
vi.stubGlobal('fetch', fetchMock)
const transfer = createManagerSessionTransfer('https://manager.example', 'tok_123')
await transfer.addReminder('ses_new', 'hello')
const [url, init] = fetchMock.mock.calls[0]!
expect(String(url)).toBe('https://manager.example/api/opencode-proxy/api/session/ses_new/synthetic')
const body = JSON.parse(init.body as string) as Record<string, unknown>
expect(body.text).toBe('hello')
expect(body.resume).toBe(false)
})
function jsonResponse(status: number, body: unknown) {
return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } })
}
it('reports an existing Manager session', async () => {
const fetchMock = vi.fn().mockResolvedValue(jsonResponse(200, { data: { id: 'ses_a' } }))
vi.stubGlobal('fetch', fetchMock)
const transfer = createManagerSessionTransfer('https://manager.example', 'tok_123')
await expect(transfer.sessionExists('ses_a')).resolves.toBe(true)
expect(String(fetchMock.mock.calls[0]![0])).toBe('https://manager.example/api/opencode-proxy/api/session/ses_a')
})
it('reports a missing Manager session only for SessionNotFoundError', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse(404, { _tag: 'SessionNotFoundError', sessionID: 'ses_a', message: 'Session not found' })))
const transfer = createManagerSessionTransfer('https://manager.example', 'tok_123')
await expect(transfer.sessionExists('ses_a')).resolves.toBe(false)
})
it('rethrows other failures instead of treating the session as missing', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('Not Found', { status: 404 })))
const transfer = createManagerSessionTransfer('https://manager.example', 'tok_123')
await expect(transfer.sessionExists('ses_a')).rejects.toBeDefined()
})
})