1
0
Fork 0
polaris-task-force/tests/int/minigame-achievement-notifications.int.spec.ts
Z8MB1E a1ab247b7b feat(minigames): notify winners and announce achievements on Discord
Phase 6 of the minigame periods feature:

- Muteable `minigame:achievement` notification type; the close pipeline now
  notifies each winner in-app, but only when the achievement row is newly
  written, so a re-run or a concurrent closer never double-sends.
- New bot service `achievementAnnouncements` polls achievements with no
  announcedAt, groups them by window, posts ONE embed per window to
  DISCORD_ANNOUNCE_CHANNEL_ID, then stamps announcedAt + discordMessageId.
  A failed post leaves the rows unannounced so the next tick retries them.
- Pure, testable renderer (bot/lib/achievementRender.ts) for grouping and the
  embed shape; window labels reuse the period/calendar helpers.
- Register the service in the bot registry and add
  DISCORD_ACHIEVEMENT_POLL_MS (default 60s).
- Tests: renderer grouping/ordering/fallbacks and the in-app notification
  path (including idempotency).
2026-10-11 17:06:52 -04:00

120 lines
3.7 KiB
TypeScript

import { getPayload, Payload } from "payload";
import config from "@/payload.config";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { User } from "@/payload-types";
import { recordMinigameEntry } from "@/lib/minigames/periods/record";
import { closeDuePeriods } from "@/lib/minigames/periods/lifecycle";
const ANCHOR = "2026-08-03T00:00:00.000Z";
const RUN = `mgnotif-${Date.now().toString(36)}`;
let payload: Payload;
let userA: number;
let originalRules: Record<string, unknown> | null = null;
const notificationsFor = async (userId: number) => {
const res = await payload.find({
collection: "user-notifications",
where: { and: [{ user: { equals: userId } }, { type: { equals: "minigame:achievement" } }] },
limit: 50,
depth: 0,
overrideAccess: true,
});
return res.docs;
};
describe("Minigame achievement notifications", () => {
beforeAll(async () => {
const payloadConfig = await config;
payload = await getPayload({ config: payloadConfig });
await clearMinigameData();
originalRules = (await payload.findGlobal({
slug: "minigame-rules",
depth: 0,
overrideAccess: true,
})) as unknown as Record<string, unknown> | null;
await payload.updateGlobal({
slug: "minigame-rules",
data: {
enabled: true,
anchorDate: ANCHOR,
periodLengthDays: 14,
placementLadderTopN: 10,
precisionMinAttempts: 1,
},
overrideAccess: true,
});
userA = (
(await payload.create({
collection: "users",
data: {
username: `${RUN}-a`,
discordUsername: `${RUN}-a`,
displayName: "Notified Operator",
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
password: "Test123",
roles: ["user"],
},
overrideAccess: true,
depth: 0,
})) as unknown as User
).id;
await recordMinigameEntry({
payload,
userId: userA,
game: "flappy",
score: 7,
durationMs: 1_000,
now: new Date("2026-08-04T12:00:00.000Z"),
});
});
afterAll(async () => {
await clearMinigameData();
if (!originalRules) return;
await payload
.updateGlobal({
slug: "minigame-rules",
data: {
enabled: Boolean(originalRules.enabled),
anchorDate: originalRules.anchorDate as string,
periodLengthDays: (originalRules.periodLengthDays as number) ?? 14,
placementLadderTopN: (originalRules.placementLadderTopN as number) ?? 10,
precisionMinAttempts: (originalRules.precisionMinAttempts as number) ?? 5,
},
overrideAccess: true,
})
.catch(() => undefined);
});
it("notifies winners in-app on close, and does not repeat on a re-run", async () => {
const summary = await closeDuePeriods(payload, new Date("2026-08-18T00:00:00.000Z"));
expect(summary.closed).toHaveLength(1);
const afterFirst = await notificationsFor(userA);
expect(afterFirst.length).toBeGreaterThan(0);
expect(afterFirst[0].title).toContain("earned for");
expect(afterFirst[0].link).toBe("/minigames/leaderboards");
// Idempotent close: nothing new is granted, so nothing new is sent.
await closeDuePeriods(payload, new Date("2026-08-18T00:00:00.000Z"));
const afterSecond = await notificationsFor(userA);
expect(afterSecond.length).toBe(afterFirst.length);
});
async function clearMinigameData() {
for (const collection of [
"achievements",
"minigame-entries",
"minigame-periods",
"minigames",
] as const) {
await payload.delete({ collection, where: { id: { greater_than: 0 } }, overrideAccess: true });
}
}
});