diff --git a/frontend-modern/scripts/__tests__/shared-template-audit.test.mjs b/frontend-modern/scripts/__tests__/shared-template-audit.test.mjs new file mode 100644 index 0000000000..6d07750586 --- /dev/null +++ b/frontend-modern/scripts/__tests__/shared-template-audit.test.mjs @@ -0,0 +1,160 @@ +import { spawnSync } from 'node:child_process'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; + +import { afterEach, describe, expect, it } from 'vitest'; + +const auditPath = path.join(process.cwd(), 'scripts', 'shared-template-audit.mjs'); +const temporaryRoots = []; + +const writeFile = (root, relativePath, content) => { + const fullPath = path.join(root, ...relativePath.split('/')); + fs.mkdirSync(path.dirname(fullPath), { recursive: true }); + fs.writeFileSync(fullPath, content); +}; + +const makeFixture = (registry, files) => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-shared-template-audit-')); + temporaryRoots.push(root); + writeFile(root, 'scripts/shared-template-registry.json', JSON.stringify(registry, null, 2)); + for (const [relativePath, content] of Object.entries(files)) { + writeFile(root, relativePath, content); + } + return { + root, + run: () => + spawnSync(process.execPath, [auditPath], { + cwd: root, + env: { ...process.env, PULSE_SHARED_TEMPLATE_AUDIT_ROOT: root }, + encoding: 'utf8', + }), + }; +}; + +afterEach(() => { + while (temporaryRoots.length > 0) { + fs.rmSync(temporaryRoots.pop(), { force: true, recursive: true }); + } +}); + +describe('shared template audit', () => { + it('detects violations across overlapping cached scopes and guards without cross-contamination', () => { + const fixture = makeFixture( + { + rules: [ + { + id: 'canonical-button', + summary: 'Use shared Button', + category: 'primitives', + canonical: { path: 'src/shared/Button.tsx' }, + requiredConsumers: [{ path: 'src/features/GoodConsumer.tsx' }], + }, + ], + patternGuards: [ + { + id: 'no-raw-search', + summary: 'Use SearchField', + category: 'primitives', + canonical: { path: 'src/shared/SearchField.tsx' }, + scopes: ['src/features'], + allPatterns: ['type="search"'], + allowedPaths: ['src/features/StaleAllowed.tsx'], + }, + { + id: 'no-raw-dialog', + summary: 'Use Dialog', + category: 'primitives', + canonical: { path: 'src/shared/Dialog.tsx' }, + scopes: ['src/features'], + allPatterns: ['role="dialog"', 'fixed inset-0'], + }, + ], + requiredPatternGuards: [ + { + id: 'missing-scope-check', + summary: 'Missing scope fails closed', + category: 'primitives', + canonical: { path: 'src/shared/SearchField.tsx' }, + scopes: ['src/missing-dir'], + triggerPatterns: ['placeholder='], + requiredPatterns: ['SearchField'], + }, + ], + }, + { + 'src/shared/Button.tsx': 'export function Button() { return null; }\n', + 'src/shared/SearchField.tsx': 'export function SearchField() { return null; }\n', + 'src/shared/Dialog.tsx': 'export function Dialog() { return null; }\n', + 'src/features/GoodConsumer.tsx': + 'import { Button } from "../shared/Button"; export const A = () =>