forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_panel_handlers.lua
More file actions
134 lines (104 loc) · 3.38 KB
/
Copy pathtest_panel_handlers.lua
File metadata and controls
134 lines (104 loc) · 3.38 KB
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
local eq = MiniTest.expect.equality
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
package.loaded["copilot.panel.handlers"] = nil
end,
},
})
T["panel_handlers()"] = MiniTest.new_set()
T["panel_handlers()"]["register and dispatch PanelSolution"] = function()
local handlers = require("copilot.panel.handlers")
local received = nil
handlers.register_panel_handlers("panel-1", {
on_solution = function(result)
received = result
end,
on_solutions_done = function() end,
})
local data = { panelId = "panel-1", completionText = "hello", score = 0.9 }
handlers.handlers.PanelSolution(nil, data)
eq(received, data)
end
T["panel_handlers()"]["dispatch PanelSolutionsDone"] = function()
local handlers = require("copilot.panel.handlers")
local received = nil
handlers.register_panel_handlers("panel-2", {
on_solution = function() end,
on_solutions_done = function(result)
received = result
end,
})
local data = { panelId = "panel-2", status = "OK" }
handlers.handlers.PanelSolutionsDone(nil, data)
eq(received, data)
end
T["panel_handlers()"]["unknown panelId does not dispatch"] = function()
local handlers = require("copilot.panel.handlers")
local called = false
handlers.register_panel_handlers("panel-3", {
on_solution = function()
called = true
end,
on_solutions_done = function() end,
})
-- Dispatch with a different panelId
handlers.handlers.PanelSolution(nil, { panelId = "unknown-panel" })
eq(called, false)
end
T["panel_handlers()"]["unregister removes callbacks"] = function()
local handlers = require("copilot.panel.handlers")
local called = false
handlers.register_panel_handlers("panel-4", {
on_solution = function()
called = true
end,
on_solutions_done = function() end,
})
handlers.unregister_panel_handlers("panel-4")
handlers.handlers.PanelSolution(nil, { panelId = "panel-4" })
eq(called, false)
end
T["panel_handlers()"]["dispatch to correct panelId among multiple"] = function()
local handlers = require("copilot.panel.handlers")
local received_a = nil
local received_b = nil
handlers.register_panel_handlers("panel-a", {
on_solution = function(result)
received_a = result
end,
on_solutions_done = function() end,
})
handlers.register_panel_handlers("panel-b", {
on_solution = function(result)
received_b = result
end,
on_solutions_done = function() end,
})
local data_b = { panelId = "panel-b", completionText = "world" }
handlers.handlers.PanelSolution(nil, data_b)
eq(received_a, nil)
eq(received_b, data_b)
end
T["panel_handlers()"]["assert on invalid panelId type in register"] = function()
local handlers = require("copilot.panel.handlers")
local ok = pcall(handlers.register_panel_handlers, nil, {
on_solution = function() end,
on_solutions_done = function() end,
})
eq(ok, false)
local ok2 = pcall(handlers.register_panel_handlers, 123, {
on_solution = function() end,
on_solutions_done = function() end,
})
eq(ok2, false)
end
T["panel_handlers()"]["assert on invalid panelId type in unregister"] = function()
local handlers = require("copilot.panel.handlers")
local ok = pcall(handlers.unregister_panel_handlers, nil)
eq(ok, false)
local ok2 = pcall(handlers.unregister_panel_handlers, 123)
eq(ok2, false)
end
return T