fix(search): 将搜索历史持久化到应用数据库
This commit is contained in:
@@ -1,34 +1,35 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { beforeEach, expect, it, vi } from 'vitest'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { useSearchStore } from './search'
|
||||
import { search } from '@/services/searchService'
|
||||
|
||||
vi.mock('@/services/searchService', () => ({ search: vi.fn() }))
|
||||
import * as service from '@/services/searchService'
|
||||
vi.mock('@/services/searchService', () => ({ search: vi.fn(), getHistory: vi.fn(), clearHistory: vi.fn() }))
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
setActivePinia(createPinia())
|
||||
vi.mocked(search).mockReset().mockResolvedValue({ results: [], total: 0, mode: 'hybrid' })
|
||||
vi.mocked(service.getHistory).mockReset().mockResolvedValue({ queries: ['saved'] })
|
||||
vi.mocked(service.clearHistory).mockReset().mockResolvedValue({ queries: [] })
|
||||
vi.mocked(service.search).mockReset().mockResolvedValue({ results: [], total: 0, mode: 'hybrid' })
|
||||
})
|
||||
|
||||
it('persists real queries across store recreation, reorders duplicates and clears history', async () => {
|
||||
it('loads application history after recreation and clears through the backend', async () => {
|
||||
await useSearchStore().loadHistory()
|
||||
setActivePinia(createPinia())
|
||||
const store = useSearchStore()
|
||||
await store.loadHistory()
|
||||
expect(store.recentQueries).toEqual(['saved'])
|
||||
await store.clearHistory()
|
||||
expect(service.clearHistory).toHaveBeenCalledOnce()
|
||||
expect(store.recentQueries).toEqual([])
|
||||
await store.doSearch({ query: ' first ' })
|
||||
await store.doSearch({ query: 'second' })
|
||||
await store.doSearch({ query: 'first' })
|
||||
setActivePinia(createPinia())
|
||||
const restored = useSearchStore()
|
||||
expect(restored.recentQueries).toEqual(['first', 'second'])
|
||||
restored.clearHistory()
|
||||
setActivePinia(createPinia())
|
||||
expect(useSearchStore().recentQueries).toEqual([])
|
||||
})
|
||||
|
||||
it('ignores corrupt storage and does not let a stale request overwrite the latest search', async () => {
|
||||
localStorage.setItem('notes-agent.search-history.v1', '{bad')
|
||||
let finish!: (value: Awaited<ReturnType<typeof search>>) => void
|
||||
vi.mocked(search).mockImplementationOnce(() => new Promise(resolve => { finish = resolve }))
|
||||
it('retains history and reports a failed delete', async () => {
|
||||
const store = useSearchStore()
|
||||
await store.loadHistory()
|
||||
vi.mocked(service.clearHistory).mockRejectedValue(new Error('offline'))
|
||||
await store.clearHistory()
|
||||
expect(store.recentQueries).toEqual(['saved'])
|
||||
expect(store.historyError).toBeTruthy()
|
||||
})
|
||||
it('ignores stale search responses and reloads server history', async () => {
|
||||
let finish!: (value: Awaited<ReturnType<typeof service.search>>) => void
|
||||
vi.mocked(service.search).mockImplementationOnce(() => new Promise(resolve => { finish = resolve }))
|
||||
const store = useSearchStore()
|
||||
const first = store.doSearch({ query: 'old' })
|
||||
await store.doSearch({ query: 'new' })
|
||||
@@ -36,5 +37,5 @@ it('ignores corrupt storage and does not let a stale request overwrite the lates
|
||||
await first
|
||||
expect(store.total).toBe(0)
|
||||
expect(store.query).toBe('new')
|
||||
expect(store.recentQueries).toEqual(['new', 'old'])
|
||||
expect(store.recentQueries).toEqual(['saved'])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user