70 lines
4.5 KiB
JavaScript
70 lines
4.5 KiB
JavaScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { activityScope, canvasPresence, toolActivity } from '../src/activity-event.mjs';
|
||
|
||
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: 'start_production_strategy' };
|
||
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);
|
||
});
|