mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
- Vendor the 36 OpenCode theme palettes into the shared package, synced from the pinned OpenCode release tag via pnpm themes:sync. - Add a colorTheme user preference across the settings schema, defaults, and the assistant internal API. - Extend palettes with success, warning, info, and diff colors, and add a resolver that maps palettes to CSS tokens and applies them to the DOM. - Apply the theme app-wide through a ThemeProvider context, adopting the tokens in message, repo, session, and settings components, agent colors, git status styles, and diff viewers. - Add a theme picker to General settings and make model quick-select follow the active theme (fixes #355).
329 lines
13 KiB
TypeScript
329 lines
13 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'bun:test'
|
|
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))
|
|
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 { 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')
|
|
})
|
|
})
|