summaryrefslogtreecommitdiff
path: root/tests/commit_transaction_test.lua
blob: 5e06445952c048f72a73a4b0d39333bd6e51948a (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 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")