|
| 1 | +/* eslint-env mocha */ |
| 2 | +/* eslint max-nested-callbacks: ["error", 8] */ |
| 3 | + |
| 4 | +'use strict' |
| 5 | + |
| 6 | +const expect = require('chai').expect |
| 7 | +const fs = require('fs') |
| 8 | +const path = require('path') |
| 9 | + |
| 10 | +const testfile = fs.readFileSync(path.join(__dirname, './data/testfile.txt')) |
| 11 | + |
| 12 | +module.exports = (common) => { |
| 13 | + describe('.pin', () => { |
| 14 | + let ipfs |
| 15 | + |
| 16 | + before(function (done) { |
| 17 | + // CI takes longer to instantiate the daemon, |
| 18 | + // so we need to increase the timeout for the |
| 19 | + // before step |
| 20 | + this.timeout(20 * 1000) |
| 21 | + |
| 22 | + common.setup((err, factory) => { |
| 23 | + expect(err).to.not.exist |
| 24 | + factory.spawnNode((err, node) => { |
| 25 | + expect(err).to.not.exist |
| 26 | + ipfs = node |
| 27 | + populate() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + function populate () { |
| 32 | + const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 33 | + |
| 34 | + ipfs.files.add(testfile, (err, res) => { |
| 35 | + expect(err).to.not.exist |
| 36 | + |
| 37 | + expect(res).to.have.length(1) |
| 38 | + expect(res[0].hash).to.equal(expectedMultihash) |
| 39 | + expect(res[0].path).to.equal(expectedMultihash) |
| 40 | + done() |
| 41 | + }) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + after((done) => { |
| 46 | + common.teardown(done) |
| 47 | + }) |
| 48 | + |
| 49 | + describe('callback API', () => { |
| 50 | + // 1st, because ipfs.files.add pins automatically |
| 51 | + it('.rm', (done) => { |
| 52 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 53 | + |
| 54 | + ipfs.pin.rm(hash, { recursive: true }, (err, pinset) => { |
| 55 | + expect(err).to.not.exist |
| 56 | + expect(pinset).to.exist |
| 57 | + ipfs.pin.ls({ type: 'direct' }, (err, pinset) => { |
| 58 | + expect(err).to.not.exist |
| 59 | + expect(pinset).to.be.empty |
| 60 | + done() |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it('.add', (done) => { |
| 66 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 67 | + |
| 68 | + ipfs.pin.add(hash, { recursive: false }, (err, pinset) => { |
| 69 | + expect(err).to.not.exist |
| 70 | + expect(pinset[0]).to.be.equal(hash) |
| 71 | + done() |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('.ls', (done) => { |
| 76 | + ipfs.pin.ls((err, pinset) => { |
| 77 | + expect(err).to.not.exist |
| 78 | + expect(Object.keys(pinset).length > 0).to.equal(true) |
| 79 | + done() |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('.ls type direct', (done) => { |
| 84 | + ipfs.pin.ls({ type: 'direct' }, (err, pinset) => { |
| 85 | + expect(err).to.not.exist |
| 86 | + expect(Object.keys(pinset).length > 0).to.equal(true) |
| 87 | + done() |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it('.ls type indirect', (done) => { |
| 92 | + ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => { |
| 93 | + expect(err).to.not.exist |
| 94 | + expect(Object.keys(pinset).length > 0).to.equal(true) |
| 95 | + done() |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('.ls type recursive', (done) => { |
| 100 | + ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => { |
| 101 | + expect(err).to.not.exist |
| 102 | + expect(Object.keys(pinset).length > 0).to.equal(true) |
| 103 | + done() |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('.ls for a specific hash', (done) => { |
| 108 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 109 | + |
| 110 | + ipfs.pin.ls(hash, (err, pinset) => { |
| 111 | + expect(err).to.not.exist |
| 112 | + expect(pinset).to.exist |
| 113 | + done() |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('promise API', () => { |
| 119 | + it('.add', () => { |
| 120 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 121 | + |
| 122 | + return ipfs.pin.add(hash, { recursive: false }) |
| 123 | + .then((pinset) => { |
| 124 | + expect(pinset[0]).to.be.equal(hash) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + it('.ls', () => { |
| 129 | + return ipfs.pin.ls() |
| 130 | + .then((pinset) => { |
| 131 | + expect(pinset).to.exist |
| 132 | + expect(Object.keys(pinset).length > 0).to.equal(true) |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + it('.ls hash', () => { |
| 137 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 138 | + |
| 139 | + return ipfs.pin.ls(hash) |
| 140 | + .then((pinset) => { |
| 141 | + expect(pinset).to.exist |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + it('.rm', () => { |
| 146 | + const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' |
| 147 | + |
| 148 | + return ipfs.pin.rm(hash, { recursive: false }) |
| 149 | + .then((pinset) => { |
| 150 | + return ipfs.pin.ls({ type: 'direct' }) |
| 151 | + }) |
| 152 | + .then((pinset) => { |
| 153 | + expect(pinset).to.be.empty |
| 154 | + }) |
| 155 | + }) |
| 156 | + }) |
| 157 | + }) |
| 158 | +} |
0 commit comments