Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.2 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { parseExclusions, findViolations } from "../../scripts/check/check-vitest-exclusions.mjs";

const ROOT = path.resolve(import.meta.dirname, "..", "..");

/** Every excluded file in the inventory is a real path — a stale entry excludes nothing. */
test("the checked-in inventory only lists files that exist", () => {
  const inv = JSON.parse(
    fs.readFileSync(path.join(ROOT, "config/quality/vitest-exclusions.json"), "utf8")
  );
  // An empty inventory is the goal state: #14493 returned every quarantined suite to normal
  // discovery. An exclusion added to vitest.config.ts without an inventory entry is still
  // caught by "the live config satisfies the gate" below.
  assert.ok(Array.isArray(inv.excluded), "inventory has an excluded list");
  for (const entry of inv.excluded) {
    assert.ok(fs.existsSync(path.join(ROOT, entry.file)), `${entry.file} exists`);
    assert.match(entry.issue, /^#\d+$/, `${entry.file} names a tracking issue`);
  }
});

test("the live config satisfies the gate", () => {
  const entries = parseExclusions(fs.readFileSync(path.join(ROOT, "vitest.config.ts"), "utf8"));
  const inv = JSON.parse(
    fs.readFileSync(path.join(ROOT, "config/quality/vitest-exclusions.json"), "utf8")
  );
  const v = findViolations(
    entries,
    (p) => fs.existsSync(path.join(ROOT, p)),
    inv.excluded.map((e: { file: string }) => e.file)
  );
  assert.deepEqual(v, { unreferenced: [], untracked: [], orphaned: [] });
});

test("parseExclusions keeps each entry's trailing comment", () => {
  const src = `  test: {\n    exclude: [\n      "node_modules/**",\n      "tests/a.test.ts", // #123 — reason\n    ]\n  }`;
  assert.deepEqual(parseExclusions(src), [
    { pattern: "node_modules/**", comment: "," },
    { pattern: "tests/a.test.ts", comment: ", // #123 — reason" },
  ]);
});

test("an exclusion with no issue reference fails the gate", () => {
  const entries = [{ pattern: "tests/a.test.ts", comment: ", // just because" }];
  const v = findViolations(entries, () => true, ["tests/a.test.ts"]);
  assert.deepEqual(v.unreferenced, ["tests/a.test.ts"]);
});

test("an exclusion absent from the inventory fails the gate", () => {
  const entries = [{ pattern: "tests/a.test.ts", comment: ", // #123" }];
  const v = findViolations(entries, () => true, []);
  assert.deepEqual(v.untracked, ["tests/a.test.ts"]);
});

test("an inventory entry that is no longer excluded fails the gate", () => {
  const v = findViolations([], () => true, ["tests/revived.test.ts"]);
  assert.deepEqual(v.orphaned, ["tests/revived.test.ts"]);
});

test("only named tooling exclusions are exempt, and a stale path is ignored", () => {
  const entries = [
    { pattern: "node_modules/**", comment: "," },
    { pattern: "tests/deleted.test.ts", comment: "," },
  ];
  const v = findViolations(entries, (p) => p !== "tests/deleted.test.ts", []);
  assert.deepEqual(v, { unreferenced: [], untracked: [], orphaned: [] });
});

test("an arbitrary glob cannot hide tests outside the inventory", () => {
  const v = findViolations([{ pattern: "tests/unit/**/*.test.tsx", comment: "," }], () => true, []);
  assert.deepEqual(v.untracked, ["tests/unit/**/*.test.tsx"]);
});