Skip to content

Commit 3dff1ac

Browse files
committed
test: add test on import car file as ReadableStream
1 parent ed5666e commit 3dff1ac

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/interface-tests/src/dag/import.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ async function createCar (blocks: Array<{ cid: CID, bytes: Uint8Array }>): Promi
4444
return out
4545
}
4646

47+
async function createReadableStreamFromCar(car: AsyncIterable<Uint8Array>): Promise<ReadableStream> {
48+
const stream = new ReadableStream({
49+
async start (controller) {
50+
try {
51+
for await (const chunk of car) {
52+
controller.enqueue(chunk)
53+
}
54+
controller.close()
55+
} catch (err) {
56+
controller.error(err)
57+
}
58+
}
59+
})
60+
61+
return stream
62+
}
63+
4764
/**
4865
* @typedef {import('ipfsd-ctl').Factory} Factory
4966
*/
@@ -179,5 +196,23 @@ export function testImport (factory: Factory<KuboNode>, options: MochaConfig): v
179196
const result = await all(ipfs.dag.import(async function * () { yield input }()))
180197
expect(result).to.have.deep.nested.property('[0].root.cid', cids[0])
181198
})
199+
200+
it('should be able to import car file as a ReadableStream', async () => {
201+
const blocks = await createBlocks(5)
202+
const car = await createCar(blocks)
203+
204+
const stream = await createReadableStreamFromCar(car)
205+
206+
const result = await all(ipfs.dag.import(stream))
207+
expect(result).to.have.lengthOf(1)
208+
expect(result).to.have.deep.nested.property('[0].root.cid', blocks[0].cid)
209+
210+
for (const { cid } of blocks) {
211+
await expect(ipfs.block.get(cid)).to.eventually.be.ok()
212+
}
213+
214+
await expect(all(ipfs.pin.ls({ paths: blocks[0].cid }))).to.eventually.have.lengthOf(1)
215+
.and.have.nested.property('[0].type', 'recursive')
216+
})
182217
})
183218
}

0 commit comments

Comments
 (0)