Support explicit current-output A/B fill, protected edited drafts, scoped cloud update offers and atomic native restore with stale-open CAS. Preserve original unknown requests, improve Pi conversation continuity and native DPI/navigation reads, and publish the authenticated template metadata contract. Validated: native backend 172 passed/5 environment tests ignored, UI 110, Pi 53, template/rule API 24 and native cloud contract 6; strict Clippy, layering and wasm. Real S01 selected B, edited/undo-redo, PCraft reopen and layered PSD roundtrip pass with exact pixels. Full nine-output generation and Photoshop acceptance remain open.
105 lines
7.2 KiB
JavaScript
105 lines
7.2 KiB
JavaScript
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 { randomUUID } from 'node:crypto';
|
|
import { createAssistantMessageEventStream, getCurrentSystemPrompt } from '@earendil-works/pi-ai/compat';
|
|
import { ProjectStore } from '../src/store.mjs';
|
|
import { evaluate, ToolLab } from '../src/tool-lab.mjs';
|
|
import { DetailPiAgent } from '../src/agent.mjs';
|
|
import { cleanup } from './cleanup.mjs';
|
|
|
|
const identity = () => ({ scope: 'a'.repeat(64), project: randomUUID() });
|
|
const binding = shop => ({ brandId: 'qa-brand', shopId: shop, categoryId: 'qa-desk', platform: 'qa-web', version: '1', source: 'explicit-local-qa-fixture', rules: [`仅使用${shop}的规则`] });
|
|
const root = () => fs.mkdtemp(path.join(os.tmpdir(), 'jiansun-pi-'));
|
|
function stream(content) {
|
|
const result = createAssistantMessageEventStream();
|
|
const message = { role: 'assistant', api: 'openai-completions', provider: 'jiansun-native-98k', model: 'qa-model', content, stopReason: content.some(c => c.type === 'toolCall') ? 'toolUse' : 'stop', timestamp: Date.now(), usage: { input: 10, output: 10, cacheRead: 0, cacheWrite: 0, totalTokens: 20, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } } };
|
|
queueMicrotask(() => { result.push({ type: 'start', partial: message }); result.push({ type: 'done', reason: message.stopReason, message }); result.end(message); });
|
|
return result;
|
|
}
|
|
const info = scope => ({ accountScope: scope, model: { id: 'qa-model', protocol: 'openai-chat' }, pending: [] });
|
|
|
|
test('generated tools have no Node/FS/network and cannot run forever', async () => {
|
|
assert.deepEqual(await evaluate('function main(){return [typeof process,typeof require,typeof fetch,typeof Deno]}', {}), ['undefined', 'undefined', 'undefined', 'undefined']);
|
|
await assert.rejects(evaluate('function main(){while(true){}}', {}, 30), /隔离工具失败/);
|
|
await assert.rejects(evaluate('function main(){return import("node:fs")}', {}), /工具必须返回|隔离工具失败/);
|
|
assert.equal(await evaluate('function main(a){return a.x+1}', { x: 2 }), 3);
|
|
});
|
|
test('only tested immutable tool versions execute; failed tests cannot replace', async () => {
|
|
const dir = await root(), lab = new ToolLab(dir);
|
|
const args = { name: 'add_one', description: 'QA', source: 'function main(i){return i+1}', tests: [{ input: 0, expected: 1 }, { input: -1, expected: 0 }, { input: 10, expected: 11 }] };
|
|
const saved = await lab.develop(args);
|
|
assert.equal((await lab.run('add_one', 4)).result, 5);
|
|
await assert.rejects(lab.develop({ ...args, source: 'function main(i){return i+2}' }), /测试1未通过/);
|
|
assert.equal((await lab.run('add_one', 4)).version, saved.version);
|
|
await assert.rejects(lab.run('../other', {}), /名称无效/);
|
|
await cleanup(dir);
|
|
});
|
|
test('snapshots, memories, sessions are isolated by account/shop/category/version', async () => {
|
|
const dir = await root(), id = identity();
|
|
const a = await ProjectStore.open(dir, id, binding('A'));
|
|
await a.recordUserEvidence('产品颜色是米白');
|
|
await a.remember({ key: 'color', value: '米白', evidence: '颜色是米白' }, [{ role: 'user', content: '产品颜色是米白' }]);
|
|
await a.recordUserEvidence('产品改为胡桃色');
|
|
await a.remember({ key: 'color', value: '胡桃', evidence: '改为胡桃色' }, [{ role: 'user', content: '产品改为胡桃色' }]);
|
|
assert.equal((await a.context()).confirmedMemory.color.previous, '米白');
|
|
await assert.rejects(a.remember({ key: 'certified', value: '已认证', evidence: '已认证测试' }, []), /用户已明确/);
|
|
const b = await ProjectStore.open(dir, id, binding('B'));
|
|
assert.notEqual(a.dir, b.dir);
|
|
assert.deepEqual((await b.context()).confirmedMemory, {});
|
|
const restored = await ProjectStore.open(dir, id, binding('A'));
|
|
assert.equal((await restored.context()).confirmedMemory.color.value, '胡桃');
|
|
const other = await ProjectStore.open(dir, { ...id, scope: 'b'.repeat(64) }, binding('A'));
|
|
assert.deepEqual((await other.context()).confirmedMemory, {});
|
|
await cleanup(dir);
|
|
});
|
|
test('actual Pi SDK executes memory tools, persists full transcript and reinjects after restart', async () => {
|
|
const dir = await root(), id = identity(), store = await ProjectStore.open(dir, id, binding('A'));
|
|
let calls = 0;
|
|
const transport = { info: info(id.scope), stream(model, ctx) {
|
|
assert.match(getCurrentSystemPrompt(ctx.messages), /"shopId":"A"/);
|
|
calls++;
|
|
if (calls === 1) return stream([{ type: 'toolCall', id: 'qa-call-1', name: 'memory_set', arguments: { key: 'color', value: '胡桃色', evidence: '颜色是胡桃色' } }]);
|
|
assert.match(getCurrentSystemPrompt(ctx.messages), /"color"/);
|
|
return stream([{ type: 'text', text: '已记录胡桃色' }]);
|
|
} };
|
|
let host = await DetailPiAgent.open({ store, transport });
|
|
assert.deepEqual(host.session.getActiveToolNames().sort(), ['context_read','memory_search','memory_set','tool_develop','tool_run'].sort());
|
|
await host.prompt('这个测试产品颜色是胡桃色,请记住。');
|
|
const messages = host.session.messages.length, entries = host.manager.getEntryCount();
|
|
await host.dispose();
|
|
host = await DetailPiAgent.open({ store, transport });
|
|
assert.equal(host.session.messages.length, messages);
|
|
assert.equal(host.manager.getEntryCount(), entries);
|
|
await host.prompt('继续:颜色是什么?');
|
|
assert.equal(host.session.settingsManager.getRetryEnabled(), false);
|
|
await host.dispose(); await cleanup(dir);
|
|
});
|
|
test('unknown preflight forbids dispatch and does not create a new prompt', async () => {
|
|
const dir = await root(), id = identity(), store = await ProjectStore.open(dir, id, binding('A'));
|
|
let calls = 0;
|
|
const transport = { info: info(id.scope), stream() { calls++; return stream([{ type:'text',text:'x' }]); } };
|
|
const broker = { info: async () => ({ ...info(id.scope), pending: [{ id: 'original', phase: 'unknown' }] }) };
|
|
const host = await DetailPiAgent.open({ store, broker, transport });
|
|
await assert.rejects(host.prompt('不应发送'), /未知请求/);
|
|
assert.equal(calls, 0); assert.equal(host.session.messages.length, 0);
|
|
await host.dispose(); await cleanup(dir);
|
|
});
|
|
test('actual Pi SDK does not retry an overload or truncated completion', async () => {
|
|
const dir = await root(), id = identity(), store = await ProjectStore.open(dir, id, binding('A'));
|
|
let calls = 0;
|
|
const host = await DetailPiAgent.open({ store, transport: { info: info(id.scope), stream(model) {
|
|
calls++;
|
|
const out = createAssistantMessageEventStream();
|
|
const error = { role: 'assistant', api: model.api, provider: model.provider, model: model.id, content: [], stopReason: 'error', errorMessage: 'HTTP 503 overload; original outcome unknown', timestamp: Date.now(), usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } } };
|
|
queueMicrotask(() => { out.push({ type: 'error', reason: 'error', error }); out.end(error); });
|
|
return out;
|
|
} } });
|
|
await assert.rejects(host.prompt('验证自动重试已关闭'), /503/);
|
|
assert.equal(calls, 1);
|
|
assert.ok(host.manager.getEntries().some(e => e.type === 'message' && e.message.role === 'user'));
|
|
await host.dispose(); await cleanup(dir);
|
|
});
|