Skip to content

Commit

Permalink
Add provides and want tests to block broker.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Mar 15, 2024
1 parent a6606f4 commit 5e410d7
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions packages/manual-block-broker/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,65 @@ const block = new Uint8Array()
describe('manual block broker', () => {
it('retrieve resolves when a block is provided', async () => {
const bb = new ManualBlockBroker()

const promise = bb.retrieve(cid, {})
const result = bb.provide(cid, block)

assert(result)

await promise
assert.equal(await promise, block)
})

it('retrieve throws when aborted', async () => {
const bb = new ManualBlockBroker()
const controller = new AbortController()
const promise = bb.retrieve(cid, { signal: controller.signal })

controller.abort()

await assert.rejects(async () => promise)
})

it('provides returns false the block is not requested', () => {
const bb = new ManualBlockBroker()
const result = bb.provide(cid, block)

assert(!result)
})

it('provides returns false when aborted', async () => {
const bb = new ManualBlockBroker()
const controller = new AbortController()
const promise = bb.retrieve(cid, { signal: controller.signal })

controller.abort()

const result = bb.provide(cid, block)

assert(!result)
await assert.rejects(async () => promise)
})

it('getWants returns the CID when it is requested', async () => {
const bb = new ManualBlockBroker()
const controller = new AbortController()
const promise = bb.retrieve(cid, { signal: controller.signal })
const wants = [...bb.getWants()]

controller.abort()

assert.deepEqual(wants, [cid])
await assert.rejects(async () => promise)
})

it('getWants no longer returns the CID when it is provided', async () => {
const bb = new ManualBlockBroker()
const promise = bb.retrieve(cid, {})

bb.provide(cid, block)

assert.equal(await promise, block)

const wants = [...bb.getWants()]

assert.deepEqual(wants, [])
})
})

0 comments on commit 5e410d7

Please sign in to comment.