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
This commit is contained in:
parent
dbd76d6759
commit
41bda24570
19 changed files with 819 additions and 703 deletions
|
|
@ -1,25 +1,21 @@
|
|||
require('spec.helpers')
|
||||
|
||||
local config = require('pending.config')
|
||||
local store = require('pending.store')
|
||||
|
||||
describe('diff', function()
|
||||
local tmpdir
|
||||
local s
|
||||
local diff = require('pending.diff')
|
||||
|
||||
before_each(function()
|
||||
tmpdir = vim.fn.tempname()
|
||||
vim.fn.mkdir(tmpdir, 'p')
|
||||
vim.g.pending = { data_path = tmpdir .. '/tasks.json' }
|
||||
config.reset()
|
||||
store.unload()
|
||||
store.load()
|
||||
s = store.new(tmpdir .. '/tasks.json')
|
||||
s:load()
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
vim.fn.delete(tmpdir, 'rf')
|
||||
vim.g.pending = nil
|
||||
config.reset()
|
||||
end)
|
||||
|
||||
describe('parse_buffer', function()
|
||||
|
|
@ -107,121 +103,112 @@ describe('diff', function()
|
|||
'- [ ] First task',
|
||||
'- [ ] Second task',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local tasks = store.active_tasks()
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local tasks = s:active_tasks()
|
||||
assert.are.equal(2, #tasks)
|
||||
assert.are.equal('First task', tasks[1].description)
|
||||
assert.are.equal('Second task', tasks[2].description)
|
||||
end)
|
||||
|
||||
it('deletes tasks removed from buffer', function()
|
||||
store.add({ description = 'Keep me' })
|
||||
store.add({ description = 'Delete me' })
|
||||
store.save()
|
||||
s:add({ description = 'Keep me' })
|
||||
s:add({ description = 'Delete me' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Keep me',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local active = store.active_tasks()
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local active = s:active_tasks()
|
||||
assert.are.equal(1, #active)
|
||||
assert.are.equal('Keep me', active[1].description)
|
||||
local deleted = store.get(2)
|
||||
local deleted = s:get(2)
|
||||
assert.are.equal('deleted', deleted.status)
|
||||
end)
|
||||
|
||||
it('updates modified tasks', function()
|
||||
store.add({ description = 'Original' })
|
||||
store.save()
|
||||
s:add({ description = 'Original' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Renamed',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal('Renamed', task.description)
|
||||
end)
|
||||
|
||||
it('updates modified when description is renamed', function()
|
||||
local t = store.add({ description = 'Original', category = 'Inbox' })
|
||||
local t = s:add({ description = 'Original', category = 'Inbox' })
|
||||
t.modified = '2020-01-01T00:00:00Z'
|
||||
store.save()
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Renamed',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal('Renamed', task.description)
|
||||
assert.is_not.equal('2020-01-01T00:00:00Z', task.modified)
|
||||
end)
|
||||
|
||||
it('handles duplicate ids as copies', function()
|
||||
store.add({ description = 'Original' })
|
||||
store.save()
|
||||
s:add({ description = 'Original' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Original',
|
||||
'/1/- [ ] Copy of original',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local tasks = store.active_tasks()
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local tasks = s:active_tasks()
|
||||
assert.are.equal(2, #tasks)
|
||||
end)
|
||||
|
||||
it('moves tasks between categories', function()
|
||||
store.add({ description = 'Moving task', category = 'Inbox' })
|
||||
store.save()
|
||||
s:add({ description = 'Moving task', category = 'Inbox' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Work',
|
||||
'/1/- [ ] Moving task',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal('Work', task.category)
|
||||
end)
|
||||
|
||||
it('does not update modified when task is unchanged', function()
|
||||
store.add({ description = 'Stable task', category = 'Inbox' })
|
||||
store.save()
|
||||
s:add({ description = 'Stable task', category = 'Inbox' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Stable task',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local modified_after_first = store.get(1).modified
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local modified_after_first = s:get(1).modified
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal(modified_after_first, task.modified)
|
||||
end)
|
||||
|
||||
it('clears due when removed from buffer line', function()
|
||||
store.add({ description = 'Pay bill', due = '2026-03-15' })
|
||||
store.save()
|
||||
s:add({ description = 'Pay bill', due = '2026-03-15' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Pay bill',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.is_nil(task.due)
|
||||
end)
|
||||
|
||||
|
|
@ -230,39 +217,36 @@ describe('diff', function()
|
|||
'## Inbox',
|
||||
'- [ ] Take out trash rec:weekly',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local tasks = store.active_tasks()
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local tasks = s:active_tasks()
|
||||
assert.are.equal(1, #tasks)
|
||||
assert.are.equal('weekly', tasks[1].recur)
|
||||
end)
|
||||
|
||||
it('updates recur field when changed inline', function()
|
||||
store.add({ description = 'Task', recur = 'daily' })
|
||||
store.save()
|
||||
s:add({ description = 'Task', recur = 'daily' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Todo',
|
||||
'/1/- [ ] Task rec:weekly',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal('weekly', task.recur)
|
||||
end)
|
||||
|
||||
it('clears recur when token removed from line', function()
|
||||
store.add({ description = 'Task', recur = 'daily' })
|
||||
store.save()
|
||||
s:add({ description = 'Task', recur = 'daily' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Todo',
|
||||
'/1/- [ ] Task',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.is_nil(task.recur)
|
||||
end)
|
||||
|
||||
|
|
@ -271,25 +255,23 @@ describe('diff', function()
|
|||
'## Inbox',
|
||||
'- [ ] Water plants rec:!weekly',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local tasks = store.active_tasks()
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local tasks = s:active_tasks()
|
||||
assert.are.equal('weekly', tasks[1].recur)
|
||||
assert.are.equal('completion', tasks[1].recur_mode)
|
||||
end)
|
||||
|
||||
it('clears priority when [N] is removed from buffer line', function()
|
||||
store.add({ description = 'Task name', priority = 1 })
|
||||
store.save()
|
||||
s:add({ description = 'Task name', priority = 1 })
|
||||
s:save()
|
||||
local lines = {
|
||||
'## Inbox',
|
||||
'/1/- [ ] Task name',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local task = store.get(1)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local task = s:get(1)
|
||||
assert.are.equal(0, task.priority)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue