From 62c32e63bd32647e663a05e8271c83ad89b9b152 Mon Sep 17 00:00:00 2001 From: Joao Andrade Date: Fri, 24 May 2024 19:30:03 +0100 Subject: [PATCH] fix: use generators for getting receipts in upload-client tests --- packages/upload-client/test/blob.test.js | 9 +- packages/upload-client/test/helpers/utils.js | 12 ++- packages/upload-client/test/index.test.js | 91 +++++++++++++++----- 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/packages/upload-client/test/blob.test.js b/packages/upload-client/test/blob.test.js index e936b8522..e407f0744 100644 --- a/packages/upload-client/test/blob.test.js +++ b/packages/upload-client/test/blob.test.js @@ -86,7 +86,9 @@ describe('Blob.add', () => { progress.push(status) }, fetchWithUploadProgress, - fetch: setupGetReceipt(link), + fetch: setupGetReceipt(function* () { + yield link + }), } ) @@ -112,7 +114,9 @@ describe('Blob.add', () => { onUploadProgress: (status) => { progressWithoutUploadProgress.push(status) }, - fetch: setupGetReceipt(link), + fetch: setupGetReceipt(function* () { + yield link + }), } ) assert.deepEqual( @@ -315,6 +319,7 @@ describe('Blob.add', () => { } ) }) + it('throws for bucket URL client error 4xx', async () => { const space = await Signer.generate() const agent = await Signer.generate() diff --git a/packages/upload-client/test/helpers/utils.js b/packages/upload-client/test/helpers/utils.js index 7cc37c841..47dd836a6 100644 --- a/packages/upload-client/test/helpers/utils.js +++ b/packages/upload-client/test/helpers/utils.js @@ -11,23 +11,27 @@ import { Assert } from '@web3-storage/content-claims/capability' export const validateAuthorization = () => ({ ok: {} }) -/** - * @param {import('multiformats').Link} content - */ -export const setupGetReceipt = (content) => { +// @ts-ignore Parameter +export const setupGetReceipt = (contentGenFn) => { + // @type {Generator} contentGenFn + const contentGen = contentGenFn() // @ts-ignore Parameter return async (url, options) => { // need to handle using regular fetch when not actually getting a receipt if ( + options || !url.pathname || (url.pathname.contains && !url.pathname.contains('/receipt/')) ) { return await fetch(url, options) } + console.log(options?.method, url) const taskID = url.pathname.replace('/receipt/', '') const issuer = await Signer.generate() + const { value: content } = contentGen.next() + console.log(content) const locationClaim = await Assert.location.delegate({ issuer, audience: issuer, diff --git a/packages/upload-client/test/index.test.js b/packages/upload-client/test/index.test.js index 25ace23c3..d294d5035 100644 --- a/packages/upload-client/test/index.test.js +++ b/packages/upload-client/test/index.test.js @@ -146,7 +146,10 @@ describe('uploadFile', () => { onShardStored: (meta) => { carCID = meta.cid }, - fetch: setupGetReceipt(expectedCar.cid), + fetch: setupGetReceipt(function* () { + yield expectedCar.cid + yield expectedCar.cid + }), } ) @@ -264,7 +267,14 @@ describe('uploadFile', () => { // so we actually end up with a shard for each block - 5 CARs! shardSize: 1024 * 1024 * 2 + 1, onShardStored: (meta) => carCIDs.push(meta.cid), - fetch: setupGetReceipt(link), + fetch: setupGetReceipt(function* () { + yield link + yield link + yield link + yield link + yield link + yield link + }), } ) @@ -342,7 +352,9 @@ describe('uploadFile', () => { file, { connection, - fetch: setupGetReceipt(link), + fetch: setupGetReceipt(function* () { + yield link + }), } ) ) @@ -363,10 +375,12 @@ describe('uploadDirectory', () => { (bytes, index) => new File([bytes], `${index}.txt`) ) const pieces = bytesList.map((bytes) => Piece.fromPayload(bytes).link) - const links = bytesList.map(async (bytes) => { - const bytesHash = await sha256.digest(bytes) - return createLink(CAR.codec.code, bytesHash) - }) + const links = await Promise.all( + bytesList.map(async (bytes) => { + const bytesHash = await sha256.digest(bytes) + return createLink(CAR.codec.code, bytesHash) + }) + ) /** @type {import('../src/types.js').CARLink?} */ let carCID = null @@ -467,7 +481,10 @@ describe('uploadDirectory', () => { onShardStored: (meta) => { carCID = meta.cid }, - fetch: setupGetReceipt(await links[0]), + fetch: setupGetReceipt(function* () { + yield links[0] + yield links[1] + }), } ) @@ -491,10 +508,12 @@ describe('uploadDirectory', () => { const files = bytesList.map( (bytes, index) => new File([bytes], `${index}.txt`) ) - const links = bytesList.map(async (bytes) => { - const bytesHash = await sha256.digest(bytes) - return createLink(CAR.codec.code, bytesHash) - }) + const links = await Promise.all( + bytesList.map(async (bytes) => { + const bytesHash = await sha256.digest(bytes) + return createLink(CAR.codec.code, bytesHash) + }) + ) const pieces = bytesList.map((bytes) => Piece.fromPayload(bytes).link) /** @type {import('../src/types.js').CARLink[]} */ const carCIDs = [] @@ -582,7 +601,11 @@ describe('uploadDirectory', () => { connection, shardSize: 500_057, // should end up with 2 CAR files onShardStored: (meta) => carCIDs.push(meta.cid), - fetch: setupGetReceipt(await links[0]), + fetch: setupGetReceipt(function* () { + yield links[0] + yield links[0] + yield links[0] + }), } ) @@ -694,10 +717,12 @@ describe('uploadDirectory', () => { new File([bytesList[2]], 'c.txt'), new File([bytesList[3]], 'a.txt'), ] - const links = bytesList.map(async (bytes) => { - const bytesHash = await sha256.digest(bytes) - return createLink(CAR.codec.code, bytesHash) - }) + const links = await Promise.all( + bytesList.map(async (bytes) => { + const bytesHash = await sha256.digest(bytes) + return createLink(CAR.codec.code, bytesHash) + }) + ) const uploadServiceForUnordered = createSimpleMockUploadServer() // uploading unsorted files should work because they should be sorted by `uploadDirectory` @@ -706,7 +731,12 @@ describe('uploadDirectory', () => { unsortedFiles, { connection: uploadServiceForUnordered.connection, - fetch: setupGetReceipt(await links[0]), + fetch: setupGetReceipt(function* () { + yield links[0] + yield links[1] + yield links[2] + yield links[3] + }), } ) @@ -717,7 +747,12 @@ describe('uploadDirectory', () => { [...unsortedFiles].sort(defaultFileComparator), { connection: uploadServiceForOrdered.connection, - fetch: setupGetReceipt(await links[0]), + fetch: setupGetReceipt(function* () { + yield links[0] + yield links[1] + yield links[2] + yield links[3] + }), } ) @@ -762,7 +797,12 @@ describe('uploadDirectory', () => { { connection: uploadServiceForCustomOrder.connection, customOrder: true, - fetch: setupGetReceipt(await links[1]), + fetch: setupGetReceipt(function* () { + yield links[1] + yield links[0] + yield links[2] + yield links[3] + }), } ) const shardsForCustomOrder = uploadServiceForCustomOrder.invocations @@ -910,7 +950,11 @@ describe('uploadCAR', () => { connection, onShardStored: (meta) => carCIDs.push(meta.cid), shardSize, - fetch: setupGetReceipt(car.roots[0].toV1()), + fetch: setupGetReceipt(function* () { + yield car.roots[0] + yield car.roots[0] + yield car.roots[0] + }), } ) @@ -1036,7 +1080,10 @@ describe('uploadCAR', () => { onShardStored: (meta) => { if (meta.piece) pieceCIDs.push(meta.piece) }, - fetch: setupGetReceipt(car.roots[0].toV1()), + fetch: setupGetReceipt(function* () { + yield car.roots[0] + yield car.roots[0] + }), } )