gongxiangp2/apps/detail-agent-pi/test/enterprise-rules.test.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

50 lines
4.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import { importEnterpriseRules } from '../src/enterprise-rules.mjs';
import { resolveRules } from '../src/rules.mjs';
import { cleanup } from './cleanup.mjs';
async function fixture() {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'pi-recovery-')), root = path.join(dir, 'library');
const write = async (relative, text) => { const p = path.join(root, relative); await fs.mkdir(path.dirname(p), { recursive: true }); await fs.writeFile(p, text); };
await write('references/brand-style-prompts.md', '### 顾家\n见专门规范\n### 欧意朗\n#### 品牌风格:现代克制\n有依据的规范\n');
await write('references/gujia-visual-style.md', '## 品牌风格:温暖克制\n原始风格甲\n## 人物素材索引\n不向模型复制整张资源索引\n## 提示词与验收\n产品外观不能改变\n');
const template = { template_id: 'T', template_name: '顾家电脑桌', brand: '顾家', category: '电脑桌', status: 'ready', mapping_version: 'v1', canvas_px: [1440, 46930], usage_guide: 'references/guide.md', mapping_asset_path: 'assets/mapping.json' };
await write('template-adapter/references/template-catalog.json', JSON.stringify({ templates: [template] }));
await write('template-adapter/references/guide.md', '## 屏号和固定元素\n模板事实不是新产品事实\n## 主图组件\n不在详情范围\n');
await write('template-adapter/assets/mapping.json', JSON.stringify({ template_id: 'T', brand: '顾家', category: '电脑桌', screens: [{ screen_id: 'S01', title: '首屏', purpose: '展示当前产品', page_role: 'content' }] }));
const platform = { capturedAt: '2026-01-01', shops: [{ id: 's1', name: '顾家' }, { id: 's2', name: '欧意朗' }, { id: 's3', name: '未知店' }], categories: [{ id: 'c1', name: '电脑桌' }, { id: 'c2', name: '书桌' }] };
return { dir, root, write, platform, template };
}
test('enterprise import uses exact real catalog identities and sourced category bindings without inventing coverage', async () => {
const f = await fixture();
try {
const c = await importEnterpriseRules(f.platform, f.root);
assert.equal(c.brands.length, 2); assert.equal(c.bindings.length, 1);
const a = resolveRules({ brandId: 'enterprise:s1', shopId: 's1', categoryId: 'c1' }, c);
assert.equal(a.shopName, '顾家'); assert.equal(a.categoryName, '电脑桌'); assert.equal(a.templateSnapshot.id, 'T');
assert.throws(() => resolveRules({ brandId: 'enterprise:s1', shopId: 's1', categoryId: 'c2' }, c), /尚未配置/);
assert.throws(() => resolveRules({ brandId: 'enterprise:s2', shopId: 's2', categoryId: 'c1' }, c), /尚未配置/);
const before = JSON.stringify(a);
await f.write('references/gujia-visual-style.md', '## 品牌风格:新的温暖风格\n## 人物素材索引\n索引\n## 提示词与验收\n新验收规范\n');
const next = await importEnterpriseRules(f.platform, f.root);
assert.notEqual(next.revision, c.revision);
assert.notEqual(resolveRules({ brandId: 'enterprise:s1', shopId: 's1', categoryId: 'c1' }, next).version, a.version);
assert.equal(JSON.stringify(a), before);
assert.ok(c.sourceFiles.every(s => /^[a-f0-9]{64}$/.test(s.sha256)));
} finally { await cleanup(f.dir); }
});
test('missing or escaped knowledge sources fail closed, with no fixture fallback', async () => {
const f = await fixture();
try {
await fs.writeFile(path.join(f.dir, 'outside.json'), '{}');
await f.write('template-adapter/references/template-catalog.json', JSON.stringify({ templates: [{ ...f.template, mapping_asset_path: '../../outside.json' }] }));
await assert.rejects(importEnterpriseRules(f.platform, f.root), /超出授权目录/);
await f.write('references/gujia-visual-style.md', '不再符合规范结构');
await assert.rejects(importEnterpriseRules(f.platform, f.root), /来源结构已改变/);
} finally { await cleanup(f.dir); }
});