diff options
| -rw-r--r-- | tests/entry_composition_test.lua | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/entry_composition_test.lua b/tests/entry_composition_test.lua new file mode 100644 index 0000000..d4bcfdb --- /dev/null +++ b/tests/entry_composition_test.lua @@ -0,0 +1,89 @@ +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 calls = {} +local finder = setmetatable({}, { __tostring = function() return "alpha" end }) +local target = { + cha = { is_dir = false }, + highlights = function() + calls[#calls + 1] = { "highlights" } + return { 1 } + end, +} +local events = { { "a", 3 }, { "alpha", 1 } } +local receiver = { index = 0 } +function receiver:recv() + self.index = self.index + 1 + local event = events[self.index] + calls[#calls + 1] = { "receive", event[1], event[2] } + return event[1], event[2] +end + +local environment = { + cx = { active = { finder = finder, current = { hovered = target } } }, + error = error, + getmetatable = getmetatable, + pairs = pairs, + pcall = pcall, + tostring = tostring, + type = type, + ya = { + sync = function(callback) + return function(mode, query) + calls[#calls + 1] = { "commit", mode, query } + return callback(nil, mode, query) + end + end, + input = function(prompt) + calls[#calls + 1] = { "prompt", prompt.title } + return receiver + end, + exec = function(action, args) + calls[#calls + 1] = { "update", action, args[1], args.previous, args.smart, args.insensitive } + return true + end, + emit = function(action, args) + calls[#calls + 1] = { "dispatch", action, args.hovered } + end, + }, +} + +local plugin = assert(load(source, "@" .. entry_path, "t", environment))() +local result = plugin:entry({ args = { "either", smart = true } }) +assert(result == 1, "successful composition must return one dispatch") + +local expected_kinds = { + "prompt", "receive", "update", "receive", "update", "commit", "highlights", "dispatch", +} +for index, kind in ipairs(expected_kinds) do + assert(calls[index][1] == kind, ("call %d: expected %s, got %s"):format(index, kind, calls[index][1])) +end +assert(calls[1][2] == "Find next:", "entry must derive the prompt from direction") +assert(calls[3][2] == "find_do" and calls[3][3] == "a", "Type must use native find") +assert(calls[5][3] == "alpha", "Submit must apply its complete query") +assert(calls[5][5] == true, "validated smart option must reach final update") +assert(calls[6][2] == "either" and calls[6][3] == "alpha", "commit must receive local mode and query") +assert(calls[8][2] == "open" and calls[8][3] == true, "file target must dispatch hovered open") + +local prompt_count = 0 +for _, call in ipairs(calls) do + if call[1] == "prompt" then + prompt_count = prompt_count + 1 + end +end +local ok = pcall(function() + plugin:entry({ args = { "unknown" } }) +end) +assert(not ok, "invalid mode must raise an argument error") +local final_prompt_count = 0 +for _, call in ipairs(calls) do + if call[1] == "prompt" then + final_prompt_count = final_prompt_count + 1 + end +end +assert(final_prompt_count == prompt_count, "argument failure must occur before prompt creation") + +print("entry composition checks passed") |