ai-chatbot/scripts/check-test-hygiene.mjs
2026-05-27 12:24:57 +08:00

45 lines
1.9 KiB
JavaScript

#!/usr/bin/env node
import { readFileSync } from "node:fs";
const packageJson = JSON.parse(readFileSync("package.json", "utf8"));
const helpersConfig = readFileSync("playwright.helpers.config.ts", "utf8");
const violations = [];
const expectIncludes = (source, expected, reason) => {
if (!source.includes(expected)) {
violations.push(`missing ${reason}: ${expected}`);
}
};
const expectNotIncludes = (source, forbidden, reason) => {
if (source.includes(forbidden)) {
violations.push(`unexpected ${reason}: ${forbidden}`);
}
};
const testHelpersScript = packageJson.scripts?.["test:helpers"] ?? "";
const qualityScript = packageJson.scripts?.["check:quality"] ?? "";
expectIncludes(testHelpersScript, "PLAYWRIGHT=True", "Playwright test env flag");
expectIncludes(testHelpersScript, "playwright test --config=playwright.helpers.config.ts", "dedicated helper Playwright config");
expectIncludes(qualityScript, "pnpm test:helpers", "helper tests in quality gate");
expectIncludes(helpersConfig, 'testDir: "./tests"', "tests directory");
expectIncludes(helpersConfig, "testMatch: /routes\\/canvas-chat-helpers\\.test\\.ts/", "single pure helper test target");
expectIncludes(helpersConfig, 'reporter: process.env.CI ? "github" : "list"', "fast non-html reporter");
expectIncludes(helpersConfig, "timeout: 60 * 1000", "bounded helper test timeout");
expectNotIncludes(helpersConfig, "webServer", "Next dev server in helper tests");
expectNotIncludes(helpersConfig, "devices[", "browser device dependency in helper tests");
expectNotIncludes(helpersConfig, "projects:", "multi-project helper test expansion");
expectNotIncludes(helpersConfig, "testMatch: /e2e", "e2e tests in helper gate");
if (violations.length > 0) {
console.error("Test hygiene check failed. Keep helper tests fast, no-server, and wired into check:quality.");
console.error(violations.join("\n"));
process.exit(1);
}
console.log("Test hygiene check passed.");