import { getTestPayload } from "../helpers/get-test-payload"; import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { listPendingExtractions } from "@/lib/operations/surfaceQuery"; import type { Structure } from "@/payload-types"; let payload: Payload; const RUN = `extractions-surf-${Date.now().toString(36)}`; const userIds: number[] = []; const resourceIds: number[] = []; const structureIds: number[] = []; const blueprintIds: number[] = []; const effectIds: number[] = []; let serverId: number; let mapId: number; let resourceA: number; let resourceB: number; let superUserId: number; let plainUserId: number; async function makeUser(label: string): Promise { const user = await payload.create({ collection: "users", data: { username: `${RUN}-${label}`, discordUsername: `${RUN}-${label}-discord`, steamId: `${RUN}-steam-${label}`, password: "Test1234", displayName: `${RUN} ${label}`, }, overrideAccess: true, depth: 0, }); userIds.push(user.id); return user.id; } async function makeResource(label: string): Promise { const resource = await payload.create({ collection: "resources", data: { name: `${RUN} ${label}`, codeName: `${RUN}-${label}`, approvalStatus: "in_progress", type: "physical", baseValue: 1, rarity: "common", unitOfMeasure: "kg", massPerUnit: 1, gridWidth: 1, gridHeight: 1, }, overrideAccess: true, depth: 0, }); resourceIds.push(resource.id); return resource.id; } async function makeHq(label: string, blueprintId: number): Promise { const structure = await payload.create({ collection: "game-structures", data: { name: `${RUN} ${label}`, type: blueprintId, map: mapId, coordinates: [100, 100], constructionStatus: "complete", storedResources: [], }, overrideAccess: true, depth: 0, }); structureIds.push(structure.id); return structure.id; } /** * Emit a real logistics extraction sync event; the pipeline derives the * operation-event + extraction-recorded effect and parks it as `pending_action`. * Returns the created effect. */ async function emitExtraction( label: string, operationId: string, hqId: number, resourceId: number, amount: number, ): Promise { const syncDoc = await payload.create({ collection: "arma-sync-events", data: { eventId: `${RUN}-${label}`, server: serverId, type: "operation.extraction", occurredAt: new Date().toISOString(), payload: { operationId, version: 1, actorId: null, kind: "logistics", hqStructureId: hqId, items: [{ resourceId, amount }], }, }, overrideAccess: true, depth: 0, }); const opEvents = await payload.find({ collection: "operation-events", where: { sourceEvent: { equals: syncDoc.id } }, limit: 1, depth: 0, overrideAccess: true, }); const effects = await payload.find({ collection: "operation-effects", where: { operationEvent: { equals: opEvents.docs[0].id } }, limit: 1, depth: 0, overrideAccess: true, }); const effectId = effects.docs[0].id as number; effectIds.push(effectId); return effectId; } beforeAll(async () => { payload = await getTestPayload(); superUserId = await makeUser("admin"); plainUserId = await makeUser("soldier"); const server = await payload.create({ collection: "game-servers", data: { serverId: `${RUN}-srv`, name: `${RUN} Server`, status: "online" }, overrideAccess: true, depth: 0, }); serverId = server.id; const map = await payload.create({ collection: "maps", data: { name: `${RUN} Map`, worldSizeWidth: 4096, worldSizeHeight: 4096, basemapMode: "image" }, overrideAccess: true, depth: 0, }); mapId = map.id; resourceA = await makeResource("Ammo"); resourceB = await makeResource("Fuel"); const blueprint = await payload.create({ collection: "structures", data: { name: `${RUN} HQ Depot`, codeName: `${RUN}-hq-depot`, approvalStatus: "in_progress", description: { root: { children: [{ text: "" }], direction: "ltr", format: "" }, version: 1 } as unknown as Structure["description"], category: "logistics", materials: [{ resource: resourceA, amount: 1 }], constructionDurationMinutes: 1, terrainType: "land", maxHealth: 100, }, overrideAccess: true, depth: 0, }); blueprintIds.push(blueprint.id); const hq = await makeHq("HQ", blueprint.id); // Two parked logistics extractions awaiting an officer's decision. await emitExtraction(`${RUN}-evt-keep`, `${RUN}-op-keep`, hq, resourceA, 25); await emitExtraction(`${RUN}-evt-hold`, `${RUN}-op-hold`, hq, resourceB, 40); // A third logistics extraction that the officer already resolved (applied) // must NOT surface as pending. const doneEffectId = await emitExtraction(`${RUN}-evt-done`, `${RUN}-op-done`, hq, resourceA, 10); await payload.update({ collection: "operation-effects", id: doneEffectId, data: { status: "applied", appliedAt: new Date().toISOString() }, overrideAccess: true, depth: 0, }); }); afterAll(async () => { if (!payload) return; for (const id of effectIds) { await payload.delete({ collection: "operation-effects", id, overrideAccess: true }).catch(() => {}); } for (const id of structureIds) { await payload.delete({ collection: "game-structures", id, overrideAccess: true }).catch(() => {}); } for (const id of blueprintIds) { await payload.delete({ collection: "structures", id, overrideAccess: true }).catch(() => {}); } for (const id of resourceIds) { await payload.delete({ collection: "resources", id, overrideAccess: true }).catch(() => {}); } await payload.delete({ collection: "maps", id: mapId, overrideAccess: true }).catch(() => {}); await payload.delete({ collection: "game-servers", id: serverId, overrideAccess: true }).catch(() => {}); for (const id of userIds) { await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {}); } }); describe("listPendingExtractions surface", () => { it("returns only pending_action extractions with resolved resource and HQ names", async () => { const result = await listPendingExtractions(payload, { id: superUserId }); expect(result.total).toBe(2); expect(result.extractions).toHaveLength(2); const byOp = Object.fromEntries(result.extractions.map((ex) => [ex.operationId, ex])); expect(byOp[`${RUN}-op-keep`]).toBeDefined(); expect(byOp[`${RUN}-op-hold`]).toBeDefined(); expect(byOp[`${RUN}-op-done`]).toBeUndefined(); const keep = byOp[`${RUN}-op-keep`]; expect(keep?.hqName).toContain("HQ"); expect(keep?.hqStructureId).toBeGreaterThan(0); expect(keep?.lines).toEqual([ { resourceId: resourceA, resourceName: `${RUN} Ammo`, amount: 25 }, ]); expect(keep?.notes).toBeNull(); const hold = byOp[`${RUN}-op-hold`]; expect(hold?.lines).toEqual([ { resourceId: resourceB, resourceName: `${RUN} Fuel`, amount: 40 }, ]); }); it("opens the queue to any authenticated viewer (read is access-controlled open)", async () => { const forSoldier = await listPendingExtractions(payload, { id: plainUserId }); expect(forSoldier.total).toBe(2); expect(forSoldier.extractions.length).toBe(2); }); });