mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 10: Phase 10 — Walkthrough model setting (frontend)
This commit is contained in:
1 parent
229b16cd86
commit
df1891a52d
4 files changed
+206
No files matched your search
@@ -70,6 +70,9 @@ describe('GeneralSettings', () => {
|
||||
expect(screen.getByRole('switch', { name: 'Show reasoning' })).not.toBeChecked()
|
||||
expect(screen.getByRole('switch', { name: 'Expand tool calls' })).not.toBeChecked()
|
||||
expect(screen.getByRole('switch', { name: 'Expand diffs' })).toBeChecked()
|
||||
|
||||
expect(screen.getByRole('heading', { name: 'Change walkthrough' })).toBeInTheDocument()
|
||||
expect(screen.getByText('Walkthrough model')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('writes the theme and toggle preferences', async () => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Label } from '@/components/ui/label'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { SessionAutomationSettings } from './SessionAutomationSettings'
|
||||
import { WalkthroughSettings } from './WalkthroughSettings'
|
||||
|
||||
export function GeneralSettings() {
|
||||
const { preferences, isLoading, updateSettings, isUpdating } = useSettings()
|
||||
@@ -196,6 +197,8 @@ export function GeneralSettings() {
|
||||
)}
|
||||
|
||||
<SessionAutomationSettings />
|
||||
|
||||
<WalkthroughSettings />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest'
|
||||
import { act, render, screen, fireEvent } from '@testing-library/react'
|
||||
import { WalkthroughSettings } from './WalkthroughSettings'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import type { UserPreferences } from '@/api/types/settings'
|
||||
import type { Provider } from '@/api/providers'
|
||||
import { createUseSettingsMock } from '@/test/test-utils'
|
||||
|
||||
vi.mock('@/hooks/useSettings')
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
useProviders: vi.fn(),
|
||||
useOpenCodeModelState: vi.fn(),
|
||||
useOpenCodeDefaultModel: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/hooks/useProviders', () => ({ useProviders: mocks.useProviders }))
|
||||
vi.mock('@/hooks/useModelSelection', () => ({
|
||||
useOpenCodeModelState: mocks.useOpenCodeModelState,
|
||||
useOpenCodeDefaultModel: mocks.useOpenCodeDefaultModel,
|
||||
}))
|
||||
|
||||
const providers: Provider[] = [
|
||||
{
|
||||
id: 'anthropic',
|
||||
name: 'Anthropic',
|
||||
models: [
|
||||
{ id: 'claude-sonnet-4', key: 'claude-sonnet-4', name: 'Claude Sonnet 4', released: 0, free: false },
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
const basePreferences: UserPreferences = {
|
||||
theme: 'dark',
|
||||
mode: 'build',
|
||||
autoScroll: true,
|
||||
expandDiffs: true,
|
||||
expandToolCalls: false,
|
||||
showReasoning: false,
|
||||
simpleChatMode: false,
|
||||
keyboardShortcuts: {},
|
||||
customCommands: [],
|
||||
}
|
||||
|
||||
function mockUseSettings(overrides: Partial<ReturnType<typeof useSettings>> = {}) {
|
||||
vi.mocked(useSettings).mockReturnValue(createUseSettingsMock({ preferences: basePreferences, ...overrides }))
|
||||
}
|
||||
|
||||
describe('WalkthroughSettings', () => {
|
||||
beforeAll(() => {
|
||||
Element.prototype.hasPointerCapture ??= () => false
|
||||
Element.prototype.setPointerCapture ??= () => {}
|
||||
Element.prototype.releasePointerCapture ??= () => {}
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
mocks.useProviders.mockReturnValue({ data: { providers, models: [] } })
|
||||
mocks.useOpenCodeModelState.mockReturnValue({ data: { favorite: [], recent: [], variant: {} } })
|
||||
mocks.useOpenCodeDefaultModel.mockReturnValue({ data: null })
|
||||
})
|
||||
|
||||
it('saves a chosen model after the debounce', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const updateSettings = vi.fn()
|
||||
mockUseSettings({ updateSettings })
|
||||
render(<WalkthroughSettings />)
|
||||
|
||||
fireEvent.focus(screen.getByLabelText('Walkthrough model'))
|
||||
fireEvent.click(screen.getByRole('option', { name: /Claude Sonnet 4/ }))
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(800)
|
||||
})
|
||||
|
||||
expect(updateSettings).toHaveBeenCalledTimes(1)
|
||||
expect(updateSettings).toHaveBeenCalledWith({ walkthroughModel: 'anthropic/claude-sonnet-4' })
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('clears the walkthrough model to an empty string', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const updateSettings = vi.fn()
|
||||
mockUseSettings({
|
||||
preferences: { ...basePreferences, walkthroughModel: 'anthropic/claude-sonnet-4' },
|
||||
updateSettings,
|
||||
})
|
||||
render(<WalkthroughSettings />)
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Clear' }))
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(800)
|
||||
})
|
||||
|
||||
expect(updateSettings).toHaveBeenCalledTimes(1)
|
||||
expect(updateSettings).toHaveBeenCalledWith({ walkthroughModel: '' })
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('does not save an unchanged value', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const updateSettings = vi.fn()
|
||||
mockUseSettings({
|
||||
preferences: { ...basePreferences, walkthroughModel: 'anthropic/claude-sonnet-4' },
|
||||
updateSettings,
|
||||
})
|
||||
render(<WalkthroughSettings />)
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(800)
|
||||
})
|
||||
|
||||
expect(updateSettings).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
|
||||
it('does not save when no model is stored and the field is empty', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const updateSettings = vi.fn()
|
||||
mockUseSettings({ updateSettings })
|
||||
render(<WalkthroughSettings />)
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(800)
|
||||
})
|
||||
|
||||
expect(updateSettings).not.toHaveBeenCalled()
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { ModelCombobox } from '@/components/model/ModelCombobox'
|
||||
|
||||
const AUTOSAVE_DELAY_MS = 800
|
||||
|
||||
export function WalkthroughSettings() {
|
||||
const { preferences, updateSettings } = useSettings()
|
||||
const storedWalkthroughModel = preferences?.walkthroughModel
|
||||
|
||||
const [walkthroughModel, setWalkthroughModel] = useState(storedWalkthroughModel ?? '')
|
||||
const committed = useRef(storedWalkthroughModel)
|
||||
|
||||
useEffect(() => {
|
||||
setWalkthroughModel(storedWalkthroughModel ?? '')
|
||||
committed.current = storedWalkthroughModel
|
||||
}, [storedWalkthroughModel])
|
||||
|
||||
const commitWalkthroughModel = useCallback(() => {
|
||||
const next = walkthroughModel.trim()
|
||||
if (next === (committed.current ?? '')) return
|
||||
committed.current = next
|
||||
updateSettings({ walkthroughModel: next })
|
||||
}, [walkthroughModel, updateSettings])
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
commitWalkthroughModel()
|
||||
}, AUTOSAVE_DELAY_MS)
|
||||
|
||||
return () => clearTimeout(timer)
|
||||
}, [commitWalkthroughModel])
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<h2 className="text-lg font-semibold text-foreground">Change walkthrough</h2>
|
||||
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between sm:gap-4">
|
||||
<div className="min-w-0 space-y-0.5">
|
||||
<Label htmlFor="walkthroughModel">Walkthrough model</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Model that writes change walkthroughs. A small, fast model works well. Leave empty to use the session's model, or the OpenCode default.
|
||||
</p>
|
||||
</div>
|
||||
<ModelCombobox
|
||||
id="walkthroughModel"
|
||||
ariaLabel="Walkthrough model"
|
||||
value={walkthroughModel}
|
||||
onChange={setWalkthroughModel}
|
||||
placeholder="Session model"
|
||||
allowCustomValue
|
||||
showClear
|
||||
className="w-full shrink-0 sm:w-64"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user