// Vitest coverage for the Phase 23 Report View's galaxy summary
// section. Representative for kv-list-shape sections (votes,
// player-status row markers). Mounts the component against a
// synthetic `RenderedReportSource` so the test focuses on shape,
// not on the live store wiring.

import "@testing-library/jest-dom/vitest";
import { render } from "@testing-library/svelte";
import { beforeEach, describe, expect, test } from "vitest";

import { i18n } from "../src/lib/i18n/index.svelte";
import type { GameReport } from "../src/api/game-state";
import { RENDERED_REPORT_CONTEXT_KEY } from "../src/lib/rendered-report.svelte";
import { EMPTY_SHIP_GROUPS } from "./helpers/empty-ship-groups";

import SectionGalaxySummary from "../src/lib/active-view/report/section-galaxy-summary.svelte";

beforeEach(() => {
	i18n.resetForTests("en");
});

function makeReport(overrides: Partial<GameReport> = {}): GameReport {
	return {
		turn: 0,
		mapWidth: 0,
		mapHeight: 0,
		planetCount: 0,
		planets: [],
		race: "",
		localShipClass: [],
		routes: [],
		localPlayerDrive: 0,
		localPlayerWeapons: 0,
		localPlayerShields: 0,
		localPlayerCargo: 0,
		...EMPTY_SHIP_GROUPS,
		...overrides,
	};
}

function mountSection(report: GameReport | null) {
	const context = new Map<unknown, unknown>([
		[RENDERED_REPORT_CONTEXT_KEY, { get report() {
			return report;
		} }],
	]);
	return render(SectionGalaxySummary, { context });
}

describe("report galaxy summary section", () => {
	test("renders the loading placeholder before the report lands", () => {
		const ui = mountSection(null);
		expect(
			ui.getByTestId("report-section-galaxy-summary"),
		).toHaveTextContent("loading report");
	});

	test("renders every kv pair for a populated report", () => {
		const ui = mountSection(
			makeReport({
				turn: 42,
				mapWidth: 1234,
				mapHeight: 4321,
				planetCount: 700,
				race: "KnightErrants",
			}),
		);
		expect(ui.getByTestId("galaxy-summary-field-turn")).toHaveTextContent("42");
		expect(ui.getByTestId("galaxy-summary-field-size")).toHaveTextContent(
			"1234 × 4321",
		);
		expect(ui.getByTestId("galaxy-summary-field-planets")).toHaveTextContent(
			"700",
		);
		expect(ui.getByTestId("galaxy-summary-field-race")).toHaveTextContent(
			"KnightErrants",
		);
	});

	test("zero-value boot state still mounts every field", () => {
		const ui = mountSection(makeReport());
		// Loading state must be gone — the kv-list takes over.
		const section = ui.getByTestId("report-section-galaxy-summary");
		expect(section).not.toHaveTextContent("loading report");
		expect(ui.getByTestId("galaxy-summary-field-turn")).toHaveTextContent("0");
		expect(ui.getByTestId("galaxy-summary-field-race")).toHaveTextContent("");
	});
});
