summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/application_boundaries_test.lua305
1 files changed, 305 insertions, 0 deletions
diff --git a/tests/application_boundaries_test.lua b/tests/application_boundaries_test.lua
new file mode 100644
index 0000000..8b3c26d
--- /dev/null
+++ b/tests/application_boundaries_test.lua
@@ -0,0 +1,305 @@
+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 capture = [[
+capture_application_model {
+ new_application_boundaries = new_application_boundaries,
+ new_search_policy = new_search_policy,
+ new_invocation_state = new_invocation_state,
+ events = {
+ type = EVENT_TYPE,
+ submit = EVENT_SUBMIT,
+ cancel = EVENT_CANCEL,
+ closed = EVENT_CLOSED,
+ },
+ updates = {
+ success = UPDATE_SUCCESS,
+ failure = UPDATE_FAILURE,
+ },
+ actions = {
+ enter = ACTION_ENTER,
+ open_hovered = ACTION_OPEN_HOVERED,
+ none = ACTION_NONE,
+ },
+}
+]]
+
+local instrumented_source, replacement_count = source:gsub(
+ "\nreturn {\n\tentry = function",
+ "\n" .. capture .. "\nreturn {\n\tentry = function",
+ 1
+)
+assert(replacement_count == 1, "application model capture point must occur once")
+
+local model
+local environment = {
+ error = error,
+ getmetatable = getmetatable,
+ pairs = pairs,
+ type = type,
+ capture_application_model = function(value)
+ model = value
+ end,
+ ya = {
+ sync = function(callback)
+ return callback
+ end,
+ },
+}
+
+local chunk = assert(load(instrumented_source, "@" .. entry_path, "t", environment))
+chunk()
+
+assert(type(model) == "table", "application model was not captured")
+assert(type(model.new_application_boundaries) == "function", "boundary constructor must be a function")
+assert(type(model.new_search_policy) == "function", "search policy constructor must be a function")
+assert(type(model.new_invocation_state) == "function", "state constructor must be a function")
+
+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_field_count(value, expected, label)
+ local count = 0
+ for _ in pairs(value) do
+ count = count + 1
+ end
+ assert_equal(count, expected, label)
+end
+
+local function expect_error(operation, expected_text)
+ local ok, message = pcall(operation)
+ assert(not ok, "expected operation to fail")
+ assert(type(message) == "string", "boundary error must be a string")
+ assert(message:find(expected_text, 1, true), "unexpected boundary error: " .. message)
+end
+
+assert_equal(model.events.type, "type", "Type event")
+assert_equal(model.events.submit, "submit", "Submit event")
+assert_equal(model.events.cancel, "cancel", "Cancel event")
+assert_equal(model.events.closed, "closed", "Closed event")
+assert_field_count(model.events, 4, "semantic event count")
+assert_equal(model.updates.success, "success", "successful update result")
+assert_equal(model.updates.failure, "failure", "failed update result")
+assert_field_count(model.updates, 2, "Find update result count")
+
+local receiver_events = {
+ { query = "a", kind = model.events.type },
+ { query = "alpha", kind = model.events.submit },
+ { kind = model.events.cancel },
+ { kind = model.events.closed },
+}
+local receiver = {
+ index = 0,
+ recv = function(self)
+ self.index = self.index + 1
+ local event = receiver_events[self.index]
+ return event.query, event.kind
+ end,
+}
+
+local calls = {}
+local update_results = {
+ { model.updates.success, false },
+ { model.updates.failure, "malformed pattern" },
+}
+local commit_results = { 1, 0 }
+
+local function prompt_double(prompt_data, ...)
+ assert_equal(select("#", ...), 0, "prompt extra argument count")
+ calls[#calls + 1] = { capability = "prompt", prompt_data = prompt_data }
+ return receiver
+end
+
+local function find_update_double(query, search_policy, ...)
+ assert_equal(select("#", ...), 0, "Find update extra argument count")
+ calls[#calls + 1] = {
+ capability = "find-update",
+ query = query,
+ search_policy = search_policy,
+ }
+ local result = assert(update_results[1], "missing deterministic Find update result")
+ table.remove(update_results, 1)
+ return result[1], result[2]
+end
+
+local function commit_double(mode, submitted_query, ...)
+ assert_equal(select("#", ...), 0, "commit extra argument count")
+ calls[#calls + 1] = {
+ capability = "commit",
+ mode = mode,
+ submitted_query = submitted_query,
+ }
+ return table.remove(commit_results, 1)
+end
+
+local function target_dispatch_double(decision, ...)
+ assert_equal(select("#", ...), 0, "target dispatch extra argument count")
+ calls[#calls + 1] = { capability = "target-dispatch", decision = decision }
+end
+
+local boundaries = model.new_application_boundaries(
+ prompt_double,
+ find_update_double,
+ commit_double,
+ target_dispatch_double
+)
+assert_field_count(boundaries, 4, "application capability count")
+for _, name in ipairs({ "prompt", "find_update", "commit", "target_dispatch" }) do
+ assert(type(boundaries[name]) == "function", name .. " capability must be a function")
+end
+
+local prompt_data = {
+ name = "find",
+ title = "Find next:",
+ value = "",
+ realtime = true,
+ debounce = 0.05,
+ pos = { "top-center", x = 0, y = 2, w = 50, h = 3 },
+}
+assert_equal(boundaries.prompt(prompt_data), receiver, "prompt receiver")
+assert_equal(calls[1].capability, "prompt", "first capability call")
+assert_equal(calls[1].prompt_data, prompt_data, "prompt data identity")
+
+for index, expected in ipairs(receiver_events) do
+ local query, kind = receiver:recv()
+ assert_equal(query, expected.query, "receiver query " .. index)
+ assert_equal(kind, expected.kind, "receiver event " .. index)
+end
+
+local intent = {
+ mode = "either",
+ previous = true,
+ smart = true,
+ insensitive = false,
+ direction = "backward",
+ effective_case = "smart",
+}
+local search_policy = model.new_search_policy(intent)
+assert_field_count(search_policy, 3, "search policy field count")
+assert_equal(search_policy.previous, true, "search policy previous")
+assert_equal(search_policy.smart, true, "search policy smart")
+assert_equal(search_policy.insensitive, false, "search policy insensitive")
+assert(search_policy.mode == nil, "search policy must exclude activation mode")
+assert(search_policy.direction == nil, "search policy must exclude derived direction")
+assert(model.new_search_policy(intent) ~= search_policy, "each search policy must be a new plain table")
+
+local complete_query = "alpha\0beta.*"
+local update_result, error_status = boundaries.find_update(complete_query, search_policy)
+assert_equal(update_result, model.updates.success, "successful Find update")
+assert_equal(error_status, false, "successful Find update error status")
+assert_equal(calls[2].capability, "find-update", "second capability call")
+assert_equal(calls[2].query, complete_query, "complete Find query")
+assert_equal(calls[2].search_policy, search_policy, "search policy identity")
+
+update_result, error_status = boundaries.find_update("[", search_policy)
+assert_equal(update_result, model.updates.failure, "failed Find update")
+assert_equal(error_status, "malformed pattern", "failed Find update error status")
+assert_equal(calls[3].query, "[", "failed Find query")
+
+local live_value = setmetatable({}, {})
+assert_equal(boundaries.commit("either", complete_query, live_value), 1, "one target dispatch")
+assert_equal(calls[4].capability, "commit", "fourth capability call")
+assert_equal(calls[4].mode, "either", "commit mode")
+assert_equal(calls[4].submitted_query, complete_query, "submitted query")
+assert_equal(boundaries.commit("file", "missing"), 0, "zero target dispatches")
+
+assert_equal(boundaries.target_dispatch(model.actions.enter), nil, "enter dispatch return")
+assert_equal(calls[6].decision, model.actions.enter, "enter dispatch decision")
+assert_equal(boundaries.target_dispatch(model.actions.open_hovered), nil, "open dispatch return")
+assert_equal(calls[7].decision, model.actions.open_hovered, "open-hovered dispatch decision")
+expect_error(function()
+ boundaries.target_dispatch(model.actions.none)
+end, "invalid decision")
+assert_equal(#calls, 7, "capability call count after rejected target decision")
+
+expect_error(function()
+ boundaries.prompt("find")
+end, "prompt data must be a table")
+expect_error(function()
+ boundaries.prompt({ callback = function() end })
+end, "plain boundary data")
+expect_error(function()
+ boundaries.prompt(setmetatable({}, {}))
+end, "plain boundary data")
+local cyclic_data = {}
+cyclic_data.self = cyclic_data
+expect_error(function()
+ boundaries.prompt(cyclic_data)
+end, "must not contain a table cycle")
+expect_error(function()
+ boundaries.find_update({}, search_policy)
+end, "Find query must be a string")
+expect_error(function()
+ boundaries.find_update("query", { previous = function() end })
+end, "plain boundary data")
+expect_error(function()
+ boundaries.commit({}, "query")
+end, "commit mode must be a string")
+expect_error(function()
+ boundaries.commit("file", {})
+end, "submitted query must be a string")
+assert_equal(#calls, 7, "capability call count after rejected boundary data")
+
+local invalid_update_boundaries = model.new_application_boundaries(
+ prompt_double,
+ function()
+ return "unknown"
+ end,
+ commit_double,
+ target_dispatch_double
+)
+expect_error(function()
+ invalid_update_boundaries.find_update("query", search_policy)
+end, "invalid result")
+
+local invalid_commit_boundaries = model.new_application_boundaries(
+ prompt_double,
+ find_update_double,
+ function()
+ return 2
+ end,
+ target_dispatch_double
+)
+expect_error(function()
+ invalid_commit_boundaries.commit("either", "query")
+end, "invalid dispatch count")
+
+local second_intent = {
+ mode = "file",
+ previous = false,
+ smart = false,
+ insensitive = true,
+ direction = "forward",
+ effective_case = "insensitive",
+}
+local first_state = model.new_invocation_state(intent, receiver)
+local second_receiver = { recv = function() end }
+local second_state = model.new_invocation_state(second_intent, second_receiver)
+assert(first_state ~= second_state, "each invocation must have a separate state record")
+assert_field_count(first_state, 4, "first invocation state field count")
+assert_field_count(second_state, 4, "second invocation state field count")
+assert_equal(first_state.intent, intent, "first invocation intent")
+assert_equal(first_state.receiver, receiver, "first invocation receiver")
+assert_equal(first_state.current_query, "", "first invocation query")
+assert_equal(first_state.error_status, false, "first invocation error status")
+assert_equal(second_state.intent, second_intent, "second invocation intent")
+assert_equal(second_state.receiver, second_receiver, "second invocation receiver")
+assert_equal(second_state.current_query, "", "second invocation query")
+assert_equal(second_state.error_status, false, "second invocation error status")
+
+first_state.current_query = "changed"
+first_state.error_status = "failed"
+assert_equal(second_state.current_query, "", "second invocation query isolation")
+assert_equal(second_state.error_status, false, "second invocation error isolation")
+for _, forbidden_field in ipairs({ "files", "entries", "candidate", "target", "hovered" }) do
+ assert(first_state[forbidden_field] == nil, "invocation state copied " .. forbidden_field)
+end
+expect_error(function()
+ model.new_invocation_state({ previous = function() end }, receiver)
+end, "plain boundary data")
+
+print("application boundary checks passed")