Files
galaxy-game/ui/frontend/tests/inspector-planet-ship-groups.test.ts
T
Ilia Denisov 24c68e9846
Tests · UI / test (push) Has been cancelled
Tests · Go / test (pull_request) Successful in 2m6s
Tests · Go / test (push) Successful in 2m6s
Tests · Integration / integration (pull_request) Successful in 1m51s
Tests · UI / test (pull_request) Successful in 3m53s
feat(model+ui): F8-05 — race on OtherGroup, real attribution + N×M label
Issue #48 п.32 ("Stationed ship groups") shipped with a fragile race
fallback: when a foreign group sat on a non-`other`-kind planet the
inspector printed a generic "foreign" label, which collapsed the
race dropdown to a single uninformative bucket. The engine FBS
contract did not carry per-group race either, so live games hit the
same gap. This patch carries race authoritatively from the engine
through every layer down to the inspector.

Wire format & engine
- `pkg/schema/fbs/report.fbs`: add `race:string` to `OtherGroup` and
  `LocalGroup` (additive — old clients ignore).
- `pkg/schema/fbs/report/`: regenerated Go bindings.
- `ui/frontend/src/proto/galaxy/fbs/report/`: regenerated TS bindings.
- `pkg/model/report.OtherGroup.Race`: new field; carried through
  `LocalGroup` via the embedded `OtherGroup`.
- `pkg/transcoder/report.go`: encode + decode `race` on both
  `LocalGroup` and `OtherGroup`.
- `game/internal/controller/report.go.otherGroup`: set `v.Race`
  from `c.g.Race[c.RaceIndex(sg.OwnerID)].Name` so every emitted
  group — own or foreign — carries the resolved race name.

Legacy parser
- `tools/local-dev/legacy-report/parser.go`: capture the
  `<Race> Groups` header into `pendingOtherGroup.race`, fill local
  group `Race` from `p.rep.Race`, propagate both into the
  `report.OtherGroup` rows.
- Tests + smoke counts updated; regenerated `KNNTS{039,041}.json`
  fixtures so the synthetic loader carries the new field.

UI
- `ui/frontend/src/api/`: `ReportShipGroupBase.race` field;
  synthetic loader + FBS decoder populate it.
- `ui/frontend/src/lib/inspectors/planet/ship-groups.svelte`: the
  stationed-groups inspector picks race directly from
  `group.race` (own falls back to `localRace`, both finally to the
  `race.unknown` placeholder). The planet-owner / "foreign"
  heuristic is gone.
- Row label changes from "N ships mass M" to a compact
  `<class>` | `<N ×>` | `<mass>` three-column layout: the count
  cell is right-aligned tabular, the mass cell is right-aligned
  monospace + tabular, matching the inspector / calculator number
  conventions. Stale i18n keys removed
  (`ship_groups.row.count`, `.row.mass`, `.race.foreign`).
- All affected unit tests (8 files) carry the new `race` field.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 16:23:17 +02:00

236 lines
6.2 KiB
TypeScript

