|
| 1 | +/* eslint-env mocha */ |
| 2 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 3 | + |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const expect = require('chai').expect |
| 7 | +const dagPB = require('ipld-dag-pb') |
| 8 | +const dagCBOR = require('ipld-dag-cbor') |
| 9 | +const series = require('async/series') |
| 10 | +const pull = require('pull-stream') |
| 11 | + |
| 12 | +module.exports = (common) => { |
| 13 | + describe.only('.dag.resolve', () => { |
| 14 | + let ipfs |
| 15 | + let nodePb |
| 16 | + let nodeCbor |
| 17 | + let cidPb |
| 18 | + let cidCbor |
| 19 | + |
| 20 | + before((done) => { |
| 21 | + common.setup((err, factory) => { |
| 22 | + expect(err).to.not.exist |
| 23 | + factory.spawnNode((err, node) => { |
| 24 | + expect(err).to.not.exist |
| 25 | + ipfs = node |
| 26 | + done() |
| 27 | + }) |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + after((done) => { |
| 32 | + common.teardown(done) |
| 33 | + }) |
| 34 | + |
| 35 | + it('populate', (done) => { |
| 36 | + series([ |
| 37 | + (cb) => { |
| 38 | + dagPB.DAGNode.create(new Buffer('I am inside a Protobuf'), (err, node) => { |
| 39 | + expect(err).to.not.exist |
| 40 | + nodePb = node |
| 41 | + cb() |
| 42 | + }) |
| 43 | + }, |
| 44 | + (cb) => { |
| 45 | + dagPB.util.cid(nodePb, (err, cid) => { |
| 46 | + expect(err).to.not.exist |
| 47 | + cidPb = cid |
| 48 | + cb() |
| 49 | + }) |
| 50 | + }, |
| 51 | + (cb) => { |
| 52 | + nodeCbor = { |
| 53 | + someData: 'I am inside a Cbor object', |
| 54 | + pb: { '/': cidPb.toBaseEncodedString() } |
| 55 | + } |
| 56 | + |
| 57 | + dagCBOR.util.cid(nodeCbor, (err, cid) => { |
| 58 | + expect(err).to.not.exist |
| 59 | + cidCbor = cid |
| 60 | + console.log(nodeCbor) |
| 61 | + cb() |
| 62 | + }) |
| 63 | + } |
| 64 | + ], store) |
| 65 | + |
| 66 | + function store () { |
| 67 | + pull( |
| 68 | + pull.values([ |
| 69 | + { node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' }, |
| 70 | + { node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' } |
| 71 | + ]), |
| 72 | + pull.asyncMap((el, cb) => { |
| 73 | + ipfs.dag.put(el.node, el.multicodec, el.hashAlg, (err) => { |
| 74 | + if (err) { |
| 75 | + console.log(err) |
| 76 | + } |
| 77 | + console.log('put', el.multicodec) |
| 78 | + |
| 79 | + cb() |
| 80 | + }) |
| 81 | + }), |
| 82 | + pull.onEnd(done) |
| 83 | + ) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + describe('callback API', () => { |
| 88 | + describe('.resolve', () => { |
| 89 | + it('dag-pb get the node', (done) => { |
| 90 | + ipfs.dag.resolve(cidPb, '/', (err, result) => { |
| 91 | + expect(err).to.not.exist |
| 92 | + |
| 93 | + dagPB.util.cid(result, (err, cid) => { |
| 94 | + expect(err).to.not.exist |
| 95 | + expect(cid).to.eql(cidPb) |
| 96 | + done() |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it('dag-pb local scope', (done) => { |
| 102 | + ipfs.dag.resolve(cidPb, 'data', (err, result) => { |
| 103 | + expect(err).to.not.exist |
| 104 | + expect(result).to.eql(new Buffer('I am inside a Protobuf')) |
| 105 | + done() |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + it.skip('dag-pb one level', (done) => {}) |
| 110 | + it.skip('dag-pb two levels', (done) => {}) |
| 111 | + |
| 112 | + it('dag-cbor get the node', (done) => { |
| 113 | + ipfs.dag.get(cidCbor, (err, result) => { |
| 114 | + // ipfs.dag.resolve(cidCbor, '/', (err, result) => { |
| 115 | + expect(err).to.not.exist |
| 116 | + |
| 117 | + console.log('get the node') |
| 118 | + |
| 119 | + dagCBOR.util.cid(result, (err, cid) => { |
| 120 | + expect(err).to.not.exist |
| 121 | + expect(cid).to.eql(cidCbor) |
| 122 | + done() |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('dag-cbor local scope', (done) => { |
| 128 | + ipfs.dag.resolve(cidCbor, 'someData', (err, result) => { |
| 129 | + expect(err).to.not.exist |
| 130 | + expect(result).to.eql('I am inside a Cbor object') |
| 131 | + done() |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + it.skip('dag-cbor one level', (done) => {}) |
| 136 | + it.skip('dag-cbor two levels', (done) => {}) |
| 137 | + it.skip('from dag-pb to dag-cbor', (done) => {}) |
| 138 | + |
| 139 | + it('from dag-cbor to dag-pb', (done) => { |
| 140 | + ipfs.dag.resolve(cidCbor, 'pb/data', (err, result) => { |
| 141 | + expect(err).to.not.exist |
| 142 | + expect(result).to.eql(new Buffer('I am inside a Protobuf')) |
| 143 | + done() |
| 144 | + }) |
| 145 | + }) |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + describe('promise API', () => { |
| 150 | + describe('.resolve', () => {}) |
| 151 | + }) |
| 152 | + }) |
| 153 | +} |
0 commit comments