diff --git a/src/pin/ls.js b/src/pin/ls.js index c9542071..691350e1 100644 --- a/src/pin/ls.js +++ b/src/pin/ls.js @@ -31,9 +31,17 @@ module.exports = (createCommon, options) => { function populate () { series([ - cb => ipfs.add(fixtures.files[0].data, { pin: false }, cb), + // two files wrapped in directories, only root CID pinned recursively + cb => { + const dir = fixtures.directory.files.map((file) => ({ path: file.path, content: file.data })) + ipfs.add(dir, { pin: false, cidVersion: 0 }, cb) + }, + cb => ipfs.pin.add(fixtures.directory.cid, { recursive: true }, cb), + // a file (CID pinned recursively) + cb => ipfs.add(fixtures.files[0].data, { pin: false, cidVersion: 0 }, cb), cb => ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb), - cb => ipfs.add(fixtures.files[1].data, { pin: false }, cb), + // a single CID (pinned directly) + cb => ipfs.add(fixtures.files[1].data, { pin: false, cidVersion: 0 }, cb), cb => ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb) ], done) } @@ -42,21 +50,24 @@ module.exports = (createCommon, options) => { after((done) => common.teardown(done)) // 1st, because ipfs.add pins automatically - it('should list recursive pins', (done) => { + it('should list all recursive pins', (done) => { ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => { expect(err).to.not.exist() expect(pinset).to.deep.include({ type: 'recursive', hash: fixtures.files[0].cid }) + expect(pinset).to.deep.include({ + type: 'recursive', + hash: fixtures.directory.cid + }) done() }) }) - it('should list indirect pins', (done) => { + it('should list all indirect pins', (done) => { ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => { expect(err).to.not.exist() - // because the pinned files have no links expect(pinset).to.not.deep.include({ type: 'recursive', hash: fixtures.files[0].cid @@ -65,14 +76,31 @@ module.exports = (createCommon, options) => { type: 'direct', hash: fixtures.files[1].cid }) + expect(pinset).to.not.deep.include({ + type: 'recursive', + hash: fixtures.directory.cid + }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[0].cid + }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[1].cid + }) done() }) }) - it('should list pins', (done) => { + it('should list all types of pins', (done) => { ipfs.pin.ls((err, pinset) => { expect(err).to.not.exist() expect(pinset).to.not.be.empty() + // check the three "roots" + expect(pinset).to.deep.include({ + type: 'recursive', + hash: fixtures.directory.cid + }) expect(pinset).to.deep.include({ type: 'recursive', hash: fixtures.files[0].cid @@ -81,13 +109,27 @@ module.exports = (createCommon, options) => { type: 'direct', hash: fixtures.files[1].cid }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[0].cid + }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[1].cid + }) done() }) }) - it('should list pins (promised)', () => { + it('should list all types of pins (promised)', () => { return ipfs.pin.ls() .then((pinset) => { + expect(pinset).to.not.be.empty() + // check our three "roots" + expect(pinset).to.deep.include({ + type: 'recursive', + hash: fixtures.directory.cid + }) expect(pinset).to.deep.include({ type: 'recursive', hash: fixtures.files[0].cid @@ -96,12 +138,21 @@ module.exports = (createCommon, options) => { type: 'direct', hash: fixtures.files[1].cid }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[0].cid + }) + expect(pinset).to.deep.include({ + type: 'indirect', + hash: fixtures.directory.files[1].cid + }) }) }) - it('should list direct pins', (done) => { + it('should list all direct pins', (done) => { ipfs.pin.ls({ type: 'direct' }, (err, pinset) => { expect(err).to.not.exist() + expect(pinset).to.have.lengthOf(1) expect(pinset).to.deep.include({ type: 'direct', hash: fixtures.files[1].cid @@ -130,5 +181,45 @@ module.exports = (createCommon, options) => { }]) }) }) + + it('should throw an error on missing direct pins for existing path', (done) => { + // ipfs.txt is an indirect pin, so lookup for direct one should throw an error + ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'direct' }, (err, pinset) => { + expect(err).to.exist() + expect(pinset).to.not.exist() + expect(err.message).to.be.equal(`path '/ipfs/${fixtures.directory.cid}/files/ipfs.txt' is not pinned`) + done() + }) + }) + + it('should throw an error on missing link for a specific path', (done) => { + ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/I-DONT-EXIST.txt`, { type: 'direct' }, (err, pinset) => { + expect(err).to.exist() + expect(pinset).to.not.exist() + expect(err.message).to.be.equal(`no link named "I-DONT-EXIST.txt" under ${fixtures.directory.cid}`) + done() + }) + }) + + it('should list indirect pins for a specific path', (done) => { + ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'indirect' }, (err, pinset) => { + expect(err).to.not.exist() + expect(pinset).to.deep.include({ + type: `indirect through ${fixtures.directory.cid}`, + hash: fixtures.directory.files[1].cid + }) + done() + }) + }) + + it('should list recursive pins for a specific hash (promised)', () => { + return ipfs.pin.ls(fixtures.files[0].cid, { type: 'recursive' }) + .then((pinset) => { + expect(pinset).to.deep.equal([{ + type: 'recursive', + hash: fixtures.files[0].cid + }]) + }) + }) }) } diff --git a/src/pin/utils.js b/src/pin/utils.js index 09debd8c..3ee67f0d 100644 --- a/src/pin/utils.js +++ b/src/pin/utils.js @@ -3,6 +3,19 @@ const loadFixture = require('aegir/fixtures') exports.fixtures = Object.freeze({ + // NOTE: files under 'directory' need to be different than standalone ones in 'files' + directory: Object.freeze({ + cid: 'QmY8KdYQSYKFU5hM7F5ioZ5yYSgV5VZ1kDEdqfRL3rFgcd', + files: Object.freeze([Object.freeze({ + path: 'test-folder/ipfs-add.js', + data: loadFixture('test/fixtures/test-folder/ipfs-add.js', 'interface-ipfs-core'), + cid: 'QmbKtKBrmeRHjNCwR4zAfCJdMVu6dgmwk9M9AE9pUM9RgG' + }), Object.freeze({ + path: 'test-folder/files/ipfs.txt', + data: loadFixture('test/fixtures/test-folder/files/ipfs.txt', 'interface-ipfs-core'), + cid: 'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w' + })]) + }), files: Object.freeze([Object.freeze({ data: loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core'), cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'