Files
opencode-manager/backend/test/routes/internal-settings.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

342 lines
13 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'bun:test'
import type { SessionPermissionModeService } from '../../src/services/session-permission-modes'
import { Hono } from 'hono'
import { Database } from 'bun:sqlite'
import { createInternalRoutes } from '../../src/routes/internal'
import { ScheduleService } from '../../src/services/schedules'
import { NotificationService } from '../../src/services/notification'
import { SettingsService } from '../../src/services/settings'
import { createOpenCodeClient } from '../../src/services/opencode/client'
import { allMigrations } from '../../src/db/migrations'
import { getOrCreateInternalToken } from '../../src/services/internal-token'
import { migrate } from '../../src/db/migration-runner'
import type { UserPreferences } from '@opencode-manager/shared/types'
import type { ScheduleWorktreeManager } from '../../src/services/schedule-worktree'
describe('internal/settings routes', () => {
let db: Database
let scheduleService: ScheduleService
let notificationService: NotificationService
let settingsService: SettingsService
let app: Hono
let token: string
beforeEach(() => {
db = new Database(':memory:')
migrate(db, allMigrations)
const openCodeClient = createOpenCodeClient()
const stubWorktreeManager = { prepare: () => Promise.resolve(null), finalize: () => Promise.resolve({ commitHash: null }) } as unknown as ScheduleWorktreeManager
scheduleService = new ScheduleService(db, openCodeClient, stubWorktreeManager)
notificationService = new NotificationService(db)
settingsService = new SettingsService(db)
app = new Hono()
app.route('/api/internal', createInternalRoutes(db, scheduleService, notificationService, settingsService, openCodeClient, {} as SessionPermissionModeService))
token = getOrCreateInternalToken(db)
})
it('GET /api/internal/settings returns 401 without bearer token', async () => {
const res = await app.request('/api/internal/settings')
expect(res.status).toBe(401)
})
it('GET /api/internal/settings returns 200 with bearer token', async () => {
const res = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { preferences: unknown; updatedAt: number }
expect(body).toHaveProperty('preferences')
expect(body).toHaveProperty('updatedAt')
})
it('GET /api/internal/settings returns merged defaults', async () => {
const res = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { preferences: { theme: string; mode: string } }
expect(body.preferences.theme).toBe('dark')
expect(body.preferences.mode).toBe('build')
})
it('GET /api/internal/settings returns manager colorTheme by default', async () => {
const res = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { preferences: { colorTheme: string } }
expect(body.preferences.colorTheme).toBe('manager')
})
it('GET /api/internal/settings recovers from a stored colorTheme removed from the catalog without losing other preferences', async () => {
db.prepare(
`INSERT INTO user_preferences (user_id, preferences, updated_at)
VALUES (?, ?, ?)`
).run('default', JSON.stringify({ colorTheme: 'removed-theme', autoScroll: false }), Date.now())
const res = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(res.status).toBe(200)
const body = await res.json() as { preferences: { colorTheme: string; autoScroll: boolean } }
expect(body.preferences.colorTheme).toBe('manager')
expect(body.preferences.autoScroll).toBe(false)
})
it('PATCH /api/internal/settings returns 401 without bearer token', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ theme: 'dark' }),
headers: { 'content-type': 'application/json' },
})
expect(res.status).toBe(401)
})
it('PATCH /api/internal/settings with { theme: "dark" } persists and returns new settings', async () => {
const patchRes = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ theme: 'dark' }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(patchRes.status).toBe(200)
const getRes = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(getRes.status).toBe(200)
const body = await getRes.json() as { preferences: { theme: string } }
expect(body.preferences.theme).toBe('dark')
})
it('PATCH /api/internal/settings with { colorTheme: "dracula" } persists and returns new settings', async () => {
const patchRes = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ colorTheme: 'dracula' }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(patchRes.status).toBe(200)
const getRes = await app.request('/api/internal/settings', {
headers: { authorization: `Bearer ${token}` },
})
expect(getRes.status).toBe(200)
const body = await getRes.json() as { preferences: { colorTheme: string } }
expect(body.preferences.colorTheme).toBe('dracula')
})
it('PATCH /api/internal/settings with { gitCredentials: [...] } returns 400 (strict reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ gitCredentials: [{ name: 'test', token: 'secret' }] }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { tts: { apiKey: "secret" } } returns 400 (strict reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ tts: { apiKey: 'secret' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { sessionDefaults: { permissionMode: "auto" } } returns 400 (strict reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ sessionDefaults: { permissionMode: 'auto' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { theme: "rainbow" } returns 400 (enum reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ theme: 'rainbow' }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { colorTheme: "not-a-theme" } returns 400 (unknown theme reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ colorTheme: 'not-a-theme' }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toBe('Invalid request body')
})
it('PATCH /api/internal/settings with { tts: { voice: "x", speed: 1.5 } } merges and preserves apiKey/endpoint', async () => {
// Seed a full TTS config (including secrets) directly via settingsService
settingsService.updateSettings({
tts: {
enabled: true,
provider: 'external',
autoPlay: false,
endpoint: 'https://custom.endpoint',
apiKey: 'sk-secret-123',
voice: 'alloy',
model: 'tts-1',
speed: 1.0,
},
} as Partial<UserPreferences>)
// Now patch only non-secret fields via the API
const patchRes = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ tts: { voice: 'nova', speed: 1.5 } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(patchRes.status).toBe(200)
const body = await patchRes.json() as { preferences: { tts: { voice: string; speed: number; apiKey: string; endpoint: string } } }
expect(body.preferences.tts.voice).toBe('nova')
expect(body.preferences.tts.speed).toBe(1.5)
expect(body.preferences.tts.apiKey).toBe('sk-secret-123')
expect(body.preferences.tts.endpoint).toBe('https://custom.endpoint')
})
it('PATCH /api/internal/settings with { tts: { voice: "nova" } } preserves autoPlay and other omitted fields', async () => {
// Seed TTS with autoPlay: true (a non-default value)
settingsService.updateSettings({
tts: {
enabled: true,
provider: 'external',
autoPlay: true,
endpoint: 'https://custom.endpoint',
apiKey: 'sk-secret-123',
voice: 'alloy',
model: 'tts-1',
speed: 1.0,
},
} as Partial<UserPreferences>)
// Patch only voice — autoPlay, provider, model, speed must remain as seeded
const patchRes = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ tts: { voice: 'nova' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(patchRes.status).toBe(200)
const body = await patchRes.json() as { preferences: { tts: { voice: string; autoPlay: boolean; speed: number; model: string } } }
expect(body.preferences.tts.voice).toBe('nova')
expect(body.preferences.tts.autoPlay).toBe(true) // must NOT reset to default false
expect(body.preferences.tts.speed).toBe(1.0) // preserved
expect(body.preferences.tts.model).toBe('tts-1') // preserved
})
it('PATCH /api/internal/settings with { tts: { apiKey: "leak" } } returns 400 (strict reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ tts: { apiKey: 'leak' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { tts: { endpoint: "http://x" } } returns 400 (strict reject)', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ tts: { endpoint: 'http://x' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
})
it('PATCH /api/internal/settings with { stt: { model: "whisper-1" } } preserves non-default language and omitted fields', async () => {
// Seed full STT config with a non-default language
settingsService.updateSettings({
stt: {
enabled: true,
provider: 'builtin',
endpoint: 'https://api.openai.com',
apiKey: 'sk-secret-456',
model: 'whisper-1',
language: 'fr-FR',
},
} as Partial<UserPreferences>)
// Patch only model — language, provider, enabled must remain as seeded
const patchRes = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ stt: { model: 'whisper-2' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(patchRes.status).toBe(200)
const body = await patchRes.json() as { preferences: { stt: { model: string; language: string; provider: string; enabled: boolean } } }
expect(body.preferences.stt.model).toBe('whisper-2')
expect(body.preferences.stt.language).toBe('fr-FR') // must NOT reset to default 'en-US'
expect(body.preferences.stt.provider).toBe('builtin') // preserved
expect(body.preferences.stt.enabled).toBe(true) // preserved
})
it('PATCH /api/internal/settings with { stt: { ... } } when no stt config exists returns 400', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ stt: { enabled: true, language: 'fr-FR' } }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(400)
const body = await res.json() as { error: string }
expect(body.error).toContain('STT is not configured')
})
it('PATCH /api/internal/settings with existing keys (theme) still works after tts/stt additions', async () => {
const res = await app.request('/api/internal/settings', {
method: 'PATCH',
body: JSON.stringify({ theme: 'light', mode: 'plan' }),
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`,
},
})
expect(res.status).toBe(200)
const body = await res.json() as { preferences: { theme: string; mode: string } }
expect(body.preferences.theme).toBe('light')
expect(body.preferences.mode).toBe('plan')
})
})