test(core): add unit tests for the relationship/action mapping, e.g. “photo:large”

pull/118/head
Romain 6 years ago
parent 832673f322
commit 79dd36f77b

@ -0,0 +1,33 @@
const downsize = require('thumbsup-downsize')
const fs = require('fs-extra')
exports.createMap = function (opts) {
const thumbSize = opts.thumbSize || 120
const largeSize = opts.largeSize || 1000
const defaultOptions = {
quality: opts.photoQuality,
args: opts.gmArgs
}
const watermark = (!opts.watermark) ? null : {
file: opts.watermark,
position: opts.watermarkPosition
}
const thumbnail = Object.assign({}, defaultOptions, {
height: thumbSize,
width: thumbSize
})
const large = Object.assign({}, defaultOptions, {
height: largeSize,
watermark: watermark,
animated: true
})
return {
'fs:copy': (task, done) => fs.copy(task.src, task.dest, done),
'fs:symlink': (task, done) => fs.symlink(task.src, task.dest, done),
'photo:thumbnail': (task, done) => downsize.image(task.src, task.dest, thumbnail, done),
'photo:large': (task, done) => downsize.image(task.src, task.dest, large, done),
'video:thumbnail': (task, done) => downsize.still(task.src, task.dest, thumbnail, done),
'video:poster': (task, done) => downsize.still(task.src, task.dest, large, done),
'video:resized': (task, done) => downsize.video(task.src, task.dest, {}, done)
}
}

@ -1,10 +1,10 @@
const debug = require('debug')('thumbsup:debug')
const error = require('debug')('thumbsup:error')
const downsize = require('thumbsup-downsize')
const fs = require('fs-extra')
const info = require('debug')('thumbsup:info')
const ListrWorkQueue = require('listr-work-queue')
const path = require('path')
const actions = require('./actions')
exports.run = function (files, problems, opts, parentTask) {
const jobs = exports.create(files, opts, problems)
@ -26,7 +26,7 @@ exports.run = function (files, problems, opts, parentTask) {
exports.create = function (files, opts, problems) {
const tasks = {}
const sourceFiles = new Set()
const actionMap = getActionMap(opts)
const actionMap = actions.createMap(opts)
// accumulate all tasks into an object
// to remove duplicate destinations
files.forEach(f => {
@ -94,34 +94,3 @@ function modifiedDate (filepath) {
return 0
}
}
function getActionMap (opts) {
const thumbSize = opts.thumbSize || 120
const largeSize = opts.largeSize || 1000
const defaultOptions = {
quality: opts.photoQuality,
args: opts.gmArgs
}
const watermark = (!opts.watermark) ? null : {
file: opts.watermark,
position: opts.watermarkPosition
}
const thumbnail = Object.assign({}, defaultOptions, {
height: thumbSize,
width: thumbSize
})
const large = Object.assign({}, defaultOptions, {
height: largeSize,
watermark: watermark,
animated: true
})
return {
'fs:copy': (task, done) => fs.copy(task.src, task.dest, done),
'fs:symlink': (task, done) => fs.symlink(task.src, task.dest, done),
'photo:thumbnail': (task, done) => downsize.image(task.src, task.dest, thumbnail, done),
'photo:large': (task, done) => downsize.image(task.src, task.dest, large, done),
'video:thumbnail': (task, done) => downsize.still(task.src, task.dest, thumbnail, done),
'video:poster': (task, done) => downsize.still(task.src, task.dest, large, done),
'video:resized': (task, done) => downsize.video(task.src, task.dest, {}, done)
}
}

@ -0,0 +1,158 @@
const downsize = require('thumbsup-downsize')
const fs = require('fs-extra')
const should = require('should/as-function')
const sinon = require('sinon')
const actions = require('../../src/steps/actions')
const ANY_TASK = {
src: 'input/IMG_0001.jpg',
dest: 'output/media/IMG_0001.jpg'
}
describe('actions', () => {
beforeEach(() => {
sinon.stub(fs, 'copy').yields(null)
sinon.stub(fs, 'symlink').yields(null)
sinon.stub(downsize, 'image').yields(null)
sinon.stub(downsize, 'video').yields(null)
sinon.stub(downsize, 'still').yields(null)
})
afterEach(() => {
fs.copy.restore()
fs.symlink.restore()
downsize.image.restore()
downsize.video.restore()
downsize.still.restore()
})
it('fs:copy = copies the original', testEnd => {
const map = actions.createMap({})
const action = map['fs:copy']
action(ANY_TASK, err => {
should(err).eql(null)
sinon.assert.calledWith(fs.copy,
ANY_TASK.src,
ANY_TASK.dest,
sinon.match.func
)
testEnd()
})
})
it('fs:symlink = creates a symbolic link', testEnd => {
const map = actions.createMap({})
const action = map['fs:symlink']
action(ANY_TASK, err => {
should(err).eql(null)
sinon.assert.calledWith(fs.symlink,
ANY_TASK.src,
ANY_TASK.dest,
sinon.match.func
)
testEnd()
})
})
it('fs:link = does nothing', testEnd => {
const map = actions.createMap({})
const action = map['fs:link']
should(action).undefined()
testEnd()
})
it('photo:thumbnail = creates a square thumbnail', testEnd => {
const map = actions.createMap({thumbSize: 200})
const action = map['photo:thumbnail']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.image)
should(downsizeArgs).property('height', 200)
should(downsizeArgs).property('width', 200)
should(downsizeArgs.animated).undefined() // don't animate GIF thumbnails
testEnd()
})
})
it('photo:large = creates a large image', testEnd => {
const map = actions.createMap({largeSize: 1000})
const action = map['photo:large']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.image)
should(downsizeArgs).property('height', 1000)
should(downsizeArgs).property('animated', true)
testEnd()
})
})
it('video:thumbnail = creates a square video still', testEnd => {
const map = actions.createMap({thumbSize: 200})
const action = map['video:thumbnail']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.still)
should(downsizeArgs).property('height', 200)
should(downsizeArgs).property('width', 200)
testEnd()
})
})
it('video:poster = creates a large video still', testEnd => {
const map = actions.createMap({largeSize: 1000})
const action = map['video:poster']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.still)
should(downsizeArgs).property('height', 1000)
testEnd()
})
})
it('video:resized = creates a web-friendly video', testEnd => {
const map = actions.createMap({})
const action = map['video:resized']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.video)
// video-processing doesn't support any arguments yet
should(downsizeArgs).eql({})
testEnd()
})
})
describe('watermark', () => {
it('can add a watermark to large images', testEnd => {
const map = actions.createMap({watermark: 'copyright.jpg'})
const action = map['photo:large']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.image)
should(downsizeArgs).propertyByPath('watermark', 'file').eql('copyright.jpg')
testEnd()
})
})
it('ignores the watermark for thumbnails', testEnd => {
// it's not supported by <downsize> anyway
const map = actions.createMap({watermark: 'copyright.jpg'})
const action = map['photo:thumbnail']
action(ANY_TASK, err => {
should(err).eql(null)
const downsizeArgs = shouldCallDownsize(downsize.image)
should(downsizeArgs.watermark).undefined()
testEnd()
})
})
})
})
function shouldCallDownsize (fn) {
sinon.assert.calledWith(fn,
ANY_TASK.src,
ANY_TASK.dest,
sinon.match.object,
sinon.match.func
)
return fn.args[0][2]
}
Loading…
Cancel
Save