Files
lingxi-ai/tests/server/run-chat-queued-item.test.ts
yi 7d10320a82
Build / build (push) Has been cancelled
NPM Lockfile Check / npm ci --ignore-scripts (push) Has been cancelled
Playwright / e2e (push) Has been cancelled
feat: 灵犀 Studio Web UI 定制版
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-05 11:29:11 +08:00

129 lines
4.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const handleBridgeRunMock = vi.hoisted(() => vi.fn(async () => {}))
const handleApiRunMock = vi.hoisted(() => vi.fn(async () => {}))
vi.mock('../../packages/server/src/services/hermes/run-chat/handle-bridge-run', () => ({
handleBridgeRun: handleBridgeRunMock,
}))
vi.mock('../../packages/server/src/services/hermes/run-chat/handle-api-run', () => ({
handleApiRun: handleApiRunMock,
loadSessionStateFromDb: vi.fn(),
resolveRunSource: vi.fn((source?: string) => source || 'cli'),
}))
vi.mock('../../packages/server/src/services/hermes/run-chat/session-command', () => ({
handleSessionCommand: vi.fn(),
isSessionCommand: vi.fn(() => false),
parseSessionCommand: vi.fn(() => null),
}))
vi.mock('../../packages/server/src/services/hermes/agent-bridge', () => ({
AgentBridgeClient: vi.fn(() => ({})),
}))
vi.mock('../../packages/server/src/services/logger', () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
}))
vi.mock('../../packages/server/src/lib/llm-prompt', () => ({
getSystemPrompt: vi.fn(() => 'system prompt'),
}))
vi.mock('../../packages/server/src/db/hermes/session-store', () => ({
getSession: vi.fn(() => ({ id: 'session-1', profile: 'default', source: 'cli' })),
}))
vi.mock('../../packages/server/src/services/hermes/hermes-profile', () => ({
getActiveProfileName: vi.fn(() => 'default'),
getProfileDir: vi.fn(() => '/tmp/hermes-default'),
listProfileNamesFromDisk: vi.fn(() => ['default']),
}))
vi.mock('../../packages/server/src/middleware/user-auth', () => ({
authenticateUserToken: vi.fn(),
isAuthEnabled: vi.fn(async () => false),
}))
vi.mock('../../packages/server/src/db/hermes/users-store', () => ({
userCanAccessProfile: vi.fn(() => true),
}))
function makeServerHarness() {
const namespace = {
adapter: { rooms: new Map() },
to: vi.fn(() => ({ emit: vi.fn() })),
use: vi.fn(),
on: vi.fn(),
}
const io = { of: vi.fn(() => namespace) }
const socket = {
id: 'socket-1',
connected: true,
handshake: { auth: {}, query: { profile: 'default' } },
data: {},
emit: vi.fn(),
join: vi.fn(),
to: vi.fn(() => ({ emit: vi.fn() })),
on: vi.fn(),
}
return { io, namespace, socket }
}
describe('ChatRunSocket queued bridge runs', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('persists normal queued bridge messages when they are dequeued', async () => {
const { ChatRunSocket } = await import('../../packages/server/src/services/hermes/run-chat')
const { io, socket } = makeServerHarness()
const server = new ChatRunSocket(io as any)
;(server as any).runQueuedItem(socket, 'session-1', {
queue_id: 'queue-normal',
input: 'queued follow-up',
source: 'cli',
profile: 'default',
}, 'default')
await vi.waitFor(() => expect(handleBridgeRunMock).toHaveBeenCalled())
const call = handleBridgeRunMock.mock.calls.at(-1)!
expect(call[2]).toEqual(expect.objectContaining({
input: 'queued follow-up',
display_input: undefined,
storage_message: undefined,
queue_id: 'queue-normal',
}))
expect(call[6]).toBe(false)
})
it('persists the visible plan command when dequeuing expanded plan command runs', async () => {
const { ChatRunSocket } = await import('../../packages/server/src/services/hermes/run-chat')
const { io, socket } = makeServerHarness()
const server = new ChatRunSocket(io as any)
;(server as any).runQueuedItem(socket, 'session-1', {
queue_id: 'queue-plan',
input: '[IMPORTANT: expanded plan skill prompt]',
displayInput: '/plan build the feature',
displayRole: 'command',
storageMessage: '/plan build the feature',
source: 'cli',
profile: 'default',
}, 'default')
await vi.waitFor(() => expect(handleBridgeRunMock).toHaveBeenCalled())
const call = handleBridgeRunMock.mock.calls.at(-1)!
expect(call[2]).toEqual(expect.objectContaining({
input: '[IMPORTANT: expanded plan skill prompt]',
display_input: '/plan build the feature',
display_role: 'command',
storage_message: '/plan build the feature',
queue_id: 'queue-plan',
}))
expect(call[6]).toBe(false)
})
})