1
0
Fork 0
polaris-task-force/tests/int/map-objectives.int.spec.ts
Z8MB1E 3bef63af33 feat(map-objectives): superuser-authored strategic objectives on mission maps
Add a superuser-authored objective layer (defend, assault, secure, etc.)
shown as labelled map markers on both the world map and mission planning
map, with per-layer toggles on the planning map.

- collection: MapObjectives (map_objectives table, enums, FKs)
- lib: objectives service + objectiveTypes + permissions + event emit
- actions/api: create/update/delete objectives server actions
- UI: feature-layer toggles, objective picker dialog, world/plan markers,
  SQF export, dossier dialog integration
- generated: regenerate payload schema + types for map_objectives
- tests: 10 integration specs + e2e spec
- docs: CHANGELOG, map-system, AGENTS guides
2026-10-27 02:12:31 -04:00

298 lines
9.8 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import type { Payload } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { User } from "@/payload-types";
import {
clearMapObjectives,
createMapObjective,
deleteMapObjective,
listMapObjectives,
normalizeObjectiveInput,
objectiveFromDoc,
objectiveWorldSize,
updateMapObjective,
} from "@/lib/map/objectives";
import {
DEFAULT_OBJECTIVE_MARKER_TYPE,
OBJECTIVE_STATUS_META,
objectiveStatusColor,
} from "@/lib/map/objectiveTypes";
import { hasPermission } from "@/utils/access-control/hasPermission";
const WORLD = { width: 8192, height: 8192 };
describe("Objective validation", () => {
it("normalizes a minimal objective and applies defaults", () => {
const result = normalizeObjectiveInput(
{ name: " OBJ\nALPHA ", position: [500.123, 900], description: " Take the ridge. " },
WORLD,
);
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.data.name).toBe("OBJ ALPHA");
expect(result.data.position).toEqual([500.12, 900]);
expect(result.data.markerType).toBe(DEFAULT_OBJECTIVE_MARKER_TYPE);
expect(result.data.status).toBe("active");
expect(result.data.redacted).toBe(false);
expect(result.data.description).toBe("Take the ridge.");
});
it("rejects missing names, unknown types, bad status and off-world points", () => {
expect(normalizeObjectiveInput({ position: [1, 1] }, WORLD)).toMatchObject({ ok: false });
expect(normalizeObjectiveInput({ name: " ", position: [1, 1] }, WORLD)).toMatchObject({
ok: false,
});
expect(
normalizeObjectiveInput({ name: "OBJ", markerType: "not_a_marker", position: [1, 1] }, WORLD),
).toMatchObject({ ok: false });
expect(
normalizeObjectiveInput({ name: "OBJ", status: "nuked", position: [1, 1] }, WORLD),
).toMatchObject({ ok: false });
expect(normalizeObjectiveInput({ name: "OBJ", position: "here" }, WORLD)).toMatchObject({
ok: false,
});
expect(normalizeObjectiveInput({ name: "OBJ", position: [99999, 1] }, WORLD)).toMatchObject({
ok: false,
});
});
it("keeps markers just off-world (same margin as planning markers)", () => {
const result = normalizeObjectiveInput({ name: "EDGE", position: [-1500, 8300] }, WORLD);
expect(result.ok).toBe(true);
});
it("coerces stored rows and drops broken geometry", () => {
expect(objectiveFromDoc({ id: 1, map: 2, position: "bad" })).toBeNull();
expect(
objectiveFromDoc({ id: 2, map: 2, name: "OBJ BRAVO", position: [10, 20], status: "bogus" }),
).toMatchObject({
name: "OBJ BRAVO",
status: "active",
markerType: DEFAULT_OBJECTIVE_MARKER_TYPE,
});
});
it("maps status to Arma marker colours", () => {
expect(objectiveStatusColor("active")).toBe("ColorRed");
expect(objectiveStatusColor("secured")).toBe("ColorGreen");
expect(objectiveStatusColor("failed")).toBe("ColorGrey");
expect(objectiveStatusColor("unknown")).toBe("ColorRed");
expect(OBJECTIVE_STATUS_META.secured.label).toBe("Secured");
});
it("reads world size with the same defaults as the map config", () => {
expect(objectiveWorldSize({ worldSizeWidth: 4096, worldSizeHeight: 2048 })).toEqual({
width: 4096,
height: 2048,
});
expect(objectiveWorldSize({})).toEqual({ width: 8192, height: 8192 });
});
});
describe("Map objective service", () => {
let payload: Payload;
const RUN = `obj-${Date.now().toString(36)}`;
let mapId: number;
let superRoleId: number;
let superuser: User;
let player: User;
const userIds: number[] = [];
const makeUser = async (username: string, roleDocs: number[] = []): Promise<User> => {
const user = (await payload.create({
collection: "users",
data: {
username,
discordUsername: username,
displayName: "OBJECTIVE TEST",
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
password: "Test123",
roles: ["user"],
roleDocs,
},
overrideAccess: true,
depth: 0,
})) as unknown as User;
userIds.push(user.id);
return user;
};
const makeMap = async (label: string): Promise<number> => {
const map = await payload.create({
collection: "maps",
data: {
name: `${RUN} ${label}`,
worldSizeWidth: 4096,
worldSizeHeight: 4096,
basemapMode: "image",
},
overrideAccess: true,
depth: 0,
});
return map.id;
};
beforeAll(async () => {
payload = await getTestPayload();
mapId = await makeMap("Map");
const role = await payload.create({
collection: "roles",
data: {
name: `${RUN}-super`,
slug: `${RUN}-super`,
isSuperuser: true,
permissions: ["system:admin-access"],
},
overrideAccess: true,
depth: 0,
});
superRoleId = role.id;
superuser = await makeUser(`${RUN}-super`, [superRoleId]);
player = await makeUser(`${RUN}-player`);
});
afterAll(async () => {
if (!payload) return;
await payload.delete({ collection: "maps", id: mapId, overrideAccess: true }).catch(() => {});
for (const id of userIds) {
for (const collection of ["bank-accounts", "profiles"] as const) {
const field = collection === "bank-accounts" ? "ownerUser" : "user";
const docs = await payload
.find({ collection, where: { [field]: { equals: id } }, depth: 0, overrideAccess: true })
.catch(() => null);
for (const doc of docs?.docs ?? []) {
await payload.delete({ collection, id: doc.id, overrideAccess: true }).catch(() => {});
}
}
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
}
await payload
.delete({ collection: "roles", id: superRoleId, overrideAccess: true })
.catch(() => {});
});
it("gates writes behind the superuser tier", async () => {
expect(await hasPermission(payload, superuser, "system:admin-access")).toBe(true);
expect(await hasPermission(payload, player, "system:admin-access")).toBe(false);
});
it("creates, updates, filters redacted rows and deletes", async () => {
const visible = await createMapObjective(
payload,
mapId,
{ name: "OBJ ALPHA", position: [500, 600], description: "Ridge line" },
superuser.id,
);
expect(visible).toMatchObject({
name: "OBJ ALPHA",
status: "active",
redacted: false,
markerType: DEFAULT_OBJECTIVE_MARKER_TYPE,
position: [500, 600],
});
const hidden = await createMapObjective(
payload,
mapId,
{ name: "OBJ BRAVO", position: [800, 900], redacted: true },
superuser.id,
);
// Player-facing list hides redacted rows; the superuser list keeps them.
expect((await listMapObjectives(payload, mapId)).map((o) => o.id)).toEqual([visible.id]);
expect(
(await listMapObjectives(payload, mapId, { includeRedacted: true })).map((o) => o.id),
).toEqual([visible.id, hidden.id]);
const secured = await updateMapObjective(payload, visible.id, {
...visible,
status: "secured",
});
expect(secured.status).toBe("secured");
expect((await listMapObjectives(payload, mapId))[0].status).toBe("secured");
await expect(
updateMapObjective(payload, visible.id, { ...visible, position: [99999, 1] }),
).rejects.toThrow("on the map");
await expect(updateMapObjective(payload, -1, visible)).rejects.toThrow("not found");
await deleteMapObjective(payload, hidden.id);
expect(
(await listMapObjectives(payload, mapId, { includeRedacted: true })).map((o) => o.id),
).toEqual([visible.id]);
expect(await clearMapObjectives(payload, mapId)).toBe(1);
expect(await listMapObjectives(payload, mapId, { includeRedacted: true })).toEqual([]);
});
it("emits create, status and reveal events targeting the objective", async () => {
const objective = await createMapObjective(
payload,
mapId,
{ name: "OBJ CHARLIE", position: [100, 100], redacted: true },
superuser.id,
);
await updateMapObjective(payload, objective.id, { ...objective, status: "failed" });
await updateMapObjective(payload, objective.id, {
...objective,
status: "failed",
redacted: false,
});
const events = await payload.find({
collection: "game-event-logs",
where: {
and: [
{ targetCollection: { equals: "map-objectives" } },
{ targetId: { equals: objective.id } },
],
},
sort: "id",
depth: 0,
limit: 20,
pagination: false,
overrideAccess: true,
});
const types = events.docs.map((event) => event.type);
expect(types).toContain("map:objective-created");
expect(types).toContain("map:objective-status");
expect(types).toContain("map:objective-reveal");
await deleteMapObjective(payload, objective.id);
const after = await payload.find({
collection: "game-event-logs",
where: {
and: [
{ targetCollection: { equals: "map-objectives" } },
{ targetId: { equals: objective.id } },
{ type: { equals: "map:objective-deleted" } },
],
},
depth: 0,
limit: 1,
overrideAccess: true,
});
expect(after.docs).toHaveLength(1);
});
it("removes a map's objectives when the map is deleted", async () => {
const doomedMapId = await makeMap("Doomed");
await createMapObjective(
payload,
doomedMapId,
{ name: "OBJ DOOMED", position: [10, 10] },
superuser.id,
);
await payload.delete({ collection: "maps", id: doomedMapId, overrideAccess: true });
const remaining = await payload.find({
collection: "map-objectives",
where: { map: { equals: doomedMapId } },
depth: 0,
limit: 1,
overrideAccess: true,
});
expect(remaining.docs).toHaveLength(0);
});
});