summaryrefslogtreecommitdiff
path: root/tests/integrity_audit_test.lua
blob: 20f74c89f189c68480dce5a102706c8529ac3b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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")