From 0d00e003d4805a0f21cd89176355f4a8db75862d Mon Sep 17 00:00:00 2001 From: Julien Herr Date: Thu, 21 May 2026 23:11:19 +0200 Subject: [PATCH] test(websub): add ctx.waitUntil coverage for processEmail notification wiring Co-Authored-By: Claude Sonnet 4.6 --- src/lib/email-processor.test.ts | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/lib/email-processor.test.ts b/src/lib/email-processor.test.ts index e7d7b58..01fd5be 100644 --- a/src/lib/email-processor.test.ts +++ b/src/lib/email-processor.test.ts @@ -213,6 +213,58 @@ describe("processEmail", () => { ); expect(metadata.emails).toHaveLength(2); }); + + it("calls ctx.waitUntil with notifySubscribers when ctx is provided", async () => { + await env.EMAIL_STORAGE.put( + `feed:${VALID_FEED_ID}:config`, + JSON.stringify({ + title: "Test", + language: "en", + site_url: "https://example.com", + feed_url: `https://example.com/rss/${VALID_FEED_ID}`, + created_at: Date.now(), + }), + ); + await env.EMAIL_STORAGE.put( + `feed:${VALID_FEED_ID}:metadata`, + JSON.stringify({ emails: [] }), + ); + + let waitUntilCalled = false; + const ctx = { + waitUntil: (p: Promise) => { + waitUntilCalled = true; + void p; // don't actually await it + }, + passThroughOnException: () => {}, + } as unknown as ExecutionContext; + + const res = await processEmail(makeInput(), env as any, ctx); + + expect(res.status).toBe(200); + expect(waitUntilCalled).toBe(true); + }); + + it("does not call ctx.waitUntil on error paths (feed not found)", async () => { + let waitUntilCalled = false; + const ctx = { + waitUntil: (p: Promise) => { + waitUntilCalled = true; + void p; + }, + passThroughOnException: () => {}, + } as unknown as ExecutionContext; + + // Feed ID is valid format but config doesn't exist → 404 + const res = await processEmail( + makeInput({ toAddress: `no.such.99@test.getmynews.app` }), + env as any, + ctx, + ); + + expect(res.status).toBe(404); + expect(waitUntilCalled).toBe(false); + }); }); describe("processEmail — attachments", () => {