feat: add bridge session commands (#743)
This commit is contained in:
@@ -7,6 +7,11 @@ const getConversationDetailMock = vi.fn()
|
||||
const getSessionDetailFromDbMock = vi.fn()
|
||||
const getUsageStatsFromDbMock = vi.fn()
|
||||
const getSessionMock = vi.fn()
|
||||
const localListSessionsMock = vi.fn()
|
||||
const localGetSessionDetailMock = vi.fn()
|
||||
const localSearchSessionsMock = vi.fn()
|
||||
const localDeleteSessionMock = vi.fn()
|
||||
const localRenameSessionMock = vi.fn()
|
||||
const getGroupChatServerMock = vi.fn()
|
||||
const getLocalUsageStatsMock = vi.fn()
|
||||
const getActiveProfileNameMock = vi.fn()
|
||||
@@ -47,6 +52,11 @@ vi.mock('../../packages/server/src/db/hermes/sessions-db', () => ({
|
||||
// Mock useLocalSessionStore to return false so we test the CLI path
|
||||
vi.mock('../../packages/server/src/db/hermes/session-store', () => ({
|
||||
useLocalSessionStore: () => false,
|
||||
listSessions: localListSessionsMock,
|
||||
searchSessions: localSearchSessionsMock,
|
||||
getSessionDetail: localGetSessionDetailMock,
|
||||
deleteSession: localDeleteSessionMock,
|
||||
renameSession: localRenameSessionMock,
|
||||
}))
|
||||
|
||||
vi.mock('../../packages/server/src/db/hermes/usage-store', () => ({
|
||||
@@ -97,6 +107,11 @@ describe('session conversations controller', () => {
|
||||
getSessionDetailFromDbMock.mockReset()
|
||||
getUsageStatsFromDbMock.mockReset()
|
||||
getSessionMock.mockReset()
|
||||
localListSessionsMock.mockReset()
|
||||
localGetSessionDetailMock.mockReset()
|
||||
localSearchSessionsMock.mockReset()
|
||||
localDeleteSessionMock.mockReset()
|
||||
localRenameSessionMock.mockReset()
|
||||
getGroupChatServerMock.mockReset()
|
||||
getGroupChatServerMock.mockReturnValue(null)
|
||||
getLocalUsageStatsMock.mockReset()
|
||||
@@ -106,57 +121,84 @@ describe('session conversations controller', () => {
|
||||
getCompressionSnapshotMock.mockReset()
|
||||
})
|
||||
|
||||
it('prefers the DB-backed conversations summary path', async () => {
|
||||
listConversationSummariesFromDbMock.mockResolvedValue([{ id: 'db-conversation' }])
|
||||
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(listConversationSummariesFromDbMock).toHaveBeenCalledWith({ source: undefined, humanOnly: true, limit: 5 })
|
||||
expect(localListSessionsMock).toHaveBeenCalledWith('default', undefined, 5)
|
||||
expect(listConversationSummariesMock).not.toHaveBeenCalled()
|
||||
expect(ctx.body).toEqual({ sessions: [{ id: 'db-conversation' }] })
|
||||
expect(ctx.body.sessions[0]).toMatchObject({ id: 'local-conversation', source: 'cli', title: 'Local' })
|
||||
})
|
||||
|
||||
it('falls back to the CLI-export conversations summary path when the DB query fails', async () => {
|
||||
listConversationSummariesFromDbMock.mockRejectedValue(new Error('db unavailable'))
|
||||
listConversationSummariesMock.mockResolvedValue([{ id: 'fallback-conversation' }])
|
||||
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 mod.listConversations(ctx)
|
||||
|
||||
expect(loggerWarnMock).toHaveBeenCalled()
|
||||
expect(listConversationSummariesMock).toHaveBeenCalledWith({ source: undefined, humanOnly: false, limit: undefined })
|
||||
expect(ctx.body).toEqual({ sessions: [{ id: 'fallback-conversation' }] })
|
||||
await expect(mod.listConversations(ctx)).rejects.toThrow('db unavailable')
|
||||
})
|
||||
|
||||
it('prefers the DB-backed conversation detail path', async () => {
|
||||
getConversationDetailFromDbMock.mockResolvedValue({ session_id: 'root', messages: [], visible_count: 0, thread_session_count: 1 })
|
||||
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(getConversationDetailFromDbMock).toHaveBeenCalledWith('root', { source: undefined, humanOnly: true })
|
||||
expect(localGetSessionDetailMock).toHaveBeenCalledWith('root')
|
||||
expect(getConversationDetailMock).not.toHaveBeenCalled()
|
||||
expect(ctx.body).toEqual({ session_id: 'root', messages: [], visible_count: 0, thread_session_count: 1 })
|
||||
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('falls back to the CLI-export conversation detail path when the DB query throws', async () => {
|
||||
getConversationDetailFromDbMock.mockRejectedValue(new Error('db unavailable'))
|
||||
getConversationDetailMock.mockResolvedValue({ session_id: 'root', messages: [{ id: 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(loggerWarnMock).toHaveBeenCalled()
|
||||
expect(getConversationDetailMock).toHaveBeenCalledWith('root', { source: undefined, humanOnly: false })
|
||||
expect(ctx.body).toEqual({ session_id: 'root', messages: [{ id: 1 }], visible_count: 1, thread_session_count: 1 })
|
||||
expect(ctx.status).toBe(404)
|
||||
expect(ctx.body).toEqual({ error: 'Conversation not found' })
|
||||
})
|
||||
|
||||
it('merges native state.db usage analytics with local Web UI usage for the requested period', async () => {
|
||||
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,
|
||||
@@ -193,34 +235,33 @@ describe('session conversations controller', () => {
|
||||
const ctx: any = { query: { days: '2' }, body: null }
|
||||
await mod.usageStats(ctx)
|
||||
|
||||
expect(getLocalUsageStatsMock).toHaveBeenCalledWith('default', 2)
|
||||
expect(getLocalUsageStatsMock).not.toHaveBeenCalled()
|
||||
expect(getUsageStatsFromDbMock).toHaveBeenCalledWith(2)
|
||||
expect(ctx.body).toMatchObject({
|
||||
total_input_tokens: 30,
|
||||
total_output_tokens: 15,
|
||||
total_cache_read_tokens: 6,
|
||||
total_cache_write_tokens: 3,
|
||||
total_reasoning_tokens: 9,
|
||||
total_sessions: 3,
|
||||
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 },
|
||||
{ model: 'local-model', input_tokens: 10, output_tokens: 5, cache_read_tokens: 2, cache_write_tokens: 1, reasoning_tokens: 3, sessions: 1 },
|
||||
])
|
||||
expect(ctx.body.daily_usage.find((row: any) => row.date === today)).toMatchObject({
|
||||
input_tokens: 30,
|
||||
output_tokens: 15,
|
||||
cache_read_tokens: 6,
|
||||
cache_write_tokens: 3,
|
||||
sessions: 3,
|
||||
input_tokens: 20,
|
||||
output_tokens: 10,
|
||||
cache_read_tokens: 4,
|
||||
cache_write_tokens: 2,
|
||||
sessions: 2,
|
||||
cost: 0.02,
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps blank model usage under an unknown bucket', async () => {
|
||||
it('keeps blank model usage as returned by state.db analytics', async () => {
|
||||
getLocalUsageStatsMock.mockReturnValue({
|
||||
input_tokens: 3,
|
||||
output_tokens: 1,
|
||||
@@ -253,14 +294,14 @@ describe('session conversations controller', () => {
|
||||
await mod.usageStats(ctx)
|
||||
|
||||
expect(ctx.body.model_usage).toEqual([
|
||||
{ model: 'unknown', input_tokens: 5, output_tokens: 2, cache_read_tokens: 3, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 2 },
|
||||
{ model: ' ', input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1 },
|
||||
])
|
||||
})
|
||||
|
||||
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' }] }
|
||||
getSessionDetailFromDbMock.mockResolvedValue(sessionData)
|
||||
localGetSessionDetailMock.mockReturnValue(sessionData)
|
||||
|
||||
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
|
||||
const setMock = vi.fn()
|
||||
@@ -268,7 +309,7 @@ describe('session conversations controller', () => {
|
||||
|
||||
await mod.exportSession(ctx)
|
||||
|
||||
expect(getSessionDetailFromDbMock).toHaveBeenCalledWith('abc-123')
|
||||
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()
|
||||
@@ -284,7 +325,7 @@ describe('session conversations controller', () => {
|
||||
{ id: 2, role: 'assistant', content: 'hi', timestamp: 1700000001 },
|
||||
],
|
||||
}
|
||||
getSessionDetailFromDbMock.mockResolvedValue(sessionData)
|
||||
localGetSessionDetailMock.mockReturnValue(sessionData)
|
||||
|
||||
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
|
||||
const setMock = vi.fn()
|
||||
@@ -301,7 +342,7 @@ describe('session conversations controller', () => {
|
||||
})
|
||||
|
||||
it('returns 404 when session not found', async () => {
|
||||
getSessionDetailFromDbMock.mockResolvedValue(null)
|
||||
localGetSessionDetailMock.mockReturnValue(null)
|
||||
getSessionMock.mockResolvedValue(null)
|
||||
|
||||
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
|
||||
@@ -315,8 +356,7 @@ describe('session conversations controller', () => {
|
||||
|
||||
it('falls back to CLI when DB query fails', async () => {
|
||||
const sessionData = { id: 'cli-123', title: 'CLI Session', messages: [] }
|
||||
getSessionDetailFromDbMock.mockRejectedValue(new Error('db unavailable'))
|
||||
getSessionMock.mockResolvedValue(sessionData)
|
||||
localGetSessionDetailMock.mockReturnValue(sessionData)
|
||||
|
||||
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
|
||||
const setMock = vi.fn()
|
||||
@@ -324,7 +364,7 @@ describe('session conversations controller', () => {
|
||||
|
||||
await mod.exportSession(ctx)
|
||||
|
||||
expect(getSessionMock).toHaveBeenCalledWith('cli-123')
|
||||
expect(localGetSessionDetailMock).toHaveBeenCalledWith('cli-123')
|
||||
expect(JSON.parse(ctx.body)).toMatchObject({ id: 'cli-123' })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user