import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { execSync } from "node:child_process";

/**
 * Parity check: the committed AxintFixer.swift must match whatever the
 * codegen script would emit from swift-fix-rules.ts right now. If this
 * test fails, run `npm run gen:swift-fixer` and commit the result.
 */
describe("AxintFixer.swift parity with swift-fix-rules.ts", () => {
  it("checked-in Swift fixer is up to date", () => {
    const root = resolve(__dirname, "../..");
    const target = resolve(
      root,
      "extensions/xcode/source-editor-extension/AxintEditor/AxintFixer.swift"
    );
    const committed = readFileSync(target, "utf8");

    // Re-run the generator in --check mode; it exits non-zero if stale.
    let ok = true;
    try {
      execSync("npx tsx scripts/gen-swift-fixer.ts --check", {
        cwd: root,
        stdio: "pipe",
      });
    } catch {
      ok = false;
    }

    expect(ok, "AxintFixer.swift is out of date. Run `npm run gen:swift-fixer`.").toBe(
      true
    );
    expect(committed).toContain("AUTO-GENERATED by scripts/gen-swift-fixer.ts");
  });
});
