summaryrefslogtreecommitdiff
path: root/tests/integrity_audit_test.lua
diff options
context:
space:
mode:
authorJackson Moore <jacksonmoore@tuta.io>2026-09-05 15:04:58 +0200
committerJackson Moore <jacksonmoore@tuta.io>2026-09-05 15:04:58 +0200
commitf17ebcfbd097603cceb85abe99adcafda81ce2d0 (patch)
tree68828cd6d8fc128f7f1a591693ea2ee06ac3731d /tests/integrity_audit_test.lua
parentf9178351551670a490c53a1aa05e6e2cd0439ebf (diff)
Add integrity audit checks
Diffstat (limited to 'tests/integrity_audit_test.lua')
-rw-r--r--tests/integrity_audit_test.lua99
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/integrity_audit_test.lua b/tests/integrity_audit_test.lua
new file mode 100644
index 0000000..20f74c8
--- /dev/null
+++ b/tests/integrity_audit_test.lua
@@ -0,0 +1,99 @@
+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")