a948eee4b9
* fix(chat): isolate concurrent session events by refactoring WebSocket event handling Refactored the WebSocket event handling mechanism to use global listeners with session-specific event routing instead of per-session listeners. This prevents event cross-talk when multiple chat sessions run concurrently. Key changes: - Client: Added sessionEventHandlers Map to route events to appropriate sessions - Client: Registered global listeners once per socket connection - Server: Extracted message processing logic into handleMessage method - Server: Improved Hermes session ID tracking with dedicated Map - Server: Added replaceByHermesSessionId for targeted message replacement Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test: fix failing tests for mocks and API return types - Fixed sessions-routes.test.ts: added missing setWorkspace and listWorkspaceFolders mocks - Fixed usage-store.test.ts: removed test for non-existent initUsageStore function - Fixed profiles-store.test.ts: corrected createProfile API return type to { success: true } - Fixed syntax error in usageStatsMock (ctx.body: → ctx.body =) All tests now pass (314 passed | 2 skipped). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
115 lines
4.1 KiB
TypeScript
115 lines
4.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { setActivePinia, createPinia } from 'pinia'
|
|
|
|
const mockProfilesApi = vi.hoisted(() => ({
|
|
fetchProfiles: vi.fn(),
|
|
fetchProfileDetail: vi.fn(),
|
|
createProfile: vi.fn(),
|
|
deleteProfile: vi.fn(),
|
|
renameProfile: vi.fn(),
|
|
switchProfile: vi.fn(),
|
|
exportProfile: vi.fn(),
|
|
importProfile: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/api/hermes/profiles', () => mockProfilesApi)
|
|
|
|
import { useProfilesStore } from '@/stores/hermes/profiles'
|
|
|
|
describe('Profiles Store', () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createPinia())
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('fetchProfiles loads profiles and sets active', async () => {
|
|
const profiles = [
|
|
{ name: 'default', active: true, model: 'gpt-4', gateway: 'running', alias: '' },
|
|
{ name: 'dev', active: false, model: 'gpt-4', gateway: 'stopped', alias: '' },
|
|
]
|
|
mockProfilesApi.fetchProfiles.mockResolvedValue(profiles)
|
|
|
|
const store = useProfilesStore()
|
|
await store.fetchProfiles()
|
|
|
|
expect(store.profiles).toEqual(profiles)
|
|
expect(store.activeProfile?.name).toBe('default')
|
|
expect(store.loading).toBe(false)
|
|
})
|
|
|
|
it('fetchProfiles sets loading state', async () => {
|
|
mockProfilesApi.fetchProfiles.mockImplementation(
|
|
() => new Promise(resolve => setTimeout(() => resolve([]), 10))
|
|
)
|
|
|
|
const store = useProfilesStore()
|
|
const fetchPromise = store.fetchProfiles()
|
|
|
|
expect(store.loading).toBe(true)
|
|
await fetchPromise
|
|
expect(store.loading).toBe(false)
|
|
})
|
|
|
|
it('createProfile calls API and refreshes list', async () => {
|
|
mockProfilesApi.createProfile.mockResolvedValue({ success: true })
|
|
mockProfilesApi.fetchProfiles.mockResolvedValue([
|
|
{ name: 'default', active: true, model: 'gpt-4', gateway: 'running', alias: '' },
|
|
{ name: 'new-profile', active: false, model: 'gpt-4', gateway: 'stopped', alias: '' },
|
|
])
|
|
|
|
const store = useProfilesStore()
|
|
const result = await store.createProfile('new-profile', false)
|
|
|
|
expect(result.success).toBe(true)
|
|
expect(mockProfilesApi.createProfile).toHaveBeenCalledWith('new-profile', false)
|
|
expect(store.profiles).toHaveLength(2)
|
|
})
|
|
|
|
it('deleteProfile clears detail cache', async () => {
|
|
mockProfilesApi.deleteProfile.mockResolvedValue(true)
|
|
mockProfilesApi.fetchProfiles.mockResolvedValue([
|
|
{ name: 'default', active: true, model: 'gpt-4', gateway: 'running', alias: '' },
|
|
])
|
|
|
|
window.localStorage.setItem('hermes_sessions_cache_v1_test', '[]')
|
|
window.localStorage.setItem('hermes_session_msgs_v1_test_session-1', '[]')
|
|
window.localStorage.setItem('hermes_in_flight_v1_test_session-1', '{}')
|
|
window.localStorage.setItem('hermes_active_session_test', 'session-1')
|
|
window.localStorage.setItem('hermes_session_pins_v1_test', '[]')
|
|
window.localStorage.setItem('hermes_human_only_v1_test', 'false')
|
|
|
|
const store = useProfilesStore()
|
|
store.detailMap['test'] = { name: 'test', path: '/tmp/test', model: '', provider: '', gateway: '', skills: 0, hasEnv: false, hasSoulMd: false }
|
|
|
|
await store.deleteProfile('test')
|
|
|
|
expect(store.detailMap['test']).toBeUndefined()
|
|
expect(window.localStorage.getItem('hermes_session_pins_v1_test')).toBeNull()
|
|
expect(window.localStorage.getItem('hermes_human_only_v1_test')).toBeNull()
|
|
})
|
|
|
|
it('fetchProfileDetail uses cache', async () => {
|
|
const detail = { name: 'cached', path: '/tmp/cached', model: 'gpt-4', provider: 'openai', gateway: 'running', skills: 5, hasEnv: true, hasSoulMd: false }
|
|
const store = useProfilesStore()
|
|
store.detailMap['cached'] = detail
|
|
|
|
const result = await store.fetchProfileDetail('cached')
|
|
|
|
expect(result).toEqual(detail)
|
|
expect(mockProfilesApi.fetchProfileDetail).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('switchProfile sets switching state', async () => {
|
|
mockProfilesApi.switchProfile.mockResolvedValue(true)
|
|
mockProfilesApi.fetchProfiles.mockResolvedValue([])
|
|
|
|
const store = useProfilesStore()
|
|
const switchPromise = store.switchProfile('dev')
|
|
|
|
expect(store.switching).toBe(true)
|
|
await switchPromise
|
|
expect(store.switching).toBe(false)
|
|
})
|
|
})
|