8e552f556d
Polish pass after the first F8-10 walkthrough:
- table-planets: moved the `foreign` chip to the end of the row and
hid the race dropdown until `foreign` is on (it never made sense
to pick a race while the bucket itself was off).
- persistent per-table filter / sort state — extracted to
`table-{planets,ship-groups,fleets}-state.svelte.ts` singletons so
a row click → map → back to the table restores the prior chip /
dropdown / sort state. Held in memory only; an F5 still resets.
- table-ship-groups: the planet and class dropdowns now narrow to
the slice surviving the owner checkboxes, so toggling `foreign`
off removes planets / classes touched only by foreign rows.
- map.svelte: camera (centre + zoom) is captured on every dispose
path into a new `GameStateStore.lastCamera` and consumed on the
next mount, so leaving the map for any other active view and
coming back restores the prior pan / zoom. A pending focus from
the tables still wins for the centre point.
- table-ship-classes: `:disabled` now reads as disabled (muted
colour, no hover ring, not-allowed cursor) — the click was already
a no-op, only the visual was lying.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
222 lines
5.5 KiB
TypeScript
222 lines
5.5 KiB
TypeScript
// Vitest coverage for the F8-10 fleets table active view.
|
|
// Mounts the component against a synthetic `RenderedReportSource`
|
|
// and a real `SelectionStore`; verifies the click semantics for both
|
|
// on-planet (focus the destination planet) and in-space (centre the
|
|
// camera on the interpolated point, leave selection untouched).
|
|
|
|
import "@testing-library/jest-dom/vitest";
|
|
import { fireEvent, render } from "@testing-library/svelte";
|
|
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
|
|
import { i18n } from "../src/lib/i18n/index.svelte";
|
|
import type {
|
|
GameReport,
|
|
ReportLocalFleet,
|
|
ReportPlanet,
|
|
} from "../src/api/game-state";
|
|
import { RENDERED_REPORT_CONTEXT_KEY } from "../src/lib/rendered-report.svelte";
|
|
import {
|
|
SELECTION_CONTEXT_KEY,
|
|
SelectionStore,
|
|
} from "../src/lib/selection.svelte";
|
|
import { activeView } from "../src/lib/app-nav.svelte";
|
|
import { resetFleetsTableState } from "../src/lib/active-view/table-fleets-state.svelte";
|
|
import { EMPTY_SHIP_GROUPS } from "./helpers/empty-ship-groups";
|
|
|
|
const pageMock = vi.hoisted(() => ({
|
|
url: new URL("http://localhost/games/g1/table/fleets"),
|
|
params: { id: "g1" } as Record<string, string>,
|
|
}));
|
|
|
|
const gotoMock = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock("$app/state", () => ({
|
|
page: pageMock,
|
|
}));
|
|
|
|
vi.mock("$app/navigation", () => ({
|
|
goto: gotoMock,
|
|
pushState: vi.fn(),
|
|
replaceState: vi.fn(),
|
|
}));
|
|
|
|
import TableFleets from "../src/lib/active-view/table-fleets.svelte";
|
|
|
|
let selection: SelectionStore;
|
|
|
|
beforeEach(() => {
|
|
selection = new SelectionStore();
|
|
i18n.resetForTests("en");
|
|
pageMock.params = { id: "g1" };
|
|
gotoMock.mockClear();
|
|
activeView.reset();
|
|
resetFleetsTableState();
|
|
});
|
|
|
|
afterEach(() => {
|
|
selection.dispose();
|
|
});
|
|
|
|
function planet(num: number, x = 0, y = 0): ReportPlanet {
|
|
return {
|
|
number: num,
|
|
name: `P${num}`,
|
|
x,
|
|
y,
|
|
kind: "local",
|
|
owner: null,
|
|
size: null,
|
|
resources: null,
|
|
industryStockpile: null,
|
|
materialsStockpile: null,
|
|
industry: null,
|
|
population: null,
|
|
colonists: null,
|
|
production: null,
|
|
freeIndustry: null,
|
|
};
|
|
}
|
|
|
|
function fleet(
|
|
overrides: Partial<ReportLocalFleet> & Pick<ReportLocalFleet, "name" | "destination">,
|
|
): ReportLocalFleet {
|
|
return {
|
|
groupCount: 1,
|
|
origin: null,
|
|
range: null,
|
|
speed: 0,
|
|
state: "In_Orbit",
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeReport(opts: {
|
|
planets?: ReportPlanet[];
|
|
fleets?: ReportLocalFleet[];
|
|
}): GameReport {
|
|
return {
|
|
turn: 1,
|
|
mapWidth: 1000,
|
|
mapHeight: 1000,
|
|
planetCount: opts.planets?.length ?? 0,
|
|
planets: opts.planets ?? [],
|
|
race: "Me",
|
|
localShipClass: [],
|
|
routes: [],
|
|
localPlayerDrive: 0,
|
|
localPlayerWeapons: 0,
|
|
localPlayerShields: 0,
|
|
localPlayerCargo: 0,
|
|
...EMPTY_SHIP_GROUPS,
|
|
localFleets: opts.fleets ?? [],
|
|
};
|
|
}
|
|
|
|
function mount(report: GameReport | null) {
|
|
const renderedReport = {
|
|
get report() {
|
|
return report;
|
|
},
|
|
};
|
|
const context = new Map<unknown, unknown>([
|
|
[RENDERED_REPORT_CONTEXT_KEY, renderedReport],
|
|
[SELECTION_CONTEXT_KEY, selection],
|
|
]);
|
|
return render(TableFleets, { context });
|
|
}
|
|
|
|
describe("fleets table", () => {
|
|
test("renders a loading placeholder before the report lands", () => {
|
|
const ui = mount(null);
|
|
expect(ui.getByTestId("fleets-loading")).toBeInTheDocument();
|
|
});
|
|
|
|
test("renders an empty placeholder when no fleets exist", () => {
|
|
const ui = mount(makeReport({ planets: [planet(1)] }));
|
|
expect(ui.getByTestId("fleets-empty")).toBeInTheDocument();
|
|
});
|
|
|
|
test("renders one row per fleet", () => {
|
|
const ui = mount(
|
|
makeReport({
|
|
planets: [planet(1), planet(2)],
|
|
fleets: [
|
|
fleet({ name: "Alpha", destination: 1 }),
|
|
fleet({
|
|
name: "Bravo",
|
|
destination: 2,
|
|
origin: 1,
|
|
range: 4,
|
|
state: "In_Space",
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
expect(ui.getAllByTestId("fleets-row")).toHaveLength(2);
|
|
});
|
|
|
|
test("planet dropdown filters by destination OR origin", async () => {
|
|
const ui = mount(
|
|
makeReport({
|
|
planets: [planet(1), planet(2), planet(3)],
|
|
fleets: [
|
|
fleet({ name: "Alpha", destination: 1 }),
|
|
fleet({
|
|
name: "Bravo",
|
|
destination: 3,
|
|
origin: 2,
|
|
range: 4,
|
|
state: "In_Space",
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
const sel = ui.getByTestId("fleets-filter-planet") as HTMLSelectElement;
|
|
await fireEvent.change(sel, { target: { value: "2" } });
|
|
const names = ui
|
|
.getAllByTestId("fleets-row")
|
|
.map((r) => r.getAttribute("data-name"));
|
|
expect(names).toEqual(["Bravo"]);
|
|
});
|
|
|
|
test("click on on-planet fleet focuses the destination planet", async () => {
|
|
const ui = mount(
|
|
makeReport({
|
|
planets: [planet(7)],
|
|
fleets: [fleet({ name: "Alpha", destination: 7 })],
|
|
}),
|
|
);
|
|
await fireEvent.click(ui.getByTestId("fleets-row"));
|
|
expect(selection.selected).toEqual({ kind: "planet", id: 7 });
|
|
expect(selection.consumePendingFocus()).toEqual({
|
|
kind: "planet",
|
|
id: 7,
|
|
});
|
|
expect(activeView.view).toBe("map");
|
|
});
|
|
|
|
test("click on in-space fleet centres camera point and leaves selection alone", async () => {
|
|
const ui = mount(
|
|
makeReport({
|
|
planets: [planet(1, 0, 0), planet(2, 100, 0)],
|
|
fleets: [
|
|
fleet({
|
|
name: "Bravo",
|
|
destination: 2,
|
|
origin: 1,
|
|
range: 25,
|
|
state: "In_Space",
|
|
}),
|
|
],
|
|
}),
|
|
);
|
|
expect(selection.selected).toBeNull();
|
|
await fireEvent.click(ui.getByTestId("fleets-row"));
|
|
expect(selection.selected).toBeNull();
|
|
// 25 world units away from dest (2 at x=100) toward origin (1 at x=0):
|
|
// dest + (range/total)*(origin-dest) = 100 + 0.25 * -100 = 75
|
|
expect(selection.consumePendingCenter()).toEqual({ x: 75, y: 0 });
|
|
expect(activeView.view).toBe("map");
|
|
});
|
|
});
|