Files
Hermes-ui/tests/client/profiles-store.test.ts
T
ekko acf4e225e6 feat: rewrite database schema synchronization with automatic recovery (#379)
Complete rewrite of the Hermes SQLite database schema synchronization mechanism
with comprehensive error handling, automatic recovery, and full test coverage.

## Database Schema Synchronization
- **Unified sync mechanism**: Single `syncTable()` function handles all schema changes
- **Automatic column sync**: Adds missing columns and removes extra columns
- **Table rebuilding**: Automatically rebuilds tables when primary keys or types change
- **Data preservation**: Preserves data during schema changes when compatible
- **Index management**: Creates and removes indexes as needed

## Error Recovery & Reliability
- **Automatic backup**: Backs up corrupted database before recovery
- **Retry limiting**: Prevents infinite loops with retry limit
- **Duplicate prevention**: Avoids multiple backup files
- **Safe file operations**: Uses copy+delete instead of rename for safety

## Composite Primary Keys
- Fixed GC_ROOM_AGENTS and GC_ROOM_MEMBERS with proper composite primary keys
- Prevents duplicate entries while allowing same roomId with different agentId/userId

## Test Coverage
- **10 new integration tests** for schema synchronization (tests/server/schema-sync.test.ts)
- **3 updated tests** for Hermes schemas (tests/server/hermes-schemas.test.ts)
- All 327 tests passing (47 test files, 325 passed, 2 skipped)

## Bug Fixes
- Fixed module import issues (unified ES6 imports, removed mixed require())
- Fixed mock issues in sessions routes tests
- Fixed i18n coverage test to handle newly added keys
- Fixed profiles store test to match current implementation

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-01 19:48:46 +08:00

107 lines
3.6 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: '' },
])
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(mockProfilesApi.deleteProfile).toHaveBeenCalledWith('test')
})
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)
})
})