import { expect, test } from "bun:test";
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { compatibilityReport, upgradeCompatibility } from "../src/compatibility-command.ts";

test("compatibility upgrade is backed up, current, and idempotent", async () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-compatibility-"));
  const file = join(root, "wrnexus.config.ts");
  writeFileSync(file, `export default { port: 3000 };\n`);
  const first = upgradeCompatibility(root);
  expect(first.changed).toBe(true);
  expect(readFileSync(first.backup, "utf8")).toContain("port: 3000");
  expect(readFileSync(file, "utf8")).toContain('compatibilityDate: "2026-08-02"');
  expect(upgradeCompatibility(root).changed).toBe(false);
  expect((await compatibilityReport(root)).needsUpgrade).toBe(false);
});
