Files
HMDesign/scripts/ai-generation-guard.test.mjs
T

131 lines
7.0 KiB
JavaScript

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';
const guard = fileURLToPath(new URL('./ai-generation-guard.mjs', import.meta.url));
const external = String.raw`D:\디자인시스템\HMDesign-main\hmdesign`;
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': '<script type="module" src="/src/main.tsx"></script>',
'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 () => <><Header><Menu title="menu" /></Header><Button>OK</Button><NewUI /></>;',
'src/NewUI.tsx': 'import "./new.css"; function Sidebar(){ return <aside className="sb-page-inner custom">New UI</aside> }\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 = () => <><GNB/><DashboardSidebar/></>;',
}, 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.equal(lines.length, 3);
assert.equal(lines[0], 'HMDesign FAST CORE: PASS');
assert.equal(lines[1], 'HMDesign: Button, GNB, GNBMobileMenu');
assert.match(lines[2], /Issues: 0/);
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 () => <Menu><HM.LNBNav><HM.LNBItem href="/">Home</HM.LNBItem></HM.LNBNav></Menu>;',
}, 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 () => <><GNB/><Side/></>;',
}, 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 () => <StoryPage><Button className="sb-page-inner"/><Demo/></StoryPage>;',
'src/Button.stories.tsx': 'import { Button } from "@hm-design/ui"; export const Demo = () => <Button/>;',
'src/story-page.tsx': 'export const StoryPage = ({ children }) => <div>{children}</div>;',
}, 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 () => <><Button/><CenterTitle/><NonPublicComponent/></>;',
}, 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. external app PARTIAL is read-only and never infers a Navigation mode', () => {
assert.ok(fs.existsSync(external), 'External test project must exist');
const names = ['src/App.tsx', 'src/main.tsx', 'src/index.css', 'src/hm-theme.css', 'package.json', 'AGENTS.md'];
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`);
});