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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
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")
|