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';
import { parse } from '@babel/parser';
import postcss from 'postcss';
import { styleAnalysis } from './guard-style-analysis.mjs';
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 r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(!r.issues.some(i => i.code.startsWith('TOKEN')));
assert.ok(r.tokenEvidence[0].references.some(t => t.name === '--hm-sem-fg-primary'));
});
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; const name = location.hash ? "fg-primary" : "missing"; export default () => ' }, root => {
assert.ok(run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'TOKEN-001' && i.severity === 'FAIL'));
});
});
test('65. fallback never hides unknown Token and declared local variables are inventoried', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ', 'src/styles.css': '@import "./tokens.css"; :root { --local: var(--hm-sem-fg-primary); } .content { color: var(--local); }' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(r.issues.some(i => i.code === 'TOKEN-001' && i.severity === 'FAIL'));
assert.ok(r.tokenEvidence[0].references.some(t => t.name === '--local' && t.status === 'PASS'));
});
});
test('66. external props, style spread and className inputs retain site uncertainty', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default ({ color, styles, classes }) => ' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
const sites = r.tokenEvidence[0].dynamic;
assert.ok(sites.some(s => s.kind === 'CLASS_NAME'));
assert.ok(sites.some(s => s.kind === 'STYLE_VALUE'));
assert.ok(sites.some(s => s.kind === 'STYLE_SPREAD'));
assert.equal(r.staticStatus, 'NOT VERIFIED');
});
});
test('67. shadowed constants and partial dynamic templates never become fake literal FAILs', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; const name="fg-primary"; export default ({ name }) => ' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(r.issues.some(i => i.code === 'TOKEN-002'));
assert.ok(!r.issues.some(i => i.code === 'TOKEN-001'));
});
});
test('68. explicit Component HEX RGB HSL spacing and radius overrides fail', () => {
for (const value of ['#abcdef', 'rgb(1,2,3)', 'hsl(1 2% 3%)']) fixture({ 'src/styles.css': `@import "./tokens.css"; [data-slot="button"] { color: ${value}; }` }, root => {
assert.ok(run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'STYLE-001' && i.severity === 'FAIL'));
});
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.equal(r.styleEvidence.staticStatus, 'FAIL');
assert.equal(r.status, 'FAIL');
});
});
test('69. documented Token override keeps static measurements and intent separate', () => {
fixture({ 'src/styles.css': '@import "./tokens.css"; [data-slot="badge"] { --hm-badge-outline-text: var(--hm-sem-fg-primary); }' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.equal(r.styleEvidence.staticStatus, 'PASS');
assert.equal(r.styleEvidence.designIntentStatus, 'NOT VERIFIED');
assert.equal(r.styleEvidence.runtimeStatus, 'NOT VERIFIED');
assert.ok(r.issues.some(i => i.code === 'REUSE-003'));
});
});
test('70. general layout and global CSS are inventoried without claiming a Component violation', () => {
fixture({ 'src/styles.css': '@import "./tokens.css"; body { margin: 0; } .content { padding: 17px; color: #abcdef; }' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(!r.issues.some(i => i.code === 'STYLE-001'));
assert.ok(r.styleEvidence.checks.some(c => c.scope === 'GLOBAL'));
assert.equal(r.styleEvidence.designIntentStatus, 'NOT VERIFIED');
});
});
test('71. disconnected and PARTIAL CSS do not expand style findings; Storybook stays reference', () => {
fixture({ 'src/disconnected.css': '[data-slot="button"] { color: #abcdef; }' }, root => {
assert.ok(!run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'STYLE-001'));
});
storyFixture({ 'src/Example.stories.tsx': 'import { Button } from "@hm-design/ui"; export const All = () => ' }, root => {
assert.ok(!run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'STYLE-001'));
});
});
test('72. malformed var() and numeric / constant Component overrides remain FAIL', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; const color="#abcdef"; export default () => ', 'src/styles.css': '@import "./tokens.css"; .content { color: var(--hm-sem-fg-primary broken); }' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(r.issues.some(i => i.code === 'TOKEN-003'));
assert.ok(r.issues.some(i => i.code === 'STYLE-001'));
});
});
test('73. var fallback does not hide a hardcoded color elsewhere in Component styling', () => {
fixture({ 'src/styles.css': '@import "./tokens.css"; [data-slot="badge"] { background: color-mix(in srgb, #abcdef 50%, var(--hm-sem-bg-content)); border-top-left-radius: -1.5px; }' }, root => {
assert.equal(run(root, ['--scope', 'FULL_SCREEN']).report.styleEvidence.staticStatus, 'FAIL');
});
fixture({ 'src/App.tsx': 'import { Badge } from "@hm-design/ui"; export default () => ' }, root => {
assert.ok(!run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'STYLE-001'));
});
});
test('74. unresolved dynamic branches do not hide a known hardcoded Component style', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default ({ value, classes }) => ' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(r.issues.some(i => i.code === 'STYLE-001'));
assert.ok(r.issues.some(i => i.code === 'TOKEN-002'));
assert.equal(r.status, 'FAIL');
});
});
test('75. complete malformed var expressions stay FAIL and style findings appear in reuse group', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ' }, root => {
const r = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.ok(r.issues.some(i => i.code === 'TOKEN-003' && i.severity === 'FAIL'));
assert.equal(r.checkedGroups.find(g => g.name === 'Component Reuse').status, 'FAIL');
assert.equal(r.status, 'FAIL');
});
});
test('52. connected public Theme catches unknown HM references in JSX and CSS', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/styles.css': '@import "./tokens.css"; .custom { border-color: var(--hm-sem-stroke-divider); }' }, root => {
const { report, status } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 1);
assert.equal(report.issues.filter(i => i.code === 'TOKEN-001').length, 2);
assert.equal(report.checkedGroups.find(g => g.name === 'Theme Token').status, 'FAIL');
});
});
test('53. valid Theme, shorthand and declared consumer variables are preserved', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/styles.css': '@import "./tokens.css"; :root { --hm-app-color: var(--hm-sem-action-primary); }' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'PASS');
assert.ok(report.tokenEvidence[0].references.some(r => r.name === '--hm-sem-fg-primary'));
});
});
test('54. disconnected CSS cannot supply a Theme variable declaration', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/disconnected.css': ':root { --hm-external-token: green; }' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.ok(report.issues.some(i => i.code === 'TOKEN-001'));
assert.ok(!report.checkedFiles.includes('src/disconnected.css'));
});
});
test('55. PARTIAL limits Token findings and keeps ancestor declarations', () => {
fixture({ 'src/App.tsx': 'import { Input } from "@hm-design/ui"; import Other from "./Other"; export default () => <>>;', 'src/Other.tsx': 'import { Button } from "@hm-design/ui"; 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, 'FAIL'); // imported dependency included
assert.equal(run(root, ['--scope', 'PARTIAL', '--files', 'src/styles.css']).report.status, 'PASS');
});
});
test('56. missing Status CSS cannot borrow its tokens from the Theme', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;' }, root => {
assert.ok(run(root, ['--scope', 'FULL_SCREEN']).report.issues.some(i => i.code === 'TOKEN-001'));
});
});
test('57. dynamic HM variables remain uncertain without hiding a literal failure', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; const key = location.hash; export default () => ;' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'FAIL');
assert.ok(report.issues.some(i => i.code === 'TOKEN-002' && i.severity === 'NOT VERIFIED'));
});
});
test('58. staticStatus distinguishes proven React bootstrap from browser evidence', () => {
fixture({ '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 () => ;' }, 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/styles.css': '@import "./tokens.css"; [data-slot="button"] { color: var(--hm-sem-fg-primary); }' }, root => {
assert.equal(run(root, ['--scope', 'FULL_SCREEN']).report.staticStatus, 'NOT VERIFIED');
});
});
test('60. Storybook token scope is isolated per Story with shared preview', () => {
storyFixture({ 'src/Example.stories.tsx': 'import { Button } from "@hm-design/ui"; export const All = () => ;', 'src/Other.stories.tsx': 'import "./other.css"; export const All = () =>
;', 'src/other.css': ':root { --hm-story-only: green; }' }, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']);
assert.ok(report.issues.some(i => i.code === 'TOKEN-001' && i.file === 'src/Example.stories.tsx'));
});
});
test('61. native role widgets require provenance review while content HTML stays allowed', () => {
fixture({ 'src/App.tsx': 'import { Card } from "@hm-design/ui"; export default () => Custom
Content
;' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'NOT VERIFIED');
assert.equal(report.issues.filter(i => i.code === 'REUSE-006').length, 1);
assert.ok(report.issues.every(i => i.severity !== 'FAIL'));
});
});
test('62. an incomplete CSS graph cannot prove an HM variable is absent', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/styles.css': '@import "./tokens.css"; @import "./unresolved.css";' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'TOKEN-001' && i.severity === 'NOT VERIFIED'));
});
});
test('63. early PARTIAL scope diagnostics survive fingerprint reporting', () => {
fixture({ 'src/App.tsx': 'export default () => ;' }, root => {
const { report, status } = run(root, ['--scope', 'PARTIAL']);
assert.equal(status, 0);
assert.ok(report.issues.some(i => i.code === 'SCOPE-002'));
assert.equal(report.staticStatus, 'NOT VERIFIED');
});
});
const guard = fileURLToPath(new URL('./ai-generation-guard.mjs', import.meta.url));
function fixture(files, callback) {
const tempRoot = fs.realpathSync(os.tmpdir());
const root = fs.mkdtempSync(path.join(tempRoot, 'hm-fast-core-'));
assert.equal(path.dirname(root), tempRoot);
try {
for (const [name, text] of Object.entries({
'index.html': '',
'src/main.tsx': 'import App from "./App"; import "./styles.css"; export default App;',
'src/styles.css': '@import "./tokens.css";\n/* app-specific styles are valid */ .custom { color: coral; }',
'src/tokens.css': '@import "tailwindcss";\n@import "@hm-design/ui/theme.css";',
'tsconfig.app.json': '{ /* JSONC */ "compilerOptions": { "paths": { "@/*": ["./src/*"] } } }',
...files,
})) {
const dest = path.join(root, name);
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.writeFileSync(dest, text);
}
callback(root);
} finally {
// Only the verified mkdtemp directory created by this test is removed.
assert.equal(path.dirname(path.resolve(root)), tempRoot);
assert.ok(path.basename(root).startsWith('hm-fast-core-'));
fs.rmSync(root, { recursive: true, force: true });
}
}
function run(target, extra, json = true) {
const started = performance.now();
const result = spawnSync(process.execPath, [guard, '--target', target, ...extra, ...(json ? ['--json'] : [])], { encoding: 'utf8' });
assert.ifError(result.error);
assert.equal(result.stderr, '');
const report = json ? JSON.parse(result.stdout) : null;
return { ...result, report, wallMs: Math.round(performance.now() - started) };
}
test('1. normal GNB_ONLY; indirect CSS, aliases, unrelated new UI and unused Stories', () => {
fixture({
'src/hm.ts': 'export { GNB as Header, GNBMobileMenu as Menu, Button } from "@hm-design/ui";',
'src/App.tsx': 'import { Header, Menu, Button } from "@/hm"; import NewUI from "./NewUI";\nexport 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 () => ;',
}, 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 () => ;',
'src/Button.stories.tsx': 'import { Button } from "@hm-design/ui"; export const Demo = () => ;',
'src/story-page.tsx': 'export const StoryPage = ({ children }) => {children}
;',
}, root => {
const { status, report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 1);
for (const code of ['SB-001', 'SB-002', 'SB-003']) assert.ok(report.issues.some(i => i.code === code), code);
assert.ok(!report.issues.some(i => i.code.startsWith('NAV')));
});
});
test('5. private paths for both packages and a non-public named export are detected', () => {
fixture({
'src/App.tsx': 'import { Button } from "@hm-design/ui/src/components/ui/button";\nimport { CenterTitle } from "@hm-design/center-graphic/src/components/CenterTitle";\nimport { NonPublicComponent } from "@hm-design/ui";\nexport default () => <>>;',
}, root => {
const { status, report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 1);
assert.equal(report.issues.filter(i => i.code === 'IMP-001').length, 2);
assert.ok(report.issues.some(i => i.code === 'IMP-002'));
});
});
test('6. PARTIAL is read-only and never infers a Navigation mode', () => {
fixture({ 'src/App.tsx': 'import { Button, GNB, GNBMobileMenu } from "@hm-design/ui"; export 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 () => ;', 'src/styles.css': '.hm-select, [data-slot="select-trigger"] { color: red; }' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'NOT VERIFIED');
for (const code of ['REUSE-003', 'REUSE-004']) assert.ok(report.issues.some(i => i.code === code));
assert.ok(report.issues.every(i => i.severity !== 'FAIL'));
});
});
test('11. PARTIAL excludes unchanged sibling errors', () => {
fixture({
'src/App.tsx': 'import { Button } from "@hm-design/ui"; import Old from "./Old"; export default () => <>>;',
'src/Old.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 () => ;',
}, root => {
const fake = run(root, ['--scope', 'FULL_SCREEN', '--result', 'preview.html']).report;
assert.equal(fake.status, 'FAIL');
assert.ok(fake.issues.some(i => i.code === 'RENDER-001'));
const real = run(root, ['--scope', 'FULL_SCREEN', '--result', 'react-preview.html']).report;
assert.equal(real.status, 'NOT VERIFIED');
assert.deepEqual(real.issues.map(i => i.code), ['RENDER-002']);
});
});
test('13. design system implementation is exempt', () => {
const internal = fileURLToPath(new URL('../packages/ui/src/components/ui/select.tsx', import.meta.url));
const { report } = run(internal, ['--scope', 'PARTIAL']);
assert.ok(!report.issues.some(i => i.severity === 'FAIL'));
});
test('14. wrappers and inline HTML strings are not falsely marked FAIL; unused barrel consumer fails', () => {
fixture({
'src/hm.ts': 'export { Select as Choice } from "@hm-design/ui";',
'src/App.tsx': 'import { Choice } from "./hm"; const Wrap = props => ; export default () => ;',
'preview.html': '',
}, root => {
const wrapped = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.equal(wrapped.status, 'NOT VERIFIED');
assert.ok(wrapped.issues.every(i => i.severity !== 'FAIL'));
assert.ok(!run(root, ['--scope', 'FULL_SCREEN', '--result', 'preview.html']).report.issues.some(i => i.code === 'REUSE-001'));
fs.writeFileSync(path.join(root, 'src/App.tsx'), 'import { Choice } from "./hm"; export default () => ;');
assert.ok(run(root, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']).report.issues.some(i => i.code === 'REUSE-002'));
});
});
const dashboardSource = `import { DashboardLayout, DashboardHeader, DashboardSidebar } from "@hm-design/ui";
export function Screen({ mode = "compact" }) {
const compact = mode === "compact";
const reference = Reference;
return } sidebar={compact ? undefined : reference}/>;
}`;
const dashboardFixture = (mode, callback) => fixture({
'src/Screen.tsx': dashboardSource,
'src/App.tsx': `import { Screen } from "./Screen"; export default () => ;`,
}, callback);
const dashArgs = ['--scope', 'FULL_SCREEN', '--mode', 'DASHBOARD_ONLY'];
test('15. compact prop proves a preserved Reference Sidebar value is not rendered', () => {
dashboardFixture('mode="compact"', root => {
const { report } = run(root, dashArgs);
assert.equal(report.status, 'PASS');
assert.ok(!report.issues.some(i => i.code.startsWith('NAV')));
});
});
test('16. the same Reference branch fails when selected; no broad Sidebar exception', () => {
dashboardFixture('mode="header-sidebar"', root => {
const { status, report } = run(root, dashArgs);
assert.equal(status, 1);
assert.ok(report.issues.some(i => i.code === 'NAV-001' && i.severity === 'FAIL'));
});
});
test('17. unknown conditional Sidebar stays NOT VERIFIED and cannot PASS', () => {
dashboardFixture('mode={window.mode}', root => {
const { report } = run(root, dashArgs);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'NAV-001' && i.severity === 'NOT VERIFIED'));
assert.ok(report.issues.some(i => i.code === 'NAV-005'));
});
});
test('18. default props and aliased/barrel component calls preserve compact evidence', () => {
fixture({
'src/Screen.tsx': dashboardSource,
'src/barrel.ts': 'export { Screen as Board } from "./Screen";',
'src/App.tsx': 'import { Board } from "./barrel"; export default () => ;',
}, root => assert.equal(run(root, dashArgs).report.status, 'PASS'));
});
test('19. multiple call sites retain the violating Reference branch', () => {
fixture({
'src/Screen.tsx': dashboardSource,
'src/App.tsx': 'import { Screen } from "./Screen"; export default () => <>>;',
}, root => assert.equal(run(root, dashArgs).report.status, 'FAIL'));
});
test('20. JSX ternary and logical literal branches: true fails, false is absent', () => {
for (const [expression, expected] of [['true && ', 'FAIL'], ['false && ', 'PASS'], ['false ? : null', 'PASS'], ['true ? : null', 'FAIL']]) {
fixture({ 'src/App.tsx': `import { DashboardHeader, DashboardSidebar } from "@hm-design/ui"; export default () => <>{${expression}}>;` }, root => assert.equal(run(root, dashArgs).report.status, expected, expression));
}
});
test('21. spread, mutable conditions and opaque render callbacks cannot conceal Sidebar', () => {
for (const source of [
'import { Screen } from "./Screen"; export default () => ;',
'import { Screen } from "./Screen"; export default () => ;',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { let show = false; show = window.show; return show && ; };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => }/>;',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const side = ; return window.forward(side); };',
]) fixture({ 'src/Screen.tsx': dashboardSource, 'src/App.tsx': source }, root => assert.notEqual(run(root, dashArgs).report.status, 'PASS', source));
});
test('22. new mixed-navigation screen fails; PARTIAL retains selected Sidebar failures', () => {
fixture({ 'src/App.tsx': 'import { GNB, GNBMobileMenu, LNB, DashboardSidebar } from "@hm-design/ui"; export default () => <>>;' }, root => {
const args = ['--mode', 'GNB_ONLY'];
for (const scope of [['--scope', 'FULL_SCREEN'], ['--scope', 'PARTIAL', '--files', 'src/App.tsx']]) {
const { report } = run(root, [...scope, ...args]);
assert.equal(report.status, 'FAIL');
for (const code of ['NAV-001', 'NAV-002']) assert.ok(report.issues.some(i => i.code === code && i.severity === 'FAIL'));
}
});
});
const storyFixture = (extra, callback) => fixture({
'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;',
'.storybook/main.ts': 'export default { stories: ["../src/**/*.stories.@(ts|tsx)"] };',
'.storybook/preview.tsx': 'import "../src/styles.css"; export default {};',
'src/Example.stories.tsx': 'import { Button } from "@hm-design/ui"; import { StoryPage } from "./story-page"; export default { title: "Components/Example" }; export const All = () => ;',
'src/story-page.tsx': 'export const StoryPage = ({children}) => {children}
;',
...extra,
}, callback);
test('23. Storybook independently discovers CSF and preview without Production pollution', () => {
storyFixture({}, root => {
const production = run(root, ['--scope', 'FULL_SCREEN']).report;
assert.equal(production.status, 'PASS');
assert.ok(!production.checkedFiles.some(f => f.endsWith('.stories.tsx')));
const stories = run(root, ['--profile', 'STORYBOOK', '--scope', 'PARTIAL', '--files', 'src/Example.stories.tsx']).report;
assert.equal(stories.status, 'PASS');
assert.ok(stories.checkedFiles.includes('src/Example.stories.tsx'));
assert.ok(!stories.issues.some(i => i.code === 'SCOPE-001'));
});
});
test('24. Storybook invalid public export and private import still fail', () => {
storyFixture({ 'src/Example.stories.tsx': 'import { NotPublic } from "@hm-design/ui"; import { Button } from "@hm-design/ui/src/components/ui/button"; export const All = () => <>>;' }, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']);
for (const code of ['IMP-001', 'IMP-002']) assert.ok(report.issues.some(i => i.code === code));
assert.equal(report.status, 'FAIL');
});
});
test('25. Storybook preview supplies CSS; proven missing theme/utilities FAIL', () => {
storyFixture({ '.storybook/preview.tsx': 'export default {};' }, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'FAIL');
for (const code of ['CSS-001', 'CSS-002']) assert.ok(report.issues.some(i => i.code === code));
});
});
test('26. computed Storybook discovery cannot silently PASS', () => {
storyFixture({ '.storybook/main.ts': 'export default { stories: getStories() };' }, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'SB-004'));
});
});
test('27. Reference navigation is preserved; an explicit Storybook mode checks violations', () => {
storyFixture({ 'src/Example.stories.tsx': 'import { DashboardSidebar } from "@hm-design/ui"; export const All = () => ;' }, root => {
assert.equal(run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']).report.status, 'PASS');
assert.equal(run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN', '--mode', 'DASHBOARD_ONLY']).report.status, 'FAIL');
});
});
test('28. normal Primary 80% gradient is parsed without modification; consumer slot intent is reviewed', () => {
fixture({
'src/App.tsx': 'import { Tabs } from "@hm-design/ui"; export default () => ;',
'src/styles.css': '@import "./tokens.css"; [data-slot="tabs-frame"][data-fill="true"][data-panel-surface="content"] { --hm-tab-fill-face: linear-gradient(to bottom, color-mix(in srgb, var(--hm-sem-palette-white) 80%, transparent) 0%, var(--hm-sem-bg-content) 100%); }',
}, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.ok(!report.issues.some(i => i.severity === 'FAIL' || i.code === 'CSS-005'));
assert.ok(report.issues.some(i => i.code === 'REUSE-003'));
});
});
test('29. malformed CSS, forged component slots and CSS replicas do not PASS', () => {
fixture({ 'src/App.tsx': 'import { Tabs } from "@hm-design/ui"; export default () => <>>;', 'src/styles.css': '@import "./tokens.css"; .hm-tabs { color: red; }' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'FAIL');
for (const code of ['REUSE-001', 'REUSE-003']) assert.ok(report.issues.some(i => i.code === code));
});
fixture({ 'src/App.tsx': 'import { Tabs } from "@hm-design/ui"; export default () => ;', 'src/styles.css': '.hm-tabs {' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.ok(report.issues.some(i => i.code === 'CSS-005'));
assert.notEqual(report.status, 'PASS');
});
});
test('30. raw CSS is not an entry; package implementation scope is never claimed as consumer PASS', () => {
fixture({ 'src/main.tsx': 'import App from "./App"; import css from "./tokens.css?raw"; export default App;', 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
for (const code of ['CSS-001', 'CSS-002']) assert.ok(report.issues.some(i => i.code === code));
});
const root = fileURLToPath(new URL('../', import.meta.url));
const { report } = run(root, ['--scope', 'PARTIAL', '--files', 'packages/ui/src/theme/tabs.css']);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'SCOPE-001'));
});
test('31. opaque spread, destructuring, mutation and callback aliases cannot hide a violation', () => {
for (const source of [
'import { DashboardSidebar } from "@hm-design/ui"; export default () => }}/>;',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const props = { sidebar: }; return ; };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const [side] = []; return side; };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { let side = ; return side; };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { let side = ; side = window.forward(side); return side; };',
'import { DashboardSidebar } from "@hm-design/ui"; function Screen({mode}) { mode = "sidebar"; return mode === "compact" ? null : ; } export default () => ;',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const options = {show:false}; window.mutate(options); return options.show && ; };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const side = ; const callback = () => side; return window.render(callback); };',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => }}/>;',
'import { DashboardSidebar } from "@hm-design/ui"; export default () => { const obj = {...window.props, nav: }; return obj.nav; };',
]) fixture({ 'src/App.tsx': source }, root => assert.notEqual(run(root, dashArgs).report.status, 'PASS', source));
});
test('32. internal Reference wrapper is reviewable; public-slot replicas and Production wrappers fail', () => {
storyFixture({ 'src/Example.stories.tsx': 'import { LNBMobileLink } from "@hm-design/ui"; export const All = () =>
;' }, root => {
const reference = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']).report;
assert.equal(reference.status, 'NOT VERIFIED');
assert.ok(reference.issues.some(i => i.code === 'REUSE-001' && i.severity === 'NOT VERIFIED'));
fs.writeFileSync(path.join(root, 'src/App.tsx'), 'import { LNBMobileLink } from "@hm-design/ui"; export default () =>
;');
assert.equal(run(root, ['--scope', 'FULL_SCREEN']).report.status, 'FAIL');
fs.writeFileSync(path.join(root, 'src/Example.stories.tsx'), 'export const All = () => ;');
assert.equal(run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']).report.status, 'FAIL');
});
});
test('33. one lazy Story cannot supply missing CSS entry evidence to another Story', () => {
storyFixture({
'.storybook/preview.tsx': 'export default {};',
'src/First.stories.tsx': 'import "./styles.css"; import { Button } from "@hm-design/ui"; export const All = () => ;',
}, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN']);
assert.equal(report.status, 'FAIL');
assert.ok(report.issues.some(i => i.code === 'CSS-001' && i.file === 'src/Example.stories.tsx'));
assert.ok(!report.issues.some(i => i.code.startsWith('CSS-') && i.file === 'src/First.stories.tsx'));
});
});
test('34. a possible violating call does not become certain through another safe call', () => {
fixture({
'src/Screen.tsx': dashboardSource,
'src/App.tsx': 'import { Screen } from "./Screen"; export default () => <>{window.flag && }>;',
}, root => {
const { report } = run(root, dashArgs);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i=>i.code === 'NAV-001' || i.code === 'NAV-005'));
});
});
test('35. a public Sidebar forwarded as a component prop still fails', () => {
for (const source of [
'import { DashboardSidebar } from "@hm-design/ui"; const Forward = ({component:Component}) => ; export default () => ;',
'import * as HM from "@hm-design/ui"; const Forward = ({component:Component}) => ; export default () => ;',
]) fixture({'src/App.tsx':source},root=> {
const { report } = run(root,dashArgs);
assert.equal(report.status,'FAIL');
assert.ok(report.issues.some(i=>i.code === 'NAV-001' && i.severity === 'FAIL'));
});
});
test('36. aliased object mutation and non-primitive identity cannot prove a false dead branch', () => {
for (const body of [
'const options = {show:false}; const alias = options; alias.show = true; return options.show && ;',
'const options = {show:false}; const alias = options; window.mutate(alias); return options.show && ;',
'const value = ; return value === value ? : null;',
'const value = () => ; return value.name === "value" ? : null;',
]) fixture({'src/App.tsx':`import { DashboardSidebar } from "@hm-design/ui"; export default () => { ${body} };`},root=>assert.notEqual(run(root,dashArgs).report.status,'PASS',body));
});
test('37. resolved Production graph without HM theme is a required-style failure', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/tokens.css': '@import "tailwindcss";' }, root => {
const { status, report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 1);
assert.ok(report.issues.some(issue => issue.code === 'CSS-001' && issue.severity === 'FAIL'));
});
});
test('38. missing Tailwind with a resolved HM component graph fails independently', () => {
fixture({ 'src/App.tsx': 'import { Input } from "@hm-design/ui"; export default () => ;', 'src/tokens.css': '@import "@hm-design/ui/theme.css";' }, root => {
const { report } = run(root, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']);
assert.ok(report.issues.some(issue => issue.code === 'CSS-002' && issue.severity === 'FAIL'));
assert.ok(!report.issues.some(issue => issue.code === 'CSS-001'));
});
});
test('39. unresolved CSS graph preserves NOT VERIFIED rather than claiming absence', () => {
fixture({ 'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;', 'src/styles.css': '@import "./missing-theme.css";' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.ok(report.issues.some(issue => issue.code === 'CSS-001' && issue.severity === 'NOT VERIFIED'));
assert.ok(report.issues.some(issue => issue.code === 'ENTRY-002'));
});
});
test('40. required status CSS absence fails without relaxing navigation or import checks', () => {
fixture({ 'src/App.tsx': 'import { Badge, dashboardStatusDefinitions } from "@hm-design/ui"; export default () => {dashboardStatusDefinitions.active.label};' }, root => {
const { report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.ok(report.issues.some(issue => issue.code === 'CSS-003' && issue.severity === 'FAIL'));
});
});
test('41. Storybook PARTIAL detects disconnected preview styles for the selected Story', () => {
storyFixture({ '.storybook/preview.tsx': 'export default {};' }, root => {
const { status, report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'PARTIAL', '--files', 'src/Example.stories.tsx']);
assert.equal(status, 1);
for (const code of ['CSS-001', 'CSS-002']) assert.ok(report.issues.some(i => i.code === code && i.severity === 'FAIL'));
});
});
test('42. explicit Story entry resolves computed discovery without claiming unselected coverage', () => {
storyFixture({
'.storybook/main.ts': 'export default { stories: getStories() };',
'src/Bad.stories.tsx': 'export const All = () => ;',
}, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'FULL_SCREEN', '--entry', 'src/Example.stories.tsx']);
assert.equal(report.status, 'PASS');
assert.ok(report.checkedFiles.includes('src/Example.stories.tsx'));
assert.ok(report.checkedFiles.includes('.storybook/preview.tsx'));
assert.ok(!report.checkedFiles.includes('src/Bad.stories.tsx'));
});
});
test('43. inherited alias configuration remains uncertain even with locally resolved imports', () => {
fixture({
'tsconfig.app.json': '{ "extends": "./base.json", "compilerOptions": { "paths": { "@/*": ["./src/*"] } } }',
'base.json': '{ "compilerOptions": { "paths": { "external/*": ["./external/*"] } } }',
'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => ;',
}, root => {
const { status, report } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 0);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'ENTRY-003' && i.severity === 'NOT VERIFIED'));
});
});
test('44. internal Reference wrapper cannot downgrade a public-slot violation in the same Story', () => {
storyFixture({ 'src/Example.stories.tsx': 'import { LNBMobileLink } from "@hm-design/ui"; export const All = () => ;' }, root => {
const { report } = run(root, ['--profile', 'STORYBOOK', '--scope', 'PARTIAL', '--files', 'src/Example.stories.tsx']);
assert.equal(report.status, 'FAIL');
assert.ok(report.issues.some(i => i.code === 'REUSE-001' && i.severity === 'NOT VERIFIED'));
assert.ok(report.issues.some(i => i.code === 'REUSE-001' && i.severity === 'FAIL'));
});
});
test('45. proven compact navigation does not approve consumer CSS intent', () => {
fixture({
'src/Screen.tsx': dashboardSource,
'src/App.tsx': 'import { Screen } from "./Screen"; export default () => ;',
'src/styles.css': '@import "./tokens.css"; [data-slot="dashboard-header"] { background: red; }',
}, root => {
const { report } = run(root, dashArgs);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(!report.issues.some(i => i.code.startsWith('NAV')));
assert.ok(report.issues.some(i => i.code === 'REUSE-003' && i.severity === 'NOT VERIFIED'));
});
});
test('46. selected package implementation stays outside consumer coverage in both profiles', () => {
const root = fileURLToPath(new URL('../', import.meta.url));
for (const profile of ['PRODUCTION', 'STORYBOOK']) {
const { report } = run(root, ['--profile', profile, '--scope', 'PARTIAL', '--files', 'packages/ui/src/components/ui/select.tsx']);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'SCOPE-001' && i.severity === 'NOT VERIFIED'));
assert.ok(!report.checkedFiles.includes('packages/ui/src/components/ui/select.tsx'));
assert.ok(!report.issues.some(i => i.severity === 'FAIL'));
}
});
test('47. external native field Label needs review, with no blanket native HTML failure', () => {
fixture({ 'src/App.tsx': 'import { Input } from "@hm-design/ui"; export default () => <>>;' }, root => {
const { report, status } = run(root, ['--scope', 'FULL_SCREEN']);
assert.equal(status, 0);
assert.equal(report.status, 'NOT VERIFIED');
assert.ok(report.issues.some(i => i.code === 'REUSE-004' && i.message.includes('