Skip to content

Commit

Permalink
fix: use generators for getting receipts in upload-client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosa committed May 24, 2024
1 parent 5a55ba2 commit 62c32e6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 28 deletions.
9 changes: 7 additions & 2 deletions packages/upload-client/test/blob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ describe('Blob.add', () => {
progress.push(status)
},
fetchWithUploadProgress,
fetch: setupGetReceipt(link),
fetch: setupGetReceipt(function* () {
yield link
}),
}
)

Expand All @@ -112,7 +114,9 @@ describe('Blob.add', () => {
onUploadProgress: (status) => {
progressWithoutUploadProgress.push(status)
},
fetch: setupGetReceipt(link),
fetch: setupGetReceipt(function* () {
yield link
}),
}
)
assert.deepEqual(
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 8 additions & 4 deletions packages/upload-client/test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<import('multiformats').Link,void,unknown>} 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,
Expand Down
91 changes: 69 additions & 22 deletions packages/upload-client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ describe('uploadFile', () => {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt(expectedCar.cid),
fetch: setupGetReceipt(function* () {
yield expectedCar.cid
yield expectedCar.cid
}),
}
)

Expand Down Expand Up @@ -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
}),
}
)

Expand Down Expand Up @@ -342,7 +352,9 @@ describe('uploadFile', () => {
file,
{
connection,
fetch: setupGetReceipt(link),
fetch: setupGetReceipt(function* () {
yield link
}),
}
)
)
Expand All @@ -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
Expand Down Expand Up @@ -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]
}),
}
)

Expand All @@ -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 = []
Expand Down Expand Up @@ -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]
}),
}
)

Expand Down Expand Up @@ -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`
Expand All @@ -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]
}),
}
)

Expand All @@ -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]
}),
}
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
}),
}
)

Expand Down Expand Up @@ -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]
}),
}
)

Expand Down

0 comments on commit 62c32e6

Please sign in to comment.