import * as cloud from "@pulumi/cloud" ; import * as assertModule from "assert" ; import * as supertestModule from "supertest" ; import * as harnessModule from "./harness" ; export type TestArgs = { assert : typeof assertModule ; harness : typeof harnessModule ; supertest : typeof supertestModule ; } ; let uniqueId = 0 ; namespace basicTests { const str = "Hello 😀" ; const bucket1 = new cloud . Bucket ( "tests-bucket" + uniqueId ++ ) ; export async function testGetAfterPut ( args ) { await bucket1 . put ( "somekey" , Buffer . from ( str , "utf-8" ) ) ; const buffer = await bucket1 . get ( "somekey" ) ; args . assert . equal ( buffer . toString ( "utf-8" ) , str ) ; await bucket1 . delete ( "somekey" ) ; } const bucket2 = new cloud . Bucket ( "tests-bucket" + uniqueId ++ ) ; bucket2 . onPut ( "handler" , async ( putEvent ) => { await bucket2 . put ( "testcomplete.json" , Buffer . from ( JSON . stringify ( putEvent ) ) ) ; } , { keyPrefix : "folder" } ) ; export async function testOnPut ( args ) { await bucket2 . put ( "folder/foo" , Buffer . from ( str , "utf-8" ) ) ; await new Promise ( resolve => setTimeout ( resolve , 10000 ) ) ; const testCompleteBuffer = await pollBucket ( bucket2 , "testcomplete.json" ) ; const testCompleteJSON = JSON . parse ( testCompleteBuffer . toString ( "utf-8" ) ) ; args . assert . equal ( testCompleteJSON . key , "folder/foo" ) ; args . assert . equal ( testCompleteJSON . size , Buffer . from ( str , "utf-8" ) . length ) ; await bucket2 . delete ( "folder/foo" ) ; await bucket2 . delete ( "testcomplete.json" ) ; } async function pollBucket ( bucket , key ) { let iteration = 0 ; for ( ; ; ) { try { let buf = await bucket . get ( key ) ; return buf ; } catch ( err ) { await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ; if ( iteration ++ > 10 ) { throw err ; } } } } } export async function runAllTests ( args , result ) { return await args . harness . testModule ( args , result , { [ "bucketTests.basicTests" ] : basicTests , } ) ; }