import { beforeEach, describe, expect, it, vi } from 'vitest' const listConversationSummariesFromDbMock = vi.fn() const getConversationDetailFromDbMock = vi.fn() const listConversationSummariesMock = vi.fn() const getConversationDetailMock = vi.fn() const getSessionDetailFromDbMock = vi.fn() const getExactSessionDetailFromDbWithProfileMock = vi.fn() const getUsageStatsFromDbMock = vi.fn() const getSessionMock = vi.fn() const deleteHermesSessionForProfileMock = vi.fn() const localListSessionsMock = vi.fn() const localGetSessionDetailMock = vi.fn() const localSearchSessionsMock = vi.fn() const localDeleteSessionMock = vi.fn() const localRenameSessionMock = vi.fn() const localCreateSessionMock = vi.fn() const localUpdateSessionMock = vi.fn() const getGroupChatServerMock = vi.fn() const getLocalUsageStatsMock = vi.fn() const getActiveProfileNameMock = vi.fn() const loggerWarnMock = vi.fn() const getCompressionSnapshotMock = vi.fn() vi.mock('../../packages/server/src/db/hermes/conversations-db', () => ({ listConversationSummariesFromDb: listConversationSummariesFromDbMock, getConversationDetailFromDb: getConversationDetailFromDbMock, })) vi.mock('../../packages/server/src/services/hermes/conversations', () => ({ listConversationSummaries: listConversationSummariesMock, getConversationDetail: getConversationDetailMock, })) vi.mock('../../packages/server/src/services/logger', () => ({ logger: { warn: loggerWarnMock, error: vi.fn(), }, })) vi.mock('../../packages/server/src/services/hermes/hermes-cli', () => ({ listSessions: vi.fn(), getSession: getSessionMock, deleteSession: vi.fn(), deleteSessionForProfile: deleteHermesSessionForProfileMock, renameSession: vi.fn(), })) vi.mock('../../packages/server/src/db/hermes/sessions-db', () => ({ listSessionSummaries: vi.fn(), searchSessionSummaries: vi.fn(), getSessionDetailFromDb: getSessionDetailFromDbMock, getExactSessionDetailFromDbWithProfile: getExactSessionDetailFromDbWithProfileMock, getUsageStatsFromDb: getUsageStatsFromDbMock, })) vi.mock('../../packages/server/src/db/hermes/session-store', () => ({ listSessions: localListSessionsMock, searchSessions: localSearchSessionsMock, getSessionDetail: localGetSessionDetailMock, deleteSession: localDeleteSessionMock, renameSession: localRenameSessionMock, createSession: localCreateSessionMock, getSession: getSessionMock, updateSession: localUpdateSessionMock, })) vi.mock('../../packages/server/src/db/hermes/usage-store', () => ({ deleteUsage: vi.fn(), getUsage: vi.fn(), getUsageBatch: vi.fn(), getLocalUsageStats: getLocalUsageStatsMock, })) vi.mock('../../packages/server/src/routes/hermes/group-chat', () => ({ getGroupChatServer: getGroupChatServerMock, })) vi.mock('../../packages/server/src/services/hermes/model-context', () => ({ getModelContextLength: vi.fn(), })) vi.mock('../../packages/server/src/services/hermes/hermes-profile', () => ({ getActiveProfileName: getActiveProfileNameMock, listProfileNamesFromDisk: () => ['default', 'travel'], })) vi.mock('../../packages/server/src/db/hermes/compression-snapshot', () => ({ getCompressionSnapshot: getCompressionSnapshotMock, })) vi.mock('../../packages/server/src/lib/context-compressor/export-compressor', () => ({ ExportCompressor: class { async compress(messages: any[]) { return { messages, meta: { totalMessages: messages.length, compressed: true, llmCompressed: true, summaryTokenEstimate: 100, verbatimCount: 0, compressedStartIndex: -1 }, } } }, })) describe('session conversations controller', () => { beforeEach(() => { vi.resetModules() listConversationSummariesFromDbMock.mockReset() getConversationDetailFromDbMock.mockReset() listConversationSummariesMock.mockReset() getConversationDetailMock.mockReset() getSessionDetailFromDbMock.mockReset() getExactSessionDetailFromDbWithProfileMock.mockReset() getUsageStatsFromDbMock.mockReset() getSessionMock.mockReset() deleteHermesSessionForProfileMock.mockReset() localListSessionsMock.mockReset() localGetSessionDetailMock.mockReset() localSearchSessionsMock.mockReset() localDeleteSessionMock.mockReset() localRenameSessionMock.mockReset() localCreateSessionMock.mockReset() localUpdateSessionMock.mockReset() getGroupChatServerMock.mockReset() getGroupChatServerMock.mockReturnValue(null) getLocalUsageStatsMock.mockReset() getActiveProfileNameMock.mockReset() getActiveProfileNameMock.mockReturnValue('default') loggerWarnMock.mockReset() getCompressionSnapshotMock.mockReset() }) it('lists conversations from the local session store', async () => { localListSessionsMock.mockReturnValue([{ id: 'local-conversation', source: 'cli', model: 'gpt-5', title: 'Local', started_at: 1, ended_at: null, last_active: Math.floor(Date.now() / 1000), message_count: 2, tool_call_count: 0, input_tokens: 1, output_tokens: 2, cache_read_tokens: 0, cache_write_tokens: 0, reasoning_tokens: 0, billing_provider: null, estimated_cost_usd: 0, actual_cost_usd: null, cost_status: '', preview: 'preview', workspace: null, }]) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { query: { humanOnly: 'true', limit: '5' }, body: null } await mod.listConversations(ctx) expect(localListSessionsMock).toHaveBeenCalledWith('default', undefined, 5) expect(listConversationSummariesMock).not.toHaveBeenCalled() expect(ctx.body.sessions[0]).toMatchObject({ id: 'local-conversation', source: 'cli', title: 'Local' }) }) it('propagates local session store errors for conversation summaries', async () => { localListSessionsMock.mockImplementation(() => { throw new Error('db unavailable') }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { query: { humanOnly: 'false' }, body: null } await expect(mod.listConversations(ctx)).rejects.toThrow('db unavailable') }) it('gets conversation messages from the local session store', async () => { localGetSessionDetailMock.mockReturnValue({ id: 'root', messages: [ { id: 1, session_id: 'root', role: 'user', content: 'hello', timestamp: 1 }, { id: 2, session_id: 'root', role: 'command', content: '/usage', timestamp: 2 }, ], }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'root' }, query: { humanOnly: 'true' }, body: null } await mod.getConversationMessages(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('root') expect(getConversationDetailMock).not.toHaveBeenCalled() expect(ctx.body).toEqual({ session_id: 'root', messages: [{ id: 1, session_id: 'root', role: 'user', content: 'hello', timestamp: 1 }], visible_count: 1, thread_session_count: 1, }) }) it('returns 404 when local conversation detail is missing', async () => { localGetSessionDetailMock.mockReturnValue(null) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'root' }, query: { humanOnly: 'false' }, body: null } await mod.getConversationMessages(ctx) expect(ctx.status).toBe(404) expect(ctx.body).toEqual({ error: 'Conversation not found' }) }) it('prefers local session detail for Hermes history detail when available', async () => { localGetSessionDetailMock.mockReturnValue({ id: 'cli-1', source: 'cli', title: 'Local complete', messages: [ { id: 1, session_id: 'cli-1', role: 'user', content: 'local full message', timestamp: 1 }, ], }) getSessionDetailFromDbMock.mockResolvedValue({ id: 'cli-1', source: 'cli', title: 'Hermes incomplete', messages: [], }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'cli-1' }, body: null } await mod.getHermesSession(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('cli-1') expect(getSessionDetailFromDbMock).not.toHaveBeenCalled() expect(getSessionMock).not.toHaveBeenCalled() expect(ctx.body.session).toMatchObject({ id: 'cli-1', title: 'Local complete', messages: [{ content: 'local full message' }], }) }) it('falls back to Hermes state.db when local history detail is missing', async () => { localGetSessionDetailMock.mockReturnValue(null) getSessionDetailFromDbMock.mockResolvedValue({ id: 'hermes-1', source: 'cli', title: 'Hermes detail', messages: [ { id: 1, session_id: 'hermes-1', role: 'user', content: 'from hermes', timestamp: 1 }, ], }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'hermes-1' }, body: null } await mod.getHermesSession(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('hermes-1') expect(getSessionDetailFromDbMock).toHaveBeenCalledWith('hermes-1') expect(getSessionMock).not.toHaveBeenCalled() expect(ctx.body.session).toMatchObject({ id: 'hermes-1', title: 'Hermes detail', messages: [{ content: 'from hermes' }], }) }) it('does not return api_server sessions from the Hermes history detail endpoint', async () => { localGetSessionDetailMock.mockReturnValue({ id: 'api-1', source: 'api_server', title: 'API Server', messages: [{ id: 1, session_id: 'api-1', role: 'user', content: 'local api', timestamp: 1 }], }) getSessionDetailFromDbMock.mockResolvedValue(null) getSessionMock.mockResolvedValue(null) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'api-1' }, body: null } await mod.getHermesSession(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('api-1') expect(getSessionDetailFromDbMock).toHaveBeenCalledWith('api-1') expect(ctx.status).toBe(404) expect(ctx.body).toEqual({ error: 'Session not found' }) }) it('returns native state.db usage analytics for the requested period', async () => { const today = new Date().toISOString().slice(0, 10) getLocalUsageStatsMock.mockReturnValue({ input_tokens: 10, output_tokens: 5, cache_read_tokens: 2, cache_write_tokens: 1, reasoning_tokens: 3, sessions: 1, by_model: [ { model: 'local-model', input_tokens: 10, output_tokens: 5, cache_read_tokens: 2, cache_write_tokens: 1, reasoning_tokens: 3, sessions: 1 }, ], by_day: [ { date: today, input_tokens: 10, output_tokens: 5, cache_read_tokens: 2, cache_write_tokens: 1, sessions: 1, errors: 0, cost: 0 }, ], }) getUsageStatsFromDbMock.mockResolvedValue({ input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, reasoning_tokens: 6, sessions: 2, cost: 0.02, total_api_calls: 7, by_model: [ { model: 'hermes-model', input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, reasoning_tokens: 6, sessions: 2 }, ], by_day: [ { date: today, input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, sessions: 2, errors: 0, cost: 0.02 }, ], }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { query: { days: '2' }, body: null } await mod.usageStats(ctx) expect(getLocalUsageStatsMock).not.toHaveBeenCalled() expect(getUsageStatsFromDbMock).toHaveBeenCalledWith(2) expect(ctx.body).toMatchObject({ total_input_tokens: 20, total_output_tokens: 10, total_cache_read_tokens: 4, total_cache_write_tokens: 2, total_reasoning_tokens: 6, total_sessions: 2, total_cost: 0.02, total_api_calls: 7, period_days: 2, }) expect(ctx.body.model_usage).toEqual([ { model: 'hermes-model', input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, reasoning_tokens: 6, sessions: 2 }, ]) expect(ctx.body.daily_usage.find((row: any) => row.date === today)).toMatchObject({ input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, sessions: 2, cost: 0.02, }) }) it('keeps blank model usage as returned by state.db analytics', async () => { getLocalUsageStatsMock.mockReturnValue({ input_tokens: 3, output_tokens: 1, cache_read_tokens: 2, cache_write_tokens: 0, reasoning_tokens: 0, sessions: 1, by_model: [ { model: '', input_tokens: 3, output_tokens: 1, cache_read_tokens: 2, cache_write_tokens: 0, reasoning_tokens: 0, sessions: 1 }, ], by_day: [], }) getUsageStatsFromDbMock.mockResolvedValue({ input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1, cost: 0, total_api_calls: 0, by_model: [ { model: ' ', input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1 }, ], by_day: [], }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { query: { days: '2' }, body: null } await mod.usageStats(ctx) expect(ctx.body.model_usage).toEqual([ { model: ' ', input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1 }, ]) }) it('sets a session model and provider in the local session store', async () => { getSessionMock.mockReturnValue({ id: 'session-1' }) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'session-1' }, request: { body: { model: 'grok-4', provider: 'xai' } }, body: null, } await mod.setModel(ctx) expect(localCreateSessionMock).not.toHaveBeenCalled() expect(localUpdateSessionMock).toHaveBeenCalledWith('session-1', { model: 'grok-4', provider: 'xai' }) expect(ctx.body).toEqual({ ok: true }) }) it('deletes a current-profile Hermes history session even when no local Web UI session exists', async () => { getActiveProfileNameMock.mockReturnValue('travel') getSessionMock.mockReturnValue(null) getExactSessionDetailFromDbWithProfileMock.mockResolvedValue({ id: 'history-only', messages: [] }) deleteHermesSessionForProfileMock.mockResolvedValue(true) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'history-only' }, body: null } await mod.remove(ctx) expect(getExactSessionDetailFromDbWithProfileMock).toHaveBeenCalledWith('history-only', 'travel') expect(deleteHermesSessionForProfileMock).toHaveBeenCalledWith('history-only', 'travel') expect(localDeleteSessionMock).not.toHaveBeenCalled() expect(ctx.body).toEqual({ ok: true, deleted: false, hermes: { attempted: true, deleted: true, profile: 'travel', error: undefined }, }) }) describe('exportSession', () => { it('returns session as JSON download with correct headers (full mode)', async () => { const sessionData = { id: 'abc-123', title: 'Test Session', messages: [{ id: 1, role: 'user', content: 'hello' }] } localGetSessionDetailMock.mockReturnValue(sessionData) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const setMock = vi.fn() const ctx: any = { params: { id: 'abc-123' }, query: {}, set: setMock, body: null } await mod.exportSession(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('abc-123') expect(setMock).toHaveBeenCalledWith('Content-Disposition', expect.stringContaining('abc-123')) expect(setMock).toHaveBeenCalledWith('Content-Type', 'application/json') expect(ctx.status).toBeUndefined() expect(JSON.parse(ctx.body)).toMatchObject({ id: 'abc-123', title: 'Test Session' }) }) it('returns full TXT export', async () => { const sessionData = { id: 'txt-123', title: 'Text Export', messages: [ { id: 1, role: 'user', content: 'hello', timestamp: 1700000000 }, { id: 2, role: 'assistant', content: 'hi', timestamp: 1700000001 }, ], } localGetSessionDetailMock.mockReturnValue(sessionData) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const setMock = vi.fn() const ctx: any = { params: { id: 'txt-123' }, query: { mode: 'full', ext: 'txt' }, set: setMock, body: null } await mod.exportSession(ctx) expect(setMock).toHaveBeenCalledWith('Content-Type', 'text/plain; charset=utf-8') expect(ctx.body).toContain('# Text Export') expect(ctx.body).toContain('[user]') expect(ctx.body).toContain('hello') expect(ctx.body).toContain('[assistant]') expect(ctx.body).toContain('hi') }) it('returns 404 when session not found', async () => { localGetSessionDetailMock.mockReturnValue(null) getSessionMock.mockResolvedValue(null) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const ctx: any = { params: { id: 'not-found' }, query: {}, set: vi.fn(), body: null } await mod.exportSession(ctx) expect(ctx.status).toBe(404) expect(ctx.body).toEqual({ error: 'Session not found' }) }) it('falls back to CLI when DB query fails', async () => { const sessionData = { id: 'cli-123', title: 'CLI Session', messages: [] } localGetSessionDetailMock.mockReturnValue(sessionData) const mod = await import('../../packages/server/src/controllers/hermes/sessions') const setMock = vi.fn() const ctx: any = { params: { id: 'cli-123' }, query: {}, set: setMock, body: null } await mod.exportSession(ctx) expect(localGetSessionDetailMock).toHaveBeenCalledWith('cli-123') expect(JSON.parse(ctx.body)).toMatchObject({ id: 'cli-123' }) }) }) })