hephaestus/heph.nvim/tests/e2e/follow_link_spec.lua

78 lines
3 KiB
Lua
Raw Normal View History

-- 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 lnum, col = h.find(buf, "%[%[B") -- the body line, below the frontmatter
assert.is_truthy(lnum)
-- Put the cursor on the target inside the brackets.
vim.api.nvim_win_set_cursor(0, { lnum, col + 2 })
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 lnum, col = h.find(buf, "%[%[New")
vim.api.nvim_win_set_cursor(0, { lnum, col + 2 }) -- 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)