|
| 1 | +/* eslint-env mocha */ |
| 2 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 3 | + |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const chai = require('chai') |
| 7 | +const dirtyChai = require('dirty-chai') |
| 8 | +const expect = chai.expect |
| 9 | +chai.use(dirtyChai) |
| 10 | +const series = require('async/series') |
| 11 | +const dagPB = require('ipld-dag-pb') |
| 12 | +const DAGNode = dagPB.DAGNode |
| 13 | + |
| 14 | +const IPFSApi = require('../src') |
| 15 | +const f = require('./utils/factory') |
| 16 | + |
| 17 | +let ipfsd |
| 18 | +let ipfs |
| 19 | + |
| 20 | +describe('.dag', function () { |
| 21 | + this.timeout(20 * 1000) |
| 22 | + before(function (done) { |
| 23 | + series([ |
| 24 | + (cb) => f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => { |
| 25 | + expect(err).to.not.exist() |
| 26 | + ipfsd = _ipfsd |
| 27 | + ipfs = IPFSApi(_ipfsd.apiAddr) |
| 28 | + cb() |
| 29 | + }) |
| 30 | + ], done) |
| 31 | + }) |
| 32 | + |
| 33 | + after((done) => { |
| 34 | + if (!ipfsd) return done() |
| 35 | + ipfsd.stop(done) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should be able to put and get a DAG node with format dag-pb', (done) => { |
| 39 | + const data = Buffer.from('some data') |
| 40 | + DAGNode.create(data, (err, node) => { |
| 41 | + expect(err).to.not.exist() |
| 42 | + ipfs.dag.put(node, {format: 'dag-pb', hashAlg: 'sha2-256'}, (err, cid) => { |
| 43 | + expect(err).to.not.exist() |
| 44 | + cid = cid.toV0() |
| 45 | + expect(cid.codec).to.equal('dag-pb') |
| 46 | + cid = cid.toBaseEncodedString('base58btc') |
| 47 | + // expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u') |
| 48 | + expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr') |
| 49 | + ipfs.dag.get(cid, (err, result) => { |
| 50 | + expect(err).to.not.exist() |
| 51 | + expect(result.value.data).to.deep.equal(data) |
| 52 | + done() |
| 53 | + }) |
| 54 | + }) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should be able to put and get a DAG node with format dag-cbor', (done) => { |
| 59 | + const cbor = {foo: 'dag-cbor-bar'} |
| 60 | + ipfs.dag.put(cbor, {format: 'dag-cbor', hashAlg: 'sha2-256'}, (err, cid) => { |
| 61 | + expect(err).to.not.exist() |
| 62 | + expect(cid.codec).to.equal('dag-cbor') |
| 63 | + cid = cid.toBaseEncodedString('base32') |
| 64 | + expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce') |
| 65 | + ipfs.dag.get(cid, (err, result) => { |
| 66 | + expect(err).to.not.exist() |
| 67 | + expect(result.value).to.deep.equal(cbor) |
| 68 | + done() |
| 69 | + }) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should callback with error when missing DAG resolver for raw multicodec', (done) => { |
| 74 | + // CIDv1 with multicodec = raw |
| 75 | + const cid = 'bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy' |
| 76 | + ipfs.dag.get(cid, (err, result) => { |
| 77 | + expect(result).to.not.exist() |
| 78 | + expect(err.message).to.equal('ipfs-api is missing DAG resolver for "raw" multicodec') |
| 79 | + done() |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments