require('spec.helpers') local config = require('pending.config') local util = require('pending.sync.util') describe('sync util', function() before_each(function() config.reset() end) after_each(function() config.reset() end) describe('fmt_counts', function() it('returns nothing to do for empty counts', function() assert.equals('nothing to do', util.fmt_counts({})) end) it('returns nothing to do when all zero', function() assert.equals('nothing to do', util.fmt_counts({ { 0, 'added' }, { 0, 'failed' } })) end) it('formats single non-zero count', function() assert.equals('3 added', util.fmt_counts({ { 3, 'added' }, { 0, 'failed' } })) end) it('joins multiple non-zero counts with pipe', function() local result = util.fmt_counts({ { 2, 'added' }, { 1, 'updated' }, { 0, 'failed' } }) assert.equals('2 added | 1 updated', result) end) end) describe('with_guard', function() it('prevents concurrent calls', function() local inner_called = false local blocked = false local msgs = {} local orig = vim.notify vim.notify = function(m, level) if level == vim.log.levels.WARN then table.insert(msgs, m) end end util.with_guard('test', function() inner_called = true util.with_guard('test2', function() blocked = true end) end) vim.notify = orig assert.is_true(inner_called) assert.is_false(blocked) assert.equals(1, #msgs) assert.truthy(msgs[1]:find('Sync already in progress')) end) it('clears guard after error', function() pcall(util.with_guard, 'err-test', function() error('boom') end) assert.is_false(util.sync_in_flight()) end) it('clears guard after success', function() util.with_guard('ok-test', function() end) assert.is_false(util.sync_in_flight()) end) end) describe('finish', function() it('calls save and recompute', function() local helpers = require('spec.helpers') local store_mod = require('pending.store') local tmpdir = helpers.tmpdir() vim.g.pending = { data_path = tmpdir .. '/tasks.json' } config.reset() package.loaded['pending'] = nil local s = store_mod.new(tmpdir .. '/tasks.json') s:load() s:add({ description = 'Test', status = 'pending', category = 'Work', priority = 0 }) util.finish(s) local reloaded = store_mod.new(tmpdir .. '/tasks.json') reloaded:load() assert.equals(1, #reloaded:tasks()) vim.fn.delete(tmpdir, 'rf') vim.g.pending = nil config.reset() package.loaded['pending'] = nil end) end) end)