summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJackson Moore <jacksonmoore@tuta.io>2026-09-05 14:05:04 +0200
committerJackson Moore <jacksonmoore@tuta.io>2026-09-05 14:05:04 +0200
commit83cba1f85b8d555b74f5eea4fd616d609bafddac (patch)
tree97ed3b1ed5af5da79792d2ecd5b6658cb2eeb746
parent1dbb35bc2343b4cd3f4061ac0bbe458793ac8043 (diff)
Add target policy tests
-rw-r--r--tests/target_policy_test.lua74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/target_policy_test.lua b/tests/target_policy_test.lua
new file mode 100644
index 0000000..4ce9b98
--- /dev/null
+++ b/tests/target_policy_test.lua
@@ -0,0 +1,74 @@
+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 instrumented_source, replacement_count = source:gsub(
+ "\nlocal commit = ya%.sync",
+ "\ncapture_target_policy(decide_target)\n\nlocal commit = ya.sync",
+ 1
+)
+assert(replacement_count == 1, "target policy capture point must occur once")
+
+local target_policy
+local sync_calls = 0
+local environment = {
+ error = error,
+ pairs = pairs,
+ type = type,
+ capture_target_policy = function(policy)
+ assert(type(policy) == "function", "target policy must be a function")
+ target_policy = policy
+ end,
+ ya = {
+ sync = function(callback)
+ sync_calls = sync_calls + 1
+ return callback
+ end,
+ emit = function()
+ error("target policy called ya.emit", 0)
+ end,
+ exec = function()
+ error("target policy called ya.exec", 0)
+ end,
+ input = function()
+ error("target policy called ya.input", 0)
+ end,
+ },
+}
+
+local chunk = assert(load(instrumented_source, "@" .. entry_path, "t", environment))
+chunk()
+
+assert(type(target_policy) == "function", "target policy was not captured")
+assert(sync_calls == 1, "plugin load must create only the commit shell")
+
+local function pack_values(...)
+ return { n = select("#", ...), ... }
+end
+
+local function assert_equal(actual, expected, label)
+ assert(actual == expected, ("%s: expected %s, got %s"):format(label, tostring(expected), tostring(actual)))
+end
+
+local decisions = {
+ { label = "directory mode with directory", mode = "directory", is_dir = true, expected = "enter" },
+ { label = "directory mode with file", mode = "directory", is_dir = false, expected = "none" },
+ { label = "file mode with directory", mode = "file", is_dir = true, expected = "none" },
+ { label = "file mode with file", mode = "file", is_dir = false, expected = "open-hovered" },
+ { label = "either mode with directory", mode = "either", is_dir = true, expected = "enter" },
+ { label = "either mode with file", mode = "either", is_dir = false, expected = "open-hovered" },
+}
+
+assert_equal(#decisions, 6, "decision fixture count")
+
+for _, case in ipairs(decisions) do
+ local calls_before = sync_calls
+ local result = pack_values(target_policy(case.mode, case.is_dir))
+ assert_equal(result.n, 1, case.label .. " return count")
+ assert_equal(result[1], case.expected, case.label .. " decision")
+ assert_equal(sync_calls, calls_before, case.label .. " host call count")
+end
+
+print("target policy checks passed")