interface-ipfs-core
Version:
A test suite and interface you can use to implement a IPFS core interface.
95 lines (79 loc) • 2.77 kB
JavaScript
/* eslint-env mocha */
import all from 'it-all'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { CarReader } from '@ipld/car'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import * as dagPB from '@ipld/dag-pb'
import * as dagCBOR from '@ipld/dag-cbor'
import loadFixture from 'aegir/fixtures'
import toBuffer from 'it-to-buffer'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testExport (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dag.export', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => {
ipfs = (await factory.spawn()).api
})
after(() => factory.clean())
it('should export a car file', async () => {
const child = dagPB.encode({
Data: uint8ArrayFromString('block-' + Math.random()),
Links: []
})
const childCid = await ipfs.block.put(child, {
format: 'dag-pb',
version: 0
})
const parent = dagPB.encode({
Links: [{
Hash: childCid,
Tsize: child.length,
Name: ''
}]
})
const parentCid = await ipfs.block.put(parent, {
format: 'dag-pb',
version: 0
})
const grandParent = dagCBOR.encode({
parent: parentCid
})
const grandParentCid = await await ipfs.block.put(grandParent, {
format: 'dag-cbor',
version: 1
})
const expectedCids = [
grandParentCid,
parentCid,
childCid
]
const reader = await CarReader.fromIterable(ipfs.dag.export(grandParentCid))
const cids = await all(reader.cids())
expect(cids).to.deep.equal(expectedCids)
})
it('export of shuffled devnet export identical to canonical original', async function () {
this.timeout(360000)
const input = loadFixture('test/fixtures/car/lotus_devnet_genesis.car', 'interface-ipfs-core')
const result = await all(ipfs.dag.import(async function * () { yield input }()))
const exported = await toBuffer(ipfs.dag.export(result[0].root.cid))
expect(exported).to.equalBytes(input)
})
it('export of shuffled testnet export identical to canonical original', async function () {
this.timeout(360000)
const input = loadFixture('test/fixtures/car/lotus_testnet_export_128.car', 'interface-ipfs-core')
const result = await all(ipfs.dag.import(async function * () { yield input }()))
const exported = await toBuffer(ipfs.dag.export(result[0].root.cid))
expect(exported).to.equalBytes(input)
})
})
}