|
| 1 | +local eq = MiniTest.expect.equality |
| 2 | + |
| 3 | +local T = MiniTest.new_set({ |
| 4 | + hooks = { |
| 5 | + pre_once = function() end, |
| 6 | + pre_case = function() |
| 7 | + -- Reset the module state before each test |
| 8 | + package.loaded["copilot.lsp.nodejs"] = nil |
| 9 | + end, |
| 10 | + }, |
| 11 | +}) |
| 12 | + |
| 13 | +T["get_node_version()"] = MiniTest.new_set() |
| 14 | + |
| 15 | +local function stub_process(stdout, code, fail, callback) |
| 16 | + local captured_args = nil |
| 17 | + local original_vim_system = vim.system |
| 18 | + vim.system = function(cmd, opts) |
| 19 | + captured_args = cmd |
| 20 | + if fail then |
| 21 | + error("Command failed") |
| 22 | + end |
| 23 | + return { |
| 24 | + wait = function() |
| 25 | + return { |
| 26 | + stdout = stdout .. "\n", |
| 27 | + code = code |
| 28 | + } |
| 29 | + end |
| 30 | + } |
| 31 | + end |
| 32 | + callback() |
| 33 | + vim.system = original_vim_system |
| 34 | + return captured_args |
| 35 | +end |
| 36 | + |
| 37 | +T["get_node_version()"]["default node command"] = function() |
| 38 | + captured_args = stub_process("v20.10.0", 0, false, function() |
| 39 | + local nodejs = require("copilot.lsp.nodejs") |
| 40 | + nodejs.setup() |
| 41 | + |
| 42 | + local version, error = nodejs.get_node_version() |
| 43 | + |
| 44 | + eq(version, "20.10.0") |
| 45 | + eq(error, nil) |
| 46 | + end) |
| 47 | + eq(captured_args, { "node", "--version" }) |
| 48 | +end |
| 49 | + |
| 50 | +T["get_node_version()"]["custom node command as string"] = function() |
| 51 | + local captured_args = stub_process("v20.10.0", 0, false, function() |
| 52 | + local nodejs = require("copilot.lsp.nodejs") |
| 53 | + nodejs.setup("/usr/local/bin/node") |
| 54 | + |
| 55 | + local version, error = nodejs.get_node_version() |
| 56 | + |
| 57 | + eq(version, "20.10.0") |
| 58 | + eq(error, nil) |
| 59 | + end) |
| 60 | + eq(captured_args, { "/usr/local/bin/node", "--version" }) |
| 61 | +end |
| 62 | + |
| 63 | +T["get_node_version()"]["custom node command as string with spaces"] = function() |
| 64 | + local captured_args = stub_process("v20.10.0", 0, false, function() |
| 65 | + local nodejs = require("copilot.lsp.nodejs") |
| 66 | + nodejs.setup("/path to/node") |
| 67 | + |
| 68 | + local version, error = nodejs.get_node_version() |
| 69 | + |
| 70 | + eq(version, "20.10.0") |
| 71 | + eq(error, nil) |
| 72 | + end) |
| 73 | + eq(captured_args, { "/path to/node", "--version" }) |
| 74 | +end |
| 75 | + |
| 76 | +T["get_node_version()"]["handles vim.system failure"] = function() |
| 77 | + local captured_args = stub_process("", -1, true, function() |
| 78 | + local nodejs = require("copilot.lsp.nodejs") |
| 79 | + nodejs.setup("node") |
| 80 | + |
| 81 | + local version, error = nodejs.get_node_version() |
| 82 | + |
| 83 | + eq(version, "") |
| 84 | + -- Error should contain failure information |
| 85 | + local expected_error_pattern = "Could not determine Node%.js version" |
| 86 | + eq(type(error), "string") |
| 87 | + eq(error:find(expected_error_pattern) ~= nil, true) |
| 88 | + end) |
| 89 | +end |
| 90 | + |
| 91 | +T["get_node_version()"]["handles process with non-zero exit code"] = function() |
| 92 | + local captured_args = stub_process("", 127, false, function() |
| 93 | + local nodejs = require("copilot.lsp.nodejs") |
| 94 | + nodejs.setup("nonexistent-node") |
| 95 | + |
| 96 | + local version, error = nodejs.get_node_version() |
| 97 | + |
| 98 | + eq(version, "") |
| 99 | + -- Error should contain failure information with exit code |
| 100 | + local expected_error_pattern = "Could not determine Node%.js version" |
| 101 | + eq(type(error), "string") |
| 102 | + eq(error:find(expected_error_pattern) ~= nil, true) |
| 103 | + eq(error:find("127") ~= nil, true) |
| 104 | + end) |
| 105 | + eq(captured_args, { "nonexistent-node", "--version" }) |
| 106 | +end |
| 107 | + |
| 108 | +T["get_node_version()"]["validates node version requirement"] = function() |
| 109 | + local captured_args = stub_process("v18.17.0", 0, false, function() |
| 110 | + local nodejs = require("copilot.lsp.nodejs") |
| 111 | + nodejs.setup("node") |
| 112 | + |
| 113 | + local version, error = nodejs.get_node_version() |
| 114 | + |
| 115 | + eq(version, "18.17.0") |
| 116 | + -- Error should indicate version requirement not met |
| 117 | + eq(type(error), "string") |
| 118 | + eq(error:find("Node%.js version 20 or newer required") ~= nil, true) |
| 119 | + eq(error:find("18%.17%.0") ~= nil, true) |
| 120 | + end) |
| 121 | + eq(captured_args, { "node", "--version" }) |
| 122 | +end |
| 123 | + |
| 124 | +T["get_execute_command()"] = MiniTest.new_set() |
| 125 | + |
| 126 | +T["get_execute_command()"]["default node command"] = function() |
| 127 | + local nodejs = require("copilot.lsp.nodejs") |
| 128 | + nodejs.setup() |
| 129 | + local cmd = nodejs.get_execute_command() |
| 130 | + eq(cmd, { "node", nodejs.server_path, "--stdio" }) |
| 131 | +end |
| 132 | + |
| 133 | +T["get_execute_command()"]["custom node command as string"] = function() |
| 134 | + local nodejs = require("copilot.lsp.nodejs") |
| 135 | + nodejs.setup("/usr/local/bin/node") |
| 136 | + local cmd = nodejs.get_execute_command() |
| 137 | + eq(cmd, { "/usr/local/bin/node", nodejs.server_path, "--stdio" }) |
| 138 | +end |
| 139 | + |
| 140 | +T["get_execute_command()"]["custom node command as string with spaces"] = function() |
| 141 | + local nodejs = require("copilot.lsp.nodejs") |
| 142 | + nodejs.setup("/path to/node") |
| 143 | + local cmd = nodejs.get_execute_command() |
| 144 | + eq(cmd, { "/path to/node", nodejs.server_path, "--stdio" }) |
| 145 | +end |
| 146 | + |
| 147 | +return T |
0 commit comments