gongxiangp2/apps/detail-agent-pi/test/enterprise-rules.test.mjs

67 lines
4.7 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 = { brandRuleSources: { s1: 'references/gujia-visual-style.md' }, 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');
const next = await importEnterpriseRules(f.platform, f.root);
assert.equal(next.brands.find(brand => brand.name === '顾家').profile.tone, '新的温暖风格');
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('a source outside the authorized directory is rejected', 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), /超出授权目录/);
} finally { await cleanup(f.dir); }
});
test('a missing source is rejected without substituting default rules', async () => {
const f = await fixture();
try {
await fs.unlink(path.join(f.root, 'references/gujia-visual-style.md'));
await assert.rejects(importEnterpriseRules(f.platform, f.root), { code: 'ENOENT' });
} finally { await cleanup(f.dir); }
});
test('a registered brand must have rule content in a recognized section', async () => {
const f = await fixture();
try {
for (const text of ['不再符合规范结构', '## 品牌风格\n', '## 人物素材索引\n### 视觉规范\n不属于品牌规则']) {
await f.write('references/gujia-visual-style.md', text);
await assert.rejects(importEnterpriseRules(f.platform, f.root), { code: 'RULE_BRAND_SOURCE' });
}
} finally { await cleanup(f.dir); }
});