test(garage): cover placement validation, access gating, and bridge sync
This commit is contained in:
parent
053c35a70c
commit
0453742f32
3 changed files with 1159 additions and 0 deletions
229
tests/e2e/garage.e2e.spec.ts
Normal file
229
tests/e2e/garage.e2e.spec.ts
Normal file
|
|
@ -0,0 +1,229 @@
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
406
tests/int/garage-bridge.int.spec.ts
Normal file
406
tests/int/garage-bridge.int.spec.ts
Normal file
|
|
@ -0,0 +1,406 @@
|
||||||
|
import { getPayload, Payload } from "payload";
|
||||||
|
import config from "@/payload.config";
|
||||||
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||||
|
import type { GameVehicle } from "@/payload-types";
|
||||||
|
import { processGarageEvent } from "@/lib/arma-bridge/garages";
|
||||||
|
|
||||||
|
let payload: Payload;
|
||||||
|
const RUN = `garagebr-${Date.now().toString(36)}`;
|
||||||
|
const SLUG = `${RUN}-motorpool`;
|
||||||
|
const CLASSNAME = `${RUN}-truck`;
|
||||||
|
const UID_PARKED = `${RUN}-uid-parked`;
|
||||||
|
const UID_UNTAGGED = `${RUN}-uid-claim`;
|
||||||
|
|
||||||
|
const LEXICAL_EMPTY = {
|
||||||
|
root: {
|
||||||
|
children: [{ text: "" }],
|
||||||
|
direction: null,
|
||||||
|
format: "" as const,
|
||||||
|
indent: 0,
|
||||||
|
type: "text",
|
||||||
|
version: 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let resourceId: number;
|
||||||
|
let blueprintId: number;
|
||||||
|
let structureId: number;
|
||||||
|
let templateId: number;
|
||||||
|
let serverId: number;
|
||||||
|
let parkedId: number;
|
||||||
|
let untaggedId: number;
|
||||||
|
const vehicleIds: number[] = [];
|
||||||
|
const commandIds: number[] = [];
|
||||||
|
const eventIds: number[] = [];
|
||||||
|
|
||||||
|
let eventSeq = 0;
|
||||||
|
function garageEvent(type: string, eventPayload: unknown[], server: number) {
|
||||||
|
eventSeq += 1;
|
||||||
|
return {
|
||||||
|
id: eventSeq,
|
||||||
|
eventId: `${RUN}:${eventSeq}`,
|
||||||
|
server,
|
||||||
|
type,
|
||||||
|
occurredAt: new Date().toISOString(),
|
||||||
|
payload: eventPayload,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function vehicleDoc(id: number) {
|
||||||
|
return (await payload.findByID({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
id,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
})) as GameVehicle;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findVehicleByUid(uid: string): Promise<GameVehicle | null> {
|
||||||
|
const found = await payload.find({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
where: { bridgeUid: { equals: uid } },
|
||||||
|
limit: 1,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
return (found.docs[0] as GameVehicle | undefined) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findGarageCommands(type: string) {
|
||||||
|
const found = await payload.find({
|
||||||
|
collection: "arma-commands",
|
||||||
|
where: { type: { equals: type } },
|
||||||
|
limit: 100,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
return found.docs.filter(
|
||||||
|
(doc) =>
|
||||||
|
Array.isArray((doc as { payload?: unknown }).payload) &&
|
||||||
|
String((doc as { payload: unknown[] }).payload[0]).includes(SLUG),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Arma garage sync", () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
const payloadConfig = await config;
|
||||||
|
payload = await getPayload({ config: payloadConfig });
|
||||||
|
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
resourceId = resource.id;
|
||||||
|
|
||||||
|
const blueprint = await payload.create({
|
||||||
|
collection: "structures",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Motor Pool Blueprint`,
|
||||||
|
codeName: `${RUN}-mp`,
|
||||||
|
approvalStatus: "in_progress",
|
||||||
|
description: LEXICAL_EMPTY,
|
||||||
|
category: "logistics",
|
||||||
|
materials: [{ resource: resourceId, amount: 1 }],
|
||||||
|
constructionDurationMinutes: 1,
|
||||||
|
terrainType: "land",
|
||||||
|
maxHealth: 100,
|
||||||
|
vehicleBay: { xSize: 4, ySize: 3 },
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
blueprintId = blueprint.id;
|
||||||
|
|
||||||
|
const structure = await payload.create({
|
||||||
|
collection: "game-structures",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Motor Pool`,
|
||||||
|
type: blueprintId,
|
||||||
|
constructionStatus: "complete",
|
||||||
|
storageSlug: SLUG,
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
structureId = structure.id;
|
||||||
|
|
||||||
|
const template = await payload.create({
|
||||||
|
collection: "vehicles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Truck Template`,
|
||||||
|
classname: CLASSNAME,
|
||||||
|
approvalStatus: "approved",
|
||||||
|
garageWidth: 2,
|
||||||
|
garageHeight: 1,
|
||||||
|
maxHealth: 100,
|
||||||
|
fuel: { fuelType: resourceId, fuelCapacity: 100, fuelConsumptionRate: 1 },
|
||||||
|
} as never,
|
||||||
|
draft: false,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
templateId = template.id;
|
||||||
|
|
||||||
|
const parked = await payload.create({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Parked Truck`,
|
||||||
|
type: templateId,
|
||||||
|
deployedAt: structureId,
|
||||||
|
status: "idle",
|
||||||
|
bridgeUid: UID_PARKED,
|
||||||
|
garageGridX: 0,
|
||||||
|
garageGridY: 0,
|
||||||
|
currentFuel: 80,
|
||||||
|
currentHealth: 100,
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
parkedId = parked.id;
|
||||||
|
vehicleIds.push(parkedId);
|
||||||
|
|
||||||
|
// Untagged instance at the same structure: claim-by-classname target.
|
||||||
|
const untagged = await payload.create({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Untagged Truck`,
|
||||||
|
type: templateId,
|
||||||
|
deployedAt: structureId,
|
||||||
|
status: "idle",
|
||||||
|
currentFuel: 100,
|
||||||
|
currentHealth: 100,
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
untaggedId = untagged.id;
|
||||||
|
vehicleIds.push(untaggedId);
|
||||||
|
|
||||||
|
const server = await payload.create({
|
||||||
|
collection: "game-servers",
|
||||||
|
data: { serverId: `${RUN}-srv`, name: `${RUN} Server`, status: "online" },
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
serverId = server.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (!payload) return;
|
||||||
|
for (const id of commandIds) {
|
||||||
|
await payload.delete({ collection: "arma-commands", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of eventIds) {
|
||||||
|
await payload.delete({ collection: "arma-sync-events", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of vehicleIds) {
|
||||||
|
await payload.delete({ collection: "game-vehicles", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (templateId) {
|
||||||
|
await payload.delete({ collection: "vehicles", id: templateId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (structureId) {
|
||||||
|
await payload.delete({ collection: "game-structures", id: structureId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (blueprintId) {
|
||||||
|
await payload.delete({ collection: "structures", id: blueprintId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (resourceId) {
|
||||||
|
await payload.delete({ collection: "resources", id: resourceId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (serverId) {
|
||||||
|
await payload.delete({ collection: "game-servers", id: serverId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("garage.deposit", () => {
|
||||||
|
it("applies fuel/damage to a known instance and preserves its cell", async () => {
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(payload, garageEvent("garage.deposit", [`garage:${SLUG}`, UID_PARKED, CLASSNAME, 0.5, 0.25], 1)),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
const doc = await vehicleDoc(parkedId);
|
||||||
|
expect(doc.currentFuel).toBe(50);
|
||||||
|
expect(doc.currentHealth).toBe(75);
|
||||||
|
expect(doc.garageGridX).toBe(0);
|
||||||
|
expect(doc.garageGridY).toBe(0);
|
||||||
|
expect(doc.status).toBe("idle");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("claims an untagged classname match at the structure (first-tag-wins)", async () => {
|
||||||
|
await processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.deposit", [`garage:${SLUG}`, UID_UNTAGGED, CLASSNAME, 1, 0], 1),
|
||||||
|
);
|
||||||
|
const doc = await vehicleDoc(untaggedId);
|
||||||
|
expect(doc.bridgeUid).toBe(UID_UNTAGGED);
|
||||||
|
// Row-major auto-place: (0,0) is taken by the parked truck, so a 2x1
|
||||||
|
// lands at (2,0).
|
||||||
|
expect(doc.garageGridX).toBe(2);
|
||||||
|
expect(doc.garageGridY).toBe(0);
|
||||||
|
expect(doc.status).toBe("idle");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a new instance for an unknown uid with a known template", async () => {
|
||||||
|
const uid = `${RUN}-uid-created`;
|
||||||
|
await processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.deposit", [`garage:${SLUG}`, uid, CLASSNAME, 0.4, 0], 1),
|
||||||
|
);
|
||||||
|
const created = await findVehicleByUid(uid);
|
||||||
|
expect(created).not.toBeNull();
|
||||||
|
vehicleIds.push(created!.id);
|
||||||
|
expect(created!.status).toBe("idle");
|
||||||
|
expect(created!.currentFuel).toBe(40);
|
||||||
|
expect(Number(typeof created!.deployedAt === "object" ? created!.deployedAt?.id : created!.deployedAt)).toBe(
|
||||||
|
structureId,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips unknown classnames without creating anything", async () => {
|
||||||
|
const before = await payload.count({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
where: { bridgeUid: { exists: true } },
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.deposit", [`garage:${SLUG}`, `${RUN}-uid-ghost`, "no_such_class", 1, 0], 1),
|
||||||
|
),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
const after = await payload.count({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
where: { bridgeUid: { exists: true } },
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
expect(after.totalDocs).toBe(before.totalDocs);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks full damage as destroyed", async () => {
|
||||||
|
await processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.deposit", [`garage:${SLUG}`, UID_UNTAGGED, CLASSNAME, 0.1, 1], 1),
|
||||||
|
);
|
||||||
|
const doc = await vehicleDoc(untaggedId);
|
||||||
|
expect(doc.status).toBe("destroyed");
|
||||||
|
expect(doc.currentHealth).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("garage.withdraw", () => {
|
||||||
|
it("clears cells and deployedAt and sets in_transit", async () => {
|
||||||
|
await processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.withdraw", [`garage:${SLUG}`, UID_PARKED, CLASSNAME, 0.4, 0], 1),
|
||||||
|
);
|
||||||
|
const doc = await vehicleDoc(parkedId);
|
||||||
|
expect(doc.garageGridX).toBeNull();
|
||||||
|
expect(doc.garageGridY).toBeNull();
|
||||||
|
expect(doc.deployedAt).toBeNull();
|
||||||
|
expect(doc.status).toBe("in_transit");
|
||||||
|
expect(doc.bridgeUid).toBe(UID_PARKED);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips unknown bridgeUids without throwing", async () => {
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(
|
||||||
|
payload,
|
||||||
|
garageEvent("garage.withdraw", [`garage:${SLUG}`, `${RUN}-uid-nope`, CLASSNAME, 1, 0], 1),
|
||||||
|
),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("garage.sync snapshot", () => {
|
||||||
|
it("applies every line and is idempotent (absence is not deletion)", async () => {
|
||||||
|
const uid = `${RUN}-uid-sync`;
|
||||||
|
const snapshot = [
|
||||||
|
[`garage:${SLUG}`, [
|
||||||
|
[uid, CLASSNAME, 0.9, 0],
|
||||||
|
[UID_UNTAGGED, CLASSNAME, 0.1, 1],
|
||||||
|
]],
|
||||||
|
];
|
||||||
|
await processGarageEvent(payload, garageEvent("garage.sync", snapshot[0], 1));
|
||||||
|
const created = await findVehicleByUid(uid);
|
||||||
|
expect(created).not.toBeNull();
|
||||||
|
vehicleIds.push(created!.id);
|
||||||
|
|
||||||
|
// Re-apply the same snapshot: no duplicates, state unchanged.
|
||||||
|
await processGarageEvent(payload, garageEvent("garage.sync", snapshot[0], 1));
|
||||||
|
const found = await payload.find({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
where: { bridgeUid: { equals: uid } },
|
||||||
|
limit: 5,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
expect(found.totalDocs).toBe(1);
|
||||||
|
// UID_PARKED was absent from the snapshot but keeps its row.
|
||||||
|
expect((await vehicleDoc(parkedId)).bridgeUid).toBe(UID_PARKED);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("garage.request -> garage.apply", () => {
|
||||||
|
it("enqueues one apply command with the parked tagged instances", async () => {
|
||||||
|
await processGarageEvent(payload, garageEvent("garage.request", [`garage:${SLUG}`], serverId));
|
||||||
|
const commands = await findGarageCommands("garage.apply");
|
||||||
|
expect(commands).toHaveLength(1);
|
||||||
|
commandIds.push(Number(commands[0].id));
|
||||||
|
const [ref, lines] = commands[0].payload as [string, Array<[string, string, number, number]>];
|
||||||
|
expect(ref).toBe(`garage:${SLUG}`);
|
||||||
|
const uids = lines.map((line) => line[0]);
|
||||||
|
expect(uids).toContain(UID_UNTAGGED);
|
||||||
|
expect(uids).not.toContain(UID_PARKED); // withdrawn, no longer parked
|
||||||
|
expect(commands[0].server).toBe(serverId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("router wiring + malformed input", () => {
|
||||||
|
it("processes a real arma-sync-events row of type garage.deposit", async () => {
|
||||||
|
const uid = `${RUN}-uid-router`;
|
||||||
|
const raw = await payload.create({
|
||||||
|
collection: "arma-sync-events",
|
||||||
|
data: {
|
||||||
|
eventId: `${RUN}-router`,
|
||||||
|
server: serverId,
|
||||||
|
type: "garage.deposit",
|
||||||
|
occurredAt: new Date().toISOString(),
|
||||||
|
payload: [`garage:${SLUG}`, uid, CLASSNAME, 1, 0],
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
eventIds.push(raw.id);
|
||||||
|
const created = await findVehicleByUid(uid);
|
||||||
|
expect(created).not.toBeNull();
|
||||||
|
vehicleIds.push(created!.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("never throws on malformed payloads or unknown refs", async () => {
|
||||||
|
await expect(processGarageEvent(payload, garageEvent("garage.deposit", [], 1))).resolves.toBeUndefined();
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(payload, garageEvent("garage.sync", ["bogus-ref", []], 1)),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(payload, garageEvent("garage.sync", [`garage:${RUN}-missing`, []], 1)),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
await expect(
|
||||||
|
processGarageEvent(payload, { ...garageEvent("garage.sync", [], 1), payload: "nope" as never }),
|
||||||
|
).resolves.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
524
tests/int/garage-placement.int.spec.ts
Normal file
524
tests/int/garage-placement.int.spec.ts
Normal file
|
|
@ -0,0 +1,524 @@
|
||||||
|
import { getPayload, Payload } from "payload";
|
||||||
|
import config from "@/payload.config";
|
||||||
|
import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest";
|
||||||
|
import type { GameVehicle, User } from "@/payload-types";
|
||||||
|
import {
|
||||||
|
autoArrangeGarageItems,
|
||||||
|
findGarageEmptySpot,
|
||||||
|
formatGarageInstanceRef,
|
||||||
|
getVehicleFootprint,
|
||||||
|
isGaragePlacementValid,
|
||||||
|
parseGarageInstanceRef,
|
||||||
|
type GarageGridItem,
|
||||||
|
} from "@/lib/garage";
|
||||||
|
import { canUserAccessStructureStorage } from "@/collections/game/GameStructures";
|
||||||
|
import {
|
||||||
|
dispatchVehicle,
|
||||||
|
moveGarageItem,
|
||||||
|
rotateGarageItem,
|
||||||
|
storeVehicle,
|
||||||
|
} from "@/app/(frontend)/logistics/garage/actions";
|
||||||
|
|
||||||
|
// Mock next/headers so the actions' authenticate() can run outside a request.
|
||||||
|
vi.mock("next/headers", () => ({
|
||||||
|
headers: async () => new Headers(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
let payload: Payload;
|
||||||
|
let authSpy: MockInstance;
|
||||||
|
|
||||||
|
const RUN = `garage-${Date.now().toString(36)}`;
|
||||||
|
const SLUG_A = `${RUN}-bay-a`;
|
||||||
|
const CLASSNAME = `${RUN}-truck`;
|
||||||
|
|
||||||
|
const LEXICAL_EMPTY = {
|
||||||
|
root: {
|
||||||
|
children: [{ text: "" }],
|
||||||
|
direction: null,
|
||||||
|
format: "" as const,
|
||||||
|
indent: 0,
|
||||||
|
type: "text",
|
||||||
|
version: 1,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Fixture tracking for cleanup
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
const userIds: number[] = [];
|
||||||
|
const roleIds: number[] = [];
|
||||||
|
const vehicleIds: number[] = [];
|
||||||
|
const commandIds: number[] = [];
|
||||||
|
const reservationIds: number[] = [];
|
||||||
|
let resourceId: number;
|
||||||
|
let blueprintAId: number;
|
||||||
|
let blueprintBId: number;
|
||||||
|
let structureAId: number;
|
||||||
|
let structureBId: number;
|
||||||
|
let templateId: number;
|
||||||
|
let serverId: number;
|
||||||
|
|
||||||
|
let officerUser: User;
|
||||||
|
let plainUser: User;
|
||||||
|
|
||||||
|
function item(id: string, gridX: number, gridY: number, w: number, h: number, rotated = false): GarageGridItem {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
vehicleId: Number(id),
|
||||||
|
gridX,
|
||||||
|
gridY,
|
||||||
|
rotated,
|
||||||
|
template: { garageWidth: w, garageHeight: h } as GarageGridItem["template"],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function makeUser(label: string, roleDocIds?: number[]): Promise<User> {
|
||||||
|
const user = (await payload.create({
|
||||||
|
collection: "users",
|
||||||
|
data: {
|
||||||
|
username: `${RUN}-${label}`,
|
||||||
|
discordUsername: `${RUN}-${label}-discord`,
|
||||||
|
displayName: `${RUN} ${label}`,
|
||||||
|
steamId: `${RUN}-steam-${label}`,
|
||||||
|
password: "Test1234",
|
||||||
|
roles: ["user"],
|
||||||
|
...(roleDocIds && roleDocIds.length > 0 ? { roleDocs: roleDocIds } : {}),
|
||||||
|
},
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
})) as unknown as User;
|
||||||
|
userIds.push(user.id);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function makeVehicle(data: Record<string, unknown>): Promise<GameVehicle> {
|
||||||
|
const vehicle = (await payload.create({
|
||||||
|
collection: "game-vehicles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Truck`,
|
||||||
|
type: templateId,
|
||||||
|
status: "idle",
|
||||||
|
currentFuel: 100,
|
||||||
|
currentHealth: 100,
|
||||||
|
...data,
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
})) as unknown as GameVehicle;
|
||||||
|
vehicleIds.push(vehicle.id);
|
||||||
|
return vehicle;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function vehicleDoc(id: number) {
|
||||||
|
return payload.findByID({ collection: "game-vehicles", id, depth: 0, overrideAccess: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Setup / teardown
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
beforeAll(async () => {
|
||||||
|
const payloadConfig = await config;
|
||||||
|
payload = await getPayload({ config: payloadConfig });
|
||||||
|
authSpy = vi.spyOn(payload, "auth");
|
||||||
|
|
||||||
|
const garageRole = await payload.create({
|
||||||
|
collection: "roles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Garage Officer`,
|
||||||
|
slug: `${RUN}-garage-officer`,
|
||||||
|
permissions: ["game-vehicles:update"],
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
roleIds.push(garageRole.id);
|
||||||
|
|
||||||
|
officerUser = await makeUser("officer", [garageRole.id]);
|
||||||
|
plainUser = await makeUser("plain");
|
||||||
|
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
resourceId = resource.id;
|
||||||
|
|
||||||
|
const blueprints = await Promise.all(
|
||||||
|
[4, 2].map((baySize, i) =>
|
||||||
|
payload.create({
|
||||||
|
collection: "structures",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Depot ${i}`,
|
||||||
|
codeName: `${RUN}-depot-${i}`,
|
||||||
|
approvalStatus: "in_progress",
|
||||||
|
description: LEXICAL_EMPTY,
|
||||||
|
category: "logistics",
|
||||||
|
materials: [{ resource: resourceId, amount: 1 }],
|
||||||
|
constructionDurationMinutes: 1,
|
||||||
|
terrainType: "land",
|
||||||
|
maxHealth: 100,
|
||||||
|
vehicleBay: i === 0 ? { xSize: 4, ySize: 4 } : { xSize: baySize, ySize: 2 },
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
blueprintAId = blueprints[0].id;
|
||||||
|
blueprintBId = blueprints[1].id;
|
||||||
|
|
||||||
|
const structureA = await payload.create({
|
||||||
|
collection: "game-structures",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Motor Pool`,
|
||||||
|
type: blueprintAId,
|
||||||
|
constructionStatus: "complete",
|
||||||
|
storageSlug: SLUG_A,
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
structureAId = structureA.id;
|
||||||
|
const structureB = await payload.create({
|
||||||
|
collection: "game-structures",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Restricted Depot`,
|
||||||
|
type: blueprintBId,
|
||||||
|
constructionStatus: "complete",
|
||||||
|
storageSlug: `${RUN}-bay-b`,
|
||||||
|
storageAccess: { mode: "users", users: [plainUser.id] },
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
structureBId = structureB.id;
|
||||||
|
|
||||||
|
const template = await payload.create({
|
||||||
|
collection: "vehicles",
|
||||||
|
data: {
|
||||||
|
name: `${RUN} Truck Template`,
|
||||||
|
classname: CLASSNAME,
|
||||||
|
approvalStatus: "approved",
|
||||||
|
garageWidth: 2,
|
||||||
|
garageHeight: 1,
|
||||||
|
maxHealth: 100,
|
||||||
|
fuel: { fuelType: resourceId, fuelCapacity: 100, fuelConsumptionRate: 1 },
|
||||||
|
} as never,
|
||||||
|
draft: false,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
templateId = template.id;
|
||||||
|
|
||||||
|
const server = await payload.create({
|
||||||
|
collection: "game-servers",
|
||||||
|
data: { serverId: `${RUN}-srv`, name: `${RUN} Server`, status: "online" },
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
serverId = server.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (!payload) return;
|
||||||
|
for (const id of commandIds) {
|
||||||
|
await payload.delete({ collection: "arma-commands", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of reservationIds) {
|
||||||
|
await payload
|
||||||
|
.delete({ collection: "operation-reservations", id, overrideAccess: true })
|
||||||
|
.catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of vehicleIds) {
|
||||||
|
await payload.delete({ collection: "game-vehicles", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (templateId) {
|
||||||
|
await payload.delete({ collection: "vehicles", id: templateId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of [structureAId, structureBId]) {
|
||||||
|
if (id) await payload.delete({ collection: "game-structures", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of [blueprintAId, blueprintBId]) {
|
||||||
|
if (id) await payload.delete({ collection: "structures", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (resourceId) {
|
||||||
|
await payload.delete({ collection: "resources", id: resourceId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of userIds) {
|
||||||
|
const profiles = await payload
|
||||||
|
.find({ collection: "profiles", where: { user: { equals: id } }, depth: 0, overrideAccess: true })
|
||||||
|
.catch(() => ({ docs: [] as { id: number }[] }));
|
||||||
|
for (const profile of profiles.docs) {
|
||||||
|
await payload.delete({ collection: "profiles", id: profile.id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
for (const id of roleIds) {
|
||||||
|
await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
if (serverId) {
|
||||||
|
await payload.delete({ collection: "game-servers", id: serverId, overrideAccess: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Pure geometry
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe("garage geometry", () => {
|
||||||
|
it("footprint swaps width/height when rotated and defaults to 1x1", () => {
|
||||||
|
const fp = getVehicleFootprint({ garageWidth: 3, garageHeight: 2 } as never, true);
|
||||||
|
expect(fp).toEqual({ width: 2, height: 3 });
|
||||||
|
expect(getVehicleFootprint(null, false)).toEqual({ width: 1, height: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects overlapping and out-of-bounds placements", () => {
|
||||||
|
const items = [item("1", 0, 0, 2, 1)];
|
||||||
|
expect(isGaragePlacementValid(items, "1", 2, 0, 2, 1, 4, 3)).toBe(true);
|
||||||
|
expect(isGaragePlacementValid(items, null, 1, 0, 2, 1, 4, 3)).toBe(false);
|
||||||
|
expect(isGaragePlacementValid(items, null, 3, 0, 2, 1, 4, 3)).toBe(false);
|
||||||
|
expect(isGaragePlacementValid(items, null, 0, 3, 1, 1, 4, 3)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("finds empty spots row-major and reports overflow on auto-arrange", () => {
|
||||||
|
const items = [item("1", 0, 0, 2, 1)];
|
||||||
|
expect(findGarageEmptySpot(items, 1, 1, 4, 2)).toEqual({ x: 2, y: 0 });
|
||||||
|
const packed = [item("1", 0, 0, 2, 1), item("2", 2, 0, 2, 1)];
|
||||||
|
expect(findGarageEmptySpot(packed, 2, 1, 4, 1)).toBeNull();
|
||||||
|
const arranged = autoArrangeGarageItems(
|
||||||
|
[item("1", 3, 2, 2, 1), item("2", 0, 0, 2, 1), item("3", 1, 1, 2, 1)],
|
||||||
|
3,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
expect(arranged.placed).toHaveLength(1);
|
||||||
|
expect(arranged.overflow).toEqual(["2", "3"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses and formats garage instance refs (A1)", () => {
|
||||||
|
expect(formatGarageInstanceRef("motor-pool")).toBe("garage:motor-pool");
|
||||||
|
expect(parseGarageInstanceRef("garage:motor_pool")).toEqual({ slug: "motor_pool" });
|
||||||
|
expect(parseGarageInstanceRef("locker:x")).toBeNull();
|
||||||
|
expect(parseGarageInstanceRef("garage:")).toBeNull();
|
||||||
|
expect(parseGarageInstanceRef(null)).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// storageAccess gating modes (D5)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe("canUserAccessStructureStorage", () => {
|
||||||
|
it("honors anyone, users and roles modes", async () => {
|
||||||
|
const structureA = await payload.findByID({
|
||||||
|
collection: "game-structures",
|
||||||
|
id: structureAId,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
canUserAccessStructureStorage(payload, officerUser, structureA as unknown as Record<string, unknown>),
|
||||||
|
).resolves.toBe(true);
|
||||||
|
|
||||||
|
const structureB = await payload.findByID({
|
||||||
|
collection: "game-structures",
|
||||||
|
id: structureBId,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
canUserAccessStructureStorage(payload, plainUser, structureB as unknown as Record<string, unknown>),
|
||||||
|
).resolves.toBe(true);
|
||||||
|
await expect(
|
||||||
|
canUserAccessStructureStorage(payload, officerUser, structureB as unknown as Record<string, unknown>),
|
||||||
|
).resolves.toBe(false);
|
||||||
|
|
||||||
|
const restrictedRole = await payload.create({
|
||||||
|
collection: "roles",
|
||||||
|
data: { name: `${RUN} Depot Crew`, slug: `${RUN}-depot-crew` },
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
roleIds.push(restrictedRole.id);
|
||||||
|
await payload.update({
|
||||||
|
collection: "game-structures",
|
||||||
|
id: structureBId,
|
||||||
|
data: { storageAccess: { mode: "roles", roles: [restrictedRole.id] } } as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
const crewUser = await makeUser("crew", [restrictedRole.id]);
|
||||||
|
const structureBRoles = await payload.findByID({
|
||||||
|
collection: "game-structures",
|
||||||
|
id: structureBId,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
canUserAccessStructureStorage(payload, crewUser, structureBRoles as unknown as Record<string, unknown>),
|
||||||
|
).resolves.toBe(true);
|
||||||
|
await expect(
|
||||||
|
canUserAccessStructureStorage(payload, plainUser, structureBRoles as unknown as Record<string, unknown>),
|
||||||
|
).resolves.toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Server actions (D6/D7)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
describe("garage server actions", () => {
|
||||||
|
let vehicle1: GameVehicle; // parked at (0,0)
|
||||||
|
let vehicle2: GameVehicle; // parked at (2,0)
|
||||||
|
let vehicle3: GameVehicle; // off-map, for store
|
||||||
|
let wreck: GameVehicle; // destroyed at (2,2)
|
||||||
|
let tagged: GameVehicle; // bridgeUid-tagged at (2,1)? placed after moves
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
vehicle1 = await makeVehicle({ deployedAt: structureAId, garageGridX: 0, garageGridY: 0 });
|
||||||
|
vehicle2 = await makeVehicle({ deployedAt: structureAId, garageGridX: 2, garageGridY: 0 });
|
||||||
|
vehicle3 = await makeVehicle({ status: "in_transit" });
|
||||||
|
wreck = await makeVehicle({
|
||||||
|
status: "destroyed",
|
||||||
|
deployedAt: structureAId,
|
||||||
|
garageGridX: 2,
|
||||||
|
garageGridY: 2,
|
||||||
|
});
|
||||||
|
tagged = await makeVehicle({
|
||||||
|
deployedAt: structureAId,
|
||||||
|
garageGridX: 0,
|
||||||
|
garageGridY: 1,
|
||||||
|
bridgeUid: `${RUN}-uid-5`,
|
||||||
|
currentFuel: 50,
|
||||||
|
currentHealth: 100,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects users without game-vehicles:update (D6)", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: plainUser } as never);
|
||||||
|
const result = await moveGarageItem(vehicle1.id, 1, 1);
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
expect(result.error).toMatch(/Insufficient permissions/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("moves a vehicle to a free cell and persists it", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const result = await moveGarageItem(vehicle1.id, 0, 2);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
const doc = await vehicleDoc(vehicle1.id);
|
||||||
|
expect(doc.garageGridX).toBe(0);
|
||||||
|
expect(doc.garageGridY).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects overlapping and out-of-bounds moves", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const overlap = await moveGarageItem(vehicle1.id, 1, 0); // hits vehicle2 at (2,0)
|
||||||
|
expect(overlap.success).toBe(false);
|
||||||
|
expect(overlap.error).toMatch(/occupied or out of bounds/i);
|
||||||
|
const oob = await moveGarageItem(vehicle1.id, 3, 0); // 2-wide truck past x=3
|
||||||
|
expect(oob.success).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rotates a parked vehicle in place", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const before = await vehicleDoc(vehicle1.id);
|
||||||
|
const result = await rotateGarageItem(vehicle1.id);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
const after = await vehicleDoc(vehicle1.id);
|
||||||
|
expect(Boolean(after.garageRotated)).toBe(!Boolean(before.garageRotated));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("dispatches an idle tagged vehicle: clears cells + deployedAt, queues garage.dispatch", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const result = await dispatchVehicle(tagged.id);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
const doc = await vehicleDoc(tagged.id);
|
||||||
|
expect(doc.status).toBe("in_transit");
|
||||||
|
expect(doc.garageGridX).toBeNull();
|
||||||
|
expect(doc.deployedAt).toBeNull();
|
||||||
|
|
||||||
|
const commands = await payload.find({
|
||||||
|
collection: "arma-commands",
|
||||||
|
where: { type: { equals: "garage.dispatch" } },
|
||||||
|
limit: 10,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
const command = commands.docs.find(
|
||||||
|
(c) => Array.isArray(c.payload) && String(c.payload[0]).includes(SLUG_A),
|
||||||
|
);
|
||||||
|
expect(command).toBeDefined();
|
||||||
|
commandIds.push(Number(command!.id));
|
||||||
|
expect(command!.payload).toEqual([`garage:${SLUG_A}`, `${RUN}-uid-5`, CLASSNAME, 0.5, 0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses to dispatch destroyed or reserved vehicles (A4/D10)", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const destroyed = await dispatchVehicle(wreck.id);
|
||||||
|
expect(destroyed.success).toBe(false);
|
||||||
|
expect(destroyed.error).toMatch(/Destroyed/);
|
||||||
|
|
||||||
|
const reservation = await payload.create({
|
||||||
|
collection: "operation-reservations",
|
||||||
|
data: {
|
||||||
|
reservationKey: `${RUN}-op`,
|
||||||
|
operationId: `${RUN}-operation`,
|
||||||
|
status: "reserved",
|
||||||
|
vehicles: [{ vehicle: vehicle1.id }],
|
||||||
|
} as never,
|
||||||
|
overrideAccess: true,
|
||||||
|
depth: 0,
|
||||||
|
});
|
||||||
|
reservationIds.push(reservation.id);
|
||||||
|
|
||||||
|
const reserved = await dispatchVehicle(vehicle1.id);
|
||||||
|
expect(reserved.success).toBe(false);
|
||||||
|
expect(reserved.error).toMatch(/reserved/i);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stores a vehicle into the first free row-major cell and queues garage.store", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
// Occupied now: vehicle2 (2,0 2x1), wreck (2,2 2x1), vehicle1 rotated at (0,2).
|
||||||
|
const result = await storeVehicle(vehicle3.id, structureAId);
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
const doc = await vehicleDoc(vehicle3.id);
|
||||||
|
expect(doc.status).toBe("idle");
|
||||||
|
expect(doc.garageGridX).toBe(0);
|
||||||
|
expect(doc.garageGridY).toBe(0);
|
||||||
|
expect(Number(typeof doc.deployedAt === "object" ? doc.deployedAt?.id : doc.deployedAt)).toBe(
|
||||||
|
structureAId,
|
||||||
|
);
|
||||||
|
// vehicle3 has no bridgeUid, so no garage.store command is queued.
|
||||||
|
const commands = await payload.find({
|
||||||
|
collection: "arma-commands",
|
||||||
|
where: { type: { equals: "garage.store" } },
|
||||||
|
limit: 10,
|
||||||
|
depth: 0,
|
||||||
|
overrideAccess: true,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
commands.docs.filter((c) => Array.isArray(c.payload) && String(c.payload[0]).includes(SLUG_A)),
|
||||||
|
).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses to store destroyed vehicles and respects the target storageAccess gate", async () => {
|
||||||
|
authSpy.mockResolvedValue({ user: officerUser } as never);
|
||||||
|
const destroyed = await storeVehicle(wreck.id, structureAId);
|
||||||
|
expect(destroyed.success).toBe(false);
|
||||||
|
expect(destroyed.error).toMatch(/Destroyed/);
|
||||||
|
|
||||||
|
// structureB is users-restricted to plainUser; the officer passes the
|
||||||
|
// permission check but fails the storage gate (D5).
|
||||||
|
const gated = await storeVehicle(vehicle3.id, structureBId);
|
||||||
|
expect(gated.success).toBe(false);
|
||||||
|
expect(gated.error).toMatch(/access/i);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue