diff --git a/backend/src/services/settings.ts b/backend/src/services/settings.ts index fa181ff0..7dd427d7 100644 --- a/backend/src/services/settings.ts +++ b/backend/src/services/settings.ts @@ -11,6 +11,8 @@ import type { import { UserPreferencesSchema, DEFAULT_USER_PREFERENCES, + DEFAULT_ACTIVITY_WINDOW_MINUTES, + isActivityWindowMinutes, } from '../types/settings' import { isColorThemeId, MANAGER_COLOR_THEME_ID } from '@opencode-manager/shared/themes' @@ -69,6 +71,10 @@ export class SettingsService { parsed.colorTheme = MANAGER_COLOR_THEME_ID } + if (!isActivityWindowMinutes(parsed.activityWindowMinutes)) { + parsed.activityWindowMinutes = DEFAULT_ACTIVITY_WINDOW_MINUTES + } + const validated = UserPreferencesSchema.parse({ ...DEFAULT_USER_PREFERENCES, ...parsed, diff --git a/backend/test/routes/internal-settings.test.ts b/backend/test/routes/internal-settings.test.ts index 75f5bf90..6216c01c 100644 --- a/backend/test/routes/internal-settings.test.ts +++ b/backend/test/routes/internal-settings.test.ts @@ -181,6 +181,39 @@ describe('internal/settings routes', () => { expect(body.error).toBe('Invalid request body') }) + it('PATCH /api/internal/settings with { activityWindowMinutes: 45 } returns 400 (non-option reject)', async () => { + const res = await app.request('/api/internal/settings', { + method: 'PATCH', + body: JSON.stringify({ activityWindowMinutes: 45 }), + 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 { activityWindowMinutes: 60 } persists an allowed option', async () => { + const patchRes = await app.request('/api/internal/settings', { + method: 'PATCH', + body: JSON.stringify({ activityWindowMinutes: 60 }), + 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: { activityWindowMinutes: number } } + expect(body.preferences.activityWindowMinutes).toBe(60) + }) + 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({ diff --git a/backend/test/routes/settings.test.ts b/backend/test/routes/settings.test.ts index 7ec0ef0e..bc9d2f3e 100644 --- a/backend/test/routes/settings.test.ts +++ b/backend/test/routes/settings.test.ts @@ -1519,6 +1519,41 @@ describe('Settings Routes - OpenCode Upgrade', () => { }) }) + describe('PATCH / - activity window validation', () => { + it.each([5, 45, 2000, 30.5])('rejects activityWindowMinutes %s with 400', async (minutes) => { + const res = await settingsApp.fetch(new Request('http://localhost/', { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ preferences: { activityWindowMinutes: minutes } }), + })) + const json = await res.json() as { error: string } + + expect(res.status).toBe(400) + expect(json.error).toBe('Invalid settings data') + expect(mockUpdateSettings).not.toHaveBeenCalled() + }) + + it('accepts an allowed activityWindowMinutes value', async () => { + mockGetSettings.mockReturnValue({ + preferences: { activityWindowMinutes: 30 }, + updatedAt: 1, + }) + mockUpdateSettings.mockReturnValue({ + preferences: { activityWindowMinutes: 60 }, + updatedAt: 2, + }) + + const res = await settingsApp.fetch(new Request('http://localhost/', { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ preferences: { activityWindowMinutes: 60 } }), + })) + + expect(res.status).toBe(200) + expect(mockUpdateSettings).toHaveBeenCalledWith({ activityWindowMinutes: 60 }, 'default') + }) + }) + describe('PATCH / - sandbox enable guard', () => { afterEach(() => { forceProcessAttestation(null) diff --git a/backend/test/services/settings.test.ts b/backend/test/services/settings.test.ts new file mode 100644 index 00000000..ca25e2da --- /dev/null +++ b/backend/test/services/settings.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it, beforeEach } from 'vitest' +import { Database } from 'bun:sqlite' +import { DEFAULT_ACTIVITY_WINDOW_MINUTES } from '@opencode-manager/shared/schemas' +import { SettingsService } from '../../src/services/settings' +import { allMigrations } from '../../src/db/migrations' +import { migrate } from '../../src/db/migration-runner' + +describe('SettingsService', () => { + let db: Database + let settingsService: SettingsService + + beforeEach(() => { + db = new Database(':memory:') + migrate(db, allMigrations) + settingsService = new SettingsService(db) + }) + + it('recovers from a stored activityWindowMinutes outside the allowed options without losing other preferences', () => { + db.prepare( + `INSERT INTO user_preferences (user_id, preferences, updated_at) + VALUES (?, ?, ?)` + ).run('default', JSON.stringify({ activityWindowMinutes: 5, autoScroll: false }), Date.now()) + + const settings = settingsService.getSettings() + + expect(settings.preferences.activityWindowMinutes).toBe(DEFAULT_ACTIVITY_WINDOW_MINUTES) + expect(settings.preferences.autoScroll).toBe(false) + }) +}) diff --git a/backend/test/shared/activity-window-minutes-schema.test.ts b/backend/test/shared/activity-window-minutes-schema.test.ts new file mode 100644 index 00000000..400685a6 --- /dev/null +++ b/backend/test/shared/activity-window-minutes-schema.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { + ACTIVITY_WINDOW_MINUTE_OPTIONS, + ActivityWindowMinutesSchema, + UserPreferencesSchema, +} from '@opencode-manager/shared/schemas' + +describe('ActivityWindowMinutesSchema', () => { + it.each(ACTIVITY_WINDOW_MINUTE_OPTIONS)('accepts the configured option %s', (minutes) => { + expect(ActivityWindowMinutesSchema.safeParse(minutes).success).toBe(true) + }) + + it.each([5, 45, 2000, 30.5])('rejects %s', (minutes) => { + expect(ActivityWindowMinutesSchema.safeParse(minutes).success).toBe(false) + }) + + it('rejects a non-option value through UserPreferencesSchema', () => { + expect(UserPreferencesSchema.shape.activityWindowMinutes.safeParse(45).success).toBe(false) + expect(UserPreferencesSchema.shape.activityWindowMinutes.safeParse(60).success).toBe(true) + }) +}) diff --git a/frontend/src/api/opencode.test.ts b/frontend/src/api/opencode.test.ts index 0d9d01ed..03ec96c1 100644 --- a/frontend/src/api/opencode.test.ts +++ b/frontend/src/api/opencode.test.ts @@ -125,6 +125,19 @@ describe('OpenCode facade', () => { 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') })) diff --git a/frontend/src/api/opencode.ts b/frontend/src/api/opencode.ts index 0630b25d..fd5220b5 100644 --- a/frontend/src/api/opencode.ts +++ b/frontend/src/api/opencode.ts @@ -48,6 +48,7 @@ export interface SessionPageInput { order?: 'asc' | 'desc' search?: string cursor?: string + includeChildren?: boolean } export interface CreateSessionInput { @@ -102,7 +103,7 @@ export async function listSessionPage(input: SessionPageInput): Promise{repoLine} - {formatShortRelativeTime(new Date(item.session.time.updated))} + {formatShortRelativeTime(new Date(item.updatedAt))} ) @@ -109,7 +109,7 @@ function CompactActivityRow({ item, isCurrent, onSelect }: ActivityRowProps) { {getRepoDisplayName(item.repo)} - {formatShortRelativeTime(new Date(item.session.time.updated))} + {formatShortRelativeTime(new Date(item.updatedAt))} ) diff --git a/frontend/src/components/navigation/MoreDrawer.test.tsx b/frontend/src/components/navigation/MoreDrawer.test.tsx index c67cd899..95048ef7 100644 --- a/frontend/src/components/navigation/MoreDrawer.test.tsx +++ b/frontend/src/components/navigation/MoreDrawer.test.tsx @@ -37,6 +37,7 @@ function createActiveWorkItem(): ActiveWorkItem { return { key: '1:s1', state: 'recent', + updatedAt: now, path: '/repos/1/sessions/s1', session: { id: 's1', diff --git a/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx b/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx index 159d41e1..beb8e825 100644 --- a/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx +++ b/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx @@ -105,12 +105,14 @@ function createRepo(id: number, name: string, fullPath: string) { } function createActiveWorkItem(): ActiveWorkItem { + const session = createSession('s1', Date.now(), '/path/to/repo1') return { key: '1:s1', state: 'recent', - path: '/repos/1/sessions/s1', - session: createSession('s1', Date.now(), '/path/to/repo1'), + session, repo: createRepo(1, 'repo1', '/path/to/repo1'), + updatedAt: session.time.updated, + path: '/repos/1/sessions/s1', } } diff --git a/frontend/src/components/navigation/ToolSidePanel.test.tsx b/frontend/src/components/navigation/ToolSidePanel.test.tsx index c174ea34..235af6ad 100644 --- a/frontend/src/components/navigation/ToolSidePanel.test.tsx +++ b/frontend/src/components/navigation/ToolSidePanel.test.tsx @@ -127,6 +127,7 @@ function createActivityItem(id: string, title: string, repoId: number, repoName: session, repo, state: 'recent', + updatedAt: session.time.updated, path: `/repos/${repoId}/sessions/${id}`, } } diff --git a/frontend/src/components/session/SessionPickerDialog.test.tsx b/frontend/src/components/session/SessionPickerDialog.test.tsx index d6dbea48..bc3eb83f 100644 --- a/frontend/src/components/session/SessionPickerDialog.test.tsx +++ b/frontend/src/components/session/SessionPickerDialog.test.tsx @@ -131,11 +131,13 @@ function activeWorkItem( state: ActiveWorkState = 'recent', ): ActiveWorkItem { const itemRepo = mocks.repos.find((candidate) => candidate.fullPath === directory) ?? mocks.repos[0] + const itemSession = session(id, title, directory) return { key: `${directory}:${id}`, - session: session(id, title, directory), + session: itemSession, repo: itemRepo, state, + updatedAt: itemSession.time.updated, path: `/repos/${itemRepo.id}/sessions/${id}`, } } diff --git a/frontend/src/components/session/SessionPickerDialog.tsx b/frontend/src/components/session/SessionPickerDialog.tsx index 54a4c5d7..677c7350 100644 --- a/frontend/src/components/session/SessionPickerDialog.tsx +++ b/frontend/src/components/session/SessionPickerDialog.tsx @@ -72,6 +72,7 @@ interface SessionPickerRow { key: string session: Session repo: Repo + updatedAt?: number } interface SessionPickerSection { @@ -262,7 +263,7 @@ const SessionPickerRowItem = memo(function SessionPickerRowItem({ > {showRepo && {getRepoDisplayName(row.repo)}} {!showRepo && label && {label}} - {formatShortRelativeTime(new Date(row.session.time.updated))} + {formatShortRelativeTime(new Date(row.updatedAt ?? row.session.time.updated))}
@@ -811,7 +812,7 @@ function ActivePickerContent({ const rows: SessionPickerRow[] = [] for (const item of groups[definition.key]) { if (needle && !(item.session.title ?? '').toLowerCase().includes(needle)) continue - rows.push({ key: item.key, session: item.session, repo: item.repo }) + rows.push({ key: item.key, session: item.session, repo: item.repo, updatedAt: item.updatedAt }) } return rows.length > 0 ? [{ id: definition.key, heading: definition.heading, rows }] : [] }) diff --git a/frontend/src/components/settings/ActivitySettings.test.tsx b/frontend/src/components/settings/ActivitySettings.test.tsx index 8f461245..8bc67cb5 100644 --- a/frontend/src/components/settings/ActivitySettings.test.tsx +++ b/frontend/src/components/settings/ActivitySettings.test.tsx @@ -49,13 +49,6 @@ describe('ActivitySettings', () => { expect(screen.getByRole('combobox', { name: 'Recent activity window' })).toHaveTextContent('2 hours') }) - it('shows a stored window that is not a preset option', () => { - mockUseSettings({ preferences: { ...basePreferences, activityWindowMinutes: 45 } }) - render() - - expect(screen.getByRole('combobox', { name: 'Recent activity window' })).toHaveTextContent('45 minutes') - }) - it('saves the chosen window in minutes', async () => { const user = userEvent.setup() const updateSettings = vi.fn() diff --git a/frontend/src/components/settings/ActivitySettings.tsx b/frontend/src/components/settings/ActivitySettings.tsx index 8e1f1e09..0c8bbae6 100644 --- a/frontend/src/components/settings/ActivitySettings.tsx +++ b/frontend/src/components/settings/ActivitySettings.tsx @@ -1,4 +1,3 @@ -import { useMemo } from 'react' import { useSettings } from '@/hooks/useSettings' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' @@ -9,15 +8,6 @@ export function ActivitySettings() { const { preferences, updateSettings } = useSettings() const activityWindowMinutes = preferences?.activityWindowMinutes ?? DEFAULT_ACTIVITY_WINDOW_MINUTES - const windowOptions = useMemo(() => { - const options: number[] = [...ACTIVITY_WINDOW_MINUTE_OPTIONS] - if (!options.includes(activityWindowMinutes)) { - options.push(activityWindowMinutes) - options.sort((a, b) => a - b) - } - return options - }, [activityWindowMinutes]) - return (

Activity

@@ -31,13 +21,18 @@ export function ActivitySettings() {