@@ -2,7 +2,7 @@ import * as z from 'zod/mini'
22import { eq } from 'drizzle-orm'
33import { drizzle } from 'drizzle-orm/d1'
44import { describe , expect , it } from 'vitest'
5- import { env , exports } from 'cloudflare:workers '
5+ import { env , SELF , runInDurableObject } from 'cloudflare:test '
66
77import * as DB from '#database/schema.ts'
88import { runVerificationJob } from '#route.verify.ts'
@@ -33,15 +33,13 @@ const getFirst = <T>(items: T[], label: string) => {
3333
3434describe ( '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} )
0 commit comments