Skip to content

Commit f990128

Browse files
committed
Add more tests
1 parent fa2bae9 commit f990128

File tree

3 files changed

+143
-33
lines changed

3 files changed

+143
-33
lines changed

workers/handler/test/capture.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { randomBytes } from "node:crypto";
2+
import { env, SELF } from "cloudflare:test";
3+
import { it, expect } 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 CacheCapture {
10+
url: string;
11+
zone: string;
12+
tags: string[];
13+
}
14+
15+
it("adds cache tags to database", async () => {
16+
const messages: ServiceBindingQueueMessage<CacheCapture>[] = [
17+
{
18+
id: randomBytes(16).toString("hex"),
19+
timestamp: new Date(1000),
20+
attempts: 1,
21+
body: {
22+
url: "https://example.com",
23+
tags: ["example"],
24+
zone: "example.com",
25+
},
26+
},
27+
];
28+
29+
const { outcome } = await SELF.queue("cache-capture", messages);
30+
expect(outcome).toBe("ok");
31+
32+
const { results } = await env.DB.prepare(
33+
"SELECT tag.value AS tag, url.zone AS zone FROM tag JOIN url ON tag.url = url.id WHERE url.value = ?",
34+
)
35+
.bind("https://example.com")
36+
.run();
37+
expect(results[0].zone).toBe("example.com");
38+
expect(results[0].tag).toBe("example");
39+
});

workers/handler/test/index.spec.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)