fix: openrouter unselect itself (#5943)

* fix: selected openrouter model does not work

* test: add tests to cover new change
This commit is contained in:
Louis 2025-07-28 10:33:23 +07:00 committed by GitHub
parent 08af8a49aa
commit fdaa3b1992
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 92 additions and 50 deletions

View File

@ -11,15 +11,22 @@ vi.mock('@/services/threads', () => ({
// Mock ulid
vi.mock('ulidx', () => ({
ulid: vi.fn(() => 'test-ulid-123')
ulid: vi.fn(() => 'test-ulid-123'),
}))
// Mock fzf
vi.mock('fzf', () => ({
Fzf: vi.fn(() => ({
find: vi.fn(() => [])
}))
find: vi.fn(() => []),
})),
}))
global.__TAURI_INTERNALS__ = {
plugins: {
path: {
sep: '/',
},
},
}
describe('useThreads', () => {
beforeEach(() => {
@ -29,7 +36,7 @@ describe('useThreads', () => {
useThreads.setState({
threads: {},
currentThreadId: undefined,
searchIndex: null
searchIndex: null,
})
})
})
@ -47,7 +54,7 @@ describe('useThreads', () => {
const threads = [
{ id: 'thread1', title: 'Thread 1', messages: [] },
{ id: 'thread2', title: 'Thread 2', messages: [] }
{ id: 'thread2', title: 'Thread 2', messages: [] },
]
act(() => {
@ -58,6 +65,34 @@ describe('useThreads', () => {
expect(result.current.threads['thread1']).toEqual(threads[0])
expect(result.current.threads['thread2']).toEqual(threads[1])
})
it('should set threads with cortex model migrated', () => {
const { result } = renderHook(() => useThreads())
const threads = [
{
id: 'thread1',
title: 'Thread 1',
messages: [],
model: { provider: 'llama.cpp', id: 'thread1:free' },
},
{
id: 'thread2',
title: 'Thread 2',
messages: [],
model: { provider: 'llama.cpp', id: 'thread2:test' },
},
]
act(() => {
result.current.setThreads(threads)
})
expect(Object.keys(result.current.threads)).toHaveLength(2)
expect(result.current.threads['thread1'].model.id).toEqual('thread1:free')
expect(result.current.threads['thread1'].model.provider).toEqual('llamacpp')
expect(result.current.threads['thread2'].model.id).toEqual('thread2/test')
expect(result.current.threads['thread2'].model.provider).toEqual('llamacpp')
})
it('should set current thread ID', () => {
const { result } = renderHook(() => useThreads())
@ -106,7 +141,7 @@ describe('useThreads', () => {
const threads = [
{ id: 'thread1', title: 'Thread 1', messages: [] },
{ id: 'thread2', title: 'Thread 2', messages: [] }
{ id: 'thread2', title: 'Thread 2', messages: [] },
]
act(() => {
@ -143,7 +178,12 @@ describe('useThreads', () => {
it('should toggle favorite', () => {
const { result } = renderHook(() => useThreads())
const thread = { id: 'thread1', title: 'Thread 1', messages: [], starred: false }
const thread = {
id: 'thread1',
title: 'Thread 1',
messages: [],
starred: false,
}
act(() => {
result.current.setThreads([thread])
@ -171,7 +211,7 @@ describe('useThreads', () => {
const threads = [
{ id: 'thread1', title: 'Thread 1', messages: [] },
{ id: 'thread2', title: 'Thread 2', messages: [] }
{ id: 'thread2', title: 'Thread 2', messages: [] },
]
act(() => {
@ -215,7 +255,7 @@ describe('useThreads', () => {
const threads = [
{ id: 'thread1', title: 'Thread 1', messages: [] },
{ id: 'thread2', title: 'Thread 2', messages: [] }
{ id: 'thread2', title: 'Thread 2', messages: [] },
]
act(() => {

View File

@ -44,7 +44,9 @@ export const useThreads = create<ThreadState>()((set, get) => ({
'llamacpp'
),
// Cortex migration: take first two parts of the ID (the last is file name which is not needed)
id: thread.model?.id.split(':').slice(0, 2).join(sep()),
id: !thread.model?.id.endsWith(':free')
? thread.model?.id.split(':').slice(0, 2).join(sep())
: thread.model?.id,
}
: undefined,
}