import assert from "node:assert/strict";
import test from "node:test";
import { validateMicrosoftClientId } from "../src/main/microsoft-config.ts";
import { redact } from "../src/main/logger.ts";
import { assertAllowedReleaseUrl } from "../src/main/network-security.ts";
import { isTrustedRendererDocument } from "../src/main/renderer-security.ts";
import { buildStatusRequest, encodeVarInt, parseStatusResponse } from "../src/main/server-status.ts";

test("Microsoft configuration accepts only a valid Entra application UUID", () => {
  assert.equal(
    validateMicrosoftClientId("12345678-1234-4234-9234-123456789ABC"),
    "12345678-1234-4234-9234-123456789abc",
  );
  assert.throws(() => validateMicrosoftClientId(""));
  assert.throws(() => validateMicrosoftClientId("not-an-app-id"));
  assert.throws(() => validateMicrosoftClientId("00000000-0000-0000-0000-000000000000"));
});

test("release URL accepts only the pinned HTTPS host", () => {
  assert.equal(
    assertAllowedReleaseUrl("https://game.bglands.fr/resources/client.jar", "game.bglands.fr").hostname,
    "game.bglands.fr",
  );
  assert.throws(() => assertAllowedReleaseUrl("http://game.bglands.fr/client.jar", "game.bglands.fr"));
  assert.throws(() => assertAllowedReleaseUrl("https://evil.example/client.jar", "game.bglands.fr"));
  assert.throws(() => assertAllowedReleaseUrl("https://game.bglands.fr.evil.example/client.jar", "game.bglands.fr"));
  assert.throws(() => assertAllowedReleaseUrl("https://user:pass@game.bglands.fr/client.jar", "game.bglands.fr"));
  assert.throws(() => assertAllowedReleaseUrl("https://game.bglands.fr:8443/client.jar", "game.bglands.fr"));
});

test("renderer IPC accepts page hashes but rejects other documents and queries", () => {
  const expected = "file:///opt/bglands/renderer/index.html";
  assert.equal(isTrustedRendererDocument(expected, expected), true);
  assert.equal(isTrustedRendererDocument(`${expected}#rules`, expected), true);
  assert.equal(isTrustedRendererDocument(`${expected}?debug=1`, expected), false);
  assert.equal(isTrustedRendererDocument("https://game.bglands.fr/renderer/index.html", expected), false);
  assert.equal(isTrustedRendererDocument("file:///opt/bglands/renderer/other.html#rules", expected), false);
});

test("logger redacts bearer and token-shaped secrets", () => {
  assert.equal(redact("Authorization: Bearer abc.DEF-123_xyz"), "Authorization: [SECRET MASQUÉ]");
  assert.equal(redact("access_token=super-secret-value"), "[SECRET MASQUÉ]");
});

test("logger bounds untrusted output", () => {
  assert.ok(redact("x".repeat(100_000)).length <= 32_000);
});

test("Minecraft status packets are framed and bounded", () => {
  assert.deepEqual([...encodeVarInt(300)], [0xac, 0x02]);
  assert.ok(buildStatusRequest("game.bglands.fr", 25565).length > 20);
  const json = Buffer.from(JSON.stringify({ players: { online: 42, max: 500 } }), "utf8");
  const body = Buffer.concat([Buffer.from([0]), encodeVarInt(json.length), json]);
  assert.deepEqual(parseStatusResponse(Buffer.concat([encodeVarInt(body.length), body])), {
    online: true,
    playersOnline: 42,
    playersMaximum: 500,
  });
  assert.equal(parseStatusResponse(Buffer.from([10, 0, 1])), undefined);
});
