mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
803 lines
30 KiB
TypeScript
803 lines
30 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
activateSkill,
|
|
addSessionContext,
|
|
askSideQuestion,
|
|
cancelForm,
|
|
clearRevert,
|
|
commitRevert,
|
|
compactSession,
|
|
createSession,
|
|
createSessionWithContext,
|
|
createSessionWithPrompt,
|
|
deleteSession,
|
|
findFiles,
|
|
forkSession,
|
|
getSession,
|
|
interruptSession,
|
|
listCommands,
|
|
listPendingForms,
|
|
listPendingPermissions,
|
|
listSessionMessages,
|
|
listSessionPage,
|
|
projectActivityTimestamp,
|
|
readSessionSnapshot,
|
|
remoteProjectName,
|
|
renameSession,
|
|
replyForm,
|
|
replyPermission,
|
|
runCommand,
|
|
runShell,
|
|
sendPrompt,
|
|
sortProjectsByActivity,
|
|
stageRevert,
|
|
switchSessionAgent,
|
|
switchSessionModel,
|
|
SIDE_QUESTION_INSTRUCTIONS,
|
|
} from './opencode'
|
|
import { FetchError } from './fetchWrapper'
|
|
|
|
const jsonResponse = (body: unknown) =>
|
|
new Response(JSON.stringify(body), {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
|
|
const sessionInfo = (id: string, directory: string) => ({
|
|
id,
|
|
projectID: 'proj_1',
|
|
title: `Session ${id}`,
|
|
time: { created: 1000, updated: 2000 },
|
|
location: { directory },
|
|
})
|
|
|
|
describe('OpenCode facade', () => {
|
|
const fetchMock = vi.fn()
|
|
|
|
beforeEach(() => {
|
|
fetchMock.mockReset()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
const lastRequest = () => {
|
|
const [input, init] = fetchMock.mock.calls.at(-1) ?? []
|
|
return { url: String(input), init: init as RequestInit }
|
|
}
|
|
|
|
it('lists root sessions for a directory through the V2 cursor API', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ data: [sessionInfo('ses_1', '/repo')], cursor: { next: 'cursor_next' } }),
|
|
)
|
|
|
|
const page = await listSessionPage({
|
|
directory: '/repo',
|
|
limit: 25,
|
|
order: 'desc',
|
|
search: 'deploy',
|
|
})
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session?limit=25&order=desc&search=deploy&parentID=null&directory=%2Frepo',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(page.items).toEqual([sessionInfo('ses_1', '/repo')])
|
|
expect(page.nextCursor).toBe('cursor_next')
|
|
})
|
|
|
|
it('passes the directory and cursor for continuation pages', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: [], cursor: {} }))
|
|
|
|
const page = await listSessionPage({ directory: '/repo', cursor: 'cursor_1' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session?parentID=null&directory=%2Frepo&cursor=cursor_1',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(page.items).toEqual([])
|
|
expect(page.nextCursor).toBeUndefined()
|
|
})
|
|
|
|
it('omits the directory when listing sessions across all directories', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ data: [sessionInfo('ses_1', '/repo')], cursor: { next: 'cursor_next' } }),
|
|
)
|
|
|
|
const page = await listSessionPage({ limit: 25, order: 'desc' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session?limit=25&order=desc&parentID=null',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(page.items).toEqual([sessionInfo('ses_1', '/repo')])
|
|
expect(page.nextCursor).toBe('cursor_next')
|
|
})
|
|
|
|
it('omits the directory on an all-directories continuation page', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: [], cursor: {} }))
|
|
|
|
await listSessionPage({ cursor: 'cursor_1' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session?parentID=null&cursor=cursor_1',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
})
|
|
|
|
it('omits the parentID filter when including child sessions', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ data: [sessionInfo('ses_child', '/repo')], cursor: {} }),
|
|
)
|
|
|
|
await listSessionPage({ directory: '/repo', limit: 25, order: 'desc', includeChildren: true })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session?limit=25&order=desc&directory=%2Frepo',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
})
|
|
|
|
it('reads a single session from the V2 session route', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_1', '/repo') }))
|
|
|
|
const session = await getSession('ses_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(session).toEqual(sessionInfo('ses_1', '/repo'))
|
|
})
|
|
|
|
it('creates a session with location and a parsed model reference', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_new', '/repo') }))
|
|
|
|
const session = await createSession({
|
|
directory: '/repo',
|
|
title: 'New session',
|
|
agent: 'build',
|
|
model: 'anthropic/claude#v1',
|
|
})
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({
|
|
title: 'New session',
|
|
agent: 'build',
|
|
model: { providerID: 'anthropic', id: 'claude', variant: 'v1' },
|
|
location: { directory: '/repo' },
|
|
}))
|
|
expect(session.id).toBe('ses_new')
|
|
})
|
|
|
|
it('creates a session with the required location and no optional fields', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_new', '/repo') }))
|
|
|
|
await createSession({ directory: '/repo' })
|
|
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ location: { directory: '/repo' } }))
|
|
})
|
|
|
|
it('deletes a session through the V2 session route', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await expect(deleteSession('ses_1')).resolves.toBeUndefined()
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
|
|
expect(lastRequest().init.method).toBe('DELETE')
|
|
})
|
|
|
|
it('renames a session with a title update', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await renameSession('ses_1', 'Renamed')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1')
|
|
expect(lastRequest().init.method).toBe('PATCH')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ title: 'Renamed' }))
|
|
})
|
|
|
|
it('forks a session before a message', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_child', '/repo') }))
|
|
|
|
const forked = await forkSession('ses_1', 'msg_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/fork')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ before: 'msg_1' }))
|
|
expect(forked.id).toBe('ses_child')
|
|
})
|
|
|
|
it('forks a session without a boundary when none is given', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: sessionInfo('ses_child', '/repo') }))
|
|
|
|
await forkSession('ses_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/fork')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({}))
|
|
})
|
|
|
|
it('adds context to a session without resuming the model', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: { id: 'inb_1' } }))
|
|
|
|
await expect(addSessionContext('ses_1', 'Run output')).resolves.toBeUndefined()
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/synthetic')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ text: 'Run output', resume: false }))
|
|
})
|
|
|
|
it('creates a session in a directory and seeds it with context', async () => {
|
|
fetchMock
|
|
.mockResolvedValueOnce(jsonResponse({ data: sessionInfo('ses_new', '/abs/repos/my-repo') }))
|
|
.mockResolvedValueOnce(jsonResponse({ data: { id: 'inb_1' } }))
|
|
|
|
const session = await createSessionWithContext({ directory: '/abs/repos/my-repo', title: 'Run (continued)' }, 'Run output')
|
|
|
|
expect(session.id).toBe('ses_new')
|
|
const [createUrl, createInit] = fetchMock.mock.calls[0] ?? []
|
|
expect(String(createUrl)).toBe('http://localhost/api/opencode/api/session')
|
|
expect((createInit as RequestInit).body).toBe(JSON.stringify({ title: 'Run (continued)', location: { directory: '/abs/repos/my-repo' } }))
|
|
const [contextUrl, contextInit] = fetchMock.mock.calls[1] ?? []
|
|
expect(String(contextUrl)).toBe('http://localhost/api/opencode/api/session/ses_new/synthetic')
|
|
expect((contextInit as RequestInit).body).toBe(JSON.stringify({ text: 'Run output', resume: false }))
|
|
})
|
|
|
|
it('deletes the new session when seeding its context fails', async () => {
|
|
fetchMock
|
|
.mockResolvedValueOnce(jsonResponse({ data: sessionInfo('ses_new', '/abs/repos/my-repo') }))
|
|
.mockResolvedValueOnce(new Response('nope', { status: 400 }))
|
|
.mockResolvedValueOnce(new Response(null, { status: 204 }))
|
|
|
|
await expect(createSessionWithContext({ directory: '/abs/repos/my-repo' }, 'Run output')).rejects.toBeInstanceOf(FetchError)
|
|
|
|
const [deleteUrl, deleteInit] = fetchMock.mock.calls[2] ?? []
|
|
expect(String(deleteUrl)).toBe('http://localhost/api/opencode/api/session/ses_new')
|
|
expect((deleteInit as RequestInit).method).toBe('DELETE')
|
|
})
|
|
|
|
it('creates a session in a directory and sends a prompt', async () => {
|
|
fetchMock
|
|
.mockResolvedValueOnce(jsonResponse({ data: sessionInfo('ses_new', '/abs/repos/my-repo') }))
|
|
.mockResolvedValueOnce(
|
|
jsonResponse({
|
|
data: {
|
|
id: 'msg_1',
|
|
sessionID: 'ses_new',
|
|
time: { created: 1000 },
|
|
type: 'user',
|
|
payload: { text: 'Fix conflicts' },
|
|
},
|
|
}),
|
|
)
|
|
|
|
const session = await createSessionWithPrompt(
|
|
{ directory: '/abs/repos/my-repo', title: 'Resolve merge conflicts' },
|
|
'Fix conflicts',
|
|
)
|
|
|
|
expect(session.id).toBe('ses_new')
|
|
const [createUrl] = fetchMock.mock.calls[0] ?? []
|
|
expect(String(createUrl)).toBe('http://localhost/api/opencode/api/session')
|
|
const [promptUrl, promptInit] = fetchMock.mock.calls[1] ?? []
|
|
expect(String(promptUrl)).toBe('http://localhost/api/opencode/api/session/ses_new/prompt')
|
|
expect((promptInit as RequestInit).method).toBe('POST')
|
|
expect((promptInit as RequestInit).body).toBe(JSON.stringify({ text: 'Fix conflicts', metadata: { ocmClient: 'desktop' } }))
|
|
})
|
|
|
|
it('deletes the new session when sending the prompt fails', async () => {
|
|
fetchMock
|
|
.mockResolvedValueOnce(jsonResponse({ data: sessionInfo('ses_new', '/abs/repos/my-repo') }))
|
|
.mockResolvedValueOnce(new Response('nope', { status: 400 }))
|
|
.mockResolvedValueOnce(new Response(null, { status: 204 }))
|
|
|
|
await expect(createSessionWithPrompt({ directory: '/abs/repos/my-repo' }, 'Fix conflicts')).rejects.toBeInstanceOf(FetchError)
|
|
|
|
const [deleteUrl, deleteInit] = fetchMock.mock.calls[2] ?? []
|
|
expect(String(deleteUrl)).toBe('http://localhost/api/opencode/api/session/ses_new')
|
|
expect((deleteInit as RequestInit).method).toBe('DELETE')
|
|
})
|
|
|
|
it('finds files through the V2 filesystem route and returns paths', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({
|
|
location: { directory: '/repo' },
|
|
data: [
|
|
{ path: 'src/index.ts', type: 'file' },
|
|
{ path: 'src/app.ts', type: 'file' },
|
|
],
|
|
}),
|
|
)
|
|
|
|
const files = await findFiles({ directory: '/repo', query: 'index', limit: 20 })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/fs/find?location%5Bdirectory%5D=%2Frepo&query=index&type=file&limit=20',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(files).toEqual(['src/index.ts', 'src/app.ts'])
|
|
})
|
|
|
|
it('finds files without a limit when none is given', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ location: { directory: '/repo' }, data: [] }))
|
|
|
|
await findFiles({ directory: '/repo', query: 'index' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/fs/find?location%5Bdirectory%5D=%2Frepo&query=index&type=file',
|
|
)
|
|
})
|
|
|
|
it('reads a session snapshot from the message page, inbox, and active sessions', async () => {
|
|
const message = {
|
|
id: 'msg_1',
|
|
type: 'user' as const,
|
|
text: 'Run the tests',
|
|
time: { created: 1000 },
|
|
}
|
|
const inbox = {
|
|
id: 'msg_2',
|
|
sessionID: 'ses_1',
|
|
time: { created: 2000 },
|
|
type: 'user' as const,
|
|
payload: { text: 'Wait for me' },
|
|
delivery: 'queue' as const,
|
|
}
|
|
fetchMock.mockImplementation((input: unknown) => {
|
|
const url = String(input)
|
|
if (url.endsWith('/api/session/ses_1/inbox')) {
|
|
return Promise.resolve(jsonResponse({ data: [inbox] }))
|
|
}
|
|
if (url.endsWith('/api/session/active')) {
|
|
return Promise.resolve(jsonResponse({ data: { ses_1: { type: 'running' } } }))
|
|
}
|
|
return Promise.resolve(jsonResponse({ data: [message], cursor: { next: 'cursor_next' } }))
|
|
})
|
|
|
|
const snapshot = await readSessionSnapshot('ses_1')
|
|
|
|
expect(snapshot).toEqual({
|
|
messages: [message],
|
|
nextCursor: 'cursor_next',
|
|
pending: [inbox],
|
|
status: 'busy',
|
|
})
|
|
expect(fetchMock.mock.calls.map(([input]) => String(input)).sort()).toEqual([
|
|
'http://localhost/api/opencode/api/session/active',
|
|
'http://localhost/api/opencode/api/session/ses_1/inbox',
|
|
'http://localhost/api/opencode/api/session/ses_1/message?order=desc',
|
|
])
|
|
})
|
|
|
|
it('requests the newest message page with descending order', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ data: [{ id: 'msg_3' }, { id: 'msg_2' }], cursor: { next: 'cursor_2' } }),
|
|
)
|
|
|
|
const page = await listSessionMessages('ses_1', { limit: 20 })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session/ses_1/message?limit=20&order=desc',
|
|
)
|
|
expect(page.messages.map((message) => message.id)).toEqual(['msg_2', 'msg_3'])
|
|
expect(page.nextCursor).toBe('cursor_2')
|
|
})
|
|
|
|
it('omits order on continuation pages so the server accepts the cursor', async () => {
|
|
const server = vi.fn((input: unknown) => {
|
|
const url = String(input)
|
|
if (url.includes('cursor=') && url.includes('order=')) {
|
|
return Promise.resolve(
|
|
new Response(
|
|
JSON.stringify({ _tag: 'InvalidCursorError', message: 'Cursor cannot be combined with order' }),
|
|
{ status: 400, headers: { 'Content-Type': 'application/json' } },
|
|
),
|
|
)
|
|
}
|
|
return Promise.resolve(
|
|
jsonResponse({ data: [{ id: 'msg_2' }, { id: 'msg_1' }], cursor: { next: 'cursor_2' } }),
|
|
)
|
|
})
|
|
fetchMock.mockImplementation(server)
|
|
|
|
const page = await listSessionMessages('ses_1', { cursor: 'cursor_1' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session/ses_1/message?cursor=cursor_1',
|
|
)
|
|
expect(page.messages.map((message) => message.id)).toEqual(['msg_1', 'msg_2'])
|
|
expect(page.nextCursor).toBe('cursor_2')
|
|
})
|
|
|
|
it('wraps V2 client failures in a FetchError', async () => { fetchMock.mockResolvedValue(new Response('upstream exploded', { status: 500 }))
|
|
|
|
const failure = await getSession('ses_1').catch((error: unknown) => error)
|
|
|
|
expect(failure).toBeInstanceOf(FetchError)
|
|
expect((failure as FetchError).statusCode).toBe(500)
|
|
})
|
|
|
|
it('maps a declared V2 not-found error to 404 instead of a network failure', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
new Response(
|
|
JSON.stringify({ _tag: 'SessionNotFoundError', sessionID: 'ses_1', message: 'Session not found' }),
|
|
{ status: 404, headers: { 'Content-Type': 'application/json' } },
|
|
),
|
|
)
|
|
|
|
const failure = await getSession('ses_1').catch((error: unknown) => error)
|
|
|
|
expect(failure).toBeInstanceOf(FetchError)
|
|
expect((failure as FetchError).statusCode).toBe(404)
|
|
expect((failure as FetchError).message).toBe('Session not found')
|
|
})
|
|
|
|
it('preserves the declared error tag as FetchError.code', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
new Response(
|
|
JSON.stringify({ _tag: 'PermissionNotFoundError', sessionID: 'ses_1', requestID: 'per_1', message: 'Permission request not found' }),
|
|
{ status: 404, headers: { 'Content-Type': 'application/json' } },
|
|
),
|
|
)
|
|
|
|
const failure = await replyPermission('ses_1', 'per_1', 'once').catch((error: unknown) => error)
|
|
|
|
expect(failure).toBeInstanceOf(FetchError)
|
|
expect((failure as FetchError).statusCode).toBe(404)
|
|
expect((failure as FetchError).code).toBe('PermissionNotFoundError')
|
|
})
|
|
|
|
it('maps a declared V2 conflict error to 409', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
new Response(
|
|
JSON.stringify({ _tag: 'SessionBusyError', sessionID: 'ses_1', message: 'Session is busy' }),
|
|
{ status: 409, headers: { 'Content-Type': 'application/json' } },
|
|
),
|
|
)
|
|
|
|
const failure = await sendPrompt({ sessionID: 'ses_1', text: 'Hi' }).catch((error: unknown) => error)
|
|
|
|
expect(failure).toBeInstanceOf(FetchError)
|
|
expect((failure as FetchError).statusCode).toBe(409)
|
|
})
|
|
|
|
it('sends a prompt with files, agents, skills, and delivery', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({
|
|
data: {
|
|
id: 'msg_1',
|
|
sessionID: 'ses_1',
|
|
time: { created: 1000 },
|
|
type: 'user',
|
|
payload: { text: 'Hello @src/a.ts' },
|
|
delivery: 'queue',
|
|
},
|
|
}),
|
|
)
|
|
|
|
const inbox = await sendPrompt({
|
|
sessionID: 'ses_1',
|
|
text: 'Hello @src/a.ts',
|
|
files: [{ uri: 'file:///repo/src/a.ts', name: 'a.ts', mention: { start: 6, end: 15, text: '@src/a.ts' } }],
|
|
agents: [{ name: 'build', mention: { start: 6, end: 12, text: '@build' } }],
|
|
skills: [{ id: 'review' }],
|
|
delivery: 'queue',
|
|
})
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/prompt')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({
|
|
text: 'Hello @src/a.ts',
|
|
files: [{ uri: 'file:///repo/src/a.ts', name: 'a.ts', mention: { start: 6, end: 15, text: '@src/a.ts' } }],
|
|
agents: [{ name: 'build', mention: { start: 6, end: 12, text: '@build' } }],
|
|
skills: [{ id: 'review' }],
|
|
metadata: { ocmClient: 'desktop' },
|
|
delivery: 'queue',
|
|
}))
|
|
expect(inbox.id).toBe('msg_1')
|
|
})
|
|
|
|
it('marks a prompt as mobile when the viewport is narrow', async () => {
|
|
vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(375)
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({
|
|
data: {
|
|
id: 'msg_1',
|
|
sessionID: 'ses_1',
|
|
time: { created: 1000 },
|
|
type: 'user',
|
|
payload: { text: 'Hi' },
|
|
},
|
|
}),
|
|
)
|
|
|
|
await sendPrompt({ sessionID: 'ses_1', text: 'Hi' })
|
|
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({
|
|
text: 'Hi',
|
|
metadata: { ocmClient: 'mobile' },
|
|
}))
|
|
})
|
|
|
|
it('marks a prompt as desktop when the viewport is wide', async () => {
|
|
vi.spyOn(window, 'innerWidth', 'get').mockReturnValue(1280)
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({
|
|
data: {
|
|
id: 'msg_1',
|
|
sessionID: 'ses_1',
|
|
time: { created: 1000 },
|
|
type: 'user',
|
|
payload: { text: 'Hi' },
|
|
},
|
|
}),
|
|
)
|
|
|
|
await sendPrompt({ sessionID: 'ses_1', text: 'Hi' })
|
|
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({
|
|
text: 'Hi',
|
|
metadata: { ocmClient: 'desktop' },
|
|
}))
|
|
})
|
|
|
|
it('switches the session model through the V2 model route', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await switchSessionModel('ses_1', { providerID: 'anthropic', id: 'claude', variant: 'v1' })
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/model')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ model: { providerID: 'anthropic', id: 'claude', variant: 'v1' } }))
|
|
})
|
|
|
|
it('switches the session agent through the V2 agent route', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await switchSessionAgent('ses_1', 'build')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/agent')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ agent: 'build' }))
|
|
})
|
|
|
|
it('runs a session command', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await runCommand({
|
|
sessionID: 'ses_1',
|
|
name: 'init',
|
|
text: 'extra context',
|
|
agents: [{ name: 'build' }],
|
|
})
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/command')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({
|
|
name: 'init',
|
|
text: 'extra context',
|
|
agents: [{ name: 'build' }],
|
|
}))
|
|
})
|
|
|
|
it('runs a session shell command', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await runShell('ses_1', 'ls -la')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/shell')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ command: 'ls -la' }))
|
|
})
|
|
|
|
it('interrupts a session', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: { interrupted: true } }))
|
|
|
|
await interruptSession('ses_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/interrupt')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
})
|
|
|
|
it('asks a side question through the session generate route with the shared instructions', async () => {
|
|
fetchMock.mockResolvedValue(jsonResponse({ data: { text: ' answer \n' } }))
|
|
|
|
const answer = await askSideQuestion('ses_1', 'why?')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/generate')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
const body = JSON.parse(String(lastRequest().init.body))
|
|
expect(body.prompt).toBe(`${SIDE_QUESTION_INSTRUCTIONS}\n\nwhy?`)
|
|
expect(body.prompt.startsWith(SIDE_QUESTION_INSTRUCTIONS)).toBe(true)
|
|
expect(body.prompt.endsWith('\n\nwhy?')).toBe(true)
|
|
expect(answer).toBe('answer')
|
|
})
|
|
|
|
it('stages, commits, and clears a session revert', async () => {
|
|
fetchMock.mockImplementation((input: RequestInfo | URL) => {
|
|
const url = String(input)
|
|
if (url.endsWith('/interrupt')) return Promise.resolve(jsonResponse({ data: { interrupted: true } }))
|
|
if (url.endsWith('/wait')) return Promise.resolve(new Response(null, { status: 204 }))
|
|
if (url.endsWith('/revert/stage')) return Promise.resolve(jsonResponse({ data: { messageID: 'msg_1' } }))
|
|
return Promise.resolve(new Response(null, { status: 204 }))
|
|
})
|
|
|
|
await stageRevert('ses_1', 'msg_1')
|
|
|
|
const stageRequests = fetchMock.mock.calls.map(([input, init]) => ({
|
|
url: String(input),
|
|
init: init as RequestInit,
|
|
}))
|
|
expect(stageRequests.map((request) => request.url)).toEqual([
|
|
'http://localhost/api/opencode/api/session/ses_1/interrupt',
|
|
'http://localhost/api/opencode/api/experimental/session/ses_1/wait',
|
|
'http://localhost/api/opencode/api/session/ses_1/revert/stage',
|
|
])
|
|
expect(stageRequests.map((request) => request.init.method)).toEqual(['POST', 'POST', 'POST'])
|
|
expect(stageRequests[2].init.body).toBe(JSON.stringify({ messageID: 'msg_1' }))
|
|
|
|
await commitRevert('ses_1')
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/revert/commit')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
|
|
await clearRevert('ses_1')
|
|
expect(lastRequest().url).toMatch(/\/revert$/)
|
|
expect(lastRequest().init.method).toBe('DELETE')
|
|
})
|
|
|
|
it('compacts a session', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ data: { id: 'cmp_1', sessionID: 'ses_1', time: { created: 1000 }, type: 'compaction', delivery: 'steer' } }),
|
|
)
|
|
|
|
const compaction = await compactSession('ses_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/compact')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(compaction.id).toBe('cmp_1')
|
|
})
|
|
|
|
it('activates a skill on the experimental session route', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await activateSkill('ses_1', 'review')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/experimental/session/ses_1/skill')
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ id: 'review' }))
|
|
})
|
|
|
|
it('lists commands scoped to a directory', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({ location: { directory: '/repo' }, data: [{ name: 'init', description: 'Init' }] }),
|
|
)
|
|
|
|
const commands = await listCommands('/repo')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/command?location%5Bdirectory%5D=%2Frepo')
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(commands).toEqual([{ name: 'init', description: 'Init' }])
|
|
})
|
|
|
|
it('lists pending permissions scoped to a directory', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
jsonResponse({
|
|
location: { directory: '/repo' },
|
|
data: [{ id: 'perm_1', sessionID: 'ses_1', action: 'shell', resources: ['ls'] }],
|
|
}),
|
|
)
|
|
|
|
const permissions = await listPendingPermissions('/repo')
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/permission/request?location%5Bdirectory%5D=%2Frepo',
|
|
)
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(permissions).toEqual([{ id: 'perm_1', sessionID: 'ses_1', action: 'shell', resources: ['ls'] }])
|
|
})
|
|
|
|
it('replies to a permission with a decision and rejection message', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await replyPermission('ses_1', 'perm_1', 'reject', 'not allowed')
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session/ses_1/permission/perm_1/reply',
|
|
)
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ decision: 'reject', message: 'not allowed' }))
|
|
})
|
|
|
|
it('replies to a permission without a message', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await replyPermission('ses_1', 'perm_1', 'once')
|
|
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ decision: 'once' }))
|
|
})
|
|
|
|
it('lists pending forms scoped to a directory', async () => {
|
|
const form = {
|
|
id: 'form_1',
|
|
sessionID: 'ses_1',
|
|
title: 'Continue?',
|
|
fields: [{ key: 'q0', type: 'string' }],
|
|
}
|
|
fetchMock.mockResolvedValue(jsonResponse({ location: { directory: '/repo' }, data: [form] }))
|
|
|
|
const forms = await listPendingForms('/repo')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/form?location%5Bdirectory%5D=%2Frepo')
|
|
expect(lastRequest().init.method).toBe('GET')
|
|
expect(forms).toEqual([form])
|
|
})
|
|
|
|
it('replies to a form with an answer keyed by field', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await replyForm('ses_1', 'form_1', { q0: 'Yes' })
|
|
|
|
expect(lastRequest().url).toBe(
|
|
'http://localhost/api/opencode/api/session/ses_1/form/form_1/reply',
|
|
)
|
|
expect(lastRequest().init.method).toBe('POST')
|
|
expect(lastRequest().init.body).toBe(JSON.stringify({ answer: { q0: 'Yes' } }))
|
|
})
|
|
|
|
it('cancels a form', async () => {
|
|
fetchMock.mockResolvedValue(new Response(null, { status: 204 }))
|
|
|
|
await cancelForm('ses_1', 'form_1')
|
|
|
|
expect(lastRequest().url).toBe('http://localhost/api/opencode/api/session/ses_1/form/form_1')
|
|
expect(lastRequest().init.method).toBe('DELETE')
|
|
})
|
|
})
|
|
|
|
const project = (
|
|
id: string,
|
|
name: string | undefined,
|
|
canonical: string,
|
|
time: { created: number; updated: number; active?: number },
|
|
) => ({ id, name, canonical, time, sandboxes: [] })
|
|
|
|
describe('project helpers', () => {
|
|
it('prefers the activity time over the update time', () => {
|
|
const withActivity = project('p1', 'Active', '/work/active', { created: 1, updated: 10, active: 900 })
|
|
|
|
expect(projectActivityTimestamp(withActivity)).toBe(900)
|
|
})
|
|
|
|
it('falls back to the update time when the project was never active', () => {
|
|
const neverActive = project('p1', 'Idle', '/work/idle', { created: 1, updated: 10, active: 0 })
|
|
|
|
expect(projectActivityTimestamp(neverActive)).toBe(10)
|
|
})
|
|
|
|
it('orders projects by activity, most recent first, without mutating the input', () => {
|
|
const stale = project('p_stale', 'Stale', '/work/stale', { created: 1, updated: 500, active: 100 })
|
|
const recent = project('p_recent', 'Recent', '/work/recent', { created: 1, updated: 100, active: 900 })
|
|
const middle = project('p_middle', 'Middle', '/work/middle', { created: 1, updated: 700 })
|
|
const projects = [stale, recent, middle]
|
|
|
|
expect(sortProjectsByActivity(projects).map((entry) => entry.id)).toEqual([
|
|
'p_recent',
|
|
'p_middle',
|
|
'p_stale',
|
|
])
|
|
expect(projects.map((entry) => entry.id)).toEqual(['p_stale', 'p_recent', 'p_middle'])
|
|
})
|
|
|
|
it('names a project from its trimmed title', () => {
|
|
expect(remoteProjectName(project('p1', ' Alpha ', '/work/alpha', { created: 1, updated: 2 }))).toBe('Alpha')
|
|
})
|
|
|
|
it('names a project from its directory when it has no title', () => {
|
|
expect(remoteProjectName(project('p1', undefined, '/work/beta', { created: 1, updated: 2 }))).toBe('beta')
|
|
expect(remoteProjectName(project('p1', ' ', '/work/gamma', { created: 1, updated: 2 }))).toBe('gamma')
|
|
})
|
|
})
|