gongxiangp2/apps/detail-agent-pi/test/history-thinking.test.mjs

37 lines
2.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { StreamOutputMeter, stripProductionToolText } from '../src/stream-presentation.mjs';
test('production shows readable prose throughout an incomplete tool payload', () => {
const prose='正在补图。';
const raw='<tool_calls>[{"name":"delegate_image_slots"}]</tool_calls>';
for(let i=1;i<=raw.length;i++)assert.equal(stripProductionToolText(prose+raw.slice(0,i),true),prose);
assert.equal(stripProductionToolText(prose+raw+'继续核对。'),prose+'继续核对。');
assert.equal(stripProductionToolText('<tool_call>{"invalid":true'), '');
});
test('thinking output meter throttles snapshots and settles on SDK usage', () => {
const meter = new StreamOutputMeter();
const partial = {
role: 'assistant',
content: [{ type: 'thinking', thinking: 'Private reasoning output' }],
usage: { output: 0 },
stopReason: 'stop',
};
const event = (type, delta) => ({ type: 'message_update', assistantMessageEvent: { type, delta, partial } });
assert.equal(meter.update(event('thinking_start'), true, 100), undefined);
assert.deepEqual(meter.update(event('thinking_delta', 'Private reasoning output'), true, 1_000),
{ tokens: 6, tokensPerSecond: null, estimated: true });
partial.content[0].thinking += ' continues';
assert.equal(meter.update(event('thinking_delta', ' continues'), true, 1_250), undefined);
partial.content[0].thinking += ' for a moment';
assert.deepEqual(meter.update(event('thinking_delta', ' for a moment'), true, 2_000),
{ tokens: 12, tokensPerSecond: 12, estimated: true });
assert.equal(meter.update(event('thinking_end'), false, 2_001), undefined);
const end = { type: 'message_end', message: { ...partial, usage: { output: 29 } } };
assert.deepEqual(meter.update(end, false), { tokens: 29, tokensPerSecond: 29, estimated: false });
assert.deepEqual(meter.update({ ...end, message: { ...end.message, stopReason: 'aborted' } }, false),
{ tokens: 12, tokensPerSecond: 12, estimated: true });
assert.equal(new StreamOutputMeter().update(end, false), undefined);
});