Files
hermes-ui/tests/server/group-chat-agent-routing-baseline.test.ts
T
toor 1e9ea89d86
Build / build (push) Canceled after 0s
NPM Lockfile Check / npm ci --ignore-scripts (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
chore: bootstrap hermes-ui skeleton from 0.6.29 snapshot
Import the client, server, skills, tests, and build scripts as the starting
point for the rebuild, then trim the surfaces this project does not ship:
Electron desktop packaging, the marketing website, and the ESP32 firmware
tree. CI keeps the web build, e2e, lockfile, release, and Docker workflows.

Rename the package and CLI entry points to hermes-ui, drop the prepare hook so
npm ci no longer triggers a build, and drop the removed tsconfig and nodemon
references. Rewrite AGENTS.md, ARCHITECTURE.md, DEVELOPMENT.md, and the READMEs
so the documented package map and hard rules match this repository.

reference_code/ is ignored: it is a read-only snapshot, not an input to the build.
2026-10-16 10:34:35 +08:00

118 lines
4.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
connectGroupChatClient,
createTestGroupChatServer,
emitAck,
} from './group-chat-test-helpers'
import { GROUP_CHAT_AGENT_SOCKET_SECRET, groupBridgeSessionId } from '../../packages/server/src/services/hermes/group-chat/agent-clients'
import { authenticateUserToken, isAuthEnabled } from '../../packages/server/src/middleware/user-auth'
import type { GroupChatServer } from '../../packages/server/src/services/hermes/group-chat'
describe('group chat agent routing baseline', () => {
let harness: Awaited<ReturnType<typeof createTestGroupChatServer>>
let groupServer: GroupChatServer
let port: number
beforeEach(async () => {
vi.clearAllMocks()
vi.mocked(isAuthEnabled).mockResolvedValue(false)
vi.mocked(authenticateUserToken).mockResolvedValue(null as any)
harness = await createTestGroupChatServer()
groupServer = harness.groupServer
port = harness.port
groupServer.getStorage().saveRoom('room-1', 'Room 1', 'ROOM1')
groupServer.getStorage().addRoomAgent('room-1', 'agent-worker', 'default', 'Worker', '', 0)
})
afterEach(() => {
harness?.cleanup()
})
async function joinHumanAndAgent() {
const human = await connectGroupChatClient(port, 'human-1', 'Human')
const agent = await connectGroupChatClient(port, 'agent-worker', 'Worker', {
source: 'agent',
agentSocketSecret: GROUP_CHAT_AGENT_SOCKET_SECRET,
})
harness.sockets.push(human, agent)
await emitAck(human, 'join', { roomId: 'room-1', inviteCode: 'ROOM1' })
await emitAck(agent, 'join', { roomId: 'room-1', inviteCode: 'ROOM1' })
return { human, agent }
}
function currentAgentSessionId() {
const room = groupServer.getStorage().getRoom('room-1')
return groupBridgeSessionId('room-1', 'default', 'Worker', String(room?.sessionSeed || '0'))
}
it('routes human messages through mention processing', async () => {
const { human } = await joinHumanAndAgent()
const processMentions = vi.spyOn(groupServer.agentClients, 'processMentions').mockResolvedValue(undefined)
await emitAck(human, 'message', { roomId: 'room-1', id: 'human-msg-1', content: '@Worker hello' })
expect(processMentions).toHaveBeenCalledWith('room-1', expect.objectContaining({
messageId: 'human-msg-1',
role: 'user',
mentionDepth: 0,
}))
})
it('does not route read-only invite member messages through agents', async () => {
vi.mocked(isAuthEnabled).mockResolvedValue(true)
vi.mocked(authenticateUserToken).mockImplementation(async (token: string) => {
if (token === 'read-only-token') return { id: 2, username: 'bob', role: 'admin', profiles: [] } as any
return null
})
const human = await connectGroupChatClient(port, 'ignored-user', 'Bob', { token: 'read-only-token' })
const agent = await connectGroupChatClient(port, 'agent-worker', 'Worker', {
source: 'agent',
agentSocketSecret: GROUP_CHAT_AGENT_SOCKET_SECRET,
})
harness.sockets.push(human, agent)
await emitAck(human, 'join', { roomId: 'room-1', inviteCode: 'ROOM1' })
await emitAck(agent, 'join', { roomId: 'room-1' })
const processMentions = vi.spyOn(groupServer.agentClients, 'processMentions').mockResolvedValue(undefined)
await emitAck(human, 'message', { roomId: 'room-1', id: 'readonly-msg-1', content: '@Worker hello' })
expect(processMentions).not.toHaveBeenCalled()
})
it('routes agent replies below the default mention-depth guard', async () => {
const { agent } = await joinHumanAndAgent()
const processMentions = vi.spyOn(groupServer.agentClients, 'processMentions').mockResolvedValue(undefined)
await emitAck(agent, 'message', {
roomId: 'room-1',
id: 'agent-msg-1',
content: '@Worker chain handoff',
role: 'assistant',
mentionDepth: 3,
agentSessionId: currentAgentSessionId(),
})
expect(processMentions).toHaveBeenCalledWith('room-1', expect.objectContaining({
messageId: 'agent-msg-1',
role: 'assistant',
mentionDepth: 3,
}))
})
it('does not route agent replies at the default mention-depth guard', async () => {
const { agent } = await joinHumanAndAgent()
const processMentions = vi.spyOn(groupServer.agentClients, 'processMentions').mockResolvedValue(undefined)
await emitAck(agent, 'message', {
roomId: 'room-1',
id: 'agent-msg-2',
content: '@Worker stop looping',
role: 'assistant',
mentionDepth: 4,
agentSessionId: currentAgentSessionId(),
})
expect(processMentions).not.toHaveBeenCalled()
})
})