Files
hermes-ui/tests/server/run-chat-usage.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

92 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { countTokens } from '../../packages/server/src/lib/context-compressor'
import {
contextTokensWithCachedOverhead,
estimateUsageTokensFromMessages,
updateMessageContextTokenUsage,
} from '../../packages/server/src/services/hermes/run-chat/usage'
describe('run-chat usage token estimates', () => {
it('counts message content instead of serialized message payloads', () => {
const messages = [
{ role: 'user', content: 'hello from user' },
{ role: 'assistant', content: 'hello from assistant' },
]
const usage = estimateUsageTokensFromMessages(messages)
expect(usage.inputTokens).toBe(countTokens('hello from user'))
expect(usage.outputTokens).toBe(countTokens('hello from assistant'))
expect(usage.inputTokens + usage.outputTokens).toBeLessThan(countTokens(JSON.stringify(messages)))
})
it('keeps assistant tool call tokens on the output side', () => {
const messages = [
{
role: 'assistant',
content: 'calling tool',
tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'lookup', arguments: '{"q":"x"}' } }],
},
]
const usage = estimateUsageTokensFromMessages(messages)
expect(usage.inputTokens).toBe(0)
expect(usage.outputTokens).toBe(countTokens('calling tool') + countTokens(String(messages[0].tool_calls || '')))
})
it('adds cached bridge fixed context when updating full context usage', () => {
const emit = vi.fn()
const state = {
messages: [],
isWorking: false,
events: [],
queue: [],
bridgeContext: { fixedContextTokens: 20_000 },
} as any
const contextTokens = updateMessageContextTokenUsage(
'session-1',
state,
emit,
1_569,
{ inputTokens: 1_200, outputTokens: 369 },
)
expect(contextTokens).toBe(21_569)
expect(state.contextTokens).toBe(21_569)
expect(emit).toHaveBeenCalledWith('usage.updated', expect.objectContaining({
session_id: 'session-1',
inputTokens: 1_200,
outputTokens: 369,
contextTokens: 21_569,
}))
})
it('falls back to message tokens when bridge fixed context is missing', () => {
const emit = vi.fn()
const state = {
messages: [],
isWorking: false,
events: [],
queue: [],
} as any
expect(contextTokensWithCachedOverhead(state, 1_569)).toBe(1_569)
const contextTokens = updateMessageContextTokenUsage(
'session-1',
state,
emit,
1_569,
{ inputTokens: 1_200, outputTokens: 369 },
)
expect(contextTokens).toBe(1_569)
expect(state.contextTokens).toBe(1_569)
expect(emit).toHaveBeenCalledWith('usage.updated', expect.objectContaining({
contextTokens: 1_569,
}))
})
})