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") local function run_dispatch_case(label, mode, is_dir, expected_count, expected_action) local order = {} local target = setmetatable({ highlights = function() order[#order + 1] = "highlights" return { 1 } end, }, { __index = function(_, key) assert(key == "cha", label .. " read an unexpected target field") order[#order + 1] = "is-dir" return { is_dir = is_dir } end, }) active = { finder = setmetatable({}, { __tostring = function() return "query" end }), current = { hovered = target }, } local emit_count = #emits assert_equal(commit(nil, mode, "query"), expected_count, label .. " dispatch count") assert_equal(#emits - emit_count, expected_count, label .. " emitted action count") assert_equal(order[1], "highlights", label .. " first guarded target access") assert_equal(order[2], "is-dir", label .. " deferred type access") assert_equal(order[3], nil, label .. " target access count") if expected_count == 1 then local emitted = emits[#emits] assert_equal(emitted.action, expected_action, label .. " action") assert(type(emitted.args) == "table", label .. " action arguments must be a table") if expected_action == "open" then assert_equal(emitted.args.hovered, true, label .. " hovered argument") assert_equal(next(emitted.args, "hovered"), nil, label .. " open argument shape") else assert_equal(next(emitted.args), nil, label .. " enter argument shape") end end end run_dispatch_case("directory mode directory", "directory", true, 1, "enter") run_dispatch_case("directory mode file", "directory", false, 0, nil) run_dispatch_case("file mode directory", "file", true, 0, nil) run_dispatch_case("file mode file", "file", false, 1, "open") run_dispatch_case("either mode directory", "either", true, 1, "enter") run_dispatch_case("either mode file", "either", false, 1, "open") print("commit transaction checks passed")