/**
 * audit3-followup smoke — component-test scaffold landed.
 *
 * Run: npx tsx scripts/smoke/audit3-component-test-scaffold.ts
 *
 * Why this exists (2026-05-29):
 *   Pre-audit3, the project had ~570 Jest tests but zero component
 *   tests — every UI regression went out of the door uncaught. This
 *   smoke asserts the scaffold (jest projects config, jest-dom matchers,
 *   jsdom env, three proof-point test files) stays in place so a future
 *   contributor can't accidentally revert to "no component tests."
 *
 * Assertions:
 *   1. jest.config.js declares two projects (node + dom).
 *   2. jest.setup.dom.ts loads @testing-library/jest-dom.
 *   3. @testing-library/react + @testing-library/jest-dom + jsdom-env
 *      are in devDependencies.
 *   4. At least three component test files exist under src/components/.
 *   5. The dom project finds them via testMatch '**.test.tsx'.
 */
import './_lib';
import { ok, fail } from './_lib';
import fs from 'fs';
import path from 'path';

const ROOT = path.resolve(__dirname, '../..');

function readFile(rel: string): string {
    return fs.readFileSync(path.join(ROOT, rel), 'utf8');
}

function main(): void {
    // 1. jest.config.js declares two projects.
    const jestConfig = readFile('jest.config.js');
    if (!/projects\s*:\s*\[/.test(jestConfig)) {
        fail(
            'audit3-component-scaffold: projects missing',
            'jest.config.js no longer uses the `projects` shape. node + dom split is gone.',
        );
    }
    if (!/displayName:\s*['"]node['"]/.test(jestConfig)) {
        fail('audit3-component-scaffold: node project missing', 'jest.config.js has no "node" project');
    }
    if (!/displayName:\s*['"]dom['"]/.test(jestConfig)) {
        fail('audit3-component-scaffold: dom project missing', 'jest.config.js has no "dom" project');
    }
    if (!/testEnvironment:\s*['"]jsdom['"]/.test(jestConfig)) {
        fail('audit3-component-scaffold: jsdom env missing', 'jest.config.js does not configure jsdom for the dom project');
    }
    if (!/\*\*\/__tests__\/\*\*\/\*\.test\.tsx/.test(jestConfig)) {
        fail('audit3-component-scaffold: testMatch wrong', 'dom project does not match *.test.tsx files');
    }
    ok('jest.config.js declares two projects (node + dom) and the dom project runs *.test.tsx under jsdom');

    // 2. jest.setup.dom.ts loads jest-dom matchers.
    if (!fs.existsSync(path.join(ROOT, 'jest.setup.dom.ts'))) {
        fail('audit3-component-scaffold: setup file missing', 'jest.setup.dom.ts does not exist');
    }
    const setupDom = readFile('jest.setup.dom.ts');
    if (!/@testing-library\/jest-dom/.test(setupDom)) {
        fail(
            'audit3-component-scaffold: matchers missing',
            'jest.setup.dom.ts does not import @testing-library/jest-dom. toBeInTheDocument etc. would not work.',
        );
    }
    ok('jest.setup.dom.ts imports @testing-library/jest-dom (matchers loaded for DOM tests)');

    // 3. devDependencies present.
    const pkg = JSON.parse(readFile('package.json'));
    const deps = { ...pkg.dependencies, ...pkg.devDependencies };
    for (const d of [
        '@testing-library/react',
        '@testing-library/jest-dom',
        '@testing-library/user-event',
        'jest-environment-jsdom',
    ]) {
        if (!deps[d]) {
            fail(
                'audit3-component-scaffold: dependency missing',
                `package.json does not depend on ${d}. The dom test project cannot resolve its imports.`,
            );
        }
    }
    ok('package.json has @testing-library/{react,jest-dom,user-event} + jest-environment-jsdom');

    // 4. At least three component test files present (proof points).
    const findTsx = (dir: string, out: string[] = []): string[] => {
        for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
            const full = path.join(dir, e.name);
            if (e.isDirectory()) findTsx(full, out);
            else if (e.name.endsWith('.test.tsx')) out.push(full);
        }
        return out;
    };
    const componentDir = path.join(ROOT, 'src/components');
    const tsxTests = findTsx(componentDir);
    if (tsxTests.length < 3) {
        fail(
            'audit3-component-scaffold: too few tests',
            `Found only ${tsxTests.length} component test file(s) under src/components/. The scaffold requires at least 3 proof points so future contributors have copy-paste templates for class components, leaf components, and composed components.`,
        );
    }
    ok(`${tsxTests.length} component test file(s) under src/components/ (≥3 required as scaffold proof points)`);

    ok('audit3-component-scaffold', 'jest projects + jsdom + jest-dom + proof-point tests wired end-to-end');
}

main();
