smoke.test.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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, writeFileSync, chmodSync, statSync } 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("config permission failures preserve private reads and terminate unsafe startup", {
  79. skip: process.platform !== "darwin",
  80. }, (t) => {
  81. const options = fixture(t);
  82. const config = path.join(options.env.MINIMAX_DATA_DIR, "config.yaml");
  83. const backup = `${config}.bak.byok-legacy-provider.smoke`;
  84. const flag = (file, value) => {
  85. const result = spawnSync("chflags", [value, file], { encoding: "utf8" });
  86. assert.equal(result.status, 0, result.stderr);
  87. };
  88. const list = () => {
  89. const result = spawnSync(process.execPath, [cli, "provider", "list"], {
  90. ...options,
  91. encoding: "utf8",
  92. timeout: 15000,
  93. // A leaked startup watcher can handle SIGTERM without releasing the process.
  94. killSignal: "SIGKILL",
  95. });
  96. assert.equal(result.error, undefined, result.stderr);
  97. return result;
  98. };
  99. try {
  100. for (const file of [config, backup]) {
  101. writeFileSync(file, "logLevel: info\n", { mode: 0o400 });
  102. flag(file, "uchg");
  103. }
  104. const privateRead = list();
  105. assert.equal(privateRead.status, 0, privateRead.stderr);
  106. assert.doesNotMatch(privateRead.stderr, /Could not restrict legacy BYOK backup/);
  107. assert.equal(statSync(config).mode & 0o777, 0o400);
  108. assert.equal(statSync(backup).mode & 0o777, 0o400);
  109. flag(backup, "nouchg");
  110. chmodSync(backup, 0o644);
  111. flag(backup, "uchg");
  112. const backupWarning = list();
  113. assert.equal(backupWarning.status, 0, backupWarning.stderr);
  114. assert.match(backupWarning.stderr, /Could not restrict legacy BYOK backup/);
  115. assert.ok(backupWarning.stderr.includes(JSON.stringify(backup)));
  116. assert.equal(statSync(backup).mode & 0o777, 0o644);
  117. flag(config, "nouchg");
  118. chmodSync(config, 0o644);
  119. flag(config, "uchg");
  120. const unsafeRead = list();
  121. assert.equal(unsafeRead.status, 1, unsafeRead.stderr);
  122. assert.match(unsafeRead.stderr, /EPERM/);
  123. assert.equal(statSync(config).mode & 0o777, 0o644);
  124. } finally {
  125. for (const file of [config, backup]) {
  126. if (existsSync(file)) flag(file, "nouchg");
  127. }
  128. }
  129. });
  130. test("offline smoke children ignore ambient proxy variables", async (t) => {
  131. const proxyNames = [
  132. "HTTP_PROXY",
  133. "HTTPS_PROXY",
  134. "ALL_PROXY",
  135. "http_proxy",
  136. "https_proxy",
  137. "all_proxy",
  138. ];
  139. for (const name of proxyNames) {
  140. await t.test(name, (t) => {
  141. const environment = {
  142. ...process.env,
  143. ...Object.fromEntries(proxyNames.map((key) => [key, ""])),
  144. [name]: "http://127.0.0.1:9",
  145. };
  146. const options = fixture(t, environment);
  147. const result = spawnSync(process.execPath, [cli, "provider", "list"], {
  148. ...options,
  149. encoding: "utf8",
  150. timeout: 15000,
  151. });
  152. assert.equal(result.status, 0, result.stderr);
  153. assert.match(result.stdout, /minimax/);
  154. assert.match(
  155. readFileSync(options.env.MCODE_TEST_NETWORK_AUDIT + ".managed", "utf8"),
  156. /https:\/\/models\.dev\/api\.json|\/mavis\/api\/v1\/models-dev\/catalog/,
  157. );
  158. assert.equal(environment[name], "http://127.0.0.1:9");
  159. });
  160. }
  161. });
  162. test("native SQLite binding opens and reads a real database", () => {
  163. const db = new Database(":memory:");
  164. try {
  165. assert.equal(db.prepare("select 42 as value").get().value, 42);
  166. } finally {
  167. db.close();
  168. }
  169. });
  170. test(
  171. "ACP starts the real runtime and answers initialize over stdio",
  172. { timeout: 45000 },
  173. async (t) => {
  174. const child = spawn(process.execPath, [cli, "acp"], {
  175. ...fixture(t),
  176. stdio: ["pipe", "pipe", "pipe"],
  177. });
  178. t.after(() => {
  179. if (child.exitCode === null) child.kill("SIGTERM");
  180. });
  181. let stderr = "";
  182. child.stderr.on("data", (chunk) => {
  183. stderr += chunk;
  184. });
  185. const response = await new Promise((resolve, reject) => {
  186. let buffer = "";
  187. const timer = setTimeout(
  188. () => reject(new Error(`ACP initialize timed out: ${stderr}`)),
  189. 30000,
  190. );
  191. child.once("error", (error) => {
  192. clearTimeout(timer);
  193. reject(error);
  194. });
  195. child.once("exit", (code) => {
  196. clearTimeout(timer);
  197. reject(new Error(`ACP exited ${code}: ${stderr}`));
  198. });
  199. child.stdout.on("data", (chunk) => {
  200. buffer += chunk;
  201. while (buffer.includes("\n")) {
  202. const index = buffer.indexOf("\n");
  203. const line = buffer.slice(0, index);
  204. buffer = buffer.slice(index + 1);
  205. try {
  206. const message = JSON.parse(line);
  207. if (message.id === 1) {
  208. clearTimeout(timer);
  209. resolve(message);
  210. }
  211. } catch {
  212. reject(new Error(`Non-JSON ACP stdout: ${line}`));
  213. }
  214. }
  215. });
  216. child.stdin.write(
  217. JSON.stringify({
  218. jsonrpc: "2.0",
  219. id: 1,
  220. method: "initialize",
  221. params: {
  222. protocolVersion: 1,
  223. clientCapabilities: { _meta: { "terminal-auth": true } },
  224. clientInfo: { name: "minimax-code-smoke", version: "1" },
  225. },
  226. }) + "\n",
  227. );
  228. });
  229. assert.equal(response.error, undefined, JSON.stringify(response));
  230. assert.equal(response.result.protocolVersion, 1);
  231. assert.equal(response.result.authMethods[0].id, "minimax-code-login");
  232. assert.equal(response.result.agentInfo.version, version);
  233. child.stdin.end();
  234. await new Promise((resolve, reject) => {
  235. const timer = setTimeout(() => {
  236. child.kill("SIGTERM");
  237. reject(new Error("ACP did not shut down after EOF"));
  238. }, 10000);
  239. child.once("exit", (code) => {
  240. clearTimeout(timer);
  241. code === 0
  242. ? resolve()
  243. : reject(new Error(`ACP shutdown ${code}: ${stderr}`));
  244. });
  245. });
  246. },
  247. );
  248. test("internal environment overrides are rejected", (t) => {
  249. const options = fixture(t);
  250. for (const args of [
  251. ["--lane", "internal"],
  252. ["--env", "test"],
  253. ]) {
  254. const result = spawnSync(process.execPath, [cli, ...args], {
  255. ...options,
  256. encoding: "utf8",
  257. timeout: 15000,
  258. });
  259. assert.notEqual(
  260. result.status,
  261. 0,
  262. `${args.join(" ")} unexpectedly succeeded`,
  263. );
  264. assert.equal(result.error, undefined);
  265. }
  266. });
  267. test("local plugin browsing remains available with managed services offline", (t) => {
  268. const options = fixture(t);
  269. for (const args of [
  270. ["plugin", "list", "--marketplace", "local", "--json"],
  271. ["plugin", "list", "--available", "--marketplace", "local", "--json"],
  272. ]) {
  273. const result = spawnSync(process.execPath, [cli, ...args], {
  274. ...options,
  275. encoding: "utf8",
  276. timeout: 15000,
  277. });
  278. assert.equal(result.status, 0, result.stderr);
  279. const output = JSON.parse(result.stdout);
  280. assert.ok(output !== null);
  281. assert.doesNotMatch(result.stdout, /"official"/);
  282. }
  283. });
  284. test("managed commands remain discoverable without logging in", (t) => {
  285. for (const args of [["login", "--help"], ["logout", "--help"], ["update", "--help"], ["acp", "login", "--help"], ["provider", "use", "--help"]]) {
  286. const result = spawnSync(process.execPath, [cli, ...args], { ...fixture(t), encoding: "utf8", timeout: 15000 });
  287. assert.equal(result.status, 0, result.stderr);
  288. assert.match(result.stdout, /Usage:/);
  289. }
  290. });
  291. test("provider add validates token limits before opening the runtime", (t) => {
  292. const options = fixture(t);
  293. const args = [
  294. "provider", "add", "--name", "Invalid limits", "--base-url", "http://127.0.0.1:1/v1",
  295. "--model", "synthetic-model",
  296. ];
  297. for (const flag of ["--context-limit", "--output-limit"]) {
  298. for (const value of ["0", "-1", "1.5", "NaN", "Infinity", "9007199254740992"]) {
  299. const result = spawnSync(process.execPath, [cli, ...args, `${flag}=${value}`], {
  300. ...options, encoding: "utf8", timeout: 15000,
  301. });
  302. assert.equal(result.status, 1, result.stderr);
  303. assert.match(result.stderr, /positive safe integer/);
  304. assert.ok(result.stderr.includes(flag), result.stderr);
  305. }
  306. }
  307. assert.equal(existsSync(path.join(options.cwd, "config.yaml")), false);
  308. });