|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import { env, SELF } from "cloudflare:test"; |
| 3 | +import { it, expect, vi } from "vitest"; |
| 4 | + |
| 5 | +// For now, you'll need to do something like this to get a correctly-typed |
| 6 | +// `Request` to pass to `worker.fetch()`. |
| 7 | +// const IncomingRequest = Request<unknown, IncomingRequestCfProperties>; |
| 8 | + |
| 9 | +interface CacheTagPurge { |
| 10 | + tag: string; |
| 11 | + zone?: string; |
| 12 | +} |
| 13 | + |
| 14 | +it("requeues purged tags as urls", async () => { |
| 15 | + const sendBatch = vi |
| 16 | + .spyOn(env.CACHE_PURGE_URL, "sendBatch") |
| 17 | + .mockResolvedValue(); |
| 18 | + const id = "test"; |
| 19 | + const url = "https://example.com"; |
| 20 | + const zone = "example.com"; |
| 21 | + const tag = "example"; |
| 22 | + |
| 23 | + await env.DB.batch([ |
| 24 | + env.DB.prepare("DELETE FROM tag WHERE url = ?").bind(url), |
| 25 | + env.DB.prepare( |
| 26 | + "INSERT OR REPLACE INTO url(id, zone, value) VALUES(?, ?, ?)", |
| 27 | + ).bind(id, zone, url), |
| 28 | + env.DB.prepare("INSERT INTO tag(url, value) VALUES (?, ?)").bind(id, tag), |
| 29 | + ]); |
| 30 | + |
| 31 | + const { results: initResults } = await env.DB.prepare( |
| 32 | + "SELECT tag.value AS tag, url.zone AS zone FROM tag JOIN url ON tag.url = url.id WHERE url.value = ?", |
| 33 | + ) |
| 34 | + .bind(url) |
| 35 | + .run(); |
| 36 | + expect(initResults.length).toBe(1); |
| 37 | + |
| 38 | + const messages: ServiceBindingQueueMessage<CacheTagPurge>[] = [ |
| 39 | + { |
| 40 | + id: randomBytes(16).toString("hex"), |
| 41 | + timestamp: new Date(1000), |
| 42 | + attempts: 1, |
| 43 | + body: { tag, zone }, |
| 44 | + }, |
| 45 | + ]; |
| 46 | + |
| 47 | + const { outcome } = await SELF.queue("cache-purge-tag", messages); |
| 48 | + expect(outcome).toBe("ok"); |
| 49 | + |
| 50 | + expect(sendBatch).toBeCalledWith([ |
| 51 | + { |
| 52 | + body: { |
| 53 | + url, |
| 54 | + zone, |
| 55 | + }, |
| 56 | + contentType: "json", |
| 57 | + }, |
| 58 | + ]); |
| 59 | + |
| 60 | + const { results } = await env.DB.prepare( |
| 61 | + "SELECT tag.value AS tag, url.zone AS zone FROM tag JOIN url ON tag.url = url.id WHERE url.value = ?", |
| 62 | + ) |
| 63 | + .bind(url) |
| 64 | + .run(); |
| 65 | + expect(results.length).toBe(0); |
| 66 | +}); |
| 67 | + |
| 68 | +it("passes when no URLs are found", async () => { |
| 69 | + const sendBatch = vi |
| 70 | + .spyOn(env.CACHE_PURGE_URL, "sendBatch") |
| 71 | + .mockResolvedValue(); |
| 72 | + const id = "test"; |
| 73 | + const url = "https://example.com"; |
| 74 | + const zone = "example.com"; |
| 75 | + const tag = "example"; |
| 76 | + |
| 77 | + const { results: initResults } = await env.DB.prepare( |
| 78 | + "SELECT tag.value AS tag, url.zone AS zone FROM tag JOIN url ON tag.url = url.id WHERE url.value = ?", |
| 79 | + ) |
| 80 | + .bind(url) |
| 81 | + .run(); |
| 82 | + expect(initResults.length).toBe(0); |
| 83 | + |
| 84 | + const messages: ServiceBindingQueueMessage<CacheTagPurge>[] = [ |
| 85 | + { |
| 86 | + id: randomBytes(16).toString("hex"), |
| 87 | + timestamp: new Date(1000), |
| 88 | + attempts: 1, |
| 89 | + body: { tag, zone }, |
| 90 | + }, |
| 91 | + ]; |
| 92 | + |
| 93 | + const { outcome } = await SELF.queue("cache-purge-tag", messages); |
| 94 | + expect(outcome).toBe("ok"); |
| 95 | + |
| 96 | + expect(sendBatch).not.toBeCalled(); |
| 97 | + |
| 98 | + const { results } = await env.DB.prepare( |
| 99 | + "SELECT tag.value AS tag, url.zone AS zone FROM tag JOIN url ON tag.url = url.id WHERE url.value = ?", |
| 100 | + ) |
| 101 | + .bind(url) |
| 102 | + .run(); |
| 103 | + expect(results.length).toBe(0); |
| 104 | +}); |
0 commit comments