Skip to content

Commit 76d1203

Browse files
committed
Add more tests which fail right now. sigh
1 parent 08d3d82 commit 76d1203

File tree

4 files changed

+104
-31
lines changed

4 files changed

+104
-31
lines changed

workers/handler/test/purge-tag.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { randomBytes } from "node:crypto";
22
import { env, SELF } from "cloudflare:test";
33
import { it, expect, vi } from "vitest";
44

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-
95
interface CacheTagPurge {
106
tag: string;
117
zone?: string;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { randomBytes } from "node:crypto";
2+
import { SELF } from "cloudflare:test";
3+
import { it, expect, vi, afterEach } from "vitest";
4+
import type Cloudflare from "cloudflare";
5+
6+
const zoneList = vi.fn<
7+
Parameters<typeof Cloudflare.prototype.zones.list>,
8+
Promise<{ result: { id: string }[] }>
9+
>();
10+
11+
const cachePurge = vi.fn<
12+
Parameters<typeof Cloudflare.prototype.cache.purge>,
13+
Promise<void>
14+
>();
15+
16+
interface CacheUrlPurge {
17+
url: string;
18+
zone: string;
19+
}
20+
21+
vi.mock("cloudflare", () => ({
22+
default: vi.fn().mockImplementation(
23+
() =>
24+
console.log("HELLO") || {
25+
zones: {
26+
list: zoneList,
27+
},
28+
cache: {
29+
purge: cachePurge,
30+
},
31+
},
32+
),
33+
}));
34+
35+
afterEach(() => {
36+
vi.clearAllMocks();
37+
});
38+
39+
it("purges a URL", async () => {
40+
const url = "https://example.com";
41+
const zone = "example.com";
42+
const id = "test";
43+
44+
zoneList.mockResolvedValueOnce({
45+
result: [{ id }],
46+
});
47+
cachePurge.mockResolvedValueOnce();
48+
49+
const messages: ServiceBindingQueueMessage<CacheUrlPurge>[] = [
50+
{
51+
id: randomBytes(16).toString("hex"),
52+
timestamp: new Date(1000),
53+
attempts: 1,
54+
body: { url, zone },
55+
},
56+
];
57+
58+
const { outcome } = await SELF.queue("cache-purge-url", messages);
59+
expect(outcome).toBe("ok");
60+
61+
expect(zoneList).toBeCalledWith({ name: zone });
62+
expect(cachePurge).toBeCalledWith({ zone_id: id, files: [url] });
63+
});

workers/handler/vitest.config.mts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import path from "node:path";
22
import {
3-
defineWorkersProject,
4-
readD1Migrations,
3+
defineWorkersProject,
4+
readD1Migrations,
55
} from "@cloudflare/vitest-pool-workers/config";
66

77
export default defineWorkersProject(async () => {
8-
// Read all migrations in the `migrations` directory
9-
const migrationsPath = path.join(__dirname, "migrations");
10-
const migrations = await readD1Migrations(migrationsPath);
8+
// Read all migrations in the `migrations` directory
9+
const migrationsPath = path.join(__dirname, "migrations");
10+
const migrations = await readD1Migrations(migrationsPath);
1111

12-
return {
13-
test: {
14-
setupFiles: ["./test/apply-migrations.ts"],
15-
poolOptions: {
16-
workers: {
17-
singleWorker: true,
18-
wrangler: {
19-
configPath: "./wrangler.toml",
20-
// environment: "production",
21-
},
22-
miniflare: {
23-
// Required to use `SELF.queue()`. This is an experimental
24-
// compatibility flag, and cannot be enabled in production.
25-
compatibilityFlags: ["service_binding_extra_handlers"],
26-
// Add a test-only binding for migrations, so we can apply them in a
27-
// setup file
28-
bindings: { TEST_MIGRATIONS: migrations },
29-
},
30-
},
31-
},
32-
},
33-
};
12+
return {
13+
test: {
14+
setupFiles: ["./test/apply-migrations.ts"],
15+
poolOptions: {
16+
workers: {
17+
singleWorker: true,
18+
wrangler: {
19+
configPath: "./wrangler.toml",
20+
environment: "test",
21+
},
22+
miniflare: {
23+
// Required to use `SELF.queue()`. This is an experimental
24+
// compatibility flag, and cannot be enabled in production.
25+
compatibilityFlags: ["service_binding_extra_handlers"],
26+
// Add a test-only binding for migrations, so we can apply them in a
27+
// setup file
28+
bindings: { TEST_MIGRATIONS: migrations },
29+
},
30+
},
31+
},
32+
},
33+
};
3434
});

workers/handler/wrangler.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,17 @@ max_batch_size = 30
115115
# [[vectorize]]
116116
# binding = "MY_INDEX"
117117
# index_name = "my-index"
118+
119+
[env.test]
120+
vars = { API_TOKEN = "test" }
121+
d1_databases = [
122+
{ binding = "DB", database_name = "cache-tag", database_id = "49a13934-d832-4521-bcdf-7127b8f8f16b" }
123+
]
124+
queues.producers = [
125+
{ binding = "CACHE_PURGE_URL", queue = "cache-purge-url" },
126+
]
127+
queues.consumers = [
128+
{ queue = "cache-capture" },
129+
{ queue = "cache-purge-tag", max_batch_size = 95 },
130+
{ queue = "cache-purge-url", max_batch_size = 30 },
131+
]

0 commit comments

Comments
 (0)