import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";

const runtimeRoot = join(process.cwd(), "src");
const forbiddenDrainageTerms = [
  "排水管网",
  "单位排水电耗",
  "排水边界",
  "距污水厂距离",
  "关联检查井",
  "管渠拓扑",
  "drainage-network",
  "DRAINAGE_",
  "DrainageFeature"
] as const;

function listRuntimeFiles(directory: string): string[] {
  return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
    const target = join(directory, entry.name);
    if (entry.isDirectory()) return listRuntimeFiles(target);
    if (!/\.(ts|tsx)$/.test(entry.name) || /\.(test|e2e)\.(ts|tsx)$/.test(entry.name)) return [];
    return [target];
  });
}

describe("supply domain boundary", () => {
  it("does not reintroduce drainage-only operating language", () => {
    const violations = listRuntimeFiles(runtimeRoot).flatMap((file) => {
      const source = readFileSync(file, "utf8");
      return forbiddenDrainageTerms
        .filter((term) => source.includes(term))
        .map((term) => `${file.replace(`${runtimeRoot}/`, "")}: ${term}`);
    });

    expect(violations).toEqual([]);
  });
});
