Refactor snippet and remove tests to focus on user behavior
Problem
The snippet and remove test suites contain tests that verify implementation details rather than user-facing behavior, particularly around buffer operations and deletion workflows.
Snippet Tests Issues (tests/zk-extras/snippet_spec.lua)
1. Implementation detail testing (lines 312-336)
Current: Tests insertion "at middle of line" with specific cursor positioning
vim.api.nvim_win_set_cursor(0, {1, 7}) -- After "before "
assert.is_true(line:match("before") ~= nil)
assert.is_true(line:match("INSERTED") ~= nil)
Issue: Tests exact buffer manipulation, not behavior
Should test:
- "Snippet insertion preserves surrounding content"
- "Cursor is positioned after inserted snippet"
- Don't verify exact character positions
2. Buffer state testing (lines 338-363)
Current: Tests snippet "with only whitespace"
vim.fn.writefile({" ", " "}, snippet_dir .. "/whitespace.md")
assert.is_true(#lines > 1) -- Multi-line snippet
Should test:
- User-visible behavior: "Empty-looking snippets are handled gracefully"
- Error messaging if snippets should not be whitespace-only
- Don't test buffer line counts
3. Good test that should be model (lines 281-309)
This is excellent: Tests undo history preservation - actual user behavior!
Remove Tests Issues (tests/zk-extras/remove_spec.lua)
1. Deletion flow testing (lines 137-170)
Current: Tests exact fallback order: trash → hard delete
local orig_move = utils.move_to_trash
utils.move_to_trash = function() return false end
-- Test hard delete is called
Issue: Tests implementation sequence, not requirement
Should test:
describe("note deletion", function()
it("successfully deletes note using available method", function()
-- Verify note is gone
-- Verify user notified of method used
-- Don't care about order of attempts
2. Missing workflow tests
Need tests for:
-
Deleting note open in multiple buffers -
Deleting currently edited note vs other note -
Deletion when file is externally modified -
Cancellation at various points in workflow -
State after deletion failure (buffer state, file state)
3. Remove_pick issues (lines 203-441)
Good behavioral tests:
- Lines 225-244: Error when .zk not found ✓
- Lines 390-415: No notes found handling ✓
Implementation detail tests:
- Lines 417-440: Tests .zk exclusion logic
- Should test: "Only user notes are shown in picker"
- Not: "Files matching /%.zk/ pattern are excluded"
Recommended Changes
1. Rewrite snippet tests
describe("snippet insertion", function()
it("preserves existing buffer content", function()
-- Setup buffer with content
-- Insert snippet
-- Verify original content intact
-- Verify snippet added
-- Don't check exact positions
it("respects cursor position", function()
-- Place cursor
-- Insert snippet
-- Verify snippet at logical position
-- Don't verify exact API calls
it("handles empty snippets gracefully", function()
-- Test user experience, not implementation
2. Rewrite deletion tests
describe("note deletion workflow", function()
it("successfully removes note from filesystem", function()
-- Create note
// Delete via command
// Verify file gone
// Verify buffers cleaned up
// Don't test trash vs hard delete order
it("handles concurrent buffer access correctly", function()
// Open note in multiple windows
// Delete note
// Verify all buffers handled correctly
it("recovers cleanly from deletion failures", function()
// Mock filesystem error
// Verify user gets helpful error
// Verify no partial state
3. Add missing integration tests
describe("snippet to deletion workflow", function()
it("allows deletion of note with inserted snippets", function()
// Create note
// Insert snippet
// Modify content
// Delete note
// Verify complete cleanup
Success Criteria
-
All tests verify user-visible behavior -
Implementation can change without breaking tests -
Edge cases are covered (multi-buffer, concurrent access) -
Error recovery is tested -
User workflow journeys are tested end-to-end
References
-
tests/zk-extras/snippet_spec.lualines 281-363 -
tests/zk-extras/remove_spec.lualines 137-441 lua/zk-extras/snippet.lualua/zk-extras/remove.lua