smoke.test.mjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { spawn, spawnSync } from "node:child_process";
  4. import { mkdtempSync, rmSync, readFileSync, existsSync } from "node:fs";
  5. import { tmpdir } from "node:os";
  6. import path from "node:path";
  7. import { fileURLToPath } from "node:url";
  8. import Database from "better-sqlite3";
  9. const root = fileURLToPath(new URL("../", import.meta.url));
  10. const cli = path.join(root, "dist/cli.js");
  11. // Full runtime startup can exceed 15s on Windows CI (ACP took 22s).
  12. // Match the ACP startup budget; lightweight help/validation stays at 15s.
  13. const runtimeTimeoutMs = process.platform === "win32" ? 30000 : 15000;
  14. function assertSuccessfulChild(result) {
  15. assert.equal(result.error, undefined,
  16. `CLI spawn failed: ${result.error?.message}; signal=${result.signal}; stderr=${result.stderr}`);
  17. assert.equal(result.status, 0, result.stderr);
  18. }
  19. const version = JSON.parse(
  20. readFileSync(path.join(root, "packages/tui/package.json"), "utf8"),
  21. ).version;
  22. function fixture(t, environment = process.env) {
  23. const dataDir = mkdtempSync(path.join(tmpdir(), "minimax-code-smoke-"));
  24. const audit = path.join(dataDir, "network-attempts.log");
  25. t.after(() => {
  26. try {
  27. assert.equal(
  28. existsSync(audit),
  29. false,
  30. existsSync(audit)
  31. ? readFileSync(audit, "utf8")
  32. : "Unexpected outbound network attempt",
  33. );
  34. } finally {
  35. rmSync(dataDir, { recursive: true, force: true });
  36. }
  37. });
  38. return {
  39. cwd: dataDir,
  40. env: {
  41. ...environment,
  42. // Proxy setup installs undici.fetch over the preloaded offline mock.
  43. // Keep host proxy settings out of these isolated test children.
  44. HTTP_PROXY: "",
  45. HTTPS_PROXY: "",
  46. ALL_PROXY: "",
  47. NO_PROXY: "",
  48. http_proxy: "",
  49. https_proxy: "",
  50. all_proxy: "",
  51. no_proxy: "",
  52. MINIMAX_DATA_DIR: dataDir,
  53. MAVIS_DATA_DIR: dataDir,
  54. MCODE_TEST_NETWORK_AUDIT: audit,
  55. MCODE_TEST_MANAGED_OFFLINE: "1",
  56. MCODE_TEST_PROCESS_PROBE: "1",
  57. NODE_OPTIONS: `--import=${new URL("./network-deny.mjs", import.meta.url).href}`,
  58. },
  59. };
  60. }
  61. test("CLI version and command help work outside the source directory", (t) => {
  62. const options = fixture(t);
  63. for (const [args, expected] of [
  64. [["--version"], version],
  65. [["--help"], "terminal coding agent"],
  66. [["exec", "--help"], "prompt"],
  67. ]) {
  68. const result = spawnSync(process.execPath, [cli, ...args], {
  69. ...options,
  70. encoding: "utf8",
  71. timeout: 15000,
  72. });
  73. assert.equal(result.status, 0, result.stderr);
  74. assert.ok(result.stdout.includes(expected), result.stdout);
  75. }
  76. });
  77. test("provider configuration loads from an isolated data directory", (t) => {
  78. const result = spawnSync(process.execPath, [cli, "provider", "list"], {
  79. ...fixture(t),
  80. encoding: "utf8",
  81. timeout: runtimeTimeoutMs,
  82. });
  83. assertSuccessfulChild(result);
  84. assert.match(result.stdout, /minimax/);
  85. assert.doesNotMatch(result.stdout, /custom_provider:/);
  86. });
  87. test("offline smoke children ignore ambient proxy variables", async (t) => {
  88. const proxyNames = [
  89. "HTTP_PROXY",
  90. "HTTPS_PROXY",
  91. "ALL_PROXY",
  92. "http_proxy",
  93. "https_proxy",
  94. "all_proxy",
  95. ];
  96. for (const name of proxyNames) {
  97. await t.test(name, (t) => {
  98. const environment = {
  99. ...process.env,
  100. ...Object.fromEntries(proxyNames.map((key) => [key, ""])),
  101. [name]: "http://127.0.0.1:9",
  102. };
  103. const options = fixture(t, environment);
  104. const result = spawnSync(process.execPath, [cli, "provider", "list"], {
  105. ...options,
  106. encoding: "utf8",
  107. timeout: runtimeTimeoutMs,
  108. });
  109. assertSuccessfulChild(result);
  110. assert.match(result.stdout, /minimax/);
  111. assert.match(
  112. readFileSync(options.env.MCODE_TEST_NETWORK_AUDIT + ".managed", "utf8"),
  113. /https:\/\/models\.dev\/api\.json|\/mavis\/api\/v1\/models-dev\/catalog/,
  114. );
  115. assert.equal(environment[name], "http://127.0.0.1:9");
  116. });
  117. }
  118. });
  119. test("native SQLite binding opens and reads a real database", () => {
  120. const db = new Database(":memory:");
  121. try {
  122. assert.equal(db.prepare("select 42 as value").get().value, 42);
  123. } finally {
  124. db.close();
  125. }
  126. });
  127. test(
  128. "ACP starts the real runtime and answers initialize over stdio",
  129. { timeout: 45000 },
  130. async (t) => {
  131. const child = spawn(process.execPath, [cli, "acp"], {
  132. ...fixture(t),
  133. stdio: ["pipe", "pipe", "pipe"],
  134. });
  135. t.after(() => {
  136. if (child.exitCode === null) child.kill("SIGTERM");
  137. });
  138. let stderr = "";
  139. child.stderr.on("data", (chunk) => {
  140. stderr += chunk;
  141. });
  142. const response = await new Promise((resolve, reject) => {
  143. let buffer = "";
  144. const timer = setTimeout(
  145. () => reject(new Error(`ACP initialize timed out: ${stderr}`)),
  146. 30000,
  147. );
  148. child.once("error", (error) => {
  149. clearTimeout(timer);
  150. reject(error);
  151. });
  152. child.once("exit", (code) => {
  153. clearTimeout(timer);
  154. reject(new Error(`ACP exited ${code}: ${stderr}`));
  155. });
  156. child.stdout.on("data", (chunk) => {
  157. buffer += chunk;
  158. while (buffer.includes("\n")) {
  159. const index = buffer.indexOf("\n");
  160. const line = buffer.slice(0, index);
  161. buffer = buffer.slice(index + 1);
  162. try {
  163. const message = JSON.parse(line);
  164. if (message.id === 1) {
  165. clearTimeout(timer);
  166. resolve(message);
  167. }
  168. } catch {
  169. reject(new Error(`Non-JSON ACP stdout: ${line}`));
  170. }
  171. }
  172. });
  173. child.stdin.write(
  174. JSON.stringify({
  175. jsonrpc: "2.0",
  176. id: 1,
  177. method: "initialize",
  178. params: {
  179. protocolVersion: 1,
  180. clientCapabilities: { _meta: { "terminal-auth": true } },
  181. clientInfo: { name: "minimax-code-smoke", version: "1" },
  182. },
  183. }) + "\n",
  184. );
  185. });
  186. assert.equal(response.error, undefined, JSON.stringify(response));
  187. assert.equal(response.result.protocolVersion, 1);
  188. assert.equal(response.result.authMethods[0].id, "minimax-code-login");
  189. assert.equal(response.result.agentInfo.version, version);
  190. child.stdin.end();
  191. await new Promise((resolve, reject) => {
  192. const timer = setTimeout(() => {
  193. child.kill("SIGTERM");
  194. reject(new Error("ACP did not shut down after EOF"));
  195. }, 10000);
  196. child.once("exit", (code) => {
  197. clearTimeout(timer);
  198. code === 0
  199. ? resolve()
  200. : reject(new Error(`ACP shutdown ${code}: ${stderr}`));
  201. });
  202. });
  203. },
  204. );
  205. test("internal environment overrides are rejected", (t) => {
  206. const options = fixture(t);
  207. for (const args of [
  208. ["--lane", "internal"],
  209. ["--env", "test"],
  210. ]) {
  211. const result = spawnSync(process.execPath, [cli, ...args], {
  212. ...options,
  213. encoding: "utf8",
  214. timeout: 15000,
  215. });
  216. assert.notEqual(
  217. result.status,
  218. 0,
  219. `${args.join(" ")} unexpectedly succeeded`,
  220. );
  221. assert.equal(result.error, undefined);
  222. }
  223. });
  224. test("local plugin browsing remains available with managed services offline", (t) => {
  225. const options = fixture(t);
  226. for (const args of [
  227. ["plugin", "list", "--marketplace", "local", "--json"],
  228. ["plugin", "list", "--available", "--marketplace", "local", "--json"],
  229. ]) {
  230. const result = spawnSync(process.execPath, [cli, ...args], {
  231. ...options,
  232. encoding: "utf8",
  233. timeout: runtimeTimeoutMs,
  234. });
  235. assertSuccessfulChild(result);
  236. const output = JSON.parse(result.stdout);
  237. assert.ok(output !== null);
  238. assert.doesNotMatch(result.stdout, /"official"/);
  239. }
  240. });
  241. test("managed commands remain discoverable without logging in", (t) => {
  242. for (const args of [["login", "--help"], ["logout", "--help"], ["update", "--help"], ["acp", "login", "--help"], ["provider", "use", "--help"]]) {
  243. const result = spawnSync(process.execPath, [cli, ...args], { ...fixture(t), encoding: "utf8", timeout: 15000 });
  244. assert.equal(result.status, 0, result.stderr);
  245. assert.match(result.stdout, /Usage:/);
  246. }
  247. });
  248. test("provider add validates token limits before opening the runtime", (t) => {
  249. const options = fixture(t);
  250. const args = [
  251. "provider", "add", "--name", "Invalid limits", "--base-url", "http://127.0.0.1:1/v1",
  252. "--model", "synthetic-model",
  253. ];
  254. for (const flag of ["--context-limit", "--output-limit"]) {
  255. for (const value of ["0", "-1", "1.5", "NaN", "Infinity", "9007199254740992"]) {
  256. const result = spawnSync(process.execPath, [cli, ...args, `${flag}=${value}`], {
  257. ...options, encoding: "utf8", timeout: 15000,
  258. });
  259. assert.equal(result.status, 1, result.stderr);
  260. assert.match(result.stderr, /positive safe integer/);
  261. assert.ok(result.stderr.includes(flag), result.stderr);
  262. }
  263. }
  264. assert.equal(existsSync(path.join(options.cwd, "config.yaml")), false);
  265. });