import assert from "node:assert/strict";
import test from "node:test";
import { codexHealth } from "./codex-health.js";

test("codex health reports setup diagnostics without exposing auth files or paths", async () => {
  const config = {
    engine: "codex",
    codexCliPath: "C:\\Tools\\codex.cmd",
    projectsRoot: "C:\\Users\\person\\Projects",
    accounts: [
      { id: "main", label: "Main", codexHome: "C:\\Users\\person\\.codex-main" },
      { id: "desktop", label: "Desktop", codexHome: "C:\\Users\\person\\.codex", historyOnly: true }
    ],
    workspaces: [{ id: "repo", label: "Repo", path: "C:\\Users\\person\\Projects\\repo" }]
  };
  const existingPaths = new Set([
    "C:\\Tools\\codex.cmd",
    "C:\\Users\\person\\.codex-main",
    "C:\\Users\\person\\.codex-main\\auth.json"
  ]);

  const result = await codexHealth(config, {
    fileExists: async (path) => existingPaths.has(path),
    runCommand: async (_command, args) => ({
      ok: true,
      stdout: args.includes("--version") ? "codex-cli 1.2.3\n" : "Usage: codex app-server\n",
      stderr: ""
    }),
    probeAppServer: async () => ({
      ok: false,
      error: "failed to read C:\\Users\\person\\.codex-main\\auth.json",
      stderr: "Bearer secret-token in C:\\Users\\person\\.codex-main\\auth.json"
    })
  });

  assert.equal(result.cli.exists, true);
  assert.equal(result.cli.version, "codex-cli 1.2.3");
  assert.deepEqual(result.accounts, [
    { id: "main", label: "Main", historyOnly: false, home: "present", authentication: "present" },
    { id: "desktop", label: "Desktop", historyOnly: true, home: "missing", authentication: "not_applicable" }
  ]);

  const serialized = JSON.stringify(result);
  assert.equal(serialized.includes("auth.json"), false);
  assert.equal(serialized.includes("secret-token"), false);
  assert.equal(serialized.includes("C:\\Users\\person"), false);
  assert.equal(serialized.includes("codexHome"), false);
});

test("codex health does not inspect authentication or probe app-server for history-only sources", async () => {
  const checkedPaths = [];
  const probedHomes = [];
  const config = {
    engine: "codex",
    codexCliPath: "codex.cmd",
    accounts: [
      { id: "history", label: "History", codexHome: "HISTORY_HOME", historyOnly: true },
      { id: "active", label: "Active", codexHome: "ACTIVE_HOME" }
    ]
  };

  const result = await codexHealth(config, {
    fileExists: async (value) => {
      checkedPaths.push(value);
      return true;
    },
    runCommand: async () => ({ ok: true, stdout: "codex 1.0", stderr: "" }),
    probeAppServer: async (_command, home) => {
      probedHomes.push(home);
      return { ok: true };
    }
  });

  assert.equal(checkedPaths.some((value) => value.includes("HISTORY_HOME\\auth.json")), false);
  assert.deepEqual(probedHomes, ["ACTIVE_HOME"]);
  assert.deepEqual(result.accounts[0], {
    id: "history",
    label: "History",
    historyOnly: true,
    home: "present",
    authentication: "not_applicable"
  });
});
