From b8c4d8b1e601282d2cf034c4ff47bf64e31edfd3 Mon Sep 17 00:00:00 2001 From: "pulse-triage[bot]" <249995291+pulse-triage[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 14:15:14 +0000 Subject: [PATCH] Cache file walks and reads in shared-template-audit SharedPrimitives.guardrails.test.ts runs scripts/shared-template-audit.mjs under Vitest's default 5000ms timeout. Without caching, each of the 152 pattern guards re-walked src/ with statSync/readdirSync and re-read every matching file from disk, taking ~9.3s in Vitest (~25.1s standalone) and timing out the guardrail test locally under Vitest's default timeout. Memoize file existence checks, sorted recursive scope traversal (using dirent file types), and UTF-8 file reads per audit run, skip Set/sort for single-scope guards, and evaluate longer patterns first while preserving exact audit rules, missing-scope checks, and failure messages. Allow overriding the audit root via PULSE_SHARED_TEMPLATE_AUDIT_ROOT for isolated regression testing and add frontend-modern/scripts/__tests__/shared-template-audit.test.mjs. Contract-Neutral: Internal caching and pattern-order optimization in shared-template-audit.mjs plus a unit test; no audit rule, registry schema, UI, API, or subsystem contract delta. Change-source: pulse-maintainer --- .../__tests__/shared-template-audit.test.mjs | 160 ++++++++++++++++++ .../scripts/shared-template-audit.mjs | 110 ++++++++---- 2 files changed, 241 insertions(+), 29 deletions(-) create mode 100644 frontend-modern/scripts/__tests__/shared-template-audit.test.mjs 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 = () => ;\n', + 'src/features/StaleAllowed.tsx': 'export const B = () => ;\n', + 'src/features/Violating.tsx': + 'export const C = () =>
;\n', + }, + ); + + const result = fixture.run(); + expect(result.status).toBe(1); + expect(result.stderr).toContain( + 'no-raw-search: src/features/Violating.tsx recreates the shared template', + ); + expect(result.stderr).toContain( + 'no-raw-search: allowed path src/features/StaleAllowed.tsx is stale', + ); + expect(result.stderr).toContain( + 'no-raw-dialog: src/features/Violating.tsx recreates the shared template', + ); + expect(result.stderr).toContain('pattern guard scope src/missing-dir does not exist'); + }); + + it('passes when all rules and cached pattern guards are satisfied', () => { + 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"'], + }, + ], + requiredPatternGuards: [ + { + id: 'require-search-field', + summary: 'Compose SearchField when searching', + category: 'primitives', + canonical: { path: 'src/shared/SearchField.tsx' }, + scopes: ['src/features'], + triggerPatterns: ['role="searchbox"'], + requiredPatterns: ['SearchField'], + }, + ], + }, + { + 'src/shared/Button.tsx': 'export function Button() { return null; }\n', + 'src/shared/SearchField.tsx': 'export function SearchField() { return null; }\n', + 'src/features/GoodConsumer.tsx': + 'import { Button } from "../shared/Button"; import { SearchField } from "../shared/SearchField"; export const A = () => <>