| @@ -15,7 +15,7 @@ registerHooks({ |
| 15 | } |
15 | } |
| 16 | }, |
16 | }, |
| 17 | }); |
17 | }); |
| 18 | const { default: worker } = await import("../src/index.ts"); |
18 | const { default: worker, PUSH_SLICE } = await import("../src/index.ts"); |
| 19 | |
19 | |
| 20 | const FIVE = "*/5 * * * *"; |
20 | const FIVE = "*/5 * * * *"; |
| 21 | const TWO = "*/2 * * * *"; |
21 | const TWO = "*/2 * * * *"; |
| @@ -172,21 +172,21 @@ test("a failed CelesTrak poll is tried once more 33 minutes later, from the 5-mi |
| 172 | |
172 | |
| 173 | test("aurora alerts go out in bounded slices and are recorded per device once answered", async () => { |
173 | test("aurora alerts go out in bounded slices and are recorded per device once answered", async () => { |
| 174 | const { db, env, calls } = await setup(); |
174 | const { db, env, calls } = await setup(); |
| 175 | for (let i = 0; i < 30; i++) device(db, i); |
175 | for (let i = 0; i < PUSH_SLICE + 10; i++) device(db, i); |
| 176 | device(db, 30, { offset: 720 }); // 10:05 local: not night |
176 | device(db, PUSH_SLICE + 10, { offset: 720 }); // 10:05 local: not night |
| 177 | const push = apns({ [token(3)]: 503, [token(4)]: 410, [token(5)]: 400 }); |
177 | const push = apns({ [token(3)]: 503, [token(4)]: 410, [token(5)]: 400 }); |
| 178 | const hosts = network({ ...swpc(6), ...push.handlers }); |
178 | const hosts = network({ ...swpc(6), ...push.handlers }); |
| 179 | |
179 | |
| 180 | await run(env, FIVE, "2026-10-20T22:05:00Z"); |
180 | await run(env, FIVE, "2026-10-20T22:05:00Z"); |
| 181 | assert.equal(push.state.sent.length, 20); |
181 | assert.equal(push.state.sent.length, PUSH_SLICE); |
| 182 | assert.ok(push.state.maxInFlight <= 6, `max in flight ${push.state.maxInFlight}`); |
182 | assert.ok(push.state.maxInFlight <= 6, `max in flight ${push.state.maxInFlight}`); |
| 183 | assert.ok(hosts.length + calls.length <= 50, `${hosts.length} fetches + ${calls.length} D1 calls`); |
183 | assert.ok(hosts.length + calls.length <= PUSH_SLICE + 30, `${hosts.length} fetches + ${calls.length} D1 calls`); |
| 184 | assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'aurora'"), 18); |
184 | assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'aurora'"), PUSH_SLICE - 2); |
| 185 | assert.equal(count(db, `SELECT count(*) AS n FROM devices WHERE token = '${token(4)}'`), 0); |
185 | assert.equal(count(db, `SELECT count(*) AS n FROM devices WHERE token = '${token(4)}'`), 0); |
| 186 | |
186 | |
| 187 | push.state.sent = []; |
187 | push.state.sent = []; |
| 188 | await run(env, FIVE, "2026-10-20T22:10:00Z"); |
188 | await run(env, FIVE, "2026-10-20T22:10:00Z"); |
| 189 | assert.deepEqual(push.state.sent.sort(), [3, ...Array.from({ length: 10 }, (_, i) => 20 + i)].map(token).sort()); |
189 | assert.deepEqual(push.state.sent.sort(), [3, ...Array.from({ length: 10 }, (_, i) => PUSH_SLICE + i)].map(token).sort()); |
| 190 | |
190 | |
| 191 | push.state.sent = []; |
191 | push.state.sent = []; |
| 192 | await run(env, FIVE, "2026-10-20T22:15:00Z"); |
192 | await run(env, FIVE, "2026-10-20T22:15:00Z"); |
| @@ -213,22 +213,22 @@ test("an aurora run cut short leaves the remaining devices for the next run", as |
| 213 | |
213 | |
| 214 | test("news digests go out in slices during the local 09:00 hour, once per local day", async () => { |
214 | test("news digests go out in slices during the local 09:00 hour, once per local day", async () => { |
| 215 | const { db, env, calls } = await setup(); |
215 | const { db, env, calls } = await setup(); |
| 216 | for (let i = 0; i < 25; i++) device(db, i, { offset: -300, aurora: 0, news: 1 }); |
216 | for (let i = 0; i < PUSH_SLICE + 5; i++) device(db, i, { offset: -300, aurora: 0, news: 1 }); |
| 217 | device(db, 25, { offset: 330, aurora: 0, news: 1 }); |
217 | device(db, PUSH_SLICE + 5, { offset: 330, aurora: 0, news: 1 }); |
| 218 | const push = apns(); |
218 | const push = apns(); |
| 219 | const articles = [{ id: 1, title: "t1", news_site: "ESA", published_at: "2026-10-20T13:00:00Z" }]; |
219 | const articles = [{ id: 1, title: "t1", news_site: "ESA", published_at: "2026-10-20T13:00:00Z" }]; |
| 220 | const hosts = network({ ...push.handlers, "api.spaceflightnewsapi.net": () => Response.json({ results: articles }) }); |
220 | const hosts = network({ ...push.handlers, "api.spaceflightnewsapi.net": () => Response.json({ results: articles }) }); |
| 221 | const snapi = () => hosts.filter((h) => h === "api.spaceflightnewsapi.net").length; |
221 | const snapi = () => hosts.filter((h) => h === "api.spaceflightnewsapi.net").length; |
| 222 | |
222 | |
| 223 | await run(env, NEWS, "2026-10-20T14:00:00Z"); |
223 | await run(env, NEWS, "2026-10-20T14:00:00Z"); |
| 224 | assert.equal(push.state.sent.length, 20); |
224 | assert.equal(push.state.sent.length, PUSH_SLICE); |
| 225 | await run(env, NEWS, "2026-10-20T14:10:00Z"); |
225 | await run(env, NEWS, "2026-10-20T14:10:00Z"); |
| 226 | assert.equal(push.state.sent.length, 25); |
226 | assert.equal(push.state.sent.length, PUSH_SLICE + 5); |
| 227 | assert.equal(new Set(push.state.sent).size, 25); |
227 | assert.equal(new Set(push.state.sent).size, PUSH_SLICE + 5); |
| 228 | await run(env, NEWS, "2026-10-20T14:20:00Z"); |
228 | await run(env, NEWS, "2026-10-20T14:20:00Z"); |
| 229 | assert.equal(push.state.sent.length, 25); |
229 | assert.equal(push.state.sent.length, PUSH_SLICE + 5); |
| 230 | assert.equal(snapi(), 2); |
230 | assert.equal(snapi(), 2); |
| 231 | assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'news' AND event LIKE '%:2026-10-20'"), 25); |
231 | assert.equal(count(db, "SELECT count(*) AS n FROM alerts_sent WHERE kind = 'news' AND event LIKE '%:2026-10-20'"), PUSH_SLICE + 5); |
| 232 | |
232 | |
| 233 | const q = calls.find((c) => c.sql.includes("news = 1"))!; |
233 | const q = calls.find((c) => c.sql.includes("news = 1"))!; |
| 234 | const plan = db.prepare(`EXPLAIN QUERY PLAN ${q.sql}`).all(...q.params).map((r) => String(r.detail)).join("\n"); |
234 | const plan = db.prepare(`EXPLAIN QUERY PLAN ${q.sql}`).all(...q.params).map((r) => String(r.detail)).join("\n"); |