diff --git a/test/files.spec.js b/test/files.spec.js index 5fb8bb3c3..5c5e707a6 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -49,7 +49,7 @@ describe('.files (the MFS API part)', function () { after((done) => ipfsd.stop(done)) - describe('Callback API', function () { + describe('the API', function () { this.timeout(120 * 1000) it('add file for testing', (done) => { @@ -63,6 +63,21 @@ describe('.files (the MFS API part)', function () { }) }) + + it('file.add with NPM Buffer', (done) => { + let Buffer = require('buffer').Buffer + let expectedBufferMultihash = 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' + let file = Buffer.from('hello') + + ipfs.files.add(file, (err, res) => { + expect(err).to.not.exist() + + expect(res).to.have.length(1) + expect(res[0].hash).to.equal(expectedBufferMultihash) + expect(res[0].path).to.equal(expectedBufferMultihash) + }) + }) + it('files.add with empty path and buffer content', (done) => { const expectedHash = 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' const content = Buffer.from('hello') @@ -308,140 +323,4 @@ describe('.files (the MFS API part)', function () { ipfs.files.rm('/test-folder', {recursive: true}, done) }) }) - - describe('Promise API', function () { - this.timeout(120 * 1000) - - it('files.add', () => { - return ipfs.files.add(testfile) - .then((res) => { - expect(res).to.have.length(1) - expect(res[0].hash).to.equal(expectedMultihash) - expect(res[0].path).to.equal(expectedMultihash) - }) - }) - - it('files.add with cid-version=1 and raw-leaves=false', () => { - const expectedHash = 'zdj7Wh9x6gXdg4UAqhRYnjBTw9eJF7hvzUU4HjpnZXHYQz9jK' - const options = { 'cid-version': 1, 'raw-leaves': false } - - return ipfs.files.add(testfile, options) - .then((res) => { - expect(res).to.have.length(1) - expect(res[0].hash).to.equal(expectedHash) - expect(res[0].path).to.equal(expectedHash) - }) - }) - - it('files.add with options', () => { - return ipfs.files.add(testfile, { pin: false }) - .then((res) => { - expect(res).to.have.length(1) - expect(res[0].hash).to.equal(expectedMultihash) - expect(res[0].path).to.equal(expectedMultihash) - }) - }) - - HASH_ALGS.forEach((name) => { - it(`files.add with hash=${name} and raw-leaves=false`, () => { - const content = String(Math.random() + Date.now()) - const file = { - path: content + '.txt', - content: Buffer.from(content) - } - const options = { hash: name, 'raw-leaves': false } - - return ipfs.files.add([file], options) - .then((res) => { - expect(res).to.have.length(1) - const cid = new CID(res[0].hash) - expect(mh.decode(cid.multihash).name).to.equal(name) - }) - }) - }) - - it('files.mkdir', () => { - return ipfs.files.mkdir('/test-folder') - }) - - it('files.cp', () => { - return ipfs.files - .cp([ - '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', - '/test-folder/test-file' - ]) - }) - - it('files.ls', () => { - return ipfs.files.ls('/test-folder') - .then((res) => { - expect(res.length).to.equal(1) - }) - }) - - it('files.write', (done) => { - ipfs.files - .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), {create: true}) - .then(() => { - return ipfs.files.read('/test-folder/test-file-2.txt') - }) - .then((buf) => { - expect(buf.toString()).to.be.equal('hello world') - done() - }) - .catch(done) - }) - - it('files.write without options', (done) => { - ipfs.files - .write('/test-folder/test-file-2.txt', Buffer.from('hello world')) - .then(() => { - return ipfs.files.read('/test-folder/test-file-2.txt') - }) - .then((buf) => { - expect(buf.toString()).to.be.equal('hello world') - done() - }) - .catch(done) - }) - - it('files.stat', () => { - return ipfs.files.stat('/test-folder/test-file') - .then((res) => { - expect(res).to.deep.equal({ - hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', - size: 12, - cumulativeSize: 20, - blocks: 0, - type: 'file' - }) - }) - }) - - it('files.stat file that does not exist()', () => { - return ipfs.files.stat('/test-folder/does-not-exist()') - .catch((err) => { - expect(err).to.exist() - expect(err.code).to.be.eql(0) - }) - }) - - it('files.read', (done) => { - if (!isNode) { return done() } - - ipfs.files.read('/test-folder/test-file') - .then((buf) => { - expect(Buffer.from(buf)).to.eql(testfile) - done() - }) - }) - - it('files.rm without options', () => { - return ipfs.files.rm('/test-folder/test-file-2.txt') - }) - - it('files.rm', () => { - return ipfs.files.rm('/test-folder', { recursive: true }) - }) - }) })