Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Nov 4, 2024
1 parent 6b0d72d commit b179910
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 73 deletions.
5 changes: 5 additions & 0 deletions packages/capabilities/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as SpaceIndex from './space/index.js'
import * as UCAN from './ucan.js'
import * as Plan from './plan.js'
import * as Usage from './usage.js'
import * as Blob from './blob.js'
import * as SpaceBlob from './space/blob.js'
import * as W3sBlob from './web3.storage/blob.js'
import * as HTTP from './http.js'
Expand Down Expand Up @@ -47,6 +48,7 @@ export {
UCAN,
Plan,
Usage,
Blob,
SpaceBlob,
W3sBlob,
HTTP,
Expand Down Expand Up @@ -97,6 +99,9 @@ export const abilitiesAsStrings = [
Plan.createAdminSession.can,
Usage.usage.can,
Usage.report.can,
Blob.blob.can,
Blob.allocate.can,
Blob.accept.can,
SpaceBlob.blob.can,
SpaceBlob.add.can,
SpaceBlob.remove.can,
Expand Down
14 changes: 12 additions & 2 deletions packages/upload-api/test/external-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ export const getExternalServiceImplementations = async (config) => {
const claimsService = await ClaimsService.activate(config)
const blobRetriever = BlobRetriever.create(claimsService)
const storageProviders = await Promise.all([
StorageNode.activate({ ...config, ...principalResolver, claimsService }),
StorageNode.activate({ ...config, ...principalResolver, claimsService }),
StorageNode.activate({
...config,
...principalResolver,
...(config.http ? {} : { port: 8989 }),
claimsService
}),
StorageNode.activate({
...config,
...principalResolver,
...(config.http ? {} : { port: 8990 }),
claimsService
}),
])
const router = Router.create(config.serviceID, storageProviders)
return {
Expand Down
33 changes: 24 additions & 9 deletions packages/upload-api/test/external-service/storage-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { connect } from '@ucanto/client'
import { BlobNotFound } from '../../src/blob/lib.js'

export const MaxUploadSize = 127 * (1 << 25)
let nextPort = 8989

/** @param {API.MultihashDigest} digest */
const contentKey = (digest) => {
Expand All @@ -22,32 +21,43 @@ const contentKey = (digest) => {
}

export class StorageNode {
/** @param {{ http?: import('http'), claimsService: API.ClaimsClientConfig } & import('@ucanto/interface').PrincipalResolver} config */
/** @param {{ http?: import('http'), port?: number, claimsService: API.ClaimsClientConfig } & import('@ucanto/interface').PrincipalResolver} config */
static async activate(config) {
const id = await ed25519.generate()
const content = new Map()
const allocations = new Set()
/** @type {URL} */
let baseURL

/** @param {API.MultihashDigest} digest */
const hasContent = async digest => {
if (config.http) {
return content.has(contentKey(digest))
}
// if no http server, the content should be available at the base URL
// a "public bucket" used in tests.
const res = await fetch(new URL(contentKey(digest), baseURL))
return res.status === 200
}

const server = Server.create({
id,
codec: CAR.inbound,
service: /** @type {import('../../src/types/blob.js').BlobService} */ ({
blob: {
allocate: Server.provideAdvanced({
capability: BlobCapabilities.allocate,
handler: ({ capability }) => {
handler: async ({ capability }) => {
const digest = Digest.decode(capability.nb.blob.digest)
const checksum = base64pad.baseEncode(digest.digest)
if (capability.nb.blob.size > MaxUploadSize) {
return Server.error(new BlobSizeLimitExceededError(capability.nb.blob.size))
}
const key = contentKey(digest)
if (content.has(key)) {
if (await hasContent(digest)) {
return Server.ok({ size: 0 })
}


const key = contentKey(digest)
const size = allocations.has(key) ? 0 : capability.nb.blob.size
allocations.add(key)

Expand All @@ -65,7 +75,7 @@ export class StorageNode {
capability: BlobCapabilities.accept,
handler: async ({ capability }) => {
const digest = Digest.decode(capability.nb.blob.digest)
if (!content.has(contentKey(digest))) {
if (!(await hasContent(digest))) {
return Server.error(new AllocatedMemoryNotWrittenError())
}

Expand All @@ -91,7 +101,7 @@ export class StorageNode {
})

if (!config.http) {
baseURL = new URL(`http://127.0.0.1:${nextPort++}`)
baseURL = new URL(`http://127.0.0.1:${config.port ?? 8989}`)
const connection = connect({ id, codec: CAR.outbound, channel: server })
return new StorageNode({ id, content, url: baseURL, connection })
}
Expand Down Expand Up @@ -151,7 +161,12 @@ export class StorageNode {
// otherwise it keep connection lingering
response.destroy()
})
await new Promise((resolve) => httpServer.listen(resolve))
await /** @type {Promise<void>} */ (new Promise((resolve) => {
if (config.port) {
return httpServer.listen(port, resolve)
}
httpServer.listen(resolve)
}))

// @ts-ignore - this is actually what it returns on http
const { port } = httpServer.address()
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"mocha": "^10.2.0",
"npm-run-all": "^4.1.5",
"playwright-test": "^12.3.4",
"typescript": "5.6.3"
"typescript": "5.2.2"
},
"eslintConfig": {
"extends": [
Expand Down
7 changes: 3 additions & 4 deletions packages/upload-client/test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { Receipt } from '@ucanto/core'
import { Assert } from '@web3-storage/content-claims/capability'
import * as Server from '@ucanto/server'
import * as HTTP from '@storacha/capabilities/http'
import * as W3sBlobCapabilities from '@storacha/capabilities/web3.storage/blob'
import { W3sBlob } from '@storacha/capabilities'
import * as BlobCapabilities from '@storacha/capabilities/blob'
import { createConcludeInvocation } from '../../src/blob/add.js'
import { randomCAR } from './random.js'

Expand Down Expand Up @@ -119,7 +118,7 @@ const setupBlobAddResponse = async function (
hasAcceptReceipt
) {
const blob = invocation.capabilities[0].nb.blob
const blobAllocateTask = await W3sBlob.allocate
const blobAllocateTask = await BlobCapabilities.allocate
.invoke({
issuer,
audience,
Expand Down Expand Up @@ -184,7 +183,7 @@ const setupBlobAddResponse = async function (
blobPutReceipt
).delegate()

const blobAcceptTask = await W3sBlobCapabilities.accept
const blobAcceptTask = await BlobCapabilities.accept
.invoke({
issuer,
audience,
Expand Down
7 changes: 4 additions & 3 deletions packages/w3up-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@
"prepare": "npm run build",
"test": "npm-run-all -p -r mock:* test:all",
"test:all": "run-s test:node test:browser",
"test:node": "hundreds -r html -r text mocha 'test/**/!(*.browser).test.js' -n experimental-vm-modules -n no-warnings -n stack-trace-limit=1000 -t 10000",
"test:node": "hundreds -r html -r text mocha 'test/**/!(*.browser).test.js' -n experimental-vm-modules -n no-warnings -n stack-trace-limit=1000 -t 10000 --bail",
"test:browser": "playwright-test --runner mocha 'test/**/!(*.node).test.js'",
"mock": "run-p mock:*",
"mock:bucket-200": "PORT=8989 STATUS=200 node test/helpers/bucket-server.js",
"mock:bucket-0-200": "PORT=8989 STATUS=200 node test/helpers/bucket-server.js",
"mock:bucket-1-200": "PORT=8990 STATUS=200 node test/helpers/bucket-server.js",
"mock:receipts-server": "PORT=9201 node test/helpers/receipts-server.js",
"coverage": "c8 report -r html && open coverage/index.html",
"rc": "npm version prerelease --preid rc",
Expand Down Expand Up @@ -171,7 +172,7 @@
"npm-run-all": "^4.1.5",
"playwright-test": "^12.3.4",
"typedoc": "^0.25.3",
"typescript": "^5.2.2"
"typescript": "5.2.2"
},
"eslintConfig": {
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/src/capability/blob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Blob } from '@storacha/upload-client'
import { Blob as BlobCapabilities } from '@storacha/capabilities'
import { SpaceBlob as BlobCapabilities } from '@storacha/capabilities'
import { sha256 } from 'multiformats/hashes/sha2'
import { Base } from '../base.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/src/capability/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Index } from '@storacha/upload-client'
import { Index as IndexCapabilities } from '@storacha/capabilities'
import { SpaceIndex as IndexCapabilities } from '@storacha/capabilities'
import { Base } from '../base.js'

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Receipt,
} from '@storacha/upload-client'
import {
Blob as BlobCapabilities,
Index as IndexCapabilities,
SpaceBlob as BlobCapabilities,
SpaceIndex as IndexCapabilities,
Upload as UploadCapabilities,
Filecoin as FilecoinCapabilities,
} from '@storacha/capabilities'
Expand Down
22 changes: 11 additions & 11 deletions packages/w3up-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ export type {
export type {
Abilities,
ServiceAbility,
BlobAdd,
BlobList,
BlobRemove,
SpaceBlobAdd,
SpaceBlobList,
SpaceBlobRemove,
StoreAdd,
StoreList,
StoreRemove,
Expand Down Expand Up @@ -119,14 +119,14 @@ export type {
} from '@storacha/access/types'

export type {
BlobAddSuccess,
BlobAddFailure,
BlobListSuccess,
BlobListFailure,
BlobRemoveSuccess,
BlobRemoveFailure,
IndexAddSuccess,
IndexAddFailure,
SpaceBlobAddSuccess,
SpaceBlobAddFailure,
SpaceBlobListSuccess,
SpaceBlobListFailure,
SpaceBlobRemoveSuccess,
SpaceBlobRemoveFailure,
SpaceIndexAddSuccess,
SpaceIndexAddFailure,
StoreAddSuccess,
StoreGetSuccess,
StoreGetFailure,
Expand Down
7 changes: 3 additions & 4 deletions packages/w3up-client/test/capability/blob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { randomBytes } from '../helpers/random.js'
import { Client } from '../../src/client.js'
import * as Test from '../test.js'
import { receiptsEndpoint } from '../helpers/utils.js'
import * as Result from '../helpers/result.js'

export const BlobClient = Test.withContext({
'should store a blob': async (
assert,
{ connection, provisionsStorage, allocationsStorage }
{ connection, provisionsStorage, registry }
) => {
const alice = new Client(await AgentData.create(), {
// @ts-ignore
Expand Down Expand Up @@ -38,9 +39,7 @@ export const BlobClient = Test.withContext({
})

// TODO we should check blobsStorage as well
assert.deepEqual(await allocationsStorage.exists(space.did(), digest), {
ok: true,
})
Result.try(await registry.find(space.did(), digest))

assert.deepEqual(digest.bytes, bytesHash.bytes)
},
Expand Down
20 changes: 5 additions & 15 deletions packages/w3up-client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { receiptsEndpoint } from './helpers/utils.js'
import { Absentee } from '@ucanto/principal'
import { DIDMailto } from '../src/capability/access.js'
import { confirmConfirmationUrl } from '../../upload-api/test/helpers/utils.js'
import * as Result from './helpers/result.js'

/** @type {Test.Suite} */
export const testClient = {
uploadFile: Test.withContext({
'should upload a file to the service': async (
assert,
{ connection, provisionsStorage, uploadTable, allocationsStorage }
{ connection, provisionsStorage, uploadTable, registry }
) => {
const bytes = await randomBytes(128)
const file = new Blob([bytes])
Expand Down Expand Up @@ -61,13 +62,7 @@ export const testClient = {
ok: true,
})

assert.deepEqual(
await allocationsStorage.exists(space.did(), expectedCar.cid.multihash),
{
ok: true,
}
)

Result.try(await registry.find(space.did(), expectedCar.cid.multihash))
assert.equal(carCID?.toString(), expectedCar.cid.toString())
assert.equal(dataCID.toString(), expectedCar.roots[0].toString())
},
Expand Down Expand Up @@ -144,7 +139,7 @@ export const testClient = {
uploadCar: Test.withContext({
'uploads a CAR file to the service': async (
assert,
{ connection, provisionsStorage, uploadTable, allocationsStorage }
{ connection, provisionsStorage, uploadTable, registry }
) => {
const car = await randomCAR(32)

Expand Down Expand Up @@ -185,12 +180,7 @@ export const testClient = {
return assert.ok(carCID)
}

assert.deepEqual(
await allocationsStorage.exists(space.did(), carCID.multihash),
{
ok: true,
}
)
Result.try(await registry.find(space.did(), carCID.multihash))
},
}),
getReceipt: Test.withContext({
Expand Down
22 changes: 22 additions & 0 deletions packages/w3up-client/test/helpers/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export * from '@ucanto/core/result'
import * as API from '@ucanto/interface'

/**
* Returns contained `ok` if result is and throws `error` if result is not ok.
*
* @template T
* @param {API.Result<T, {}>} result
* @returns {T}
*/
export const unwrap = ({ ok, error }) => {
if (error) {
throw error
} else {
return /** @type {T} */ (ok)
}
}

/**
* Also expose as `Result.try` which is arguably more clear.
*/
export { unwrap as try }
Loading

0 comments on commit b179910

Please sign in to comment.