smoke.test.mjs 7.6 KB

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