import { expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { builtinDevToolbarPanels } from "../src/server/builtin-panels.ts";

test("built-in providers expose bounded operational data without environment secrets", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-toolbar-providers-"));
  try {
    mkdirSync(join(root, "app", "queues"), { recursive: true });
    mkdirSync(join(root, "app", "locales"), { recursive: true });
    writeFileSync(join(root, "app", "queues", "email.ts"), "export default {};\n");
    writeFileSync(
      join(root, "app", "locales", "en.json"),
      JSON.stringify({ hello: "Hello", bye: "Bye" }),
    );
    writeFileSync(join(root, "app", "locales", "fr.json"), JSON.stringify({ hello: "Bonjour" }));
    const panels = builtinDevToolbarPanels({
      root,
      platform: { routes: { pages: 1, api: 2, realtime: 3 } },
      database: { queries: [{ durationMs: 120 }], issues: [{ code: "SLOW" }] },
      version: { current: "1.0.0", latest: "1.1.0" },
    });
    expect(panels.map((panel) => panel.id)).toEqual([
      "database",
      "queues",
      "realtime",
      "translations",
      "css",
      "performance",
      "upgrades",
    ]);
    expect(panels.find((panel) => panel.id === "queues")?.badge).toBe(1);
    expect(panels.find((panel) => panel.id === "translations")?.badge).toBe(1);
    expect(JSON.stringify(panels)).not.toContain("process.env");
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});
