source-size-policy.test.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import ts from "typescript";
  2. import assert from "node:assert/strict";
  3. import { execFile } from "node:child_process";
  4. import { mkdtemp, mkdir, readFile, writeFile, rm } from "node:fs/promises";
  5. import { tmpdir } from "node:os";
  6. import { dirname, join, resolve } from "node:path";
  7. import { promisify } from "node:util";
  8. import { test } from "node:test";
  9. const exec = promisify(execFile);
  10. const root = resolve(import.meta.dirname, "../..");
  11. test("800 行为默认阻断阈值,已接纳的大文件仅降级行数诊断", async () => {
  12. const cwd = await mkdtemp(join(tmpdir(), "zcode-size-policy-"));
  13. try {
  14. const { config } = ts.parseConfigFileTextToJson(
  15. ".oxlintrc.json",
  16. await readFile(join(root, ".oxlintrc.json"), "utf8"),
  17. );
  18. // 只验证规则本身;根 runner 排除 CLI,由独立 CLI runner 检查。
  19. config.ignorePatterns = [];
  20. await writeFile(join(cwd, ".oxlintrc.json"), JSON.stringify(config));
  21. const regular = "packages/example/src/new.ts";
  22. const accepted = "apps/zcode-cli/packages/bootstrap/src/zcode-protocol-v4/v4-gateway.ts";
  23. async function lint(file, lines) {
  24. await mkdir(dirname(join(cwd, file)), { recursive: true });
  25. await writeFile(
  26. join(cwd, file),
  27. Array.from({ length: lines }, (_, i) => `export const value${i} = ${i};`).join("\n"),
  28. );
  29. const packageRoot = file.startsWith("apps/zcode-cli/")
  30. ? "apps/zcode-cli/packages/bootstrap"
  31. : ".";
  32. const target = packageRoot === "." ? file : file.slice(packageRoot.length + 1);
  33. const result = await exec(
  34. process.execPath,
  35. [join(root, "node_modules/oxlint/bin/oxlint"), "--format", "json", target],
  36. { cwd: join(cwd, packageRoot) },
  37. ).catch((error) => error);
  38. return JSON.parse(result.stdout).diagnostics;
  39. }
  40. assert.equal(
  41. (await lint(regular, 800)).filter((x) => x.code === "eslint(max-lines)").length,
  42. 0,
  43. );
  44. assert.equal(
  45. (await lint(regular, 801)).find((x) => x.code === "eslint(max-lines)")?.severity,
  46. "error",
  47. );
  48. assert.equal(
  49. (await lint(accepted, 801)).find((x) => x.code === "eslint(max-lines)")?.severity,
  50. "warning",
  51. );
  52. } finally {
  53. await rm(cwd, { recursive: true, force: true });
  54. }
  55. });