gongxiangp2/apps/detail-agent-pi/scripts/canvas-mechanics.mjs
WuYongJie 1b6425b552 feat(desktop): connect native Pi sessions with scoped rules and recovery
Track the fixed PhotoCraft source baseline with the existing native client integration. Use the actual Pi SDK for normal sends, durable session recovery, demand-loaded enterprise rules and checked undoable canvas tools. Retain request identities and never resend unknown inference. Include local packaging and real-call acceptance evidence.
2026-10-22 10:49:58 +08:00

54 lines
4.0 KiB
JavaScript

import fs from 'node:fs/promises';
import path from 'node:path';
import assert from 'node:assert/strict';
import { NativeCanvas } from '../src/native-bridge.mjs';
import { atomic } from '../src/store.mjs';
const config = JSON.parse(await fs.readFile(process.argv[2], 'utf8'));
const canvas = await NativeCanvas.connect(config.canvas);
const command = (command, params = {}) => canvas.call('engine.execute', { command, params });
try {
let state = await canvas.begin('本地画布机制验收:逐步编辑、接手和共享撤销(不计入模型验收)');
const original = state.context;
assert.equal(original.width, 1440);
const title = original.layers.find(l => /^S0?1\//.test(l.name) && l.text && l.text.length > 3 && !l.locked.all);
if (!title) throw Error('首屏标题未找到');
await canvas.end('设置查看位置');
await command('layer.select', { layer: title.id });
await command('view.fitArtboardOnScreen');
await canvas.begin('本地画布机制验收:逐步编辑、接手和共享撤销(不计入模型验收)');
await canvas.call('ui.screenshot', { path: path.join(config.output, 'mechanics-before.png'), focus: false });
state = await canvas.read();
const changed = await canvas.edit({ command: 'type.edit', params: { layer: title.id, text: '温润质感' }, label: '验收中文标题' }, state.revision);
const afterText = changed.context.layers.find(l => l.id === title.id);
assert.equal(afterText.text, '温润质感');
assert.equal(afterText.appearance.font, title.appearance.font);
assert.equal(afterText.appearance.sizePt, title.appearance.sizePt);
await canvas.call('ui.screenshot', { path: path.join(config.output, 'mechanics-step-text.png'), focus: false });
const moved = await canvas.edit({ command: 'layer.translate', params: { layer: title.id, dx: 12, dy: 0 }, label: '验收移动12px' }, changed.revision);
assert.notDeepEqual(moved.context.layers.find(l => l.id === title.id).appearance.transform, afterText.appearance.transform);
await canvas.end('画布已完成两步修改,可在同一份历史里撤销。');
await command('edit.undo'); await command('edit.undo');
state = await canvas.begin('本地撤销恢复检查');
assert.deepEqual(state.context.layers, original.layers);
await canvas.end('撤销后文字、位置、字体及其他图层与原稿一致。');
await command('edit.redo'); await command('edit.redo');
state = await canvas.begin('本地接手检查:排队后立即接手');
const beforeTakeover = state.context.layers;
await canvas.action('pi-edit', { revision: state.revision, edit: { command: 'type.edit', params: { layer: title.id, text: '这条不应执行' }, label: '应被接手拦截' } });
await canvas.call('jiansun.action', { action: 'handover', input: {} });
await new Promise(r => setTimeout(r, 700));
state = await canvas.read();
assert.equal(state.allowed, false); assert.equal(state.queued, 0);
assert.deepEqual(state.context.layers, beforeTakeover);
await assert.rejects(canvas.action('pi-edit', { revision: state.revision, edit: { command: 'layer.translate', params: { layer: title.id, dx: 1, dy: 0 }, label: '接手后禁止修改' } }), /接手|修改/);
await canvas.call('ui.screenshot', { path: path.join(config.output, 'mechanics-handover.png'), focus: false });
await canvas.end('已接手,后续操作已停止。');
await command('edit.undo'); await command('edit.undo');
state = await canvas.begin('最终原稿核对');
assert.deepEqual(state.context.layers, original.layers);
await canvas.end('画布机制检查通过;已恢复原稿。');
const result = { kind: 'host-mechanics-only', actualModelCalls: 0, title: { id: title.id, name: title.name, font: title.appearance.font }, width: original.width, height: original.height, passed: ['two visible queued edits', 'same undo/redo history', 'queued takeover', 'reject later edits', 'full layer state restored'] };
await atomic(path.join(config.output, 'canvas-mechanics.json'), result);
console.log(JSON.stringify(result, null, 2));
} finally { canvas.close(); }