文件
OmniCard/admin/tests/nfc-rules.test.ts
wikiboss 62e0ae4435 feat: 同步管理端、用户端与后端业务,并接通线上 Web 接口
补齐平台订阅、外部登录、CRM 同步与用户端页面;Nginx 转发用户端接口,登录页使用 Google 与 Apple 标志。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-23 18:01:47 +08:00

243 行
10 KiB
TypeScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

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';
const adminRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const rulesPagePath = path.join(adminRoot, 'src/pages/nfc/rules/index.tsx');
const rulesLocalePath = path.join(adminRoot, 'src/pages/nfc/rules/locale/index.ts');
const routesPath = path.join(adminRoot, 'src/routes.ts');
const mainLocalePath = path.join(adminRoot, 'src/locale/index.ts');
test('1. nfc/rules 页面必须与后端 /admin/nfc/rule 接口契约完全匹配', () => {
assert.ok(fs.existsSync(rulesPagePath), 'NFC 场景路由规则页面必须存在');
const content = fs.readFileSync(rulesPagePath, 'utf-8');
assert.ok(
content.includes('admin/nfc/rule/page'),
'必须请求 POST /api/admin/nfc/rule/page'
);
assert.ok(
content.includes('admin/nfc/rule/add'),
'必须请求 POST /api/admin/nfc/rule/add'
);
assert.ok(
content.includes('admin/nfc/rule/update'),
'必须请求 POST /api/admin/nfc/rule/update'
);
assert.ok(
content.includes('admin/nfc/rule/delete'),
'必须请求 POST /api/admin/nfc/rule/delete'
);
assert.ok(
content.includes('admin/nfc/rule/resolveScene'),
'必须支持调用 POST /api/admin/nfc/rule/resolveScene 进行场景解析'
);
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('Message.success'), '成功必须使用 Arco Message.success');
assert.ok(
!content.includes('Mock.js') && !content.includes("from 'mockjs'"),
'禁止用 Mock.js 假数据冒充成功'
);
assert.ok(!/\bany\b/.test(content), '页面中禁止使用 any');
});
test('2. nfc/rules 必须复用 Arco 组件、设计 Token 与场景路由契约', () => {
const content = fs.readFileSync(rulesPagePath, 'utf-8');
// Arco 组件
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('TimePicker'), '必须使用 Arco TimePicker');
assert.ok(content.includes('Select'), '必须使用 Arco Select');
assert.ok(content.includes('Switch'), '必须使用 Arco Switch');
assert.ok(content.includes('Tag'), '必须使用 Arco Tag');
assert.ok(content.includes('Popconfirm'), '必须使用 Arco Popconfirm');
assert.ok(
content.includes("from '@arco-design/web-react'"),
'必须从 @arco-design/web-react 引入组件'
);
assert.ok(!content.includes('<Pagination'), '禁止手写分页器,必须使用 Table 自带分页');
// 契约复用
assert.ok(content.includes('NFCSceneRule'), '必须使用 NFCSceneRule 类型');
assert.ok(content.includes('NFCSceneMode'), '必须使用 NFCSceneMode 类型');
assert.ok(content.includes("from '@/types/omni'"), '必须复用 admin/src/types/omni.ts');
// 场景字段
assert.ok(content.includes('startTime'), '必须支持时段 startTime');
assert.ok(content.includes('endTime'), '必须支持时段 endTime');
assert.ok(content.includes('countryCode'), '必须支持地理国家 countryCode');
assert.ok(content.includes('weekdays'), '必须支持生效星期 weekdays');
assert.ok(content.includes('targetCardId'), '必须支持目标名片 targetCardId');
assert.ok(content.includes('sceneMode'), '必须支持场景模式 sceneMode');
assert.ok(content.includes('deviceId'), '必须支持设备 ID deviceId');
// 设计 Token 与单主 CTA 约束
assert.ok(content.includes('lightColors'), '颜色必须引用 lightColors token');
assert.ok(!/#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b/.test(content), '禁止硬编码颜色代码');
assert.ok(
(content.match(/type="primary"/g) || []).length === 1,
'一屏只允许 1 个主 CTA (type="primary")'
);
// 禁止 emoji
const emojiRegex = /[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/u;
assert.ok(!emojiRegex.test(content), '页面严禁包含 emoji');
});
test('3. nfc/rules 路由与国际化配置必须完整', () => {
// 检查 routes.ts
assert.ok(fs.existsSync(routesPath), 'routes.ts 必须存在');
const routesContent = fs.readFileSync(routesPath, 'utf-8');
assert.ok(
routesContent.includes("key: 'nfc/rules'"),
'routes.ts 中必须注册 key: nfc/rules'
);
assert.ok(
routesContent.includes("name: 'menu.nfc.rules'"),
'routes.ts 中必须声明 name: menu.nfc.rules'
);
// 检查主语言包
assert.ok(fs.existsSync(mainLocalePath), '主语言包 index.ts 必须存在');
const mainLocaleContent = fs.readFileSync(mainLocalePath, 'utf-8');
assert.ok(
mainLocaleContent.includes("'menu.nfc.rules'"),
'主语言包必须包含 menu.nfc.rules 词条'
);
// 检查页面语言包
assert.ok(fs.existsSync(rulesLocalePath), '规则页面语言包必须存在');
const rulesLocaleContent = fs.readFileSync(rulesLocalePath, 'utf-8');
assert.ok(rulesLocaleContent.includes("'en-US'"), '页面语言包必须包含英文案');
assert.ok(rulesLocaleContent.includes("'zh-CN'"), '页面语言包必须包含中文案');
assert.ok(rulesLocaleContent.includes('nfcRules.request.fail'), '必须包含失败提示文案');
assert.ok(rulesLocaleContent.includes('nfcRules.columns.name'), '必须包含规则名称文案');
assert.ok(rulesLocaleContent.includes('nfcRules.columns.sceneMode'), '必须包含场景模式文案');
assert.ok(rulesLocaleContent.includes('nfcRules.columns.timeRange'), '必须包含生效时段文案');
assert.ok(rulesLocaleContent.includes('nfcRules.columns.countryCode'), '必须包含地理国家文案');
assert.ok(rulesLocaleContent.includes('nfcRules.action.create'), '必须包含新建规则文案');
assert.ok(rulesLocaleContent.includes('nfcRules.action.resolve'), '必须包含路由解析文案');
assert.ok(!/\bany\b/.test(rulesLocaleContent), 'locale 禁止使用 any');
});
test('4. placeholder 必须走 i18n,禁止硬编码英文 e.g. 占位', () => {
const content = fs.readFileSync(rulesPagePath, 'utf-8');
const rulesLocaleContent = fs.readFileSync(rulesLocalePath, 'utf-8');
const placeholderKeys = [
'nfcRules.filter.countryCode.placeholder',
'nfcRules.form.name.placeholder',
'nfcRules.form.deviceId.placeholder',
'nfcRules.form.targetCardId.placeholder',
'nfcRules.form.countryCode.placeholder',
'nfcRules.resolve.deviceId.placeholder',
'nfcRules.resolve.countryCode.placeholder',
];
for (const key of placeholderKeys) {
assert.ok(
content.includes(`t['${key}']`),
`页面 placeholder 必须使用 t['${key}']`
);
assert.ok(
rulesLocaleContent.includes(`'${key}'`),
`locale 必须包含 ${key}`
);
}
assert.ok(rulesLocaleContent.includes("'en-US'"), 'placeholder 必须有英文');
assert.ok(rulesLocaleContent.includes("'zh-CN'"), 'placeholder 必须有中文');
assert.ok(rulesLocaleContent.includes("'ar-SA'"), 'placeholder 必须有阿语');
assert.ok(!content.includes('e.g.'), '页面禁止硬编码 e.g. placeholder');
assert.ok(
!content.includes('US, AE, CN'),
'页面禁止硬编码国家筛选 placeholder'
);
assert.ok(
!content.includes('Gitex Dubai'),
'页面禁止硬编码规则名称 placeholder'
);
});
test('5. 新建规则地理字段必须为空,禁止预选迪拜/阿联酋/示例城市', () => {
const content = fs.readFileSync(rulesPagePath, 'utf-8');
// 新建时必须显式清空 countryCode,不得依赖隐式残留
assert.ok(
/handleOpenCreate[\s\S]*?setFieldsValue\(\{[\s\S]*?countryCode:\s*(undefined|null|['"]{2})/.test(
content
),
'handleOpenCreate 必须显式设置 countryCode 为空 (undefined/null/"")'
);
// 禁止把示例国家/城市写入表单初始值
const bannedGeoAssignments = [
/countryCode:\s*['"]AE['"]/i,
/countryCode:\s*['"]UAE['"]/i,
/countryCode:\s*['"]Dubai['"]/i,
/countryCode:\s*['"]CN['"]/,
/countryCode:\s*['"]US['"]/,
/countryCode:\s*['"]SG['"]/,
/city:\s*['"]Dubai['"]/i,
/city:\s*['"]迪拜['"]/,
];
for (const pattern of bannedGeoAssignments) {
assert.ok(
!pattern.test(content),
`禁止预填地理示例值: ${pattern}`
);
}
// Form.Item countryCode 禁止 defaultValue / initialValue 预选(仅检查该字段自身)
const countryCodeItems =
content.match(
/<Form\.Item[^>]*field=["']countryCode["'][\s\S]*?<\/Form\.Item>/g
) || [];
assert.ok(countryCodeItems.length > 0, '必须存在 countryCode Form.Item');
for (const item of countryCodeItems) {
assert.ok(
!/(defaultValue|initialValue)=["'][^"']+["']/.test(item),
'countryCode Form.Item 禁止 defaultValue/initialValue 预选'
);
}
// 页面源码不得把迪拜/阿联酋当表单默认展示(locale 占位文案不在本文件)
assert.ok(!content.includes('Dubai'), '页面禁止硬编码 Dubai');
assert.ok(!content.includes('迪拜'), '页面禁止硬编码迪拜');
assert.ok(!content.includes('阿联酋'), '页面禁止硬编码阿联酋');
assert.ok(!content.includes('United Arab Emirates'), '页面禁止硬编码 UAE 全称');
// 编辑仅回填接口 countryCode;无值时空串,不得塞示例
assert.ok(
/countryCode:\s*rule\.countryCode\s*\|\|\s*['"]{2}/.test(content),
'编辑必须用 rule.countryCode || "",无接口值则空'
);
// 模拟解析弹窗打开时必须重置地理字段,不得预填
assert.ok(
content.includes('resolveForm.resetFields()'),
'打开解析弹窗必须 resolveForm.resetFields() 清空地理等字段'
);
assert.ok(
/resolveForm\.setFieldsValue\(\{[\s\S]*?countryCode:\s*(undefined|null|['"]{2})/.test(
content
),
'打开解析弹窗必须显式清空 countryCode'
);
// 禁止任何 defaultValue/initialValue 指向示例国家码
assert.ok(
!/(defaultValue|initialValue)=["'](AE|UAE|Dubai|CN|US|SG)["']/i.test(content),
'禁止 defaultValue/initialValue 预选示例国家'
);
});