|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | -import { startWakePolling, UNREAD_POLL_INTERVAL_MS, wakesToToast } from "./wake-poll"; |
| 2 | +import { |
| 3 | + planWakeToasts, |
| 4 | + startWakePolling, |
| 5 | + UNREAD_POLL_INTERVAL_MS, |
| 6 | + wakesToToast, |
| 7 | +} from "./wake-poll"; |
3 | 8 |
|
4 | 9 | function harness() { |
5 | 10 | let hidden = false; |
@@ -124,3 +129,42 @@ describe("wakesToToast", () => { |
124 | 129 | expect(wakesToToast(undefined, new Set())).toEqual([]); |
125 | 130 | }); |
126 | 131 | }); |
| 132 | + |
| 133 | +describe("planWakeToasts", () => { |
| 134 | + const MAX = 3; |
| 135 | + const batch = (n: number) => Array.from({ length: n }, (_, i) => i); |
| 136 | + |
| 137 | + it("toasts a small batch individually but still counts it toward the running total", () => { |
| 138 | + const { plan, pending } = planWakeToasts(batch(2), 0, MAX); |
| 139 | + |
| 140 | + expect(plan).toEqual({ mode: "individual", wakes: [0, 1] }); |
| 141 | + expect(pending).toBe(2); |
| 142 | + }); |
| 143 | + |
| 144 | + it("summarizes when a single batch is over the max", () => { |
| 145 | + const { plan, pending } = planWakeToasts(batch(4), 0, MAX); |
| 146 | + |
| 147 | + expect(plan).toEqual({ mode: "summary", count: 4 }); |
| 148 | + expect(pending).toBe(4); |
| 149 | + }); |
| 150 | + |
| 151 | + it("accumulates across consecutive polls instead of showing only the latest", () => { |
| 152 | + // First batch of 2 is below the max: individual toasts, nothing pending yet. |
| 153 | + const first = planWakeToasts(batch(2), 0, MAX); |
| 154 | + expect(first.plan.mode).toBe("individual"); |
| 155 | + |
| 156 | + // A second batch of 3 pushes the running total to 5, so the grouped toast claims |
| 157 | + // the cumulative count, not just this batch's 3. |
| 158 | + const second = planWakeToasts(batch(3), first.pending, MAX); |
| 159 | + expect(second.plan).toEqual({ mode: "summary", count: 5 }); |
| 160 | + expect(second.pending).toBe(5); |
| 161 | + }); |
| 162 | + |
| 163 | + it("grows the visible summary as later batches arrive", () => { |
| 164 | + const first = planWakeToasts(batch(4), 0, MAX); |
| 165 | + const second = planWakeToasts(batch(3), first.pending, MAX); |
| 166 | + |
| 167 | + expect(second.plan).toEqual({ mode: "summary", count: 7 }); |
| 168 | + expect(second.pending).toBe(7); |
| 169 | + }); |
| 170 | +}); |
0 commit comments