local entry_path = "yazi-enter.yazi/main.lua" local file = assert(io.open(entry_path, "rb")) local source = assert(file:read("*a")) file:close() local function count_plain(text) local matches = 0 local position = 1 while true do local first = source:find(text, position, true) if first == nil then return matches end matches = matches + 1 position = first + #text end end assert(count_plain("pcall(ya.exec, FIND_ACTION, new_find_request(query, search_policy))") == 1, "Find must have one awaited manager boundary") assert(count_plain('ya.emit("enter", {})') == 1, "enter must have one dispatch site") assert(count_plain('ya.emit("open", { hovered = true })') == 1, "open-hovered must have one exact dispatch site") assert(not source:find("cx.active.current.files", 1, true), "plugin must not copy the current entry list") for _, forbidden in ipairs({ "fs.remove", "fs.rename", "fs.write", "Command(", "os.execute", "io.popen" }) do assert(not source:find(forbidden, 1, true), "forbidden side effect boundary: " .. forbidden) end local function load_plugin(active, events, mutate_after_update, emit) local receiver = { index = 0 } function receiver:recv() self.index = self.index + 1 return events[self.index][1], events[self.index][2] end local environment = { cx = setmetatable({}, { __index = function(_, key) assert(key == "active") return active.value end }), error = error, getmetatable = getmetatable, pairs = pairs, pcall = pcall, tostring = tostring, type = type, ya = { sync = function(callback) return function(mode, query) return callback(nil, mode, query) end end, input = function() return receiver end, exec = function() if mutate_after_update then mutate_after_update(active) end return true end, emit = emit, }, } return assert(load(source, "@" .. entry_path, "t", environment))() end local function finder(query) return setmetatable({}, { __tostring = function() return query end }) end local function target(matched, is_dir) return { cha = { is_dir = is_dir }, highlights = function() return matched and { 1 } or nil end, } end local cases = { { "finder change", function(active) active.value.finder = finder("other") end }, { "tab change", function(active) active.value = { finder = nil, current = { hovered = nil } } end }, { "folder change", function(active) active.value.current = { hovered = target(false, false) } end }, { "hovered removal", function(active) active.value.current.hovered = nil end }, } for _, case in ipairs(cases) do local dispatches = 0 local active = { value = { finder = finder("query"), current = { hovered = target(true, false) } }, } local plugin = load_plugin(active, { { "query", 1 } }, case[2], function() dispatches = dispatches + 1 end) assert(plugin:entry({ args = { "either" } }) == 0, case[1] .. " must return zero") assert(dispatches == 0, case[1] .. " must dispatch zero targets") end local dispatches = 0 local active = { value = { finder = finder("query"), current = { hovered = target(true, false) } }, } local plugin = load_plugin(active, { { "query", 1 } }, nil, function(action, args) dispatches = dispatches + 1 assert(action == "open" and args.hovered == true, "commit must request hovered open") return false, "native target failure" end) assert(plugin:entry({ args = { "file" } }) == 1, "dispatch ownership ends after the target request") assert(dispatches == 1, "target failure must retain one native dispatch") print("integrity audit checks passed")