import assert from "node:assert/strict";
import test from "node:test";
import { collectVpnTelemetry } from "../src/vpn.mjs";

test("reports an optional VPN as disabled without reading the host", async () => {
  const telemetry = await collectVpnTelemetry({ vpn: { enabled: false } }, {
    readFile: async () => { throw new Error("must not read"); },
  });
  assert.deepEqual(telemetry, { enabled: false, state: "disabled" });
});

test("reports a recent WireGuard handshake without exposing keys", async () => {
  const lastHandshakeAt = new Date(Date.now() - 30_000).toISOString();
  const telemetry = await collectVpnTelemetry({
    vpn: {
      enabled: true,
      interface: "wg-vimify",
      tunnelIp: "10.77.0.10/32",
      hubEndpoint: "vpn.example.com:51820",
      statusFile: "/run/vimify-vpn/status.json",
    },
  }, {
    readFile: async (path) => path.includes("operstate")
      ? "unknown\n"
      : JSON.stringify({ lastHandshakeAt, privateKey: "must-not-leak" }),
  });

  assert.equal(telemetry.state, "healthy");
  assert.equal(telemetry.lastHandshakeAt, lastHandshakeAt);
  assert.equal(telemetry.tunnelIp, "10.77.0.10/32");
  assert.equal("privateKey" in telemetry, false);
});
