nodestream-transform-checksum
Version:
Checksum calculator for Nodestream
130 lines (94 loc) • 3.42 kB
JavaScript
/**
* Nodestream - Checksum Transform
*
* @author Robert Rossmann <robert.rossmann@me.com>
* @copyright 2016 Robert Rossmann
* @license BSD-3-Clause
*/
const expect = require('chai').expect
const stream = require('stream')
const Checksum = require('../lib/checksum')
describe('Class: Checksum', function() {
let checksum
beforeEach(function() {
checksum = new Checksum({})
})
it('should exist', function() {
expect(Checksum).to.be.a('function')
})
it("should have identity 'checksum'", function() {
expect(Checksum.identity).to.equal('checksum')
})
describe('.transform()', function() {
it('should exist', function() {
expect(checksum).to.have.property('transform')
expect(checksum.transform).to.be.a('function')
})
it('should return a new stream', function() {
const file = new stream.PassThrough()
const transformed = checksum.transform(file)
expect(transformed).to.be.instanceof(stream)
expect(transformed).to.not.equal(file)
})
})
describe('.results()', function() {
it('should exist', function() {
expect(checksum).to.have.property('results')
expect(checksum.results).to.be.a('function')
})
it('should default to md5 if no algorithm is provided', function(done) {
const file = new stream.PassThrough()
const transformed = checksum.transform(file)
file.end('hello world')
// Allow the pipes to update downstream targets
transformed.on('finish', () => {
const results = checksum.results()
expect(results).to.be.an('object')
expect(results.algorithm).to.equal('md5')
return done()
})
})
it('should return object with algorithm and value', function(done) {
const file = new stream.PassThrough()
const transformed = checksum.transform(file)
file.end('hello world')
// Allow the pipes to update downstream targets
transformed.on('finish', () => {
const results = checksum.results()
expect(results).to.be.an('object')
// Calculated by external utility
expect(results.value).to.equal('5eb63bbbe01eeed093cb22bb8f5acdc3')
return done()
})
})
it('should allow algorithm selection', function(done) {
checksum = new Checksum({ algorithm: 'sha1' })
const file = new stream.PassThrough()
const transformed = checksum.transform(file)
file.end('hello world')
// Allow the pipes to update downstream targets
transformed.on('finish', () => {
const results = checksum.results()
expect(results).to.be.an('object')
expect(results.algorithm).to.equal('sha1')
// Calculated by external utility
expect(results.value).to.equal('2aae6c35c94fcfb415dbe95f408b9ce91ee846ed')
return done()
})
})
it('should return raw buffer if asked for', function(done) {
checksum = new Checksum({ buffer: true })
const file = new stream.PassThrough()
const transformed = checksum.transform(file)
file.end('hello world')
// Allow the pipes to update downstream targets
transformed.on('finish', () => {
const results = checksum.results()
expect(results).to.be.an('object')
expect(results.value).to.be.instanceof(Buffer)
return done()
})
})
})
})