require('spec.helpers') local config = require('pending.config') describe('archive', function() local tmpdir local pending before_each(function() tmpdir = vim.fn.tempname() vim.fn.mkdir(tmpdir, 'p') vim.g.pending = { data_path = tmpdir .. '/tasks.json' } config.reset() package.loaded['pending'] = nil pending = require('pending') pending.store():load() end) after_each(function() vim.fn.delete(tmpdir, 'rf') vim.g.pending = nil config.reset() package.loaded['pending'] = nil end) it('removes done tasks completed more than 30 days ago', function() local s = pending.store() local t = s:add({ description = 'Old done task' }) s:update(t.id, { status = 'done', ['end'] = '2020-01-01T00:00:00Z' }) pending.archive() assert.are.equal(0, #s:active_tasks()) end) it('keeps done tasks completed fewer than 30 days ago', function() local s = pending.store() local recent_end = os.date('!%Y-%m-%dT%H:%M:%SZ', os.time() - (5 * 86400)) local t = s:add({ description = 'Recent done task' }) s:update(t.id, { status = 'done', ['end'] = recent_end }) pending.archive() local active = s:active_tasks() assert.are.equal(1, #active) assert.are.equal('Recent done task', active[1].description) end) it('respects a custom day count', function() local s = pending.store() local eight_days_ago = os.date('!%Y-%m-%dT%H:%M:%SZ', os.time() - (8 * 86400)) local t = s:add({ description = 'Old for 7 days' }) s:update(t.id, { status = 'done', ['end'] = eight_days_ago }) pending.archive(7) assert.are.equal(0, #s:active_tasks()) end) it('keeps tasks within the custom day cutoff', function() local s = pending.store() local five_days_ago = os.date('!%Y-%m-%dT%H:%M:%SZ', os.time() - (5 * 86400)) local t = s:add({ description = 'Recent for 7 days' }) s:update(t.id, { status = 'done', ['end'] = five_days_ago }) pending.archive(7) local active = s:active_tasks() assert.are.equal(1, #active) end) it('never archives pending tasks regardless of age', function() local s = pending.store() s:add({ description = 'Still pending' }) pending.archive() local active = s:active_tasks() assert.are.equal(1, #active) assert.are.equal('pending', active[1].status) end) it('removes deleted tasks past the cutoff', function() local s = pending.store() local t = s:add({ description = 'Old deleted task' }) s:update(t.id, { status = 'deleted', ['end'] = '2020-01-01T00:00:00Z' }) pending.archive() local all = s:tasks() assert.are.equal(0, #all) end) it('keeps deleted tasks within the cutoff', function() local s = pending.store() local recent_end = os.date('!%Y-%m-%dT%H:%M:%SZ', os.time() - (5 * 86400)) local t = s:add({ description = 'Recent deleted' }) s:update(t.id, { status = 'deleted', ['end'] = recent_end }) pending.archive() local all = s:tasks() assert.are.equal(1, #all) end) it('reports the correct count in vim.notify', function() local s = pending.store() local messages = {} local orig_notify = vim.notify vim.notify = function(msg, ...) table.insert(messages, msg) return orig_notify(msg, ...) end local t1 = s:add({ description = 'Old 1' }) local t2 = s:add({ description = 'Old 2' }) s:add({ description = 'Keep' }) s:update(t1.id, { status = 'done', ['end'] = '2020-01-01T00:00:00Z' }) s:update(t2.id, { status = 'done', ['end'] = '2020-01-01T00:00:00Z' }) pending.archive() vim.notify = orig_notify local found = false for _, msg in ipairs(messages) do if msg:find('Archived 2') then found = true break end end assert.is_true(found) end) it('leaves only kept tasks in store.active_tasks after archive', function() local s = pending.store() local t1 = s:add({ description = 'Old done' }) s:add({ description = 'Keep pending' }) local recent_end = os.date('!%Y-%m-%dT%H:%M:%SZ', os.time() - (5 * 86400)) local t3 = s:add({ description = 'Keep recent done' }) s:update(t1.id, { status = 'done', ['end'] = '2020-01-01T00:00:00Z' }) s:update(t3.id, { status = 'done', ['end'] = recent_end }) pending.archive() local active = s:active_tasks() assert.are.equal(2, #active) local descs = {} for _, task in ipairs(active) do descs[task.description] = true end assert.is_true(descs['Keep pending']) assert.is_true(descs['Keep recent done']) end) it('persists archived tasks to disk after unload/reload', function() local s = pending.store() local t = s:add({ description = 'Archived task' }) s:update(t.id, { status = 'done', ['end'] = '2020-01-01T00:00:00Z' }) pending.archive() s:load() assert.are.equal(0, #s:active_tasks()) end) end)