Skip to content

Commit

Permalink
chore: remove receipt mocking from upload-client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosa committed Jun 3, 2024
1 parent 3212305 commit 161e086
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 139 deletions.
18 changes: 6 additions & 12 deletions packages/upload-client/test/blob.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'assert'
import { create as createLink } from 'multiformats/link'
import { sha256 } from 'multiformats/hashes/sha2'
import * as Client from '@ucanto/client'
import * as Server from '@ucanto/server'
Expand All @@ -17,7 +16,7 @@ import {
setupBlobAddSuccessResponse,
setupBlobAdd4xxResponse,
setupBlobAdd5xxResponse,
setupGetReceipt,
receiptsEndpoint,
} from './helpers/utils.js'
import { fetchWithUploadProgress } from '../src/fetch-with-upload-progress.js'

Expand All @@ -27,7 +26,6 @@ describe('Blob.add', () => {
const agent = await Signer.generate()
const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const link = createLink(CAR.codec.code, bytesHash)

const proofs = [
await BlobCapabilities.add.delegate({
Expand Down Expand Up @@ -87,9 +85,7 @@ describe('Blob.add', () => {
progress.push(status)
},
fetchWithUploadProgress,
fetch: setupGetReceipt(() => {
return link
}),
receiptsEndpoint,
}
)

Expand All @@ -114,9 +110,7 @@ describe('Blob.add', () => {
onUploadProgress: (status) => {
progressWithoutUploadProgress.push(status)
},
fetch: setupGetReceipt(() => {
return link
}),
receiptsEndpoint,
}
)
assert.deepEqual(multihashWithoutUploadProgress, bytesHash)
Expand Down Expand Up @@ -238,11 +232,11 @@ describe('Blob.add', () => {
{
connection,
retries: 0,
receiptsEndpoint: 'http://localhost:9201',
receiptsEndpoint: 'http://localhost:9201/unavailable/',
}
),
{
message: 'blob/accept receipt not yet available',
message: 'failed to fetch blob/accept receipt',
}
)
})
Expand Down Expand Up @@ -797,7 +791,7 @@ describe('Blob.remove', () => {
Blob.remove(
{ issuer: agent, with: space.did(), proofs, audience: serviceSigner },
bytesHash,
{ connection }
{ connection, receiptsEndpoint }
),
{ message: 'failed space/blob/remove invocation' }
)
Expand Down
65 changes: 46 additions & 19 deletions packages/upload-client/test/helpers/receipts-server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
import { createServer } from 'http'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { parseLink } from '@ucanto/server'
import * as Signer from '@ucanto/principal/ed25519'
import { Receipt, Message } from '@ucanto/core'
import * as CAR from '@ucanto/transport/car'
import { Assert } from '@web3-storage/content-claims/capability'
import { randomCAR } from './random.js'

const port = process.env.PORT ?? 9201
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fixtureName = process.env.FIXTURE_NAME || 'workflow.car'

const server = createServer((req, res) => {
const server = createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', '*')
res.setHeader('Access-Control-Allow-Headers', '*')

fs.readFile(
path.resolve(`${__dirname}`, '..', 'fixtures', fixtureName),
(error, content) => {
if (error) {
res.writeHead(500)
res.end()
}
res.writeHead(200, {
'Content-disposition': 'attachment; filename=' + fixtureName,
})
res.end(content)
}
)
const taskID = req.url?.split('/')[1] ?? ''
if (taskID === 'unavailable') {
res.writeHead(404)
res.end()
return
}

const issuer = await Signer.generate()
const content = (await randomCAR(128)).cid
const locationClaim = await Assert.location.delegate({
issuer,
audience: issuer,
with: issuer.toDIDKey(),
nb: {
content,
location: ['http://localhost'],
},
expiration: Infinity,
})

const receipt = await Receipt.issue({
issuer,
fx: {
fork: [locationClaim],
},
ran: parseLink(taskID),
result: {
ok: {
site: locationClaim.link(),
},
},
})

const message = await Message.build({
receipts: [receipt],
})
const request = CAR.request.encode(message)
res.writeHead(200)
res.end(request.body)
})

server.listen(port, () => console.log(`Listening on :${port}`))
Expand Down
55 changes: 2 additions & 53 deletions packages/upload-client/test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,13 @@
import { parseLink } from '@ucanto/server'
import * as Signer from '@ucanto/principal/ed25519'
import { Receipt, Message } from '@ucanto/core'
import * as CAR from '@ucanto/transport/car'
import { Receipt } from '@ucanto/core'
import * as Server from '@ucanto/server'
import * as HTTP from '@web3-storage/capabilities/http'
import * as W3sBlobCapabilities from '@web3-storage/capabilities/web3.storage/blob'
import { W3sBlob } from '@web3-storage/capabilities'
import { createConcludeInvocation } from '../../../upload-client/src/blob.js'
import { Assert } from '@web3-storage/content-claims/capability'

export const validateAuthorization = () => ({ ok: {} })

// @ts-ignore Parameter
export const setupGetReceipt = (contentGen) => {
// @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)
}

const taskID = url.pathname.replace('/receipt/', '')
const issuer = await Signer.generate()

const content = contentGen(taskID)
const locationClaim = await Assert.location.delegate({
issuer,
audience: issuer,
with: issuer.toDIDKey(),
nb: {
content,
location: ['http://localhost'],
},
expiration: Infinity,
})

const receipt = await Receipt.issue({
issuer,
fx: {
fork: [locationClaim],
},
ran: parseLink(taskID),
result: {
ok: {
site: locationClaim.link(),
},
},
})

const message = await Message.build({
receipts: [receipt],
})
const request = CAR.request.encode(message)
return new Response(request.body.buffer)
}
}
export const receiptsEndpoint = 'http://localhost:9201'

export const setupBlobAddSuccessResponse = async function (
// @ts-ignore
Expand Down
66 changes: 11 additions & 55 deletions packages/upload-client/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import assert from 'assert'
import { create as createLink } from 'multiformats/link'
import { sha256 } from 'multiformats/hashes/sha2'
import * as Client from '@ucanto/client'
import * as Server from '@ucanto/server'
import { provide } from '@ucanto/server'
Expand All @@ -21,7 +19,7 @@ import { mockService } from './helpers/mocks.js'
import {
validateAuthorization,
setupBlobAddSuccessResponse,
setupGetReceipt,
receiptsEndpoint,
} from './helpers/utils.js'
import {
blockEncodingLength,
Expand Down Expand Up @@ -148,9 +146,7 @@ describe('uploadFile', () => {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt(() => {
return expectedCar.cid
}),
receiptsEndpoint,
}
)

Expand All @@ -171,8 +167,6 @@ describe('uploadFile', () => {
const space = await Signer.generate()
const agent = await Signer.generate() // The "user" that will ask the service to accept the upload
const bytes = await randomBytes(1024 * 1024 * 5)
const bytesHash = await sha256.digest(bytes)
const link = createLink(CAR.codec.code, bytesHash)
const file = new Blob([bytes])
const piece = Piece.fromPayload(bytes).link
/** @type {import('../src/types.js').CARLink[]} */
Expand Down Expand Up @@ -270,9 +264,7 @@ 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(() => {
return link
}),
receiptsEndpoint,
}
)

Expand All @@ -283,8 +275,6 @@ describe('uploadFile', () => {
const space = await Signer.generate()
const agent = await Signer.generate() // The "user" that will ask the service to accept the upload
const bytes = await randomBytes(128)
const bytesHash = await sha256.digest(bytes)
const link = createLink(CAR.codec.code, bytesHash)
const file = new Blob([bytes])

const proofs = await Promise.all([
Expand Down Expand Up @@ -352,9 +342,7 @@ describe('uploadFile', () => {
file,
{
connection,
fetch: setupGetReceipt(() => {
return link
}),
receiptsEndpoint,
}
)
)
Expand All @@ -375,12 +363,6 @@ describe('uploadDirectory', () => {
(bytes, index) => new File([bytes], `${index}.txt`)
)
const pieces = bytesList.map((bytes) => Piece.fromPayload(bytes).link)
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 @@ -483,9 +465,7 @@ describe('uploadDirectory', () => {
onShardStored: (meta) => {
carCID = meta.cid
},
fetch: setupGetReceipt(() => {
return links[0]
}),
receiptsEndpoint,
}
)

Expand All @@ -509,12 +489,6 @@ describe('uploadDirectory', () => {
const files = bytesList.map(
(bytes, index) => new File([bytes], `${index}.txt`)
)
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 @@ -604,9 +578,7 @@ describe('uploadDirectory', () => {
connection,
shardSize: 500_057, // should end up with 2 CAR files
onShardStored: (meta) => carCIDs.push(meta.cid),
fetch: setupGetReceipt(() => {
return links[0]
}),
receiptsEndpoint,
}
)

Expand Down Expand Up @@ -720,12 +692,6 @@ describe('uploadDirectory', () => {
new File([bytesList[2]], 'c.txt'),
new File([bytesList[3]], 'a.txt'),
]
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 @@ -734,9 +700,7 @@ describe('uploadDirectory', () => {
unsortedFiles,
{
connection: uploadServiceForUnordered.connection,
fetch: setupGetReceipt(() => {
return links[0]
}),
receiptsEndpoint,
}
)

Expand All @@ -747,9 +711,7 @@ describe('uploadDirectory', () => {
[...unsortedFiles].sort(defaultFileComparator),
{
connection: uploadServiceForOrdered.connection,
fetch: setupGetReceipt(() => {
return links[0]
}),
receiptsEndpoint,
}
)

Expand Down Expand Up @@ -794,9 +756,7 @@ describe('uploadDirectory', () => {
{
connection: uploadServiceForCustomOrder.connection,
customOrder: true,
fetch: setupGetReceipt(() => {
return links[1]
}),
receiptsEndpoint,
}
)
const shardsForCustomOrder = uploadServiceForCustomOrder.invocations
Expand Down Expand Up @@ -946,9 +906,7 @@ describe('uploadCAR', () => {
connection,
onShardStored: (meta) => carCIDs.push(meta.cid),
shardSize,
fetch: setupGetReceipt(() => {
return car.roots[0]
}),
receiptsEndpoint,
}
)

Expand Down Expand Up @@ -1076,9 +1034,7 @@ describe('uploadCAR', () => {
onShardStored: (meta) => {
if (meta.piece) pieceCIDs.push(meta.piece)
},
fetch: setupGetReceipt(() => {
return car.roots[0]
}),
receiptsEndpoint,
}
)

Expand Down

0 comments on commit 161e086

Please sign in to comment.