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 { routes } from '../src/routes.ts'; import defaultI18n from '../src/locale/index.ts'; const adminRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const builderPagePath = path.join(adminRoot, 'src/pages/card/builder/index.tsx'); const builderLocalePath = path.join( adminRoot, 'src/pages/card/builder/locale/index.ts' ); function flattenRouteKeys(items: typeof routes): string[] { return items.flatMap((item) => [ item.key, ...(item.children ? flattenRouteKeys(item.children) : []), ]); } test('1. routes.ts 已注册 card/builder,对应 pages/card/builder', () => { const keys = flattenRouteKeys(routes); assert.ok(keys.includes('card/builder'), '应存在路由 key: card/builder'); const cardGroup = routes.find((item) => item.key === 'card'); assert.ok( cardGroup?.children?.some( (child) => child.key === 'card/builder' && child.name === 'menu.card.builder' ), 'card 分组下应有 menu.card.builder / card/builder' ); assert.ok(fs.existsSync(builderPagePath), '必须存在 pages/card/builder/index.tsx'); }); test('2. locale 必须包含 menu.card.builder 中英文案', () => { assert.equal(defaultI18n['en-US']['menu.card.builder'], 'Bento Builder'); assert.equal(defaultI18n['zh-CN']['menu.card.builder'], 'Bento 搭建器'); }); test('3. builder 必须请求 getBlocksByCardId / batchSaveLayout 并支持 colSpan', () => { const content = fs.readFileSync(builderPagePath, 'utf-8'); assert.ok( content.includes('getBlocksByCardId'), '必须请求 POST /api/admin/card/bento/getBlocksByCardId' ); assert.ok( content.includes('batchSaveLayout'), '必须请求 POST /api/admin/card/bento/batchSaveLayout' ); assert.ok(content.includes('colSpan'), '必须读写 colSpan'); assert.ok(content.includes('rowSpan'), '必须读写 rowSpan'); assert.ok( content.includes('{ cardId: Number(id) }'), '拉取块必须提交 { cardId: Number(id) }' ); assert.ok( content.includes('code === 1000') || content.includes('code ===1000'), '必须按 Cool-Admin code === 1000 判断成功' ); }); test('4. builder 必须覆盖 hero/case/article 等 Bento 类型', () => { const content = fs.readFileSync(builderPagePath, 'utf-8'); assert.ok(content.includes('hero'), '组件库必须包含 hero'); assert.ok(content.includes('case'), '组件库必须包含 case'); assert.ok(content.includes('article'), '组件库必须包含 article'); assert.ok(content.includes('social'), '组件库必须包含 social'); assert.ok(content.includes('video'), '组件库必须包含 video'); assert.ok(content.includes('form'), '组件库必须包含 form'); assert.ok(content.includes('BentoBlock'), '必须复用 BentoBlock 类型'); assert.ok(content.includes('BentoBlockType'), '必须复用 BentoBlockType 类型'); }); test('5. builder 必须用 query id、Arco 三栏与单一保存 CTA', () => { const content = fs.readFileSync(builderPagePath, 'utf-8'); assert.ok(content.includes('useLocation'), '名片 id 必须来自 useLocation'); assert.ok( content.includes('query-string') || content.includes('queryString'), '必须用 query-string 解析 id' ); assert.ok(content.includes('Layout'), '必须使用 Arco Layout'); assert.ok(content.includes('Input.TextArea'), '必须使用 Input.TextArea'); assert.ok(content.includes('Tabs'), '必须使用 Arco Tabs'); assert.ok( content.includes("from '@arco-design/web-react'"), '必须从 @arco-design/web-react 引入组件' ); assert.ok( content.includes("from '@/style/tokens'"), '颜色必须引用 admin/src/style/tokens' ); assert.ok(content.includes('cardBuilder.save') || content.includes('保存排版'), '必须提供保存排版主 CTA'); assert.ok(content.includes('Empty') || content.includes('cardBuilder.empty'), '缺 id 必须 Empty 提示'); assert.ok(content.includes('/card/list'), '缺 id 必须提示回资产库'); assert.ok(!/\bany\b/.test(content), '禁止使用 any'); assert.ok( !/[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/u.test(content), '禁止使用 emoji' ); }); test('6. builder 页面文案走 i18n 且无 emoji', () => { assert.ok(fs.existsSync(builderLocalePath), '必须存在 builder locale'); const localeContent = fs.readFileSync(builderLocalePath, 'utf-8'); assert.ok(localeContent.includes('cardBuilder.save'), '必须有保存排版词条'); assert.ok(localeContent.includes('cardBuilder.empty'), '必须有缺 id 空状态词条'); assert.ok( !/[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/u.test(localeContent), 'locale 禁止使用 emoji' ); assert.ok(!/\bany\b/.test(localeContent), 'locale 禁止使用 any'); });