1
0
Fork 0
polaris-task-force/tests/int/bot-achievement-render.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

80 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
achievementWindowLabel,
groupAchievementsForAnnouncement,
renderAchievementAnnouncement,
type AchievementAnnouncementRow,
} from "@/bot/lib/achievementRender";
const row = (over: Partial<AchievementAnnouncementRow>): AchievementAnnouncementRow => ({
id: 1,
type: "period-champion",
window: "period",
windowStart: "2026-10-12T00:00:00.000Z",
windowEnd: "2026-10-26T00:00:00.000Z",
user: { id: 7, displayName: "Alpha" },
value: 42,
detail: "42 placement points across 5 boards",
...over,
});
describe("Achievement announcement rendering", () => {
it("labels period, month and year windows", () => {
expect(
achievementWindowLabel("period", "2026-10-12T00:00:00.000Z", "2026-10-26T00:00:00.000Z"),
).toBe("Oct 12 to Oct 25, 2026");
expect(
achievementWindowLabel("month", "2026-10-01T00:00:00.000Z", "2026-11-01T00:00:00.000Z"),
).toBe("October 2026");
expect(
achievementWindowLabel("year", "2026-01-01T00:00:00.000Z", "2027-01-01T00:00:00.000Z"),
).toBe("2026");
});
it("groups by window and orders awards canonically", () => {
const groups = groupAchievementsForAnnouncement([
row({ id: 3, type: "precision", user: { id: 9, username: "charlie" } }),
row({ id: 1, type: "period-champion" }),
row({ id: 2, type: "diligence", user: { id: 8, displayName: "Bravo" } }),
row({
id: 4,
type: "player-of-month",
window: "month",
windowStart: "2026-10-01T00:00:00.000Z",
windowEnd: "2026-11-01T00:00:00.000Z",
}),
]);
expect(groups).toHaveLength(2);
expect(groups.map((group) => group.label)).toEqual(["October 2026", "Oct 12 to Oct 25, 2026"]);
expect(groups[0].achievements.map((a) => a.type)).toEqual(["player-of-month"]);
expect(groups[1].achievements.map((a) => a.type)).toEqual([
"period-champion",
"diligence",
"precision",
]);
});
it("renders one embed shape per group with the winner and detail", () => {
const [group] = groupAchievementsForAnnouncement([
row({ id: 1, type: "period-champion" }),
row({ id: 2, type: "dedication", detail: "1h 12m played", user: { id: 8, displayName: "Bravo" } }),
]);
const rendered = renderAchievementAnnouncement(group);
expect(rendered.title).toBe("Minigame results: Oct 12 to Oct 25, 2026");
expect(rendered.fields).toEqual([
{ name: "Period Champion", value: "Alpha · 42 placement points across 5 boards" },
{ name: "Dedication (Most Time)", value: "Bravo · 1h 12m played" },
]);
});
it("falls back for an unknown award type and an unpopulated user", () => {
const [group] = groupAchievementsForAnnouncement([
row({ type: "mystery-award", user: 5, detail: null }),
]);
const rendered = renderAchievementAnnouncement(group);
expect(rendered.fields[0].name).toBe("mystery-award");
expect(rendered.fields[0].value).toBe("Unknown · 42");
});
});