Files
opencode-manager/backend/test/routes/internal-opencode-workspaces.test.ts
T
Chris Scott 96e02e9646 feat(sessions): add permission modes, session goals, ocm session tool and multi-run
Per-session permission modes: each root session runs in ask or auto.
Auto answers each permission request once from the backend, child
sessions inherit their root's mode, forks copy the source's mode, and
scheduled runs and agent-created sessions stay on ask. A lock reason
reports why a session's mode cannot change.

Session goals: arm a goal in the composer and a backend audit loop keeps
the session working until an auditor model returns done or blocked. The
loop is bounded by continuation and token limits, survives restarts, and
pushes an outcome notification. Goals cannot start on scheduled-run or
child sessions.

ocm session tool: allow-listed internal routes to list, create, follow up
on, read replies from, and fork sessions. Created and forked sessions are
pinned to ask, listing covers a repo's workspaces, and replies wait for
the session to settle with a capped response.

Multi-run: launch one prompt on up to five models, optionally each in its
own OpenCode workspace, with per-entry status and discard.

Also add the shared owners this group needed (session launcher, session
reply, workspace create/remove, multi-run storage) and update the feature
docs.
2026-10-03 21:16:05 -04:00

150 lines
5.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest'
import type { SessionPermissionModeService } from '../../src/services/session-permission-modes'
import { Hono } from 'hono'
import type { Database } from 'bun:sqlite'
import { createInternalRoutes } from '../../src/routes/internal'
import type { ScheduleService } from '../../src/services/schedules'
import type { NotificationService } from '../../src/services/notification'
import type { SettingsService } from '../../src/services/settings'
import type { OpenCodeClient } from '../../src/services/opencode/client'
import type { Repo } from '../../src/types/repo'
const mockDb = {
prepare: vi.fn().mockReturnValue({
run: vi.fn(),
get: vi.fn(),
all: vi.fn(),
}),
exec: vi.fn(),
close: vi.fn(),
transaction: vi.fn((fn: () => void) => fn()),
} as unknown as Database
vi.mock('bun:sqlite', () => ({
Database: vi.fn(() => mockDb),
}))
const mockListRepos = vi.fn()
vi.mock('../../src/db/queries', async () => {
const { getRepoDisplayName } = await vi.importActual<typeof import('@opencode-manager/shared/utils')>('@opencode-manager/shared/utils')
return {
listRepos: (...args: unknown[]) => mockListRepos(...args),
getRepoName: (repo: Parameters<typeof getRepoDisplayName>[0]) => getRepoDisplayName(repo),
}
})
vi.mock('../../src/db/migration-runner', () => ({
migrate: vi.fn(),
}))
vi.mock('../../src/services/internal-token', () => ({
getOrCreateInternalToken: vi.fn().mockReturnValue('test-internal-token'),
}))
vi.mock('../../src/services/schedules', () => ({
ScheduleService: vi.fn(),
}))
vi.mock('../../src/services/notification', () => ({
NotificationService: vi.fn(),
}))
vi.mock('../../src/services/settings', () => ({
SettingsService: vi.fn(),
}))
vi.mock('../../src/services/opencode/client', () => ({
createOpenCodeClient: vi.fn(),
}))
function makeRepo(overrides: Partial<Repo>): Repo {
return {
id: 1,
localPath: 'test-repo',
fullPath: '/tmp/test-repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
...overrides,
}
}
describe('internal-opencode-workspaces routes', () => {
let app: Hono
let token: string
beforeEach(() => {
vi.clearAllMocks()
mockListRepos.mockReturnValue([])
const scheduleService = {} as ScheduleService
const notificationService = {} as NotificationService
const settingsService = {} as SettingsService
const openCodeClient = {
forwardRaw: vi.fn(),
} as unknown as OpenCodeClient
app = new Hono()
app.route('/api/internal', createInternalRoutes(mockDb, scheduleService, notificationService, settingsService, openCodeClient, {} as SessionPermissionModeService))
token = 'test-internal-token'
})
it('GET /api/internal/opencode-workspaces returns 401 without bearer token', async () => {
const res = await app.request('/api/internal/opencode-workspaces')
expect(res.status).toBe(401)
})
it('GET /api/internal/opencode-workspaces returns 200 with bearer token', async () => {
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: unknown[] }
expect(body).toHaveProperty('workspaces')
expect(Array.isArray(body.workspaces)).toBe(true)
})
it('GET /api/internal/opencode-workspaces only returns ready repos', async () => {
mockListRepos.mockReturnValue([
makeRepo({ id: 1, cloneStatus: 'ready', localPath: 'ready-repo' }),
makeRepo({ id: 2, cloneStatus: 'cloning', localPath: 'cloning-repo' }),
makeRepo({ id: 3, cloneStatus: 'error', localPath: 'error-repo' }),
])
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: Array<{ repoId: number; cloneStatus: string }> }
expect(body.workspaces.length).toBe(1)
expect(body.workspaces[0]?.cloneStatus).toBe('ready')
expect(body.workspaces[0]?.repoId).toBeDefined()
})
it('GET /api/internal/opencode-workspaces returns workspace structure', async () => {
mockListRepos.mockReturnValue([
makeRepo({ id: 1, localPath: 'test-repo', cloneStatus: 'ready' }),
makeRepo({ id: 2, localPath: 'worktree-repo', cloneStatus: 'ready', isWorktree: true }),
])
const res = await app.request('/api/internal/opencode-workspaces', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { workspaces: Array<{ repoId: number; name: string; branch: string | null; cloneStatus: string; directory: string; isWorktree: boolean; extra: { repoId: number; localPath: string; fullPath: string } }> }
expect(body.workspaces.length).toBe(2)
const workspace = body.workspaces.find((w) => w.repoId === 1)!
expect(workspace).toHaveProperty('repoId')
expect(workspace).toHaveProperty('name')
expect(workspace).toHaveProperty('branch')
expect(workspace).toHaveProperty('cloneStatus')
expect(workspace).toHaveProperty('directory')
expect(workspace).toHaveProperty('projectId')
expect(workspace).toHaveProperty('extra')
expect(workspace.extra).toHaveProperty('repoId')
expect(workspace.extra).toHaveProperty('localPath')
expect(workspace.extra).toHaveProperty('fullPath')
const worktreeWorkspace = body.workspaces.find((w) => w.repoId === 2)!
expect(workspace.isWorktree).toBe(false)
expect(worktreeWorkspace.isWorktree).toBe(true)
})
})