// Vitest coverage for the "stationed ship groups" subsection of the
// planet inspector. The map deliberately hides on-planet groups; this
// subsection is the player's view of the fleets in orbit.
//
// F8-05 (issue #48 п.32) moved the race column from the row into a
// dropdown above the table. The dropdown only renders when more than
// one race is stationed; it seeds with the player's own race when
// local groups are stationed here, otherwise with the first race
// alphabetically. Single-race cases skip the dropdown and render
// straight through. The race column is dropped in both modes — the
// dropdown's value already names the active race.
import "@testing-library/jest-dom/vitest";
import { fireEvent, render } from "@testing-library/svelte";
import { beforeEach, describe, expect, test } from "vitest";
import { i18n } from "../src/lib/i18n/index.svelte";
import type {
ReportLocalShipGroup,
ReportOtherShipGroup,
ReportPlanet,
} from "../src/api/game-state";
import ShipGroups from "../src/lib/inspectors/planet/ship-groups.svelte";
beforeEach(() => {
i18n.resetForTests("en");
});
const HOME_PLANET: ReportPlanet = {
number: 17,
name: "Castle",
x: 100,
y: 100,
kind: "local",
owner: null,
size: 1000,
resources: 10,
industryStockpile: 0,
materialsStockpile: 0,
industry: 1000,
population: 1000,
colonists: 100,
production: "Capital",
freeIndustry: 1000,
};
const FOREIGN_PLANET: ReportPlanet = {
...HOME_PLANET,
number: 99,
name: "Outpost",
kind: "other",
owner: "Klingons",
};
function localGroup(
overrides: Partial<ReportLocalShipGroup> = {},
): ReportLocalShipGroup {
return {
id: "uuid-1",
count: 1,
class: "Frontier",
tech: { drive: 5, weapons: 0, shields: 0, cargo: 1 },
cargo: "NONE",
load: 0,
destination: 17,
origin: null,
range: null,
speed: 0,
mass: 12,
state: "In_Orbit",
fleet: null,
race: "Earthlings",
...overrides,
};
}
function otherGroup(
overrides: Partial<ReportOtherShipGroup> = {},
): ReportOtherShipGroup {
return {
count: 3,
class: "Bird-of-Prey",
tech: { drive: 6, weapons: 4, shields: 3, cargo: 0 },
cargo: "NONE",
load: 0,
destination: 99,
origin: null,
range: null,
speed: 0,
mass: 25,
race: "Klingons",
...overrides,
};
}
describe("planet inspector — stationed ship groups", () => {
test("renders one row per in-orbit local group; the dropdown is hidden with a single race", () => {
const ui = render(ShipGroups, {
props: {
planet: HOME_PLANET,
localShipGroups: [
localGroup({ id: "g1", count: 2, class: "Frontier", mass: 24 }),
localGroup({ id: "g2", count: 7, class: "Furgon", mass: 173.25 }),
],
otherShipGroups: [],
localRace: "Earthlings",
},
});
const rows = ui.getAllByTestId("inspector-planet-ship-groups-row");
expect(rows.length).toBe(2);
// Race no longer appears in the row (it is hoisted to the
// dropdown — and the dropdown itself is hidden when only one
// race is present).
expect(
ui.queryByTestId("inspector-planet-ship-groups-race-filter"),
).toBeNull();
expect(rows[0]).not.toHaveTextContent("Earthlings");
expect(rows[0]).toHaveTextContent("Frontier");
expect(rows[0]).toHaveTextContent("2");
expect(rows[0]).toHaveTextContent("24");
expect(rows[1]).toHaveTextContent("Furgon");
expect(rows[1]).toHaveTextContent("173.25");
});
test("multiple races surface a dropdown that filters the table", async () => {
const ui = render(ShipGroups, {
props: {
planet: FOREIGN_PLANET,
localShipGroups: [
localGroup({ id: "own-1", destination: 99, class: "Frontier" }),
],
otherShipGroups: [
otherGroup({ class: "Bird-of-Prey", destination: 99 }),
],
localRace: "Earthlings",
},
});
const select = ui.getByTestId(
"inspector-planet-ship-groups-race-filter",
) as HTMLSelectElement;
// Own ships are stationed → own race wins as the default;
// alphabetical ordering puts the foreign one second.
expect(select.value).toBe("Earthlings");
expect(Array.from(select.options).map((o) => o.value)).toEqual([
"Earthlings",
"Klingons",
]);
expect(
ui.getAllByTestId("inspector-planet-ship-groups-row").length,
).toBe(1);
expect(
ui.getByTestId("inspector-planet-ship-groups-row"),
).toHaveTextContent("Frontier");
await fireEvent.change(select, { target: { value: "Klingons" } });
const rows = ui.getAllByTestId("inspector-planet-ship-groups-row");
expect(rows.length).toBe(1);
expect(rows[0]).toHaveTextContent("Bird-of-Prey");
});
test("with no own ships the dropdown collapses to the single foreign race", () => {
const ui = render(ShipGroups, {
props: {
planet: {
...HOME_PLANET,
owner: "Andorians",
kind: "other",
},
localShipGroups: [],
otherShipGroups: [
otherGroup({ class: "Bird-of-Prey", destination: 17 }),
],
localRace: "Earthlings",
},
});
// Single foreign race → no dropdown.
expect(
ui.queryByTestId("inspector-planet-ship-groups-race-filter"),
).toBeNull();
expect(
ui.getAllByTestId("inspector-planet-ship-groups-row").length,
).toBe(1);
});
test("filters out groups stationed on a different planet", () => {
const ui = render(ShipGroups, {
props: {
planet: HOME_PLANET,
localShipGroups: [
localGroup({ id: "g1", destination: 17 }),
localGroup({ id: "g2", destination: 99 }),
],
otherShipGroups: [],
localRace: "Earthlings",
},
});
expect(ui.getAllByTestId("inspector-planet-ship-groups-row").length).toBe(
1,
);
});
test("excludes in-hyperspace groups even when destination matches", () => {
const ui = render(ShipGroups, {
props: {
planet: HOME_PLANET,
localShipGroups: [
localGroup({ id: "stationed", destination: 17 }),
localGroup({
id: "fleeing",
destination: 17,
origin: 99,
range: 5,
}),
],
otherShipGroups: [],
localRace: "Earthlings",
},
});
expect(ui.getAllByTestId("inspector-planet-ship-groups-row").length).toBe(
1,
);
});
test("subsection collapses entirely when nothing is stationed", () => {
const ui = render(ShipGroups, {
props: {
planet: HOME_PLANET,
localShipGroups: [],
otherShipGroups: [],
localRace: "Earthlings",
},
});
expect(ui.queryByTestId("inspector-planet-ship-groups")).toBeNull();
});
});