import { describe, expect, it } from "vitest"; import { fullSave } from "../../schema/test/save-fixture"; import { GameEngine, MemoryBackend, noNotifications, NotificationSync, plannedNotifications, SaveStorage, type NotificationScheduler, type ScheduledNotification, } from "../src/index"; import { makeOwner, putBoard, setCredits, signUp, TestClock, testContract } from "./support"; /** The platform plugin, recording what is scheduled. */ class FakeScheduler implements NotificationScheduler { scheduled: ScheduledNotification[] = []; async replaceAll(list: ScheduledNotification[]) { this.scheduled = list; } async requestPermission() {} } function setup(enabled = () => true) { const clock = new TestClock(); const scheduler = new FakeScheduler(); const app = new GameEngine({ storage: new SaveStorage(new MemoryBackend()), clock: { now: () => clock.now }, notifications: new NotificationSync(scheduler, enabled), }); return { app, clock, scheduler }; } /** An owner who dispatches the Courier on a board contract; returns the flight. */ async function dispatchOne(app: GameEngine) { const guest = await signUp(app); const { shipId } = await makeOwner(app, guest.avatarId); await setCredits(app, 10_000); const contract = testContract(); await putBoard(app, [contract]); const { dispatch } = await app.call("createDispatch", { kind: "contract", contractId: contract.id, shipId, pilotId: guest.avatarId, }); return dispatch; } describe("local notifications (Step 14.9, decision 0.5.91)", () => { it("schedules one notification at a dispatch's arrival time", async () => { const { app, scheduler } = setup(); const dispatch = await dispatchOne(app); expect(scheduler.scheduled).toEqual([ { id: 1, at: dispatch.arrivalAt, title: "Deadhead", body: "Test Courier delivered to Moon", }, ]); }); it("combines events in the same minute into one notification", () => { const save = fullSave(); const landing = save.dispatches.find((d) => d.status === "in-flight")!; // A second ship lands 20 seconds after the first, in the same minute. const minuteStart = Math.floor(landing.arrivalAt / 60_000) * 60_000; landing.arrivalAt = minuteStart + 10_000; save.dispatches.push({ ...structuredClone(landing), id: "d2", shipId: "s2", arrivalAt: minuteStart + 30_000, record: { ...landing.record, id: "d2", shipId: "s2", destinationId: "mars" }, }); const now = save.lastTimeSeen; const planned = plannedNotifications(save, now); const combined = planned.find((n) => n.at === minuteStart + 30_000)!; expect(combined.title).toBe("Deadhead: 2 updates"); expect(combined.body.split("\n")).toEqual([ "Dusty Mule delivered to Moon", "Swift Heron delivered to Mars", ]); // The resting veteran and the shop job are separate minutes. expect(planned.map((n) => n.at)).toEqual([...planned.map((n) => n.at)].sort((a, b) => a - b)); expect(planned.length).toBeGreaterThanOrEqual(2); }); it("cancels everything when a new company starts", async () => { const { app, scheduler } = setup(); await dispatchOne(app); expect(scheduler.scheduled).toHaveLength(1); await app.call("newCompany", {}); expect(scheduler.scheduled).toEqual([]); }); it("schedules nothing with notifications turned off, and catches up when turned on", async () => { let on = false; const { app, scheduler } = setup(() => on); await dispatchOne(app); expect(scheduler.scheduled).toEqual([]); on = true; await app.resyncNotifications(); expect(scheduler.scheduled).toHaveLength(1); }); it("does nothing on the web (no-op scheduler), without errors", async () => { const app = new GameEngine({ storage: new SaveStorage(new MemoryBackend()), clock: { now: () => new TestClock().now }, notifications: new NotificationSync(noNotifications), }); await expect(dispatchOne(app)).resolves.toBeDefined(); await expect(app.requestNotificationPermission()).resolves.toBeUndefined(); }); });