import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

test("X server status indicator is settings-derived and renders before Don't Sleep", async () => {
  const statusBarSource = await readFile(
    new URL("../src/modules/workspace/StatusBar.tsx", import.meta.url),
    "utf8",
  );
  const tauriSource = await readFile(new URL("../src/lib/tauri.ts", import.meta.url), "utf8");
  const rustSource = await readFile(new URL("../src-tauri/src/lib.rs", import.meta.url), "utf8");

  assert.match(statusBarSource, /function XServerStatusIcon\(\)/);
  assert.match(statusBarSource, /state\.sshSettings\.managedXServerEnabled/);
  assert.doesNotMatch(statusBarSource, /is_ssh_x_server_running/);
  assert.doesNotMatch(statusBarSource, /X_SERVER_STATUS_POLL_MS/);

  const actions = statusBarSource.match(/<div className="status-bar-actions">(?<body>[\s\S]*?)<\/div>/)
    ?.groups?.body;
  assert.ok(actions, "status bar actions cluster should exist");
  assert.ok(
    actions.indexOf("<XServerStatusIcon />") < actions.indexOf("<DontSleepStatusIcon />"),
    "X server status should render before Don't Sleep",
  );

  assert.doesNotMatch(tauriSource, /is_ssh_x_server_running/);
  assert.doesNotMatch(rustSource, /is_ssh_x_server_running/);
  assert.doesNotMatch(rustSource, /X server status/);
});

test("VcXsrv is listed as an Install Helper utility", async () => {
  const installerSource = await readFile(
    new URL("../src/modules/installer/sections.ts", import.meta.url),
    "utf8",
  );
  const manualSource = await readFile(new URL("../docs/manual/18-installer.md", import.meta.url), "utf8");

  assert.match(
    installerSource,
    /titleKey: "installer\.section\.utilities",\s*ids: \[[^\]]*"vcxsrv"[^\]]*\]/,
  );
  assert.match(manualSource, /\*\*Utilities\*\* \(`installer\.section\.utilities`\).*VcXsrv/s);
});
