Skip to content

Commit

Permalink
fix: fix tests by mocking out w3up in more places
Browse files Browse the repository at this point in the history
abstract w3up mocking a bit and use it in more places
  • Loading branch information
travis committed Apr 12, 2024
1 parent cb4b333 commit 389931d
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 132 deletions.
26 changes: 26 additions & 0 deletions packages/api/test/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ProviderInput } from '@ucanto/server'
import { InferInvokedCapability } from '@ucanto/client'
import { Store, Upload, Filecoin } from '@web3-storage/capabilities'
import { FilecoinInfoSuccess } from '@web3-storage/capabilities/types'
import { Server as HttpServer } from 'http'

interface MockW3upOptions {
did?: string
onHandleFilecoinInfo?: (
invocation: ProviderInput<InferInvokedCapability<typeof Filecoin.info>>
) => Promise<FilecoinInfoSuccess | undefined>
onHandleUploadGet?: (
invocation: ProviderInput<InferInvokedCapability<typeof Upload.get>>
) => Promise<UploadGetSuccess | undefined>
onHandleStoreAdd?: (
invocation: ProviderInput<InferInvokedCapability<typeof Store.add>>
) => Promise<void>
onHandleUploadAdd?: (
invocation: ProviderInput<InferInvokedCapability<typeof Upload.add>>
) => Promise<void>
}

