Files

93 lines
4.1 KiB
Lua
Raw Permalink Normal View History

-- development/tests/test-calendar.lua — driven by development/tests/test-calendar.sh.
--
-- Headless Neovim harness for the calendar-vim integration wired through
-- `require('nuwiki').setup()`. The diary hooks themselves are pure VimL and
-- covered exhaustively by development/tests/test-calendar-vim.vim; this
-- harness focuses on the Neovim-only pieces:
-- * setup() registering the FileType autocmd that overrides calendar-vim's
-- own default hooks ('calendar#diary' / 'calendar#sign'),
-- * the window-close behaviour of calendar_action under the real (non-ex)
-- window API,
-- * disabling via use_calendar = false.
--
-- The opt-out case runs in a second nvim invocation (see the shell wrapper)
-- because setup() registers the autocmd once per process.
--
-- Results are written to $NUWIKI_CALENDAR_RESULTS so the wrapper can render
-- them without scraping nvim's output.
local OUT = vim.env.NUWIKI_CALENDAR_RESULTS or '/tmp/nuwiki-calendar-results.txt'
local WIKI = vim.env.NUWIKI_CALENDAR_WIKI
local MODE = vim.env.NUWIKI_CALENDAR_MODE or 'default'
local out_lines = {}
local pass, fail = 0, 0
local function record(ok, name, detail)
local suffix = (detail and detail ~= '') and ('' .. detail) or ''
table.insert(out_lines, (ok and 'PASS' or 'FAIL') .. ' ' .. name .. suffix)
if ok then pass = pass + 1 else fail = fail + 1 end
end
local function finish()
table.insert(out_lines, '')
table.insert(out_lines, string.format('SUMMARY: %d passed, %d failed', pass, fail))
vim.fn.writefile(out_lines, OUT)
vim.cmd(fail == 0 and 'qall!' or 'cquit!')
end
-- Open the seeded wiki index so the FileType vimwiki autocmd fires.
vim.cmd('edit ' .. vim.fn.fnameescape(WIKI .. '/index.wiki'))
if MODE == 'optout' then
-- use_calendar = false: setup() must not register the wiring autocmd, so
-- calendar-vim's own defaults (set by the stub) survive untouched.
record(vim.bo.filetype == 'vimwiki', 'optout.ftplugin_attached',
'filetype=' .. vim.bo.filetype)
record(vim.g.calendar_action ~= 'vimwiki#diary#calendar_action',
'optout.action_not_wired', 'g:calendar_action=' .. tostring(vim.g.calendar_action))
record(vim.g.calendar_sign ~= 'vimwiki#diary#calendar_sign',
'optout.sign_not_wired', 'g:calendar_sign=' .. tostring(vim.g.calendar_sign))
finish()
return
end
-- ===== Hook wiring overrides calendar-vim defaults =====
record(vim.bo.filetype == 'vimwiki', 'ftplugin_attached',
'filetype=' .. vim.bo.filetype)
record(vim.g.calendar_action == 'vimwiki#diary#calendar_action',
'hook.action_wired', 'g:calendar_action=' .. tostring(vim.g.calendar_action))
record(vim.g.calendar_sign == 'vimwiki#diary#calendar_sign',
'hook.sign_wired', 'g:calendar_sign=' .. tostring(vim.g.calendar_sign))
-- ===== calendar_sign reflects diary-file existence =====
record(vim.fn['nuwiki#diary#calendar_sign'](30, 5, 2026) == '*',
'sign.existing_date_marked',
'got ' .. tostring(vim.fn['nuwiki#diary#calendar_sign'](30, 5, 2026)))
record(vim.fn['nuwiki#diary#calendar_sign'](31, 5, 2026) == '',
'sign.absent_date_unmarked',
'got ' .. tostring(vim.fn['nuwiki#diary#calendar_sign'](31, 5, 2026)))
-- ===== calendar_action: path + window-close (real window API) =====
-- Open a second window to act as the calendar, invoke from it, and confirm
-- the diary lands at WIKI/diary/<date>.wiki while the calendar window closes.
vim.cmd('new')
local cal_win = vim.api.nvim_get_current_win()
local wins_before = #vim.api.nvim_list_wins()
vim.fn['nuwiki#diary#calendar_action'](31, 5, 2026, 0, '')
local opened = vim.fn.expand('%:p')
local want = WIKI .. '/diary/2026-05-31.wiki'
record(opened == want, 'action.opens_wiki_diary_path', 'opened=' .. opened)
record(not opened:match(vim.fn.expand('~') .. '/diary'),
'action.not_home_diary', 'opened=' .. opened)
record(not vim.api.nvim_win_is_valid(cal_win),
'action.closes_calendar_window',
'cal_win valid=' .. tostring(vim.api.nvim_win_is_valid(cal_win)))
record(#vim.api.nvim_list_wins() == wins_before - 1,
'action.window_count_decremented',
'before=' .. wins_before .. ' after=' .. #vim.api.nvim_list_wins())
record(vim.bo.filetype == 'vimwiki', 'action.sets_filetype',
'filetype=' .. vim.bo.filetype)
finish()