import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { performance } from 'node:perf_hooks'; import test from 'node:test'; test('64. finite conditional templates and concatenation validate every Token branch', () => { fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; const name = location.hash ? "fg-primary" : "bg-content"; export default () => ;' }, root => { const { report } = run(root, ['--scope', 'FULL_SCREEN', '--result', 'index.html']); assert.equal(report.status, 'NOT VERIFIED'); assert.equal(report.staticStatus, 'PASS'); assert.equal(report.runtimeStatus, 'NOT VERIFIED'); assert.ok(report.issues.some(i => i.code === 'RENDER-002')); assert.ok(report.checkedGroups.every(g => g.basis && Array.isArray(g.evidence))); }); }); test('59. staticStatus keeps CSS intent uncertainty and actual failures', () => { fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/NewUI.tsx': 'import "./new.css"; function Sidebar(){ return }\nexport default Sidebar;', 'src/new.css': '.sb-page-inner { color: purple; } .sidebar { display: flex; }', 'src/Unused.stories.tsx': 'import { GNB, DashboardSidebar } from "@hm-design/ui"; export const Example = () => <>;', }, root => { const result = run(root, ['--scope', 'FULL_SCREEN', '--mode', 'GNB_ONLY'], false); assert.equal(result.status, 0); const lines = result.stdout.trim().split(/\r?\n/); assert.deepEqual(lines, ['PASS']); console.log(`Normal GNB wall time: ${result.wallMs} ms`); }); }); test('2. normal LNB_ONLY; namespace and local alias resolve to the official LNB', () => { fixture({ 'src/App.tsx': 'import * as HM from "@hm-design/ui"; const Menu = HM.LNB;\nexport default () => Home;', }, root => { const { status, report } = run(root, ['--scope', 'FULL_SCREEN', '--mode', 'LNB_ONLY']); assert.equal(status, 0); assert.equal(report.status, 'PASS'); assert.deepEqual(report.checkedHMElements, ['LNB', 'LNBItem', 'LNBNav']); assert.ok(report.checkedGroups.every(g => g.status === 'PASS')); console.log(`Normal LNB static analysis: ${report.durationMs} ms`); }); }); test('3. GNB_ONLY detects a reused official Sidebar through a re-export alias', () => { fixture({ 'src/hm.ts': 'export { GNB, DashboardSidebar as Side } from "@hm-design/ui";', 'src/App.tsx': 'import { GNB, Side } from "./hm";\nexport default () => <>;', }, root => { const { status, report } = run(root, ['--scope', 'FULL_SCREEN', '--mode', 'GNB_ONLY']); assert.equal(status, 1); assert.equal(report.status, 'FAIL'); assert.ok(report.issues.some(i => i.code === 'NAV-001' && i.file === 'src/App.tsx' && i.line === 2)); assert.ok(report.checkedHMElements.includes('DashboardSidebar')); }); }); test('4. Production Story import, StoryPage helper and Storybook CSS are detected', () => { fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui";\nimport { Demo } from "./Button.stories";\nimport { StoryPage } from "./story-page";\nexport default () => ;' }, external => { const names = ['src/App.tsx', 'src/main.tsx', 'src/styles.css', 'src/tokens.css', 'tsconfig.app.json', 'index.html']; const hashes = () => names.map(name => createHash('sha256').update(fs.readFileSync(path.join(external, name))).digest('hex')); const before = hashes(); const { status, report } = run(external, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']); assert.equal(status, 0); assert.equal(report.status, 'NOT VERIFIED'); assert.deepEqual(report.issues.map(i => i.code), ['NAV-003']); assert.equal(report.checkedGroups[2].status, 'PASS'); assert.ok(report.checkedHMElements.includes('Button')); assert.ok(report.checkedHMElements.includes('GNB')); assert.deepEqual(hashes(), before); console.log(`External PARTIAL: ${report.status}; ${report.checkedHMElements.length} HM elements; ${report.durationMs} ms`); }); }); test('7. native Select with fake data-slot and import-only reuse fails', () => { fixture({ 'src/App.tsx': 'import { Select as Choice } from "@hm-design/ui";\nexport default () => ;' }, root => { const { status, report } = run(root, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']); assert.equal(status, 1); for (const code of ['REUSE-001', 'REUSE-002']) assert.ok(report.issues.some(i => i.code === code && i.file === 'src/App.tsx')); assert.ok(!report.checkedHMElements.includes('Select')); }); }); test('8. fake slots without an HM import, createElement and namespace unused', () => { for (const source of [ 'export default () =>
;', 'import React from "react"; export default () => React.createElement("select", { "data-slot": "select" });', 'import * as HM from "@hm-design/ui"; export default () =>
;', ]) fixture({ 'src/App.tsx': source }, root => { const { report } = run(root, ['--scope', 'FULL_SCREEN']); assert.equal(report.status, 'FAIL'); assert.ok(report.issues.some(i => /^REUSE-00[12]$/.test(i.code))); }); }); test('9. official aliases, namespace, forwarding, types and barrels are not unused', () => { fixture({ 'src/hm.ts': 'export { Select as Choice } from "@hm-design/ui";', 'src/App.tsx': 'import { Choice } from "./hm"; import { Button } from "@hm-design/ui"; import type { ButtonAppearance } from "@hm-design/ui"; import * as HM from "@hm-design/ui"; const Forward = ({ component: Component }) => ; export default () => <>;', }, root => { const { report } = run(root, ['--scope', 'FULL_SCREEN']); assert.equal(report.status, 'PASS'); }); }); test('10. native controls and CSS customization need review, not FAIL', () => { fixture({ 'src/App.tsx': 'export default () => ;', }, root => { assert.equal(run(root, ['--scope', 'FULL_SCREEN']).report.status, 'FAIL'); assert.equal(run(root, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']).report.status, 'PASS'); // Selecting a leaf checks that leaf; its unchanged siblings stay outside scope. assert.equal(run(root, ['--scope', 'PARTIAL', '--files', 'src/Old.tsx']).report.issues.some(i => i.file === 'src/App.tsx' && i.code.startsWith('REUSE')), false); }); }); test('12. final HTML imitation is flagged; React preview remains browser NOT VERIFIED', () => { fixture({ 'preview.html': '', 'react-preview.html': '
', 'src/main.tsx': 'import { createRoot } from "react-dom/client"; import App from "./App"; import "./styles.css"; createRoot(document.getElementById("root")).render();', 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () =>
;', 'src/styles.css': '@import "./tokens.css"; .legacy [data-slot="button"] { gap: 6px; padding: 0 10px; border-radius: 4px; }' }, root => { const r = run(root, ['--scope', 'FULL_SCREEN']).report; assert.ok(!r.issues.some(i => i.code === 'STYLE-001')); const checks = r.styleEvidence.checks.filter(c => c.selector.startsWith('.legacy')); assert.equal(checks.length, 3); assert.ok(checks.every(c => c.applicability.status === 'NOT APPLICABLE' && c.designIntentStatus === 'NOT VERIFIED')); assert.equal(r.styleEvidence.finalStatus, 'NOT VERIFIED'); }); }); test('78. present ancestor and class on a public Component preserve actual violations', () => { for (const jsx of ['
', '
;', 'src/styles.css': '@import "./tokens.css"; .legacy [data-slot="button"] { padding: 17px; }' }, root => { assert.ok(run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'STYLE-001')); }); }); test('80. dynamic classes spreads and DOM mutation retain strict detection', () => { for (const app of [ 'export default ({classes}) =>
;', 'export default ({props}) =>
;', 'document.body.classList.add(location.hash); export default () =>