Files
opencode-manager/frontend/src/hooks/useRemoveMessage.test.ts
T
Chris Scott 65b72e55ae refactor: unify model selection with the TUI
Align the manager's model selection with the OpenCode TUI: one
model.json-backed preference store, one provider catalog, one ordering
and default-ownership rule, and one set of availability, ref-formatting
and variant helpers shared across chat, schedules, agents and the goal
auditor.

- Add a shared TUI-compatible model-preference module and make the
  backend model.json store match its format.
- Consolidate the frontend provider catalog, availability check, model
  ref formatter and variant persistence into single owners.
- Drive chat, schedule, agent and goal-auditor model lists from a shared
  useModelSections hook and buildModelSections, following TUI ordering.
- Extract a shared ModelCombobox with a taller list, and list models the
  TUI way in ModelQuickSelect.
- Add a shared isRecord util and model-selection comparison helpers.
2026-10-06 20:44:27 -04:00

155 lines
4.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createElement } from 'react'
import { useRefreshMessage } from './useRemoveMessage'
const mocks = vi.hoisted(() => ({
stageRevert: vi.fn(),
commitRevert: vi.fn(),
sendPrompt: vi.fn(),
switchSessionModel: vi.fn(),
switchSessionAgent: vi.fn(),
}))
vi.mock('@/api/opencode', async () => {
const actual = await vi.importActual('@/api/opencode')
return {
...actual,
stageRevert: mocks.stageRevert,
commitRevert: mocks.commitRevert,
sendPrompt: mocks.sendPrompt,
switchSessionModel: mocks.switchSessionModel,
switchSessionAgent: mocks.switchSessionAgent,
}
})
vi.mock('@/lib/toast', () => ({
showToast: { error: vi.fn(), success: vi.fn() },
}))
const createTestQueryClient = () => new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
const sessionInfo = (overrides: Record<string, unknown> = {}) => ({
id: 'test-session',
projectID: 'proj_1',
time: { created: 1000, updated: 1000 },
location: { directory: '/test' },
agent: 'build',
model: { providerID: 'anthropic', id: 'claude-sonnet-4' },
...overrides,
})
describe('useRefreshMessage', () => {
let queryClient: QueryClient
const renderRefresh = () =>
renderHook(() => useRefreshMessage({ sessionId: 'test-session', directory: '/test' }), {
wrapper: ({ children }) =>
createElement(QueryClientProvider, { client: queryClient }, children),
})
const setSession = (session: Record<string, unknown>) => {
queryClient.setQueryData(['opencode', 'session', 'test-session', '/test'], session)
}
beforeEach(() => {
vi.clearAllMocks()
queryClient = createTestQueryClient()
mocks.stageRevert.mockResolvedValue(undefined)
mocks.commitRevert.mockResolvedValue(undefined)
mocks.sendPrompt.mockResolvedValue({})
mocks.switchSessionModel.mockResolvedValue(undefined)
mocks.switchSessionAgent.mockResolvedValue(undefined)
})
it('resends the edited text without synthesizing an agent attachment or unchanged switches', async () => {
setSession(sessionInfo())
const { result } = renderRefresh()
await result.current.mutateAsync({
assistantMessageID: 'assistant-1',
userMessageContent: 'edited text',
model: 'anthropic/claude-sonnet-4',
agent: 'build',
})
expect(mocks.stageRevert).toHaveBeenCalledWith('test-session', 'assistant-1')
expect(mocks.commitRevert).toHaveBeenCalledWith('test-session')
expect(mocks.sendPrompt).toHaveBeenCalledWith({
sessionID: 'test-session',
text: 'edited text',
})
expect(mocks.switchSessionModel).not.toHaveBeenCalled()
expect(mocks.switchSessionAgent).not.toHaveBeenCalled()
})
it('preserves the session variant when resending with the same full ref', async () => {
setSession(sessionInfo({
model: { providerID: 'anthropic', id: 'claude-sonnet-4', variant: 'high' },
}))
const { result } = renderRefresh()
await result.current.mutateAsync({
assistantMessageID: 'assistant-1',
userMessageContent: 'edited text',
model: 'anthropic/claude-sonnet-4#high',
agent: 'build',
})
expect(mocks.switchSessionModel).not.toHaveBeenCalled()
expect(mocks.switchSessionAgent).not.toHaveBeenCalled()
})
it('switches the model before resending when the edited message model changed', async () => {
setSession(sessionInfo())
const { result } = renderRefresh()
await result.current.mutateAsync({
assistantMessageID: 'assistant-1',
userMessageContent: 'edited text',
model: 'openai/gpt-4',
agent: 'build',
})
expect(mocks.switchSessionModel).toHaveBeenCalledWith('test-session', {
providerID: 'openai',
id: 'gpt-4',
})
expect(mocks.switchSessionAgent).not.toHaveBeenCalled()
expect(mocks.switchSessionModel.mock.invocationCallOrder[0]).toBeLessThan(
mocks.sendPrompt.mock.invocationCallOrder[0],
)
})
it('switches the agent before resending when the selected agent changed', async () => {
setSession(sessionInfo())
const { result } = renderRefresh()
await result.current.mutateAsync({
assistantMessageID: 'assistant-1',
userMessageContent: 'edited text',
model: 'anthropic/claude-sonnet-4',
agent: 'plan',
})
expect(mocks.switchSessionAgent).toHaveBeenCalledWith('test-session', 'plan')
expect(mocks.switchSessionModel).toHaveBeenCalledWith('test-session', {
providerID: 'anthropic',
id: 'claude-sonnet-4',
})
expect(mocks.switchSessionAgent.mock.invocationCallOrder[0]).toBeLessThan(
mocks.switchSessionModel.mock.invocationCallOrder[0],
)
})
})