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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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")
|