import assert from "node:assert/strict";
import test from "node:test";
import { parseCommunitySnapshot } from "../src/main/community.ts";

const valid = {
  schemaVersion: 1,
  updatedAt: "2026-08-30T13:00:00Z",
  playersOnline: 12,
  servers: [{ id: "survival-1", label: "Survie publique", players: 9 }],
  fortunes: [{ name: "Tyralulu", platform: "JAVA", value: 7144, secondary: 0 }],
  playtime: [{ name: "Baptium_", platform: "JAVA", value: 7200, secondary: 0 }],
  clans: [{ name: "Royaume de France", tag: "FR", level: 1, members: 6, claims: 369, bankBalance: 0, power: 1079 }],
  pvpAvailable: false,
};

test("community parser accepts the bounded public contract", () => {
  const parsed = parseCommunitySnapshot(valid);
  assert.equal(parsed.state, "live");
  assert.equal(parsed.clans[0]?.name, "Royaume de France");
});

test("community parser rejects oversized or malformed content", () => {
  assert.throws(() => parseCommunitySnapshot({ ...valid, playersOnline: -1 }));
  assert.throws(() => parseCommunitySnapshot({ ...valid, fortunes: Array.from({ length: 21 }, () => valid.fortunes[0]) }));
  assert.throws(() => parseCommunitySnapshot({ ...valid, fortunes: [{ ...valid.fortunes[0], platform: "OTHER" }] }));
});
