hephaestus/heph.nvim/tests/e2e/follow_link_spec.lua
Erich Blume 9249ca46a1
Some checks failed
Build / validate (pull_request) Failing after 4m13s
heph.nvim: follow-or-create wiki links + :Heph doc
Pressing <CR> on a [[wiki-link]] whose target doesn't exist now creates a doc
with that title and opens it (the zettelkasten gesture), and materializes the
source's backlink: if the source has unsaved edits, saving re-extracts and
links it (and persists the edits); otherwise the wiki link is added directly
(a no-op re-save wouldn't re-extract). Adds :Heph doc <title> to create a
standalone wiki entry. e2e covers both the saved-source and just-typed-source
paths.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-02 11:07:09 -07:00

79 lines
3 KiB
Lua

-- Workflow (c): follow a [[link]] under the cursor on <CR> to the target doc.
local h = require("e2e.helpers")
describe("follow link", function()
local ctx
before_each(function()
ctx = h.start()
end)
after_each(function()
h.stop(ctx)
end)
it("follows [[B]] under the cursor to doc B", function()
local b = h.create_doc("B", "the B doc")
local a = h.create_doc("A", "see [[B]] here")
local buf = h.open(a.id)
local line = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]
local open_at = line:find("%[%[B") -- start of "[[B"
assert.is_truthy(open_at)
-- Put the cursor on the target inside the brackets.
vim.api.nvim_win_set_cursor(0, { 1, open_at + 1 })
require("heph.link").follow()
local cur = vim.api.nvim_get_current_buf()
assert.are.equal("heph://node/" .. b.id, vim.api.nvim_buf_get_name(cur))
assert.are.equal(b.id, vim.b[cur].heph_node_id)
end)
it("creates the target doc when following an unresolved [[link]]", function()
local a = h.create_doc("Daily", "see [[New Topic]]")
local buf = h.open(a.id)
local at = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]:find("%[%[New")
vim.api.nvim_win_set_cursor(0, { 1, at + 1 }) -- inside [[New Topic]]
require("heph.link").follow()
-- A new doc titled "New Topic" was created and opened.
local created = ctx.q:call("node.resolve", { title = "New Topic" })
assert.is_truthy(created, "expected the target doc to be created")
assert.are.equal("heph://node/" .. created.id, vim.api.nvim_buf_get_name(0))
-- ...and the source now backlinks it (the wiki-link materialized).
local linked = false
for _, l in ipairs(ctx.q:call("links.backlinks", { id = created.id })) do
if l.src_id == a.id and l.link_type == "wiki" then
linked = true
end
end
assert.is_true(linked, "expected the source to backlink the created doc")
end)
it("creates + links from an unsaved [[link]] just typed into the buffer", function()
-- The real gesture: open a note, type a new [[link]], <CR> without :w.
local a = h.create_doc("Journalish", "")
local buf = h.open(a.id)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { "ref [[Fresh Note]]" })
assert.is_true(vim.bo[buf].modified)
local at = vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1]:find("%[%[Fresh")
vim.api.nvim_win_set_cursor(0, { 1, at + 1 })
require("heph.link").follow()
local created = ctx.q:call("node.resolve", { title = "Fresh Note" })
assert.is_truthy(created)
assert.are.equal("heph://node/" .. created.id, vim.api.nvim_buf_get_name(0))
-- The source's pending edit was persisted and the backlink materialized.
assert.are.equal("ref [[Fresh Note]]", ctx.q:call("node.get", { id = a.id }).body)
local linked = false
for _, l in ipairs(ctx.q:call("links.backlinks", { id = created.id })) do
if l.src_id == a.id and l.link_type == "wiki" then
linked = true
end
end
assert.is_true(linked, "expected the source edit saved and backlink materialized")
end)
end)