Skip to content

Commit 971882e

Browse files
committed
✅🤖 address follow-up review comments on grapher r2 e2e tests
- simplify test worker context by always injecting a test ASSETS stub - add a test-only r2-has-key endpoint for assertions - strengthen fallback test to assert key is absent in primary and present in fallback before fetch
1 parent e2eeb81 commit 971882e

2 files changed

Lines changed: 43 additions & 39 deletions

File tree

functions/test/grapher-config-r2.e2e.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ async function seedR2(params: {
4545
expect(response.status).toBe(200)
4646
}
4747

48+
async function r2HasKey(params: {
49+
bucket: "primary" | "fallback"
50+
key: string
51+
}) {
52+
const response = await workerFetch(
53+
`/__test__/r2-has-key?bucket=${params.bucket}&key=${encodeURIComponent(
54+
params.key
55+
)}`
56+
)
57+
expect(response.status).toBe(200)
58+
const body = (await response.json()) as { exists: boolean }
59+
return body.exists
60+
}
61+
4862
describe("grapher config endpoint with local R2 bindings", () => {
4963
beforeAll(async () => {
5064
worker = await unstable_startWorker({
@@ -114,6 +128,9 @@ describe("grapher config endpoint with local R2 bindings", () => {
114128
value: lifeExpectancyFixture,
115129
})
116130

131+
expect(await r2HasKey({ bucket: "primary", key })).toBe(false)
132+
expect(await r2HasKey({ bucket: "fallback", key })).toBe(true)
133+
117134
const response = await workerFetch(
118135
"/grapher/life-expectancy.config.json"
119136
)

functions/test/grapher-config.e2e.worker.ts

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { onRequest as grapherOnRequest } from "../grapher/[slug].js"
2-
import { onRequestGet as multiDimOnRequestGet } from "../multi-dim/[slug].json.js"
32
import type { Env } from "../_common/env.js"
43

54
interface SeedR2Body {
@@ -35,19 +34,15 @@ async function deleteAllObjects(bucket: R2Bucket): Promise<void> {
3534
}
3635

3736
function makeGrapherContext(request: Request, env: Env) {
38-
const envWithAssets = env.ASSETS
39-
? env
40-
: ({
41-
...env,
42-
ASSETS: {
43-
fetch: async () => new Response("Not found", { status: 404 }),
44-
connect: () => {
45-
throw new Error(
46-
"ASSETS.connect is not implemented in tests"
47-
)
48-
},
49-
},
50-
} as unknown as Env)
37+
const envWithAssets = {
38+
...env,
39+
ASSETS: {
40+
fetch: async () => new Response("Not found", { status: 404 }),
41+
connect: () => {
42+
throw new Error("ASSETS.connect is not implemented in tests")
43+
},
44+
},
45+
} as unknown as Env
5146

5247
const context = {
5348
request,
@@ -67,16 +62,6 @@ function makeGrapherContext(request: Request, env: Env) {
6762
return context as unknown as Parameters<typeof grapherOnRequest>[0]
6863
}
6964

70-
function makeMultiDimContext(request: Request, env: Env, slug: string) {
71-
const context = {
72-
request,
73-
env,
74-
params: { slug },
75-
}
76-
77-
return context as unknown as Parameters<typeof multiDimOnRequestGet>[0]
78-
}
79-
8065
export default {
8166
async fetch(request: Request, env: Env): Promise<Response> {
8267
const url = new URL(request.url)
@@ -105,12 +90,23 @@ export default {
10590
return Response.json({ ok: true })
10691
}
10792

108-
if (request.method === "GET" && url.pathname === "/__test__/env") {
109-
return Response.json({
110-
branch: env.CF_PAGES_BRANCH,
111-
bucketPath: env.GRAPHER_CONFIG_R2_BUCKET_PATH,
112-
fallbackPath: env.GRAPHER_CONFIG_R2_BUCKET_FALLBACK_PATH,
113-
})
93+
if (
94+
request.method === "GET" &&
95+
url.pathname === "/__test__/r2-has-key"
96+
) {
97+
const bucket = url.searchParams.get("bucket") as
98+
| "primary"
99+
| "fallback"
100+
| null
101+
const key = url.searchParams.get("key")
102+
if (!bucket || !key) {
103+
return new Response("Missing bucket or key", {
104+
status: 400,
105+
})
106+
}
107+
108+
const object = await getBucket(env, bucket).head(key)
109+
return Response.json({ exists: !!object })
114110
}
115111

116112
if (
@@ -121,15 +117,6 @@ export default {
121117
return grapherOnRequest(context)
122118
}
123119

124-
const multiDimMatch = url.pathname.match(
125-
/^\/multi-dim\/([^/]+)\.json$/
126-
)
127-
if (multiDimMatch) {
128-
const slug = decodeURIComponent(multiDimMatch[1])
129-
const context = makeMultiDimContext(request, env, slug)
130-
return multiDimOnRequestGet(context)
131-
}
132-
133120
return new Response("Not found", { status: 404 })
134121
} catch (error) {
135122
const message =

0 commit comments

Comments
 (0)