pending.nvim/spec/archive_spec.lua
Barrett Ruth 0e0568769d
refactor: organize tests and dry (#49)
* refactor(store): convert singleton to Store.new() factory

Problem: store.lua used module-level _data singleton, making
project-local stores impossible and creating hidden global state.

Solution: introduce Store metatable with all operations as instance
methods. M.new(path) constructs an instance; M.resolve_path()
searches upward for .pending.json and falls back to
config.get().data_path. Singleton module API is removed.

* refactor(diff): accept store instance as parameter

Problem: diff.apply called store singleton methods directly, coupling
it to global state and preventing use with project-local stores.

Solution: change signature to apply(lines, s, hidden_ids?) where s is
a pending.Store instance. All store operations now go through s.

* refactor(buffer): add set_store/store accessors, drop singleton dep

Problem: buffer.lua imported store directly and called singleton
methods, preventing it from working with per-project store instances.

Solution: add module-level _store, M.set_store(s), and M.store()
accessors. open() and render() use _store instead of the singleton.
init.lua will call buffer.set_store(s) before buffer.open().

* refactor(complete,health,sync,plugin): update callers to store instance API

Problem: complete.lua, health.lua, sync/gcal.lua, and plugin/pending.lua
all called singleton store methods directly.

Solution: complete.lua uses buffer.store() for category lookups;
health.lua uses store.new(store.resolve_path()) and reports the
resolved path; gcal.lua calls require('pending').store() for task
access; plugin tab-completion creates ephemeral store instances via
store.new(store.resolve_path()). Add 'init' to the subcommands list.

* feat(init): thread Store instance through init, add :Pending init

Problem: init.lua called singleton store methods throughout, and there
was no way to create a project-local .pending.json file.

Solution: add module-level _store and private get_store() that
lazy-constructs via store.new(store.resolve_path()). Add public
M.store() accessor used by specs and sync backends. M.open() calls
buffer.set_store(get_store()) before buffer.open(). All store
callsites converted to get_store():method(). goto_file() and
add_here() derive the data directory from get_store().path.

Add M.init() which creates .pending.json in cwd and dispatches from
M.command() as ':Pending init'.

* test: update all specs for Store instance API

Problem: every spec used the old singleton API (store.unload(),
store.load(), store.add(), etc.) and diff.apply(lines, hidden).

Solution: lower-level specs (store, diff, views, complete, file) use
s = store.new(path); s:load() directly. Higher-level specs (archive,
edit, filter, status, sync) reset package.loaded['pending'] in
before_each and use pending.store() to access the live instance.
diff.apply calls updated to diff.apply(lines, s, hidden_ids).

* docs(pending): document :Pending init and store resolution

Add *pending-store-resolution* section explaining upward .pending.json
discovery and fallback to the global data_path. Document :Pending init
under COMMANDS. Add a cross-reference from the data_path config field.

* ci: format

* ci: remove unused variable
2026-02-26 20:03:42 -05:00

150 lines
4.8 KiB
Lua

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)