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 commit local active local emits = {} local environment = { cx = setmetatable({}, { __index = function(_, key) assert(key == "active", "commit read an unexpected context field") return active end, }), error = error, getmetatable = getmetatable, pairs = pairs, pcall = pcall, tostring = tostring, type = type, ya = { sync = function(callback) assert(commit == nil, "commit transaction must be declared once") commit = callback return callback end, emit = function(action, args) emits[#emits + 1] = { action = action, args = args } end, }, } local chunk = assert(load(source, "@" .. entry_path, "t", environment)) chunk() assert(type(commit) == "function", "commit transaction was not captured") local function assert_equal(actual, expected, label) assert(actual == expected, ("%s: expected %s, got %s"):format(label, tostring(expected), tostring(actual))) end local function run_guard_case(label, tab, query) active = tab local emit_count = #emits assert_equal(commit(nil, "either", query), 0, label .. " dispatch count") assert_equal(#emits, emit_count, label .. " emitted action count") end run_guard_case("missing finder", { finder = nil, current = { hovered = nil }, }, "query") run_guard_case("missing hovered target", { finder = setmetatable({}, { __tostring = function() return "query" end }), current = { hovered = nil }, }, "query") local mismatch_highlights_calls = 0 local mismatch_cha_reads = 0 local mismatch_target = setmetatable({ highlights = function() mismatch_highlights_calls = mismatch_highlights_calls + 1 return { 1 } end, }, { __index = function(_, key) if key == "cha" then mismatch_cha_reads = mismatch_cha_reads + 1 end end, }) run_guard_case("finder mismatch", { finder = setmetatable({}, { __tostring = function() return "other" end }), current = { hovered = mismatch_target }, }, "query") assert_equal(mismatch_highlights_calls, 0, "highlights calls before finder equality") assert_equal(mismatch_cha_reads, 0, "type reads before finder equality") local unmatched_cha_reads = 0 local unmatched_target = setmetatable({ highlights = function() return nil end, }, { __index = function(_, key) if key == "cha" then unmatched_cha_reads = unmatched_cha_reads + 1 end end, }) run_guard_case("missing highlights", { finder = setmetatable({}, { __tostring = function() return "query" end }), current = { hovered = unmatched_target }, }, "query") assert_equal(unmatched_cha_reads, 0, "type reads before highlight proof") print("commit guard checks passed")