gongxiangp2/apps/detail-agent-pi/test/history-thinking.test.mjs
WuYongJie f37cc4e38b 修复小壹制作续写、画布超时与 SKU 资料作用域
按结构读取旧模板与可选配置,递归脱敏 SDK 会话,并覆盖 Windows Node 24 回归用例。
2026-12-05 12:57:43 +08:00

70 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { SessionManager } from '@earendil-works/pi-coding-agent';
import { protectSession } from '../src/credentials.mjs';
import { cleanup } from './cleanup.mjs';
test('SDK session files redact nested tool arguments, signed reasoning and errors on persistence', async t => {
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'jiansun-pi-session-redaction-'));
t.after(()=>cleanup(dir));
const manager=protectSession(SessionManager.create(dir,dir));
const message={
role:'assistant',timestamp:Date.now(),stopReason:'error',
errorMessage:'回执失败:authorization: Bearer fake-error-secret',
originalRequestId:'operation-original-id',
content:[
{type:'thinking',thinking:'private_key: fake-thinking-secret',thinkingSignature:'opaque-thinking-signature'},
{type:'toolCall',id:'original-tool-id',name:'example_tool',arguments:{
width:24,nested:[{api_key:'fake-tool-secret',text:'token=fake-inline-secret',title:'原样保留'}],
}},
],
};
manager.appendMessage({role:'user',timestamp:Date.now(),content:'请处理'});
manager.appendMessage(message);
const jsonl=await fs.readFile(manager.getSessionFile(),'utf8');
assert.doesNotMatch(jsonl,/fake-(?:error|thinking|tool|inline)-secret/);
const saved=SessionManager.open(manager.getSessionFile()).getBranch().at(-1).message;
assert.equal(saved.originalRequestId,'operation-original-id');
assert.equal(saved.content[0].thinkingSignature,'opaque-thinking-signature');
assert.deepEqual(saved.content[1],{type:'toolCall',id:'original-tool-id',name:'example_tool',
arguments:{width:24,nested:[{text:'[已脱敏]',title:'原样保留'}]}});
assert.equal(message.content[1].arguments.nested[0].api_key,'fake-tool-secret');
});
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);
});