Adds reconcileModlistBuffer, which resends the modlist-updated message once per Scheduled mission with a modlist attached, when its start is within DISCORD_MODLIST_BUFFER_HOURS (default 8). Tracked by the new discordModlistBufferSentAt marker, so it is independent of the roll-call modlist sync. Also fixes type errors in modlist-file-access.int.spec.ts.
283 lines
9.5 KiB
TypeScript
283 lines
9.5 KiB
TypeScript
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import { Payload } from "payload";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types";
|
|
import { reconcileModlistBuffer } from "@/bot/services/missionEmbeds";
|
|
|
|
let payload: Payload;
|
|
|
|
const RUN = `modlist-buffer-${Date.now().toString(36)}`;
|
|
|
|
/**
|
|
* 8-hour modlist buffer: for each Scheduled mission with a modlist attached,
|
|
* the buffer posts the modlist message once, 8 hours before start, and stamps
|
|
* `discordModlistBufferSentAt`. These tests exercise the query + stamp path
|
|
* directly (the roll-call sync is separate), so the mock client only needs to
|
|
* record the send and never touches the filesystem.
|
|
*/
|
|
describe("reconcileModlistBuffer (8h modlist reminder)", () => {
|
|
let mapId: number;
|
|
let campaignId: number;
|
|
let authorId: number;
|
|
let modlistId: number;
|
|
const missionIds: number[] = [];
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
|
|
const map = (await payload.create({
|
|
collection: "maps",
|
|
data: { name: `${RUN} Map`, worldSizeWidth: 8192, worldSizeHeight: 8192, basemapMode: "image" },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as MissionMap;
|
|
mapId = map.id;
|
|
|
|
const campaign = (await payload.create({
|
|
collection: "campaigns",
|
|
data: { name: `${RUN} Campaign`, summary: "modlist buffer test campaign", status: "concept", campaignMode: "custom" },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as Campaign;
|
|
campaignId = campaign.id;
|
|
|
|
const author = (await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username: `${RUN}-author`,
|
|
discordUsername: `${RUN}-author`,
|
|
displayName: "AUTHOR",
|
|
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
|
|
password: "Test123",
|
|
roles: ["user"],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as User;
|
|
authorId = author.id;
|
|
|
|
// A real mod-lists doc so resolveModlist can resolve gameSetup.requiredMods.
|
|
// The launcher export starts with an XML prologue, which Payload sniffs as
|
|
// application/xml (one of the allowed mimeTypes for the collection).
|
|
const modlistBuffer = Buffer.from(
|
|
'<?xml version="1.0" encoding="utf-8"?><html><head><meta name="arma:PresetName" content="buffer" /></head><body>buffer preset</body></html>',
|
|
);
|
|
const modlist = (await payload.create({
|
|
collection: "mod-lists",
|
|
data: {
|
|
modlistName: `${RUN} Preset`,
|
|
author: authorId,
|
|
},
|
|
file: {
|
|
data: modlistBuffer,
|
|
mimetype: "application/xml",
|
|
name: `${RUN}-preset.html`,
|
|
size: modlistBuffer.byteLength,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as { id: number; filename: string; url: string | null; modlistName: string };
|
|
modlistId = modlist.id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
for (const id of missionIds) {
|
|
await payload.delete({ collection: "missions", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
await payload
|
|
.delete({ collection: "mod-lists", id: modlistId, overrideAccess: true })
|
|
.catch(() => {});
|
|
await payload
|
|
.delete({ collection: "campaigns", id: campaignId, overrideAccess: true })
|
|
.catch(() => {});
|
|
await payload.delete({ collection: "maps", id: mapId, overrideAccess: true }).catch(() => {});
|
|
const accounts = await payload
|
|
.find({
|
|
collection: "bank-accounts",
|
|
where: { ownerUser: { equals: authorId } },
|
|
limit: 5,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
})
|
|
.catch(() => null);
|
|
for (const account of accounts?.docs ?? []) {
|
|
await payload
|
|
.delete({ collection: "bank-accounts", id: account.id, overrideAccess: true })
|
|
.catch(() => {});
|
|
}
|
|
const profiles = await payload
|
|
.find({
|
|
collection: "profiles",
|
|
where: { user: { equals: authorId } },
|
|
limit: 5,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
})
|
|
.catch(() => null);
|
|
for (const profile of profiles?.docs ?? []) {
|
|
await payload
|
|
.delete({ collection: "profiles", id: profile.id, overrideAccess: true })
|
|
.catch(() => {});
|
|
}
|
|
await payload
|
|
.delete({ collection: "users", id: authorId, overrideAccess: true })
|
|
.catch(() => {});
|
|
});
|
|
|
|
const makeMission = async (
|
|
label: string,
|
|
status: Mission["ownershipAndStatus"]["status"],
|
|
start: Date,
|
|
withModlist: boolean,
|
|
): Promise<Mission> => {
|
|
const mission = (await payload.create({
|
|
collection: "missions",
|
|
data: {
|
|
name: `${RUN} ${label}`,
|
|
codeName: `${RUN}-${label}`,
|
|
summary: "modlist buffer test mission",
|
|
operationType: "main",
|
|
classification: {
|
|
map: mapId,
|
|
missionType: "PvE",
|
|
campaign: campaignId,
|
|
startDateTime: start.toISOString(),
|
|
estimatedDuration: 240,
|
|
},
|
|
ownershipAndStatus: {
|
|
authors: [authorId],
|
|
status,
|
|
visibility: "unit",
|
|
},
|
|
missionRoles: {
|
|
maxPlayers: 16,
|
|
},
|
|
gameDetails: {
|
|
serverDetails: {
|
|
serverIp: "127.0.0.1",
|
|
serverPort: 2302,
|
|
},
|
|
gameSetup: withModlist
|
|
? {
|
|
requiredMods: modlistId,
|
|
}
|
|
: undefined,
|
|
},
|
|
briefing: [],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as Mission;
|
|
missionIds.push(mission.id);
|
|
return mission;
|
|
};
|
|
|
|
const fetchMission = (id: number): Promise<Mission> =>
|
|
payload.findByID({
|
|
collection: "missions",
|
|
id,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
}) as unknown as Promise<Mission>;
|
|
|
|
const mockClient = () => {
|
|
const send = vi.fn().mockResolvedValue({ id: "msg-1" });
|
|
return { client: { channels: { fetch: async () => ({ send }) } } as never, send };
|
|
};
|
|
|
|
it("posts the modlist message once, 8 hours before start, and stamps the marker", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() + 8); // exactly at the buffer moment
|
|
|
|
const mission = await makeMission("buffer-due", "Scheduled", start, true);
|
|
const { client, send } = mockClient();
|
|
|
|
const posted = await reconcileModlistBuffer(client, payload, now);
|
|
|
|
expect(posted).toBe(1);
|
|
expect(send).toHaveBeenCalledTimes(1);
|
|
|
|
const updated = await fetchMission(mission.id);
|
|
expect(updated.discordModlistBufferSentAt).toBeTruthy();
|
|
});
|
|
|
|
it("does not post when the mission is outside the 8h window", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() + 10); // 10h out — buffer not due yet
|
|
|
|
const mission = await makeMission("buffer-not-due", "Scheduled", start, true);
|
|
const { client, send } = mockClient();
|
|
|
|
const posted = await reconcileModlistBuffer(client, payload, now);
|
|
|
|
expect(posted).toBe(0);
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeNull();
|
|
});
|
|
|
|
it("does not post for a mission whose start has already passed", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() - 1); // started an hour ago
|
|
|
|
const mission = await makeMission("buffer-past", "Scheduled", start, true);
|
|
const { client, send } = mockClient();
|
|
|
|
const posted = await reconcileModlistBuffer(client, payload, now);
|
|
|
|
expect(posted).toBe(0);
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeNull();
|
|
});
|
|
|
|
it("does not post for a Scheduled mission with no modlist attached", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() + 6);
|
|
|
|
const mission = await makeMission("buffer-no-modlist", "Scheduled", start, false);
|
|
const { client, send } = mockClient();
|
|
|
|
const posted = await reconcileModlistBuffer(client, payload, now);
|
|
|
|
expect(posted).toBe(0);
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeNull();
|
|
});
|
|
|
|
it("does not post for a non-Scheduled mission", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() + 6);
|
|
|
|
const mission = await makeMission("buffer-active", "Active", start, true);
|
|
const { client, send } = mockClient();
|
|
|
|
const posted = await reconcileModlistBuffer(client, payload, now);
|
|
|
|
expect(posted).toBe(0);
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeNull();
|
|
});
|
|
|
|
it("is idempotent — a second sweep finds the already-stamped mission", async () => {
|
|
const now = new Date("2026-08-24T12:00:00.000Z");
|
|
const start = new Date(now);
|
|
start.setHours(now.getHours() + 8);
|
|
|
|
const mission = await makeMission("buffer-idempotent", "Scheduled", start, true);
|
|
const { client: client1, send: send1 } = mockClient();
|
|
await reconcileModlistBuffer(client1, payload, now);
|
|
expect(send1).toHaveBeenCalledTimes(1);
|
|
|
|
const { client: client2, send: send2 } = mockClient();
|
|
const posted = await reconcileModlistBuffer(client2, payload, now);
|
|
|
|
expect(posted).toBe(0);
|
|
expect(send2).not.toHaveBeenCalled();
|
|
expect((await fetchMission(mission.id)).discordModlistBufferSentAt).toBeTruthy();
|
|
});
|
|
});
|