feat: improve lightweight AI generation guard

This commit is contained in:
shkim4 committed 2026-10-08 20:32:15 +09:00
1 parent c86d12bebc
commit 4daca6d357
7 files changed
+327 -162

No files matched your search

+94 -8
View File
@@ -9,7 +9,6 @@ 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-'));
@@ -55,10 +54,7 @@ test('1. normal GNB_ONLY; indirect CSS, aliases, unrelated new UI and unused Sto
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/);
assert.deepEqual(lines, ['PASS']);
console.log(`Normal GNB wall time: ${result.wallMs} ms`);
});
});
@@ -113,9 +109,9 @@ test('5. private paths for both packages and a non-public named export are detec
});
});
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'];
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 () => <GNB><GNBMobileMenu/><Button>OK</Button></GNB>;' }, 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']);
@@ -127,4 +123,94 @@ test('6. external app PARTIAL is read-only and never infers a Navigation mode',
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 () => <select data-slot="select"><option>A</option></select>;' }, 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 () => <div data-slot="select-trigger"/>;',
'import React from "react"; export default () => React.createElement("select", { "data-slot": "select" });',
'import * as HM from "@hm-design/ui"; export default () => <div/>;',
]) 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 }) => <Component/>; export default () => <><Choice/><HM.Input/><Forward component={Button}/></>;',
}, 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 () => <select className="hm-select"/>;', '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 () => <><Button/><Old/></>;',
'src/Old.tsx': 'export default () => <select data-slot="select"/>;',
}, 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': '<select data-slot="select"><option>A</option></select>',
'react-preview.html': '<div id="root"></div><script type="module" src="/src/main.tsx"></script>',
'src/main.tsx': 'import { createRoot } from "react-dom/client"; import App from "./App"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App/>);',
'src/App.tsx': 'import { Button } from "@hm-design/ui"; export default () => <Button/>;',
}, 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 => <Choice {...props}/>; export default () => <Wrap data-slot="select"/>;',
'preview.html': '<script>const example = \'<select data-slot="select"></select>\';</script>',
}, 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 () => <div/>;');
assert.ok(run(root, ['--scope', 'PARTIAL', '--files', 'src/App.tsx']).report.issues.some(i => i.code === 'REUSE-002'));
});
});