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