1
0
Fork 0
polaris-task-force/tests/e2e/garage.e2e.spec.ts

229 lines
7.1 KiB
TypeScript

import { expect, test } from "@playwright/test";
import type { Payload } from "payload";
import { bootstrapTestDatabase, resolveTestDatabaseUri } from "../test-db";
/**
* Garage surface: grid of parked vehicles per bay structure, Elsewhere for
* off-grid instances, drag-to-rearrange persisting through the server action,
* and the legacy /logistics/game-vehicles redirect.
*
* NOTE: exercises the real dev server under the dev-server protocol (the
* webServer env points DATABASE_URI at the dedicated test database).
*/
const RUN = `e2e-garage-${Date.now().toString(36)}`;
const STRUCTURE_NAME = `${RUN} Motor Pool`;
const PARKED_NAME = `${RUN} Parked HEMTT`;
const UNPLACED_NAME = `${RUN} Stray Truck`;
const LEXICAL_EMPTY = {
root: {
children: [{ text: "" }],
direction: null,
format: "" as const,
indent: 0,
type: "text",
version: 1,
},
};
let payload: Payload;
const cleanupIds: Array<{ collection: string; id: number }> = [];
async function seed() {
process.env.DATABASE_URI = resolveTestDatabaseUri();
const { getPayload } = await import("payload");
const { default: config } = await import("@payload-config");
payload = await getPayload({ config });
const resource = await payload.create({
collection: "resources",
data: {
name: `${RUN} Fuel`,
codeName: `${RUN}-fuel`,
approvalStatus: "in_progress",
type: "physical",
baseValue: 1,
rarity: "common",
unitOfMeasure: "L",
massPerUnit: 1,
gridWidth: 1,
gridHeight: 1,
} as never,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "resources", id: resource.id });
const blueprint = await payload.create({
collection: "structures",
data: {
name: `${RUN} Blueprint`,
codeName: RUN,
approvalStatus: "in_progress",
description: LEXICAL_EMPTY,
category: "logistics",
materials: [{ resource: resource.id, amount: 1 }],
constructionDurationMinutes: 1,
terrainType: "land",
maxHealth: 100,
vehicleBay: { xSize: 4, ySize: 3 },
} as never,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "structures", id: blueprint.id });
const structure = await payload.create({
collection: "game-structures",
data: {
name: STRUCTURE_NAME,
type: blueprint.id,
constructionStatus: "complete",
storageSlug: `${RUN}-pool`,
} as never,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "game-structures", id: structure.id });
const template = await payload.create({
collection: "vehicles",
data: {
name: `${RUN} HEMTT`,
classname: `${RUN}-hemtt`,
approvalStatus: "approved",
garageWidth: 2,
garageHeight: 1,
maxHealth: 100,
fuel: { fuelType: resource.id, fuelCapacity: 100, fuelConsumptionRate: 1 },
} as never,
draft: false,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "vehicles", id: template.id });
const parked = await payload.create({
collection: "game-vehicles",
data: {
name: PARKED_NAME,
type: template.id,
deployedAt: structure.id,
status: "idle",
currentFuel: 80,
currentHealth: 100,
garageGridX: 0,
garageGridY: 0,
} as never,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "game-vehicles", id: parked.id });
const stray = await payload.create({
collection: "game-vehicles",
data: {
name: UNPLACED_NAME,
type: template.id,
deployedAt: structure.id,
status: "idle",
currentFuel: 40,
currentHealth: 100,
} as never,
overrideAccess: true,
depth: 0,
});
cleanupIds.push({ collection: "game-vehicles", id: stray.id });
return { parkedId: parked.id };
}
async function login(page: import("@playwright/test").Page) {
await page.goto("/login");
await page.getByLabel("Username").fill("dev");
await page.getByLabel("Password").fill("Test123");
await page.getByRole("button", { name: "Log in" }).click();
await page.waitForURL((url) => !url.pathname.startsWith("/login"), { timeout: 30_000 });
}
test.describe("Garage", () => {
let parkedId: number;
test.beforeAll(async () => {
await bootstrapTestDatabase();
const seeded = await seed();
parkedId = seeded.parkedId;
});
test.afterAll(async () => {
if (!payload) return;
for (const { collection, id } of cleanupIds) {
await payload.delete({ collection, id, overrideAccess: true } as never).catch(() => {});
}
});
test("guests never see the garage", async ({ page }) => {
await page.goto("/logistics/garage");
await expect(page.getByRole("heading", { level: 1 })).toHaveText("Polaris Task Force");
});
test("renders the bay grid, Elsewhere, and redirects the legacy route", async ({ page }) => {
await login(page);
await page.goto("/logistics/garage");
await expect(page.getByRole("heading", { name: "Garage" })).toBeVisible();
await expect(page.getByRole("heading", { name: STRUCTURE_NAME })).toBeVisible();
await expect(page.getByRole("button", { name: new RegExp(PARKED_NAME) })).toBeVisible();
// Elsewhere lists the unplaced instance.
await expect(page.getByRole("heading", { name: "Elsewhere" })).toBeVisible();
await expect(page.getByRole("link", { name: UNPLACED_NAME })).toBeVisible();
// Sidebar entry renamed (D8).
await expect(page.getByRole("link", { name: "Garage" }).first()).toBeVisible();
// Legacy list URL redirects to the garage.
await page.goto("/logistics/game-vehicles");
await page.waitForURL((url) => url.pathname === "/logistics/garage", { timeout: 30_000 });
});
test("dragging a vehicle persists its new cell", async ({ page }) => {
await login(page);
await page.goto("/logistics/garage");
const face = page.getByRole("button", { name: new RegExp(PARKED_NAME) });
await expect(face).toBeVisible();
const box = await face.boundingBox();
expect(box).not.toBeNull();
// Drag one grid step right (the bay is otherwise empty, so any single
// cell shift lands on a free spot regardless of the responsive cell size).
const step = Math.max(12, Math.floor((box!.width / 2) - 4));
await page.mouse.move(box!.x + box!.width / 2, box!.y + box!.height / 2);
await page.mouse.down();
await page.mouse.move(box!.x + box!.width / 2 + step, box!.y + box!.height / 2, { steps: 8 });
await page.mouse.move(box!.x + box!.width / 2 + step + 6, box!.y + box!.height / 2, { steps: 4 });
await page.mouse.up();
// The move goes through the server action; poll the test DB for it.
await expect
.poll(
async () => {
const doc = await payload.findByID({
collection: "game-vehicles",
id: parkedId,
depth: 0,
overrideAccess: true,
});
return doc.garageGridX;
},
{ timeout: 20_000, message: "expected garageGridX to change after the drag" },
)
.toBeGreaterThan(0);
// And it survives a reload.
await page.reload();
await expect(face).toBeVisible();
});
});