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(' { // 检查 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( /]*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 预选示例国家' ); });