-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix tests by mocking out w3up in more places
abstract w3up mocking a bit and use it in more places
- Loading branch information
Showing
9 changed files
with
182 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
*/ | ||
|
@@ -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', | ||
])), | ||
}, | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
@@ -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) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.