-- Wiki-links by node id (ยง8.4): the `[[` picker searches nodes and inserts a -- canonical `[[NODEID]]` link; a "Create" entry mints a new doc. The inserted -- id resolves on follow and materializes as a `wiki` link on save. local h = require("e2e.helpers") describe("link insert picker", function() local ctx before_each(function() ctx = h.start() end) after_each(function() h.stop(ctx) end) -- Drive `link.insert` with a stubbed query + choice. local function with_picker(query, pick, fn) vim.g.heph_force_ui_select = true local oi, os = vim.ui.input, vim.ui.select vim.ui.input = function(_o, cb) cb(query) end vim.ui.select = function(items, _o, cb) cb(pick(items)) end local ok, err = pcall(fn) vim.ui.input, vim.ui.select, vim.g.heph_force_ui_select = oi, os, nil assert.is_true(ok, tostring(err)) end it("inserts a canonical [[NODEID]] for a searched node and materializes the link", function() local target = h.create_doc("Roofing", "the roofing doc") local src = h.create_doc("Daily", "") local buf = h.open(src.id) -- Put the cursor on an empty body line below the frontmatter. vim.api.nvim_buf_set_lines(buf, -1, -1, false, { "" }) vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 0 }) with_picker("roofing", function(items) return items[1] -- the search hit (FTS matches the "roofing" token) end, function() require("heph.link").insert() end) -- The buffer now carries `[[]]`, and saving materializes the link. assert.is_truthy(h.find(buf, "%[%[" .. target.id), "[[id]] not inserted") h.save(buf) local linked = false for _, l in ipairs(ctx.q:call("links.backlinks", { id = target.id })) do if l.src_id == src.id and l.link_type == "wiki" then linked = true end end assert.is_true(linked, "expected a wiki link from src to the picked node") end) it("conceals a link's id, leaving the name as a styled label", function() local target = h.create_doc("Roofing", "") -- A bare canonical link in the source; node.get expands it to [[id|Roofing]]. local src = h.create_doc("Daily", "see [[" .. target.id .. "]]") local buf = h.open(src.id) -- The expanded link is visible in the buffer... assert.is_truthy(h.find(buf, "%[%[" .. target.id .. "|Roofing%]%]"), "expanded link missing") -- ...and conceal extmarks hide the `[[id|` prefix + `]]` suffix. local ns = vim.api.nvim_get_namespaces()["heph_link_conceal"] assert.is_truthy(ns, "conceal namespace not registered") local marks = vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true }) local conceals = 0 for _, m in ipairs(marks) do if m[4] and m[4].conceal == "" then conceals = conceals + 1 end end assert.is_true(conceals >= 2, "expected prefix+suffix conceal extmarks, got " .. conceals) assert.are.equal(2, vim.wo.conceallevel, "conceallevel not set for the buffer's window") end) it("creates a new doc when the Create entry is chosen", function() local src = h.create_doc("Notes", "") local buf = h.open(src.id) vim.api.nvim_buf_set_lines(buf, -1, -1, false, { "" }) vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(buf), 0 }) with_picker("Brand New Topic", function(items) return items[#items] -- the "+ Create" sentinel is last end, function() require("heph.link").insert() end) local created = ctx.q:call("node.resolve", { title = "Brand New Topic" }) assert.is_truthy(created, "create entry should mint the doc") assert.is_truthy(h.find(buf, "%[%[" .. created.id), "[[new id]] not inserted") end) end)