gongxiangp2/apps/detail-agent-pi/test/activity-event.test.mjs

121 lines
7.4 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 { activityText, activityScope, activityToolLabel, canvasPresence, toolActivity, toolFailure } from '../src/activity-event.mjs';
test('CodeMode failure reports the nested tool error before earlier successful output', () => {
const event={type:'tool_execution_end',toolName:'codemode',toolCallId:'script',isError:true,
result:{content:[{type:'text',text:'successful write\n'.repeat(200)}],details:{calls:[
{name:'native_call',status:'ok'},
{name:'native_call',status:'error',error:JSON.stringify({failure:{category:'revision_conflict',message:'当前修订 20'}})},
]}}};
const activity=toolActivity(event,{},100);
assert.equal(activity.failureCategory,'revision_conflict');
assert.equal(activity.status,'stopped');
assert.equal(activity.message,'native_call: 当前修订 20');
});
test('a continuation notice parsed as JSON gives an actionable canvas script failure', () => {
const engineError = "Script error:\nSyntaxError: unexpected token: 'Showing'\n at <input>:2002:2\n at parse (native)";
const event = {type:'tool_execution_end',toolName:'codemode',toolCallId:'script',isError:true,
result:{content:[{type:'text',text:engineError}],details:{calls:[{name:'read',status:'ok'}]}}};
const activity = toolActivity(event,{},100);
assert.equal(activity.status,'failed');
assert.match(activity.message,/续读提示.*分段读取/u);
event.result.details.calls = [];
assert.equal(toolActivity(event,{},100).message,engineError);
});
test('project file and goal tools have distinct Chinese activity labels', () => {
for (const [toolName,label] of Object.entries({read:'读取资料',write:'写入文件',edit:'编辑文件',
ls:'列出目录',find:'查找文件',grep:'搜索内容'})) {
assert.equal(activityToolLabel(toolName),label);
const activity=toolActivity({type:'tool_execution_start',toolName,toolCallId:'call'},{},100);
assert.equal(activity.toolName,toolName);
assert.equal(activity.summary,'开始:'+label);
}
});
test('accepted image work and unknown outcomes remain pending with distinct labels', () => {
const event={type:'tool_execution_end',toolName:'dispatch_image_request',toolCallId:'image-call'};
const accepted=toolActivity({...event,result:{details:{phase:'accepted',operationId:'original'}}},{},100);
assert.equal(accepted.status,'pending');
assert.equal(accepted.pendingKind,'active');
assert.match(accepted.summary,/^进行中/u);
const unknown=toolActivity({...event,result:{details:{phase:'unknown',pendingOriginal:'original'}}},{},100);
assert.equal(unknown.status,'pending');
assert.equal(unknown.pendingKind,undefined);
assert.match(unknown.summary,/^待核查/u);
});
test('canvas presence projects only the live canvas address', () => {
const event = { type: 'tool_execution_start', toolName: 'write_copy_fields', toolCallId: 'copy',
args: { items: [{ layer: 54, text: '未提交的文案' }, { layer: 55, text: '第二行' }] } };
assert.deepEqual(canvasPresence(event, toolActivity(event, {}, 100)), { nativeLayerId: 54, writing: true });
assert.equal(canvasPresence({ ...event, args: { ...event.args, recordOnly: true } }, toolActivity(event, {}, 100)), undefined);
const end = { type: 'tool_execution_end', toolName: event.toolName, toolCallId: event.toolCallId,
result: { details: { appliedLayers: [54, 55] } } };
assert.deepEqual(canvasPresence(end, toolActivity(end, {}, 100)), { nativeLayerId: 55, writing: true });
assert.equal(canvasPresence({ ...end, result: {} }, toolActivity(end, {}, 100)), undefined);
const screen = { ...event, toolName: 'read_output_screen', args: { pages: [0, 2] } };
assert.deepEqual(canvasPresence(screen, toolActivity(screen, {}, 100)), { page: 0 });
const native = { ...event, toolName: 'native_call', args: { method: 'engine.execute',
params: { command: 'layer.translate', params: { layer: 54, dx: 5 } } } };
assert.deepEqual(canvasPresence(native, toolActivity(native, {}, 100)), { nativeLayerId: 54, writing: false });
assert.equal(toolActivity(event, {}, 100).presence, undefined);
});
test('Responses call IDs join start and completion without widening scope IDs', () => {
const id = 'call_4fca7b5|fc_0c72ae9';
const scope = activityScope({ scope: 'account', project: 'project' }, { run: 'run' }, {});
const call = { toolCallId: id, toolName: 'plan_production' };
const start = toolActivity({ ...call, type: 'tool_execution_start' }, scope, 100);
const end = toolActivity({ ...call, type: 'tool_execution_end', durationMs: 80 }, scope, 180);
assert.equal(start.toolCallId, id);
assert.equal(end.toolCallId, id);
assert.equal(start.status, 'started');
assert.equal(end.status, 'completed');
assert.equal(end.summary, '完成:梳理制作安排');
assert.deepEqual(activityScope({ scope: id, project: id }, { run: id }, {}), {});
assert.equal(toolActivity({ ...call, toolCallId: 'call/foo', type: 'tool_execution_start' }, scope, 100), null);
});
test('only completed copy writes project bounded field labels and actual text into activity', () => {
const row = { field: '栏'.repeat(79) + '🪵', text: '木'.repeat(239) + '🪵', layer: 243, stack: 'internal' };
const copyUpdates = { total: 49, truncated: true,
items: [row, { field: '规格', text: '' }, ...Array(47).fill(row)], args: { layer: 243 } };
const event = { type: 'tool_execution_end', toolName: 'write_copy_fields', toolCallId: 'call_copy',
result: { details: { copyUpdates, appliedLayers: [243] }, content: ['raw content'] } };
const activity = toolActivity(event, {}, 100);
assert.deepEqual(activity.copyUpdates, {
total: 49, truncated: true,
items: [{ field: '栏'.repeat(79) + '<27>', text: '木'.repeat(239) + '<27>' },
{ field: '规格', text: '' },
...Array(46).fill({ field: '栏'.repeat(79) + '<27>', text: '木'.repeat(239) + '<27>' })],
});
assert.doesNotMatch(JSON.stringify(activity), /243|internal|appliedLayers|raw content/);
for (const change of [
{ type: 'tool_execution_start' }, { toolName: 'canvas_edit' }, { isError: true },
{ result: { details: { copyUpdates, pendingOriginal: 'original' } } },
{ result: { details: { copyUpdates: { total: 1, truncated: false, items: [{ field: '规格', text: null }] } } } },
{ result: { details: { copyUpdates: { ...copyUpdates, truncated: false } } } },
]) assert.equal(toolActivity({ ...event, ...change }, {}, 100).copyUpdates, undefined);
});
test('knowledge and provider diagnostics stay out of the visible activity and conversation',()=>{
const path='Rules/小壹共用/模板原图换商品.md';
const file='C:/local/knowledge/'+path;
const knowledgeVersion='SFY-CHAIR-001@2026-12-04.1';
const skills={knowledgeVersion,documents:new Map([[file,{path}]])};
const error=`锁定资料 ${knowledgeVersion} 未读到 ${file}(request_outcome_unknown)`;
assert.equal(activityText(error,skills),'锁定资料 当前资料 未读到 项目资料');
assert.equal(activityText('查看 /skills/'+path,skills),'查看 项目资料');
const event={type:'tool_execution_end',toolName:'read',toolCallId:'read',isError:true,
result:{content:[{type:'text',text:error}]}};
assert.equal(toolFailure(event).message,error);
assert.equal(toolActivity(event,{},100).summary,'失败:读取资料 · 资料读取未完成');
assert.equal(
activityText('读取 \\\\?\\C:\\Users\\demo\\cache\\item.json 失败:ECONNRESET'),
'读取 本地文件 失败:连接暂未确认',
);
});