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
|
|
@ -8,21 +8,25 @@ local views = require('pending.views')
|
|||
|
||||
describe('file token', function()
|
||||
local tmpdir
|
||||
local s
|
||||
|
||||
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()
|
||||
package.loaded['pending'] = nil
|
||||
package.loaded['pending.buffer'] = nil
|
||||
s = store.new(tmpdir .. '/tasks.json')
|
||||
s:load()
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
vim.fn.delete(tmpdir, 'rf')
|
||||
vim.g.pending = nil
|
||||
config.reset()
|
||||
store.unload()
|
||||
package.loaded['pending'] = nil
|
||||
package.loaded['pending.buffer'] = nil
|
||||
end)
|
||||
|
||||
describe('parse.body', function()
|
||||
|
|
@ -78,89 +82,88 @@ describe('file token', function()
|
|||
|
||||
describe('diff reconciliation', function()
|
||||
it('stores file field in _extra on write', function()
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one file:src/auth.lua:42',
|
||||
}
|
||||
diff.apply(lines)
|
||||
local updated = store.get(t.id)
|
||||
diff.apply(lines, s)
|
||||
local updated = s:get(t.id)
|
||||
assert.is_not_nil(updated._extra)
|
||||
assert.are.equal('src/auth.lua:42', updated._extra.file)
|
||||
end)
|
||||
|
||||
it('updates file field when token changes', function()
|
||||
local t = store.add({ description = 'Task one', _extra = { file = 'old.lua:1' } })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one', _extra = { file = 'old.lua:1' } })
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one file:new.lua:99',
|
||||
}
|
||||
diff.apply(lines)
|
||||
local updated = store.get(t.id)
|
||||
diff.apply(lines, s)
|
||||
local updated = s:get(t.id)
|
||||
assert.are.equal('new.lua:99', updated._extra.file)
|
||||
end)
|
||||
|
||||
it('clears file field when token is removed from line', function()
|
||||
local t = store.add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one',
|
||||
}
|
||||
diff.apply(lines)
|
||||
local updated = store.get(t.id)
|
||||
diff.apply(lines, s)
|
||||
local updated = s:get(t.id)
|
||||
assert.is_nil(updated._extra)
|
||||
end)
|
||||
|
||||
it('preserves other _extra fields when file is cleared', function()
|
||||
local t = store.add({
|
||||
local t = s:add({
|
||||
description = 'Task one',
|
||||
_extra = { file = 'src/auth.lua:42', _gcal_event_id = 'abc123' },
|
||||
})
|
||||
store.save()
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one',
|
||||
}
|
||||
diff.apply(lines)
|
||||
local updated = store.get(t.id)
|
||||
diff.apply(lines, s)
|
||||
local updated = s:get(t.id)
|
||||
assert.is_not_nil(updated._extra)
|
||||
assert.is_nil(updated._extra.file)
|
||||
assert.are.equal('abc123', updated._extra._gcal_event_id)
|
||||
end)
|
||||
|
||||
it('round-trips file field through JSON', function()
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one file:src/auth.lua:42',
|
||||
}
|
||||
diff.apply(lines)
|
||||
store.unload()
|
||||
store.load()
|
||||
local loaded = store.get(t.id)
|
||||
diff.apply(lines, s)
|
||||
s:load()
|
||||
local loaded = s:get(t.id)
|
||||
assert.is_not_nil(loaded._extra)
|
||||
assert.are.equal('src/auth.lua:42', loaded._extra.file)
|
||||
end)
|
||||
|
||||
it('accepts optional hidden_ids parameter without error', function()
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
local lines = {
|
||||
'/' .. t.id .. '/- [ ] Task one',
|
||||
}
|
||||
assert.has_no_error(function()
|
||||
diff.apply(lines, {})
|
||||
diff.apply(lines, s, {})
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('LineMeta', function()
|
||||
it('category_view populates file field in LineMeta', function()
|
||||
local t = store.add({
|
||||
local t = s:add({
|
||||
description = 'Task one',
|
||||
_extra = { file = 'src/auth.lua:42' },
|
||||
})
|
||||
store.save()
|
||||
local tasks = store.active_tasks()
|
||||
s:save()
|
||||
local tasks = s:active_tasks()
|
||||
local _, meta = views.category_view(tasks)
|
||||
local task_meta = nil
|
||||
for _, m in ipairs(meta) do
|
||||
|
|
@ -174,12 +177,12 @@ describe('file token', function()
|
|||
end)
|
||||
|
||||
it('priority_view populates file field in LineMeta', function()
|
||||
local t = store.add({
|
||||
local t = s:add({
|
||||
description = 'Task one',
|
||||
_extra = { file = 'src/auth.lua:42' },
|
||||
})
|
||||
store.save()
|
||||
local tasks = store.active_tasks()
|
||||
s:save()
|
||||
local tasks = s:active_tasks()
|
||||
local _, meta = views.priority_view(tasks)
|
||||
local task_meta = nil
|
||||
for _, m in ipairs(meta) do
|
||||
|
|
@ -193,9 +196,9 @@ describe('file token', function()
|
|||
end)
|
||||
|
||||
it('file field is nil in LineMeta when task has no file', function()
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local tasks = store.active_tasks()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
local tasks = s:active_tasks()
|
||||
local _, meta = views.category_view(tasks)
|
||||
local task_meta = nil
|
||||
for _, m in ipairs(meta) do
|
||||
|
|
@ -212,17 +215,18 @@ describe('file token', function()
|
|||
describe(':Pending edit -file', function()
|
||||
it('clears file reference from task', function()
|
||||
local pending = require('pending')
|
||||
local t = store.add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
s:save()
|
||||
pending.edit(tostring(t.id), '-file')
|
||||
local updated = store.get(t.id)
|
||||
s:load()
|
||||
local updated = s:get(t.id)
|
||||
assert.is_nil(updated._extra)
|
||||
end)
|
||||
|
||||
it('shows feedback when file reference is removed', function()
|
||||
local pending = require('pending')
|
||||
local t = store.add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one', _extra = { file = 'src/auth.lua:42' } })
|
||||
s:save()
|
||||
local messages = {}
|
||||
local orig_notify = vim.notify
|
||||
vim.notify = function(msg, level)
|
||||
|
|
@ -236,8 +240,8 @@ describe('file token', function()
|
|||
|
||||
it('does not error when task has no file', function()
|
||||
local pending = require('pending')
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
assert.has_no_error(function()
|
||||
pending.edit(tostring(t.id), '-file')
|
||||
end)
|
||||
|
|
@ -245,13 +249,14 @@ describe('file token', function()
|
|||
|
||||
it('preserves other _extra fields when -file is used', function()
|
||||
local pending = require('pending')
|
||||
local t = store.add({
|
||||
local t = s:add({
|
||||
description = 'Task one',
|
||||
_extra = { file = 'src/auth.lua:42', _gcal_event_id = 'abc' },
|
||||
})
|
||||
store.save()
|
||||
s:save()
|
||||
pending.edit(tostring(t.id), '-file')
|
||||
local updated = store.get(t.id)
|
||||
s:load()
|
||||
local updated = s:get(t.id)
|
||||
assert.is_not_nil(updated._extra)
|
||||
assert.is_nil(updated._extra.file)
|
||||
assert.are.equal('abc', updated._extra._gcal_event_id)
|
||||
|
|
@ -263,9 +268,10 @@ describe('file token', function()
|
|||
local pending = require('pending')
|
||||
local buffer = require('pending.buffer')
|
||||
|
||||
local t = store.add({ description = 'Task one' })
|
||||
store.save()
|
||||
local t = s:add({ description = 'Task one' })
|
||||
s:save()
|
||||
|
||||
buffer.set_store(s)
|
||||
local bufnr = buffer.open()
|
||||
vim.bo[bufnr].filetype = 'pending'
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
|
|
@ -306,12 +312,13 @@ describe('file token', function()
|
|||
local pending = require('pending')
|
||||
local buffer = require('pending.buffer')
|
||||
|
||||
local t = store.add({
|
||||
local t = s:add({
|
||||
description = 'Task one',
|
||||
_extra = { file = 'nonexistent/path.lua:1' },
|
||||
})
|
||||
store.save()
|
||||
s:save()
|
||||
|
||||
buffer.set_store(s)
|
||||
local bufnr = buffer.open()
|
||||
vim.bo[bufnr].filetype = 'pending'
|
||||
vim.api.nvim_set_current_buf(bufnr)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue