forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nodejs.lua
More file actions
188 lines (154 loc) · 6.72 KB
/
test_nodejs.lua
File metadata and controls
188 lines (154 loc) · 6.72 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
local eq = MiniTest.expect.equality
local stub = require("tests.stubs.nodejs")
local T = MiniTest.new_set({
hooks = {
pre_once = function() end,
pre_case = function()
-- Reset the module state before each test
package.loaded["copilot.lsp.nodejs"] = nil
stub.nodejs = require("copilot.lsp.nodejs")
end,
},
})
T["get_node_version()"] = MiniTest.new_set()
T["get_node_version()"]["default node command"] = function()
local captured_args = stub.valid_node_22(function()
stub.nodejs.setup()
local version, error = stub.nodejs.get_node_version()
eq(version, stub.valid_node_version_22)
eq(error, nil)
end)
eq(captured_args, { "node", "--version" })
end
T["get_node_version()"]["custom node command as string"] = function()
local captured_args = stub.valid_node_22(function()
stub.nodejs.setup("/usr/local/bin/node")
local version, error = stub.nodejs.get_node_version()
eq(version, stub.valid_node_version_22)
eq(error, nil)
end)
eq(captured_args, { "/usr/local/bin/node", "--version" })
end
T["get_node_version()"]["custom node command as string with spaces"] = function()
local captured_args = stub.valid_node_22(function()
stub.nodejs.setup("/path to/node")
local version, error = stub.nodejs.get_node_version()
eq(version, stub.valid_node_version_22)
eq(error, nil)
end)
eq(captured_args, { "/path to/node", "--version" })
end
T["get_node_version()"]["custom node command as table"] = function()
local captured_args = stub.valid_node_22(function()
stub.nodejs.setup({ "mise", "x", "node@lts", "--", "node" })
local version, error = stub.nodejs.get_node_version()
eq(version, stub.valid_node_version_22)
eq(error, nil)
end)
eq(captured_args, { "mise", "x", "node@lts", "--", "node", "--version" })
end
T["get_node_version()"]["handles vim.system failure"] = function()
local captured_args = stub.process("", -1, true, function()
stub.nodejs.setup("node")
local _, error = stub.nodejs.get_node_version()
error = error or ""
eq(error:find("Could not determine Node.js version") ~= nil, true)
end)
eq(captured_args, { "node", "--version" })
end
T["get_node_version()"]["handles process with non-zero exit code"] = function()
local captured_args = stub.process("", 127, false, function()
stub.nodejs.setup("nonexistent-node")
local _, error = stub.nodejs.get_node_version()
error = error or ""
eq(error:find("Could not determine Node.js version") ~= nil, true)
end)
eq(captured_args, { "nonexistent-node", "--version" })
end
T["get_node_version()"]["validates node version requirement"] = function()
local captured_args = stub.invalid_node(function()
stub.nodejs.setup("node")
local _, error = stub.nodejs.get_node_version()
error = error or ""
eq(error:find("Node.js version 22 or newer required") ~= nil, true)
end)
eq(captured_args, { "node", "--version" })
end
T["get_execute_command()"] = MiniTest.new_set()
T["get_execute_command()"]["default node command v22, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
eq(stub.nodejs.setup(), true)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "node", "--experimental-sqlite", vim.fn.expand(stub.default_server_path), "--stdio" })
end, stub.valid_node_22)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["default node command v24, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
eq(stub.nodejs.setup(), true)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "node", "--experimental-sqlite", vim.fn.expand(stub.default_server_path), "--stdio" })
end, stub.valid_node_24)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["default node command, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
eq(stub.nodejs.setup(), true)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "node", vim.fn.expand(stub.default_server_path), "--stdio" })
end)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["default node command, custom server path"] = function()
stub.get_runtime_server_path(function()
eq(stub.nodejs.setup(nil, vim.fn.expand(stub.custom_server_path)), true)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "node", vim.fn.expand(stub.custom_server_path), "--stdio" })
end)
end
T["get_execute_command()"]["custom node command as string, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
eq(stub.nodejs.setup("/usr/local/bin/node"), true)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "/usr/local/bin/node", vim.fn.expand(stub.default_server_path), "--stdio" })
end)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["custom node command as string, custom server path"] = function()
stub.get_runtime_server_path(function()
stub.nodejs.setup("/usr/local/bin/node", stub.custom_server_path)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "/usr/local/bin/node", stub.custom_server_path, "--stdio" })
end)
end
T["get_execute_command()"]["custom node command as string with spaces, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
stub.nodejs.setup("/path to/node")
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "/path to/node", vim.fn.expand(stub.default_server_path), "--stdio" })
end)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["custom node command as string with spaces, custom server path"] = function()
stub.get_runtime_server_path(function()
stub.nodejs.setup("/path to/node", stub.custom_server_path)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "/path to/node", stub.custom_server_path, "--stdio" })
end)
end
T["get_execute_command()"]["custom node command as table, default server path"] = function()
local captured_path = stub.get_runtime_server_path(function()
stub.nodejs.setup({ "mise", "x", "node@lts", "--", "node" })
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "mise", "x", "node@lts", "--", "node", vim.fn.expand(stub.default_server_path), "--stdio" })
end)
eq(captured_path, stub.default_server_path)
end
T["get_execute_command()"]["custom node command as table, custom server path"] = function()
stub.get_runtime_server_path(function()
stub.nodejs.setup({ "mise", "x", "node@lts", "--", "node" }, stub.custom_server_path)
local cmd = stub.nodejs.get_execute_command()
eq(cmd, { "mise", "x", "node@lts", "--", "node", stub.custom_server_path, "--stdio" })
end)
end
return T