forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.lua
More file actions
164 lines (146 loc) · 4.29 KB
/
test_api.lua
File metadata and controls
164 lines (146 loc) · 4.29 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
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
local eq = MiniTest.expect.equality
local child_helper = require("tests.child_helper")
local child = child_helper.new_child_neovim("test_api")
local u = require("tests.utils")
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
child.run_pre_case(true)
child.configure_copilot()
end,
post_once = child.stop,
},
})
T["api()"] = MiniTest.new_set()
-- check_status tests
T["api()"]["check_status coroutine mode returns user and status"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local data
coroutine.wrap(function()
local err, d = api.check_status(c)
data = d
end)()
vim.wait(500, function() return data ~= nil end, 10)
return data
]])
eq(result.user, "someUser")
eq(result.status, "OK")
end
T["api()"]["check_status callback mode returns user and status"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local cb_data
api.check_status(c, {}, function(err, data, ctx)
cb_data = data
end)
vim.wait(500, function() return cb_data ~= nil end, 10)
return cb_data
]])
eq(result.user, "someUser")
eq(result.status, "OK")
end
-- get_version test
T["api()"]["get_version returns version string"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local data
coroutine.wrap(function()
local err, d = api.get_version(c)
data = d
end)()
vim.wait(500, function() return data ~= nil end, 10)
return data
]])
eq(result.version, "1.430.0")
end
-- get_models test
T["api()"]["get_models returns 3 models"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local data
coroutine.wrap(function()
local err, d = api.get_models(c)
data = d
end)()
vim.wait(500, function() return data ~= nil end, 10)
return data
]])
eq(#result, 3)
eq(result[1].id, "gpt-4o")
eq(result[2].id, "gpt-4o-mini")
eq(result[3].id, "claude-sonnet")
end
-- notify test
T["api()"]["notify does not error"] = function()
-- api.notify calls client:notify which may return nil for mocked servers
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local ok, err = pcall(function()
api.notify(c, "$/setTrace", { value = "off" })
end)
return ok
]])
eq(result, true)
end
-- notify_accepted test
T["api()"]["notify_accepted does not error"] = function()
-- notifyAccepted handler is synchronous in the stub, so the coroutine
-- resumes immediately. We use callback mode to avoid coroutine timing issues.
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local finished = false
api.notify_accepted(c, { uuid = "test-uuid" }, function(err, data, ctx)
finished = true
end)
vim.wait(500, function() return finished end, 10)
return finished
]])
eq(result, true)
end
-- notify_rejected test
T["api()"]["notify_rejected does not error"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local finished = false
api.notify_rejected(c, { uuids = {"test-uuid"} }, function(err, data, ctx)
finished = true
end)
vim.wait(500, function() return finished end, 10)
return finished
]])
eq(result, true)
end
-- sign_in_initiate test
T["api()"]["sign_in_initiate returns userCode"] = function()
local result = child.lua([[
local api = require("copilot.api")
local client = require("copilot.client")
local c = client.get()
local data
coroutine.wrap(function()
local err, d = api.sign_in_initiate(c)
data = d
end)()
vim.wait(500, function() return data ~= nil end, 10)
return data
]])
eq(result.userCode, "ABCD-1234")
u.expect_match(result.verificationUri, "github.com")
end
return T