|
| 1 | +import { describe, it, expect } from "bun:test"; |
| 2 | +import { |
| 3 | + notifyWebRevalidate, |
| 4 | + type WebRevalidateEnv, |
| 5 | + type RevalidateableSource, |
| 6 | +} from "../../workers/api/src/lib/web-revalidate.js"; |
| 7 | + |
| 8 | +const SECRET_VALUE = "shared-revalidate-secret"; |
| 9 | +const SECRET = { |
| 10 | + async get() { |
| 11 | + return SECRET_VALUE; |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const SOURCE: RevalidateableSource = { |
| 16 | + slug: "nextjs", |
| 17 | + orgId: "org_1", |
| 18 | + productId: null, |
| 19 | + isHidden: false, |
| 20 | +}; |
| 21 | + |
| 22 | +const DB = { |
| 23 | + async resolveOrgSlug(id: string) { |
| 24 | + return id === "org_1" ? "vercel" : null; |
| 25 | + }, |
| 26 | + async resolveProductSlug(id: string) { |
| 27 | + return id === "prod_1" ? "next" : null; |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +function envOn(overrides: Partial<WebRevalidateEnv> = {}): WebRevalidateEnv { |
| 32 | + return { |
| 33 | + WEB_SERVICE_KEY: SECRET, |
| 34 | + WEB_BASE_URL: "https://releases.sh", |
| 35 | + ...overrides, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +interface Recorded { |
| 40 | + url: string; |
| 41 | + method: string; |
| 42 | + authorization: string | null; |
| 43 | + body: unknown; |
| 44 | +} |
| 45 | + |
| 46 | +/** Records the outbound ping and replies with `status`. */ |
| 47 | +function recorder(status = 200): { calls: Recorded[]; fetchImpl: typeof fetch } { |
| 48 | + const calls: Recorded[] = []; |
| 49 | + const fetchImpl = (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 50 | + const headers = new Headers(init?.headers); |
| 51 | + calls.push({ |
| 52 | + url: String(input), |
| 53 | + method: init?.method ?? "GET", |
| 54 | + authorization: headers.get("authorization"), |
| 55 | + body: init?.body ? JSON.parse(String(init.body)) : null, |
| 56 | + }); |
| 57 | + return new Response(JSON.stringify({ revalidated: [] }), { status }); |
| 58 | + }) as unknown as typeof fetch; |
| 59 | + return { calls, fetchImpl }; |
| 60 | +} |
| 61 | + |
| 62 | +describe("notifyWebRevalidate", () => { |
| 63 | + it("posts the affected slugs to the web revalidate endpoint", async () => { |
| 64 | + const { calls, fetchImpl } = recorder(); |
| 65 | + const res = await notifyWebRevalidate(envOn(), DB, SOURCE, 3, { fetchImpl }); |
| 66 | + |
| 67 | + expect(res.status).toBe("revalidated"); |
| 68 | + expect(calls).toHaveLength(1); |
| 69 | + expect(calls[0]!.url).toBe("https://releases.sh/api/revalidate"); |
| 70 | + expect(calls[0]!.method).toBe("POST"); |
| 71 | + expect(calls[0]!.authorization).toBe(`Bearer ${SECRET_VALUE}`); |
| 72 | + expect(calls[0]!.body).toEqual({ orgSlug: "vercel", sourceSlug: "nextjs" }); |
| 73 | + }); |
| 74 | + |
| 75 | + it("includes the product slug when the source has a product", async () => { |
| 76 | + const { calls, fetchImpl } = recorder(); |
| 77 | + await notifyWebRevalidate(envOn(), DB, { ...SOURCE, productId: "prod_1" }, 1, { fetchImpl }); |
| 78 | + |
| 79 | + expect(calls[0]!.body).toEqual({ |
| 80 | + orgSlug: "vercel", |
| 81 | + sourceSlug: "nextjs", |
| 82 | + productSlug: "next", |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it("skips when no secret binding is configured", async () => { |
| 87 | + const { calls, fetchImpl } = recorder(); |
| 88 | + const res = await notifyWebRevalidate(envOn({ WEB_SERVICE_KEY: undefined }), DB, SOURCE, 1, { |
| 89 | + fetchImpl, |
| 90 | + }); |
| 91 | + |
| 92 | + expect(res).toEqual({ status: "skipped", reason: "no_secret_binding" }); |
| 93 | + expect(calls).toEqual([]); |
| 94 | + }); |
| 95 | + |
| 96 | + it("skips when the secret binding resolves empty", async () => { |
| 97 | + const { calls, fetchImpl } = recorder(); |
| 98 | + const res = await notifyWebRevalidate( |
| 99 | + envOn({ |
| 100 | + WEB_SERVICE_KEY: { |
| 101 | + async get() { |
| 102 | + return undefined; |
| 103 | + }, |
| 104 | + }, |
| 105 | + }), |
| 106 | + DB, |
| 107 | + SOURCE, |
| 108 | + 1, |
| 109 | + { fetchImpl }, |
| 110 | + ); |
| 111 | + |
| 112 | + expect(res).toEqual({ status: "skipped", reason: "secret_unset" }); |
| 113 | + expect(calls).toEqual([]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("skips when nothing was inserted", async () => { |
| 117 | + const { calls, fetchImpl } = recorder(); |
| 118 | + const res = await notifyWebRevalidate(envOn(), DB, SOURCE, 0, { fetchImpl }); |
| 119 | + |
| 120 | + expect(res).toEqual({ status: "skipped", reason: "no_releases" }); |
| 121 | + expect(calls).toEqual([]); |
| 122 | + }); |
| 123 | + |
| 124 | + it("skips a hidden source, whose pages are filtered from public reads", async () => { |
| 125 | + const { calls, fetchImpl } = recorder(); |
| 126 | + const res = await notifyWebRevalidate(envOn(), DB, { ...SOURCE, isHidden: true }, 1, { |
| 127 | + fetchImpl, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(res).toEqual({ status: "skipped", reason: "source_hidden" }); |
| 131 | + expect(calls).toEqual([]); |
| 132 | + }); |
| 133 | + |
| 134 | + it("skips an org-less source, which has no org page to revalidate", async () => { |
| 135 | + const { calls, fetchImpl } = recorder(); |
| 136 | + const res = await notifyWebRevalidate(envOn(), DB, { ...SOURCE, orgId: null }, 1, { |
| 137 | + fetchImpl, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(res).toEqual({ status: "skipped", reason: "no_org" }); |
| 141 | + expect(calls).toEqual([]); |
| 142 | + }); |
| 143 | + |
| 144 | + it("reports a non-2xx response as an error without throwing", async () => { |
| 145 | + const { fetchImpl } = recorder(401); |
| 146 | + const res = await notifyWebRevalidate(envOn(), DB, SOURCE, 1, { fetchImpl }); |
| 147 | + |
| 148 | + expect(res.status).toBe("error"); |
| 149 | + expect(res.httpStatus).toBe(401); |
| 150 | + }); |
| 151 | + |
| 152 | + // The pre-flight awaits (secret read, slug lookups) are as failure-prone as the |
| 153 | + // ping itself. Left outside the try they escape as a rejected promise, which |
| 154 | + // `Promise.allSettled` in runBatchIngestEffects swallows — no log line, no |
| 155 | + // result, and the page silently stays stale until the backstop. |
| 156 | + it("swallows a rejected secret binding and reports it as an error", async () => { |
| 157 | + const { calls, fetchImpl } = recorder(); |
| 158 | + const res = await notifyWebRevalidate( |
| 159 | + envOn({ |
| 160 | + WEB_SERVICE_KEY: { |
| 161 | + async get() { |
| 162 | + throw new Error("secrets store unavailable"); |
| 163 | + }, |
| 164 | + }, |
| 165 | + }), |
| 166 | + DB, |
| 167 | + SOURCE, |
| 168 | + 1, |
| 169 | + { fetchImpl }, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(res.status).toBe("error"); |
| 173 | + expect(res.reason).toContain("secrets store unavailable"); |
| 174 | + expect(calls).toEqual([]); |
| 175 | + }); |
| 176 | + |
| 177 | + it("swallows a rejected org-slug lookup and reports it as an error", async () => { |
| 178 | + const { calls, fetchImpl } = recorder(); |
| 179 | + const failingDb = { |
| 180 | + async resolveOrgSlug() { |
| 181 | + throw new Error("D1_ERROR: network"); |
| 182 | + }, |
| 183 | + resolveProductSlug: DB.resolveProductSlug, |
| 184 | + }; |
| 185 | + const res = await notifyWebRevalidate(envOn(), failingDb, SOURCE, 1, { fetchImpl }); |
| 186 | + |
| 187 | + expect(res.status).toBe("error"); |
| 188 | + expect(res.reason).toContain("D1_ERROR"); |
| 189 | + expect(calls).toEqual([]); |
| 190 | + }); |
| 191 | + |
| 192 | + it("swallows a rejected product-slug lookup and reports it as an error", async () => { |
| 193 | + const { calls, fetchImpl } = recorder(); |
| 194 | + const failingDb = { |
| 195 | + resolveOrgSlug: DB.resolveOrgSlug, |
| 196 | + async resolveProductSlug() { |
| 197 | + throw new Error("D1_ERROR: network"); |
| 198 | + }, |
| 199 | + }; |
| 200 | + const res = await notifyWebRevalidate( |
| 201 | + envOn(), |
| 202 | + failingDb, |
| 203 | + { ...SOURCE, productId: "prod_1" }, |
| 204 | + 1, |
| 205 | + { fetchImpl }, |
| 206 | + ); |
| 207 | + |
| 208 | + expect(res.status).toBe("error"); |
| 209 | + expect(calls).toEqual([]); |
| 210 | + }); |
| 211 | + |
| 212 | + // A dropped ping must never fail the ingest that triggered it — the page just |
| 213 | + // stays stale until the 24h backstop in web's `lib/isr.ts`. |
| 214 | + it("swallows a network failure and reports it as an error", async () => { |
| 215 | + const fetchImpl = (async () => { |
| 216 | + throw new Error("connect ECONNREFUSED"); |
| 217 | + }) as unknown as typeof fetch; |
| 218 | + const res = await notifyWebRevalidate(envOn(), DB, SOURCE, 1, { fetchImpl }); |
| 219 | + |
| 220 | + expect(res.status).toBe("error"); |
| 221 | + expect(res.reason).toContain("ECONNREFUSED"); |
| 222 | + }); |
| 223 | +}); |
0 commit comments