Files
hermes-web-ui/tests/server/sessions-controller.test.ts
2026-05-25 12:29:24 +08:00

711 lines
26 KiB
TypeScript

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 getSessionDetailFromDbWithProfileMock = 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()
const listUserProfilesMock = 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,
getSessionDetailFromDbWithProfile: getSessionDetailFromDbWithProfileMock,
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/users-store', () => ({
listUserProfiles: listUserProfilesMock,
}))
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()
getSessionDetailFromDbWithProfileMock.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()
listUserProfilesMock.mockReset()
listUserProfilesMock.mockReturnValue([])
})
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(undefined, undefined, 5)
expect(listConversationSummariesMock).not.toHaveBeenCalled()
expect(ctx.body.sessions[0]).toMatchObject({ id: 'local-conversation', source: 'cli', title: 'Local' })
})
it('lists all account-accessible single-chat sessions when only the active profile header is present', async () => {
listUserProfilesMock.mockReturnValue([{ profile_name: 'default' }, { profile_name: 'travel' }])
localListSessionsMock.mockReturnValue([
{
id: 'default-session',
profile: 'default',
source: 'cli',
model: 'gpt-5',
title: 'Default',
started_at: 1,
ended_at: null,
last_active: 3,
message_count: 1,
tool_call_count: 0,
input_tokens: 0,
output_tokens: 0,
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: '',
},
{
id: 'travel-session',
profile: 'travel',
source: 'cli',
model: 'gpt-5',
title: 'Travel',
started_at: 2,
ended_at: null,
last_active: 4,
message_count: 1,
tool_call_count: 0,
input_tokens: 0,
output_tokens: 0,
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: '',
},
{
id: 'secret-session',
profile: 'secret',
source: 'cli',
model: 'gpt-5',
title: 'Secret',
started_at: 3,
ended_at: null,
last_active: 5,
message_count: 1,
tool_call_count: 0,
input_tokens: 0,
output_tokens: 0,
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: '',
},
])
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = {
query: {},
state: {
user: { id: 1, role: 'admin' },
profile: { name: 'travel' },
},
body: null,
}
await mod.list(ctx)
expect(localListSessionsMock).toHaveBeenCalledWith(undefined, undefined, 2000)
expect(ctx.body.sessions.map((session: any) => session.id)).toEqual(['default-session', 'travel-session'])
})
it('filters the single-chat session list when profile is explicitly provided', async () => {
localListSessionsMock.mockReturnValue([])
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = {
query: { profile: 'travel' },
state: { profile: { name: 'default' } },
body: null,
}
await mod.list(ctx)
expect(localListSessionsMock).toHaveBeenCalledWith('travel', undefined, 2000)
})
it('searches all account-accessible single-chat sessions unless profile is explicit', async () => {
localSearchSessionsMock.mockReturnValue([])
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = {
query: { q: 'docker', limit: '10' },
state: { profile: { name: 'travel' } },
body: null,
}
await mod.search(ctx)
expect(localSearchSessionsMock).toHaveBeenCalledWith(undefined, 'docker', 10)
})
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('reads Hermes history detail from the requested profile database', async () => {
localGetSessionDetailMock.mockReturnValue(null)
getSessionDetailFromDbWithProfileMock.mockResolvedValue({
id: 'travel-session',
source: 'cli',
title: 'Travel detail',
messages: [
{ id: 1, session_id: 'travel-session', role: 'user', content: 'from travel', timestamp: 1 },
],
})
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { params: { id: 'travel-session' }, query: { profile: 'travel' }, body: null }
await mod.getHermesSession(ctx)
expect(localGetSessionDetailMock).toHaveBeenCalledWith('travel-session')
expect(getSessionDetailFromDbWithProfileMock).toHaveBeenCalledWith('travel-session', 'travel')
expect(getSessionDetailFromDbMock).not.toHaveBeenCalled()
expect(getSessionMock).not.toHaveBeenCalled()
expect(ctx.body.session).toMatchObject({
id: 'travel-session',
profile: 'travel',
title: 'Travel detail',
messages: [{ content: 'from travel' }],
})
})
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('loads usage analytics from the request-scoped profile state database', async () => {
getUsageStatsFromDbMock.mockResolvedValue({
input_tokens: 12,
output_tokens: 6,
cache_read_tokens: 3,
cache_write_tokens: 1,
reasoning_tokens: 2,
sessions: 1,
cost: 0.01,
total_api_calls: 4,
by_model: [
{ model: 'research-model', input_tokens: 12, output_tokens: 6, cache_read_tokens: 3, cache_write_tokens: 1, reasoning_tokens: 2, sessions: 1 },
],
by_day: [],
})
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { query: { days: '2' }, state: { profile: { name: 'research' } }, body: null }
await mod.usageStats(ctx)
expect(getUsageStatsFromDbMock).toHaveBeenCalledWith(2, undefined, 'research')
expect(ctx.body).toMatchObject({
total_input_tokens: 12,
total_output_tokens: 6,
total_sessions: 1,
total_cost: 0.01,
total_api_calls: 4,
})
expect(ctx.body.model_usage).toEqual([
{ model: 'research-model', input_tokens: 12, output_tokens: 6, cache_read_tokens: 3, cache_write_tokens: 1, reasoning_tokens: 2, sessions: 1 },
])
})
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 },
})
})
it('batch deletes sessions from their requested profiles', async () => {
listUserProfilesMock.mockReturnValue([{ profile_name: 'default' }, { profile_name: 'travel' }])
getSessionMock.mockImplementation((id: string) => ({
id,
profile: id === 'travel-session' ? 'travel' : 'default',
}))
getExactSessionDetailFromDbWithProfileMock.mockResolvedValue({ id: 'matched', messages: [] })
deleteHermesSessionForProfileMock.mockResolvedValue(true)
localDeleteSessionMock.mockReturnValue(true)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = {
request: {
body: {
sessions: [
{ id: 'default-session', profile: 'default' },
{ id: 'travel-session', profile: 'travel' },
],
},
},
state: {
user: { id: 1, role: 'admin' },
},
body: null,
}
await mod.batchRemove(ctx)
expect(getExactSessionDetailFromDbWithProfileMock).toHaveBeenCalledWith('default-session', 'default')
expect(getExactSessionDetailFromDbWithProfileMock).toHaveBeenCalledWith('travel-session', 'travel')
expect(deleteHermesSessionForProfileMock).toHaveBeenCalledWith('default-session', 'default')
expect(deleteHermesSessionForProfileMock).toHaveBeenCalledWith('travel-session', 'travel')
expect(localDeleteSessionMock).toHaveBeenCalledWith('default-session')
expect(localDeleteSessionMock).toHaveBeenCalledWith('travel-session')
expect(ctx.body).toMatchObject({ ok: true, deleted: 2, failed: 0, hermesDeleted: 2 })
})
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' })
})
})
})