-- 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("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)