Skip to content

Commit d9bf774

Browse files
committed
test: align contract verification worker tests with cloudflare runtime APIs
1 parent 0d77568 commit d9bf774

13 files changed

Lines changed: 129 additions & 88 deletions

apps/contract-verification/test/e2e/verification.test.ts

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as z from 'zod/mini'
22
import { eq } from 'drizzle-orm'
33
import { drizzle } from 'drizzle-orm/d1'
44
import { describe, expect, it } from 'vitest'
5-
import { env, exports } from 'cloudflare:workers'
5+
import { env, SELF, runInDurableObject } from 'cloudflare:test'
66

77
import * as DB from '#database/schema.ts'
88
import { runVerificationJob } from '#route.verify.ts'
@@ -33,15 +33,13 @@ const getFirst = <T>(items: T[], label: string) => {
3333

3434
describe('full verification flow', () => {
3535
async function createVerificationJob(): Promise<string> {
36-
const verifyResponse = await exports.default.fetch(
37-
new Request(
38-
`https://test.local/v2/verify/${counterFixture.chainId}/${counterFixture.address}`,
39-
{
40-
method: 'POST',
41-
headers: { 'Content-Type': 'application/json' },
42-
body: JSON.stringify(verifyRequestData),
43-
},
44-
),
36+
const verifyResponse = await SELF.fetch(
37+
`https://test.local/v2/verify/${counterFixture.chainId}/${counterFixture.address}`,
38+
{
39+
method: 'POST',
40+
headers: { 'Content-Type': 'application/json' },
41+
body: JSON.stringify(verifyRequestData),
42+
},
4543
)
4644

4745
if (verifyResponse.status !== 202) {
@@ -81,8 +79,8 @@ describe('full verification flow', () => {
8179
},
8280
)
8381

84-
const statusResponse = await exports.default.fetch(
85-
new Request(`https://test.local/v2/verify/${verificationId}`),
82+
const statusResponse = await SELF.fetch(
83+
`https://test.local/v2/verify/${verificationId}`,
8684
)
8785
expect(statusResponse.status).toBe(200)
8886

@@ -99,8 +97,8 @@ describe('full verification flow', () => {
9997
it('verifies a simple contract and persists to database', async () => {
10098
const verificationId = await runSuccessfulVerification()
10199

102-
const statusResponse = await exports.default.fetch(
103-
new Request(`https://test.local/v2/verify/${verificationId}`),
100+
const statusResponse = await SELF.fetch(
101+
`https://test.local/v2/verify/${verificationId}`,
104102
)
105103
expect(statusResponse.status).toBe(200)
106104
const status = z.parse(
@@ -109,10 +107,8 @@ describe('full verification flow', () => {
109107
)
110108
expect(status.error).toBeUndefined()
111109

112-
const lookupResponse = await exports.default.fetch(
113-
new Request(
114-
`https://test.local/v2/contract/${counterFixture.chainId}/${counterFixture.address}?fields=sources,signatures`,
115-
),
110+
const lookupResponse = await SELF.fetch(
111+
`https://test.local/v2/contract/${counterFixture.chainId}/${counterFixture.address}?fields=sources,signatures`,
116112
)
117113

118114
expect(lookupResponse.status).toBe(200)
@@ -199,15 +195,13 @@ describe('full verification flow', () => {
199195
it('returns 409 for already verified contract', async () => {
200196
await runSuccessfulVerification()
201197

202-
const secondResponse = await exports.default.fetch(
203-
new Request(
204-
`https://test.local/v2/verify/${counterFixture.chainId}/${counterFixture.address}`,
205-
{
206-
method: 'POST',
207-
headers: { 'Content-Type': 'application/json' },
208-
body: JSON.stringify(verifyRequestData),
209-
},
210-
),
198+
const secondResponse = await SELF.fetch(
199+
`https://test.local/v2/verify/${counterFixture.chainId}/${counterFixture.address}`,
200+
{
201+
method: 'POST',
202+
headers: { 'Content-Type': 'application/json' },
203+
body: JSON.stringify(verifyRequestData),
204+
},
211205
)
212206

213207
expect(secondResponse.status).toBe(409)
@@ -216,4 +210,65 @@ describe('full verification flow', () => {
216210
'already_verified',
217211
)
218212
})
213+
214+
it('stores the job in the DO and completes via the DO alarm handler', async () => {
215+
const verificationId = await createVerificationJob()
216+
const stub = env.VERIFICATION_JOB_RUNNER.get(
217+
env.VERIFICATION_JOB_RUNNER.idFromName(verificationId),
218+
)
219+
220+
const storedState = await runInDurableObject(
221+
stub,
222+
async (_instance, state) => {
223+
return {
224+
job: await state.storage.get('job'),
225+
alarm: await state.storage.getAlarm(),
226+
}
227+
},
228+
)
229+
expect(storedState.job).toBeDefined()
230+
231+
await runInDurableObject(stub, async (instance, state) => {
232+
const job = await state.storage.get<{
233+
jobId: string
234+
chainId: number
235+
address: string
236+
stdJsonInput: unknown
237+
compilerVersion: string
238+
contractIdentifier: string
239+
}>('job')
240+
if (!job) {
241+
return
242+
}
243+
244+
await state.storage.put('job', {
245+
...job,
246+
chainId: 999_999,
247+
})
248+
await instance.alarm()
249+
})
250+
251+
const finalState = await runInDurableObject(
252+
stub,
253+
async (_instance, state) => {
254+
return {
255+
job: await state.storage.get('job'),
256+
alarm: await state.storage.getAlarm(),
257+
}
258+
},
259+
)
260+
expect(finalState.job).toBeUndefined()
261+
expect(finalState.alarm).toBeNull()
262+
263+
const statusResponse = await SELF.fetch(
264+
`https://test.local/v2/verify/${verificationId}`,
265+
)
266+
expect(statusResponse.status).toBe(200)
267+
const status = z.parse(
268+
VerificationStatusSchema,
269+
await statusResponse.json(),
270+
)
271+
expect(status.isJobCompleted).toBe(true)
272+
expect(status.error?.customCode).toBe('internal_error')
273+
})
219274
})

apps/contract-verification/test/integration/rate-limit-whitelist.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from 'cloudflare:workers'
1+
import { env } from 'cloudflare:test'
22
import { describe, expect, it, vi } from 'vitest'
33

44
import { app } from '#index.tsx'

apps/contract-verification/test/integration/route.lookup-verified.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hash, Hex } from 'ox'
22
import { eq } from 'drizzle-orm'
3-
import { env } from 'cloudflare:workers'
3+
import { env } from 'cloudflare:test'
44
import { drizzle } from 'drizzle-orm/d1'
55
import { describe, expect, it } from 'vitest'
66

apps/contract-verification/test/integration/route.lookup.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Hex } from 'ox'
22
import * as z from 'zod/mini'
33
import { eq } from 'drizzle-orm'
4-
import { env } from 'cloudflare:workers'
4+
import { env } from 'cloudflare:test'
55
import { drizzle } from 'drizzle-orm/d1'
66
import { describe, it, expect } from 'vitest'
77

apps/contract-verification/test/integration/route.verify-legacy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as CBOR from 'cbor-x'
2-
import { env } from 'cloudflare:workers'
2+
import { env } from 'cloudflare:test'
33
import { drizzle } from 'drizzle-orm/d1'
44
import { Hono } from 'hono'
55
import { Hash, Hex } from 'ox'

apps/contract-verification/test/integration/route.verify-status.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { env } from 'cloudflare:workers'
1+
import { SELF, env } from 'cloudflare:test'
22
import { eq } from 'drizzle-orm'
33
import { drizzle } from 'drizzle-orm/d1'
44
import { Hex, Hash } from 'ox'
@@ -12,14 +12,13 @@ import {
1212
verifiedContractsTable,
1313
verificationJobsTable,
1414
} from '#database/schema.ts'
15-
import { app } from '#index.tsx'
1615

1716
function statusUrl(id: string): string {
18-
return `/v2/verify/${id}`
17+
return `https://test.local/v2/verify/${id}`
1918
}
2019

2120
async function getStatus(id: string): Promise<Response> {
22-
return app.request(statusUrl(id), { method: 'GET' }, env)
21+
return SELF.fetch(statusUrl(id), { method: 'GET' })
2322
}
2423

2524
const db = () => drizzle(env.CONTRACTS_DB)

0 commit comments

Comments
 (0)