repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
neovim/tests/bugabinga/image/raster_spec.lua
Rawdescribe('bugabinga.image.raster', function()
local raster
local uv = vim.uv
local root = vim.fn.tempname()
local original_stdpath
local original_system
local serial = 0
local function be32(value)
return string.char(
math.floor(value / 16777216) % 256,
math.floor(value / 65536) % 256,
math.floor(value / 256) % 256,
value % 256
)
end
local function png(width, height)
return '\137PNG\r\n\26\n' .. '\0\0\0\13IHDR' .. be32(width) .. be32(height) .. string.rep('\0', 5)
end
local function save(path, value)
local file = assert(io.open(path, 'wb'))
file:write(value)
file:close()
end
before_each(function()
vim.fn.mkdir(root, 'p')
original_stdpath = vim.fn.stdpath
original_system = vim.system
vim.fn.stdpath = function(name)
if name == 'cache' then
return vim.fs.joinpath(root, 'cache')
end
return original_stdpath(name)
end
package.loaded['bugabinga.image.raster'] = nil
raster = require 'bugabinga.image.raster'
end)
after_each(function()
vim.fn.stdpath = original_stdpath
vim.system = original_system
vim.fn.delete(root, 'rf')
end)
local function source(extension, bytes)
serial = serial + 1
local path = vim.fs.joinpath(root, tostring(serial) .. '.' .. extension)
save(path, bytes or 'source image')
return path
end
it('reads valid PNG dimensions', function()
local path = source('png', png(32, 17))
assert.same({ 32, 17 }, { raster.dimensions(path) })
end)
it('rejects malformed PNG headers and dimensions', function()
local bad_header = source('png', 'not a png')
local bad_size = source('png', png(0, 17))
assert.is_nil(raster.dimensions(bad_header))
assert.is_nil(raster.dimensions(bad_size))
end)
it('converts first frame with argv and reuses validated cache', function()
local path = source('gif')
local calls = 0
vim.system = function(args, options, callback)
calls = calls + 1
assert.equal('ffmpeg', args[1])
assert.equal(30000, options.timeout)
for i, value in ipairs(args) do
if value == '-frames:v' then assert.equal('1', args[i + 1]) end
if value == '-i' then assert.equal(path, args[i + 1]) end
end
assert.is_true(vim.tbl_contains(args, '-frames:v'))
assert.is_true(options.text)
save(args[#args], png(8, 6))
callback({ code = 0, stderr = '' })
return { kill = function() end }
end
local first
raster.prepare(path, function(err, result)
assert.is_nil(err)
first = result
end)
vim.wait(100, function() return first ~= nil end)
assert.equal(8, first.width)
assert.equal(6, first.height)
local second
raster.prepare(path, function(err, result)
assert.is_nil(err)
second = result
end)
vim.wait(100, function() return second ~= nil end)
assert.equal(first.path, second.path)
assert.equal(1, calls)
end)
it('prepares PNG directly without launching a converter', function()
local path = source('PNG', png(16, 9))
vim.system = function() error('PNG must not launch a converter') end
local output
raster.prepare(path, function(err, result) assert.is_nil(err); output = result end)
assert.is_true(vim.wait(100, function() return output ~= nil end))
assert.equal(path, output.path)
end)
it('discards a conversion if its source changes before completion', function()
local path = source('webp')
local complete, partial
vim.system = function(args, _, callback)
partial, complete = args[#args], callback
return { kill = function() end }
end
local failure
raster.prepare(path, function(err) failure = err end)
save(path, 'source changed while ffmpeg was running')
save(partial, png(8, 6))
complete({ code = 0, stderr = '' })
assert.is_true(vim.wait(100, function() return failure ~= nil end))
assert.matches('source changed', failure)
assert.is_nil(uv.fs_stat(partial))
end)
it('invalidates cache when source fingerprint changes', function()
local path = source('jpg')
local calls = 0
vim.system = function(args, _, callback)
calls = calls + 1
save(args[#args], png(4, 3))
callback({ code = 0, stderr = '' })
return { kill = function() end }
end
local function run()
local result
raster.prepare(path, function(_, value) result = value end)
vim.wait(100, function() return result ~= nil end)
end
run()
save(path, 'changed source with another size')
run()
assert.equal(2, calls)
end)
it('reports converter failure and suppresses cancelled callbacks', function()
local path = source('webp')
vim.system = function(_, _, callback)
callback({ code = 1, stderr = 'conversion failed' })
return { kill = function() end }
end
local failure
raster.prepare(path, function(err) failure = err end)
vim.wait(100, function() return failure ~= nil end)
assert.matches('conversion failed', failure)
local called = false
local cancel_path = source('jpeg')
vim.system = function(_, _, callback)
vim.schedule(function() callback({ code = 0, stderr = '' }) end)
return { kill = function() end }
end
local cancel = raster.prepare(cancel_path, function() called = true end)
cancel()
vim.wait(30)
assert.is_false(called)
end)
end)