From f3a0323a80f6087b8a3aec8c7b14245e1f203b95 Mon Sep 17 00:00:00 2001 From: Jackson Moore Date: Sat, 5 Sep 2026 14:00:00 +0200 Subject: Add invocation intent tests --- tests/invocation_intent_test.lua | 146 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tests/invocation_intent_test.lua diff --git a/tests/invocation_intent_test.lua b/tests/invocation_intent_test.lua new file mode 100644 index 0000000..5eb3e3a --- /dev/null +++ b/tests/invocation_intent_test.lua @@ -0,0 +1,146 @@ +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 prompt_calls = 0 +local environment = { + error = error, + pairs = pairs, + type = type, + ya = { + sync = function(callback) + return callback + end, + input = function() + prompt_calls = prompt_calls + 1 + error("prompt boundary called during argument validation", 0) + end, + }, +} + +local chunk = assert(load(source, "@" .. entry_path, "t", environment)) +local plugin = chunk() + +local function parse(args) + return plugin:entry({ args = args }) +end + +local function assert_equal(actual, expected, label) + assert(actual == expected, ("%s: expected %s, got %s"):format(label, tostring(expected), tostring(actual))) +end + +local function assert_default_intent(intent, mode) + assert_equal(intent.mode, mode, "mode") + assert_equal(intent.previous, false, "default previous") + assert_equal(intent.smart, false, "default smart") + assert_equal(intent.insensitive, false, "default insensitive") + assert_equal(intent.direction, "forward", "default direction") + assert_equal(intent.effective_case, "sensitive", "default effective case") + + local fields = 0 + for _ in pairs(intent) do + fields = fields + 1 + end + assert_equal(fields, 6, "intent field count") +end + +local function expect_argument_error(args, expected_text) + local calls_before = prompt_calls + local ok, message = pcall(parse, args) + assert(not ok, "expected argument validation to fail") + assert(type(message) == "string", "argument error must be a string") + assert(message:find("yazi-enter:", 1, true) == 1, "argument error must identify the plugin") + assert(message:find(expected_text, 1, true), "unexpected argument error: " .. message) + assert_equal(prompt_calls, calls_before, "prompt calls after argument error") +end + +for _, mode in ipairs({ "directory", "file", "either" }) do + assert_default_intent(parse({ mode }), mode) +end + +local boolean_forms = { + { label = "Boolean true", value = true, expected = true }, + { label = "Boolean false", value = false, expected = false }, + { label = "yes", value = "yes", expected = true }, + { label = "no", value = "no", expected = false }, +} + +for _, option in ipairs({ "previous", "smart", "insensitive" }) do + for _, form in ipairs(boolean_forms) do + local args = { "either" } + args[option] = form.value + local intent = parse(args) + assert_equal(intent[option], form.expected, option .. " from " .. form.label) + + if option == "previous" then + local direction = form.expected and "backward" or "forward" + assert_equal(intent.direction, direction, "direction from " .. form.label) + elseif option == "smart" then + local effective_case = form.expected and "smart" or "sensitive" + assert_equal(intent.effective_case, effective_case, "case from smart " .. form.label) + else + local effective_case = form.expected and "insensitive" or "sensitive" + assert_equal(intent.effective_case, effective_case, "case from insensitive " .. form.label) + end + end +end + +local case_combinations = { + { smart = false, insensitive = false, expected = "sensitive" }, + { smart = false, insensitive = true, expected = "insensitive" }, + { smart = true, insensitive = false, expected = "smart" }, + { smart = true, insensitive = true, expected = "smart" }, +} + +for _, case in ipairs(case_combinations) do + local intent = parse({ "file", smart = case.smart, insensitive = case.insensitive }) + assert_equal(intent.smart, case.smart, "preserved smart flag") + assert_equal(intent.insensitive, case.insensitive, "preserved insensitive flag") + assert_equal(intent.effective_case, case.expected, "effective case precedence") +end + +local raw_args = { "either", previous = "yes", smart = false, insensitive = "no" } +local first_intent = parse(raw_args) +local second_intent = parse(raw_args) +assert(first_intent ~= second_intent, "each parse must return an invocation-local intent") +assert_equal(raw_args[1], "either", "raw mode after parsing") +assert_equal(raw_args.previous, "yes", "raw previous after parsing") +assert_equal(raw_args.smart, false, "raw smart after parsing") +assert_equal(raw_args.insensitive, "no", "raw insensitive after parsing") + +expect_argument_error({}, "mode must be directory, file, or either") +expect_argument_error({ "folder" }, "mode must be directory, file, or either") +expect_argument_error({ "Directory" }, "mode must be directory, file, or either") +expect_argument_error({ "FILE" }, "mode must be directory, file, or either") +expect_argument_error({ "Either" }, "mode must be directory, file, or either") +expect_argument_error({ true }, "mode must be directory, file, or either") + +expect_argument_error({ "directory", "extra" }, "unexpected positional argument at index 2") +expect_argument_error({ [1] = "file", [4] = "extra" }, "unexpected positional argument at index 4") +expect_argument_error({ "either", unknown = true }, "unknown option --unknown") +expect_argument_error({ "either", Smart = true }, "unknown option --Smart") + +local rejected_values = { + { label = "string true", value = "true" }, + { label = "string false", value = "false" }, + { label = "uppercase yes", value = "YES" }, + { label = "uppercase no", value = "NO" }, + { label = "empty string", value = "" }, + { label = "zero", value = 0 }, + { label = "one", value = 1 }, + { label = "table", value = {} }, +} + +for _, option in ipairs({ "previous", "smart", "insensitive" }) do + for _, rejected in ipairs(rejected_values) do + local args = { "either" } + args[option] = rejected.value + expect_argument_error(args, "option --" .. option .. " must be true, false, 'yes', or 'no'") + end +end + +assert_equal(prompt_calls, 0, "total prompt calls") + +print("invocation intent checks passed") -- cgit v1.2.3