interface MockW3up {
server: HttpServer
did: string
}
10 changes: 9 additions & 1 deletion packages/api/test/maintenance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ import {
getMiniflareContext,
setupMiniflareContext,
} from './scripts/test-context.js'
import {
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

/** @typedef {import('../src/middleware/maintenance.js').Mode} Mode */

test.before(async (t) => {
await setupMiniflareContext(t)
await setupMiniflareContext(t, {
overrides: {
...(await w3upMiniflareOverrides(await createMockW3upServer())),
},
})
})

/**
Expand Down
99 changes: 35 additions & 64 deletions packages/api/test/nfts-get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import {
createMockW3up,
locate,
encodeDelegationAsCid,
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

const nftStorageSpace = ed25519.generate()
const nftStorageApiPrincipal = ed25519.generate()
const nftStorageAccountEmailAllowListedForW3up = '[email protected]'
const mockW3upDID = 'did:web:test.web3.storage'
/**
* @type {import('@web3-storage/access').PieceLink}
*/
Expand All @@ -45,70 +43,43 @@ const mockDeals = [
const cidWithShards = parseLink(
'bafybeiccy35oi3gajocq5bbg7pnaxb3kv5ibtdz3tc3kari53qhbjotzey'
)
const mockW3up = Promise.resolve(
(async function () {
const server = createServer(
await createMockW3up({
did: mockW3upDID,
// @ts-expect-error not returning a full upload get response for now
async onHandleUploadGet(invocation) {
if (invocation.capability.nb.root?.equals(cidWithShards)) {
return {
// grabbed this shard CID from staging, it should correspond to a piece named bafkzcibeslzwmewd4pugjanyiayot5m76a67dvdir25v6ms6kbuozy2sxotplrrrce
shards: [
parseLink(
'bagbaieragf62xatg3bqrfafdy3lpk2fte7526kvxnltqsnhjr45cz6jjk7mq'
),
],
}
} else {
return {
shards: [],
}
}
},
async onHandleFilecoinInfo(invocation) {
if (invocation.capability.nb.piece.equals(mockPieceLink)) {
return {
deals: mockDeals,
aggregates: [],
piece: mockPieceLink,
}
} else {
return undefined
}
},
})
)
server.listen(0)
await new Promise((resolve) =>
server.addListener('listening', () => resolve(undefined))
)
return {
server,
}
})()
)

test.before(async (t) => {
const mockW3up = await createMockW3upServer({
async onHandleUploadGet(invocation) {
if (invocation.capability.nb.root?.equals(cidWithShards)) {
return {
// grabbed this shard CID from staging, it should correspond to a piece named bafkzcibeslzwmewd4pugjanyiayot5m76a67dvdir25v6ms6kbuozy2sxotplrrrce
shards: [
parseLink(
'bagbaieragf62xatg3bqrfafdy3lpk2fte7526kvxnltqsnhjr45cz6jjk7mq'
),
],
}
} else {
return {
shards: [],
}
}
},
async onHandleFilecoinInfo(invocation) {
if (invocation.capability.nb.piece.equals(mockPieceLink)) {
return {
deals: mockDeals,
aggregates: [],
piece: mockPieceLink,
}
} else {
return undefined
}
},
})
await setupMiniflareContext(t, {
overrides: {
W3UP_URL: locate((await mockW3up).server).url.toString(),
W3UP_DID: mockW3upDID,
W3_NFTSTORAGE_SPACE: (await nftStorageSpace).did(),
W3_NFTSTORAGE_PRINCIPAL: ed25519.format(await nftStorageApiPrincipal),
W3_NFTSTORAGE_PROOF: (
await encodeDelegationAsCid(
await delegate({
issuer: await nftStorageSpace,
audience: await nftStorageApiPrincipal,
capabilities: [
{ can: 'upload/get', with: (await nftStorageSpace).did() },
{ can: 'filecoin/info', with: (await nftStorageSpace).did() },
],
})
)
).toString(base64),
...(await w3upMiniflareOverrides(mockW3up, [
'upload/get',
'filecoin/info',
])),
},
})
})
Expand Down
10 changes: 9 additions & 1 deletion packages/api/test/nfts-metaplex-upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import {
getTestServiceConfig,
setupMiniflareContext,
} from './scripts/test-context.js'
import {
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

/** @type {number} */
let metaplexUserId

test.before(async (t) => {
await setupMiniflareContext(t)
await setupMiniflareContext(t, {
overrides: {
...(await w3upMiniflareOverrides(await createMockW3upServer())),
},
})

const config = getTestServiceConfig(t)
const rawClient = getRawClient(config)
Expand Down
13 changes: 12 additions & 1 deletion packages/api/test/nfts-store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ import {
} from './scripts/test-context.js'
import { File, Blob } from 'nft.storage/src/platform.js'
import { FormData } from 'undici'
import {
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

const overrides = (async () =>
await w3upMiniflareOverrides(await createMockW3upServer()))()

test.beforeEach(async (t) => {
await setupMiniflareContext(t)
await setupMiniflareContext(t, {
overrides: {
...(await overrides),
},
})
})

test('should store image', async (t) => {
Expand Down
73 changes: 18 additions & 55 deletions packages/api/test/nfts-upload.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import test from 'ava'
import { CID } from 'multiformats/cid'
import * as Block from 'multiformats/block'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import * as pb from '@ipld/dag-pb'
import { CarWriter } from '@ipld/car'
import { packToBlob } from 'ipfs-car/pack/blob'
import { MultihashIndexSortedReader } from 'cardex'
import { TreewalkCarSplitter } from 'carbites/treewalk'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { createClientWithUser, getRawClient } from './scripts/helpers.js'
import { createCar } from './scripts/car.js'
Expand All @@ -25,75 +20,43 @@ import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { FormData } from 'undici'
import { createCarCid } from '../src/utils/car.js'
import { createServer } from 'node:http'
import { ed25519 } from '@ucanto/principal'
import { delegate } from '@ucanto/core'
import { base64 } from 'multiformats/bases/base64'

import {
createMockW3up,
locate,
encodeDelegationAsCid,
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const nftStorageSpace = ed25519.generate()
const nftStorageApiPrincipal = ed25519.generate()
const nftStorageAccountEmailAllowListedForW3up = '[email protected]'
const mockW3upDID = 'did:web:test.web3.storage'
let mockW3upStoreAddCount = 0
let mockW3upUploadAddCount = 0
const mockW3up = Promise.resolve(
(async function () {
const server = createServer(
await createMockW3up({
did: mockW3upDID,
async onHandleStoreAdd(invocation) {
mockW3upStoreAddCount++
},
async onHandleUploadAdd(invocation) {
mockW3upUploadAddCount++
},
})
)
server.listen(0)
await new Promise((resolve) =>
server.addListener('listening', () => resolve(undefined))
)
return {
server,
}
})()
)

/**
* @type {import('./bindings.js').MockW3up}
*/
let mockW3up
test.before(async (t) => {
mockW3up = await createMockW3upServer({
async onHandleStoreAdd(invocation) {
mockW3upStoreAddCount++
},
async onHandleUploadAdd(invocation) {
mockW3upUploadAddCount++
},
})

const linkdexUrl = 'http://fake.api.net'
await setupMiniflareContext(t, {
overrides: {
LINKDEX_URL: linkdexUrl,
W3UP_URL: locate((await mockW3up).server).url.toString(),
W3UP_DID: mockW3upDID,
W3_NFTSTORAGE_SPACE: (await nftStorageSpace).did(),
W3_NFTSTORAGE_PRINCIPAL: ed25519.format(await nftStorageApiPrincipal),
W3_NFTSTORAGE_PROOF: (
await encodeDelegationAsCid(
await delegate({
issuer: await nftStorageSpace,
audience: await nftStorageApiPrincipal,
capabilities: [
{ can: 'store/add', with: (await nftStorageSpace).did() },
{ can: 'upload/add', with: (await nftStorageSpace).did() },
],
})
)
).toString(base64),
...(await w3upMiniflareOverrides(mockW3up)),
},
})
})

test.after(async (t) => {
;(await mockW3up).server.close()
mockW3up.server.close()
})

test.serial('should upload a single file', async (t) => {
Expand Down
13 changes: 12 additions & 1 deletion packages/api/test/pin-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import {
getTestServiceConfig,
setupMiniflareContext,
} from './scripts/test-context.js'
import {
createMockW3upServer,
w3upMiniflareOverrides,
} from './utils/w3up-testing.js'

const overrides = (async () =>
await w3upMiniflareOverrides(await createMockW3upServer()))()

test.beforeEach(async (t) => {
await setupMiniflareContext(t)
await setupMiniflareContext(t, {
overrides: {
...(await overrides),
},
})
})

test('should pin with just cid', async (t) => {
Expand Down
Loading

0 comments on commit 389931d

Please sign in to comment.