补齐平台订阅、外部登录、CRM 同步与用户端页面;Nginx 转发用户端接口,登录页使用 Google 与 Apple 标志。 Co-authored-by: Cursor <cursoragent@cursor.com>
260 行
10 KiB
TypeScript
260 行
10 KiB
TypeScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import defaultI18n from '../src/locale/index.ts';
|
||
|
||
const adminRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||
const translationPagePath = path.join(adminRoot, 'src/pages/ai/translation/index.tsx');
|
||
const translationLocalePath = path.join(adminRoot, 'src/pages/ai/translation/locale/index.ts');
|
||
const routesPath = path.join(adminRoot, 'src/routes.ts');
|
||
|
||
test('1. 路由与主词典必须注册 ai/translation 与 menu.ai.translation', () => {
|
||
assert.ok(fs.existsSync(routesPath), 'routes.ts 必须存在');
|
||
const routesContent = fs.readFileSync(routesPath, 'utf-8');
|
||
|
||
assert.ok(
|
||
routesContent.includes("'ai/translation'") || routesContent.includes('"ai/translation"'),
|
||
'routes.ts 必须注册 key 为 ai/translation 的路由'
|
||
);
|
||
assert.ok(
|
||
routesContent.includes("'menu.ai.translation'") || routesContent.includes('"menu.ai.translation"'),
|
||
'routes.ts 必须注册 name 为 menu.ai.translation'
|
||
);
|
||
|
||
assert.ok(defaultI18n['en-US']['menu.ai.translation'], '主词典 en-US 必须包含 menu.ai.translation');
|
||
assert.ok(defaultI18n['zh-CN']['menu.ai.translation'], '主词典 zh-CN 必须包含 menu.ai.translation');
|
||
});
|
||
|
||
test('2. ai/translation 国际化语言包必须包含中英文案且零 any', () => {
|
||
assert.ok(fs.existsSync(translationLocalePath), '翻译页面语言包必须存在');
|
||
const localeContent = fs.readFileSync(translationLocalePath, 'utf-8');
|
||
|
||
assert.ok(localeContent.includes("'en-US'"), '必须包含 en-US 词条');
|
||
assert.ok(localeContent.includes("'zh-CN'"), '必须包含 zh-CN 词条');
|
||
assert.ok(localeContent.includes('aiTranslation.playground.submit'), '必须包含实时润色提交按钮文案');
|
||
assert.ok(localeContent.includes('aiTranslation.glossary.action.add'), '必须包含术语库添加按钮文案');
|
||
assert.ok(localeContent.includes('aiTranslation.glossary.columns.sourceTerm'), '必须包含源术语表头文案');
|
||
assert.ok(localeContent.includes('aiTranslation.glossary.columns.targetTerm'), '必须包含目标术语表头文案');
|
||
assert.ok(!/\bany\b/.test(localeContent), 'locale 文件禁止使用 any');
|
||
});
|
||
|
||
test('3. ai/translation 页面必须实现实时润色与术语库管理,使用 Arco 组件与规范 API', () => {
|
||
assert.ok(fs.existsSync(translationPagePath), '润色中心页面必须存在');
|
||
const content = fs.readFileSync(translationPagePath, 'utf-8');
|
||
|
||
// Arco 组件库引入
|
||
assert.ok(
|
||
content.includes("from '@arco-design/web-react'"),
|
||
'必须从 @arco-design/web-react 引入组件'
|
||
);
|
||
assert.ok(content.includes('Table'), '必须使用 Arco Table');
|
||
assert.ok(content.includes('Form'), '必须使用 Arco Form');
|
||
assert.ok(content.includes('Modal'), '必须使用 Arco Modal');
|
||
assert.ok(content.includes('Select'), '必须使用 Arco Select');
|
||
assert.ok(content.includes('Tag'), '必须使用 Arco Tag');
|
||
assert.ok(content.includes('Input'), '必须使用 Arco Input');
|
||
assert.ok(content.includes('Popconfirm'), '必须使用 Arco Popconfirm');
|
||
assert.ok(content.includes('Upload'), '必须使用 Arco Upload 实现批量导入');
|
||
|
||
// 接口请求
|
||
assert.ok(
|
||
content.includes('/api/admin/ai/localize/localize') || content.includes('admin/ai/localize/localize'),
|
||
'必须调用润色接口 /api/admin/ai/localize/localize'
|
||
);
|
||
assert.ok(
|
||
content.includes('admin/ai/glossary/page'),
|
||
'必须请求术语库分页接口 /api/admin/ai/glossary/page'
|
||
);
|
||
assert.ok(
|
||
content.includes('admin/ai/glossary/add'),
|
||
'必须请求术语库新增接口 /api/admin/ai/glossary/add'
|
||
);
|
||
assert.ok(
|
||
content.includes('admin/ai/glossary/update'),
|
||
'必须请求术语库更新接口 /api/admin/ai/glossary/update'
|
||
);
|
||
assert.ok(
|
||
content.includes('admin/ai/glossary/delete'),
|
||
'必须请求术语库删除接口 /api/admin/ai/glossary/delete'
|
||
);
|
||
assert.ok(
|
||
content.includes('admin/ai/glossary/batchImport'),
|
||
'必须请求术语库批量导入接口 /api/admin/ai/glossary/batchImport'
|
||
);
|
||
|
||
// 业务契约复用与规范
|
||
assert.ok(
|
||
content.includes("from '@/types/omni'"),
|
||
'必须复用 admin/src/types/omni.ts 中的契约类型'
|
||
);
|
||
assert.ok(content.includes('AiGlossaryItem'), '必须使用 AiGlossaryItem 契约类型');
|
||
assert.ok(
|
||
content.includes('code === 1000') || content.includes('code ===1000'),
|
||
'必须按 Cool-Admin code === 1000 规范判断响应成功'
|
||
);
|
||
assert.ok(content.includes('Message.error'), '错误提示必须使用 Arco Message.error');
|
||
assert.ok(!content.includes('Mock.js') && !content.includes("from 'mockjs'"), '禁止用 Mock.js 假数据冒充成功');
|
||
assert.ok(!content.includes('<Pagination'), '禁止手写分页器,必须使用 Table 自带分页');
|
||
});
|
||
|
||
test('4. 必须遵守瑞士国际主义设计规范与 Token 约束 (零 emoji, 零 any, 零硬编码颜色)', () => {
|
||
const content = fs.readFileSync(translationPagePath, 'utf-8');
|
||
|
||
assert.ok(!/\bany\b/.test(content), '页面严禁使用 any 类型');
|
||
assert.ok(content.includes('lightColors'), '颜色必须引用 lightColors token');
|
||
assert.ok(!/#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b/.test(content), '禁止硬编码 HEX 颜色');
|
||
|
||
// 禁止 emoji (常见 emoji Unicode 范围)
|
||
const emojiRegex = /[\u{1F300}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/u;
|
||
assert.ok(!emojiRegex.test(content), '页面禁止包含任何 emoji 表情符号');
|
||
|
||
// 确保只有一个主 CTA (type="primary")
|
||
const primaryButtonMatches = content.match(/type="primary"/g) || [];
|
||
assert.equal(
|
||
primaryButtonMatches.length,
|
||
1,
|
||
'瑞士国际主义风格约束:整屏只允许 1 个主 CTA (type="primary")'
|
||
);
|
||
});
|
||
|
||
function readTranslationPage(): string {
|
||
return fs.readFileSync(translationPagePath, 'utf-8');
|
||
}
|
||
|
||
function readTranslationLocale(): string {
|
||
return fs.readFileSync(translationLocalePath, 'utf-8');
|
||
}
|
||
|
||
test('5. R17: 术语导入 / 润色 / 保存仅在 code===1000 时 Message.success,文案走 locale', () => {
|
||
const content = readTranslationPage();
|
||
const localeContent = readTranslationLocale();
|
||
|
||
assert.ok(content.includes('Message.success'), '真实成功仍应 Message.success');
|
||
assert.ok(content.includes('Message.error'), '失败必须 Message.error');
|
||
|
||
// 每个 Message.success 必须出现在 code === 1000 成功分支内
|
||
const successMatches = [...content.matchAll(/Message\.success\s*\(/g)];
|
||
assert.ok(successMatches.length > 0, '至少应有一处成功提示');
|
||
for (const match of successMatches) {
|
||
const idx = match.index ?? 0;
|
||
const preceding = content.slice(Math.max(0, idx - 400), idx);
|
||
assert.ok(
|
||
/code\s*===\s*1000/.test(preceding),
|
||
`Message.success 必须位于 code === 1000 分支内,附近片段: ${preceding.slice(-100)}`
|
||
);
|
||
}
|
||
|
||
// 润色:code===1000 且有 data 才成功
|
||
assert.ok(
|
||
/LOCALIZE_API|localize\/localize[\s\S]{0,900}code\s*===\s*1000[\s\S]{0,160}Message\.success/.test(
|
||
content
|
||
) ||
|
||
/handlePolishSubmit[\s\S]{0,900}code\s*===\s*1000[\s\S]{0,200}Message\.success/.test(
|
||
content
|
||
),
|
||
'润色提交须在 code === 1000 后 Message.success'
|
||
);
|
||
assert.ok(
|
||
/code\s*===\s*1000\s*&&\s*.*data|res\.code\s*===\s*1000\s*&&\s*res\.data/.test(content),
|
||
'润色成功须同时校验 code === 1000 与 data'
|
||
);
|
||
|
||
// 术语保存:add/update 仅在 code===1000 成功
|
||
assert.ok(
|
||
/handleSaveTerm[\s\S]{0,900}code\s*===\s*1000[\s\S]{0,160}Message\.success/.test(content),
|
||
'术语保存须在 code === 1000 后 Message.success'
|
||
);
|
||
|
||
// 术语导入:batchImport 仅在 code===1000 成功
|
||
assert.ok(
|
||
/batchImport|GLOSSARY_IMPORT_API|handleBatchImportFile[\s\S]{0,900}code\s*===\s*1000[\s\S]{0,160}Message\.success/.test(
|
||
content
|
||
),
|
||
'术语导入须在 code === 1000 后 Message.success'
|
||
);
|
||
|
||
// 成功/失败文案走 locale
|
||
for (const key of [
|
||
'aiTranslation.playground.success',
|
||
'aiTranslation.request.saveSuccess',
|
||
'aiTranslation.request.importSuccess',
|
||
'aiTranslation.request.importFail',
|
||
'aiTranslation.request.fail',
|
||
]) {
|
||
assert.ok(localeContent.includes(`'${key}'`), `locale 必须包含 ${key}`);
|
||
assert.ok(
|
||
content.includes(`t['${key}']`) || content.includes(key),
|
||
`页面必须引用 ${key}`
|
||
);
|
||
}
|
||
});
|
||
|
||
test('6. R17: 失败 / 空文件 / code 非 1000 不得 Message.success,禁止本地插入假术语', () => {
|
||
const content = readTranslationPage();
|
||
|
||
// catch / else 失败分支附近不得出现 Message.success
|
||
const failBlocks = [
|
||
...content.matchAll(
|
||
/catch\s*(?:\([^)]*\))?\s*\{[\s\S]{0,320}\}|else\s*\{[\s\S]{0,320}\}/g
|
||
),
|
||
];
|
||
for (const block of failBlocks) {
|
||
const text = block[0];
|
||
if (/Message\.error/.test(text)) {
|
||
assert.ok(
|
||
!/Message\.success/.test(text),
|
||
`失败分支内不得 Message.success: ${text.slice(0, 120)}`
|
||
);
|
||
}
|
||
}
|
||
|
||
assert.ok(
|
||
!/catch[\s\S]{0,220}Message\.success/.test(content),
|
||
'catch 失败后禁止 Message.success'
|
||
);
|
||
assert.ok(
|
||
!/code\s*!==\s*1000[\s\S]{0,140}Message\.success/.test(content),
|
||
'code 非 1000 时禁止 Message.success'
|
||
);
|
||
|
||
// 空文件:须显式拦截并用 Message.error,不得 success
|
||
assert.ok(
|
||
/file\.size\s*===\s*0|!file\.size|Number\(file\.size\)\s*===\s*0/.test(content),
|
||
'空文件须显式校验 file.size === 0'
|
||
);
|
||
assert.ok(
|
||
/file\.size[\s\S]{0,220}Message\.error/.test(content) ||
|
||
/size\s*===\s*0[\s\S]{0,220}Message\.error/.test(content),
|
||
'空文件须 Message.error,不得 Message.success'
|
||
);
|
||
assert.ok(
|
||
/!Array\.isArray|Array\.isArray\(parsed\)/.test(content),
|
||
'导入须校验 JSON 为数组,非法结构不得假装成功'
|
||
);
|
||
assert.ok(
|
||
/Array\.isArray\(parsed\)[\s\S]{0,180}Message\.error|!Array\.isArray\(parsed\)[\s\S]{0,120}Message\.error/.test(
|
||
content
|
||
),
|
||
'非数组导入须 Message.error'
|
||
);
|
||
|
||
// 禁止本地乐观插入假术语(未过接口就 setGlossaryData 追加)
|
||
assert.ok(
|
||
!/setGlossaryData\(\s*\[[\s\S]{0,80}\.\.\.[\s\S]{0,80}glossaryData/.test(content) &&
|
||
!/setGlossaryData\(\s*\[[\s\S]{0,120}sourceTerm/.test(content),
|
||
'禁止本地插入假术语,导入/保存成功后应 refreshGlossary 拉真实数据'
|
||
);
|
||
assert.ok(
|
||
/refreshGlossary|setReloadTick/.test(content),
|
||
'导入/保存成功后须刷新术语列表'
|
||
);
|
||
|
||
// code 非 1000 分支须 Message.error
|
||
assert.ok(
|
||
/Message\.error\(\s*res\.message\s*\|\|/.test(content),
|
||
'code 非 1000 须 Message.error(res.message || fallback)'
|
||
);
|
||
});
|