smoke.test.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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) {
  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. ...process.env,
  34. MINIMAX_DATA_DIR: dataDir,
  35. MAVIS_DATA_DIR: dataDir,
  36. MCODE_TEST_NETWORK_AUDIT: audit,
  37. MCODE_TEST_MANAGED_OFFLINE: "1",
  38. NODE_OPTIONS: `--import=${new URL("./network-deny.mjs", import.meta.url).href}`,
  39. },
  40. };
  41. }
  42. test("CLI version and command help work outside the source directory", (t) => {
  43. const options = fixture(t);
  44. for (const [args, expected] of [
  45. [["--version"], version],
  46. [["--help"], "terminal coding agent"],
  47. [["exec", "--help"], "prompt"],
  48. ]) {
  49. const result = spawnSync(process.execPath, [cli, ...args], {
  50. ...options,
  51. encoding: "utf8",
  52. timeout: 15000,
  53. });
  54. assert.equal(result.status, 0, result.stderr);
  55. assert.ok(result.stdout.includes(expected), result.stdout);
  56. }
  57. });
  58. test("provider configuration loads from an isolated data directory", (t) => {
  59. const result = spawnSync(process.execPath, [cli, "provider", "list"], {
  60. ...fixture(t),
  61. encoding: "utf8",
  62. timeout: 15000,
  63. });
  64. assert.equal(result.status, 0, result.stderr);
  65. assert.match(result.stdout, /minimax/);
  66. assert.doesNotMatch(result.stdout, /custom_provider:/);
  67. });
  68. test("native SQLite binding opens and reads a real database", () => {
  69. const db = new Database(":memory:");
  70. try {
  71. assert.equal(db.prepare("select 42 as value").get().value, 42);
  72. } finally {
  73. db.close();
  74. }
  75. });
  76. test(
  77. "ACP starts the real runtime and answers initialize over stdio",
  78. { timeout: 45000 },
  79. async (t) => {
  80. const child = spawn(process.execPath, [cli, "acp"], {
  81. ...fixture(t),
  82. stdio: ["pipe", "pipe", "pipe"],
  83. });
  84. t.after(() => {
  85. if (child.exitCode === null) child.kill("SIGTERM");
  86. });
  87. let stderr = "";
  88. child.stderr.on("data", (chunk) => {
  89. stderr += chunk;
  90. });
  91. const response = await new Promise((resolve, reject) => {
  92. let buffer = "";
  93. const timer = setTimeout(
  94. () => reject(new Error(`ACP initialize timed out: ${stderr}`)),
  95. 30000,
  96. );
  97. child.once("error", (error) => {
  98. clearTimeout(timer);
  99. reject(error);
  100. });
  101. child.once("exit", (code) => {
  102. clearTimeout(timer);
  103. reject(new Error(`ACP exited ${code}: ${stderr}`));
  104. });
  105. child.stdout.on("data", (chunk) => {
  106. buffer += chunk;
  107. while (buffer.includes("\n")) {
  108. const index = buffer.indexOf("\n");
  109. const line = buffer.slice(0, index);
  110. buffer = buffer.slice(index + 1);
  111. try {
  112. const message = JSON.parse(line);
  113. if (message.id === 1) {
  114. clearTimeout(timer);
  115. resolve(message);
  116. }
  117. } catch {
  118. reject(new Error(`Non-JSON ACP stdout: ${line}`));
  119. }
  120. }
  121. });
  122. child.stdin.write(
  123. JSON.stringify({
  124. jsonrpc: "2.0",
  125. id: 1,
  126. method: "initialize",
  127. params: {
  128. protocolVersion: 1,
  129. clientCapabilities: { _meta: { "terminal-auth": true } },
  130. clientInfo: { name: "minimax-code-smoke", version: "1" },
  131. },
  132. }) + "\n",
  133. );
  134. });
  135. assert.equal(response.error, undefined, JSON.stringify(response));
  136. assert.equal(response.result.protocolVersion, 1);
  137. assert.equal(response.result.authMethods[0].id, "minimax-code-login");
  138. assert.equal(response.result.agentInfo.version, version);
  139. child.stdin.end();
  140. await new Promise((resolve, reject) => {
  141. const timer = setTimeout(() => {
  142. child.kill("SIGTERM");
  143. reject(new Error("ACP did not shut down after EOF"));
  144. }, 10000);
  145. child.once("exit", (code) => {
  146. clearTimeout(timer);
  147. code === 0
  148. ? resolve()
  149. : reject(new Error(`ACP shutdown ${code}: ${stderr}`));
  150. });
  151. });
  152. },
  153. );
  154. test("internal environment overrides are rejected", (t) => {
  155. const options = fixture(t);
  156. for (const args of [
  157. ["--lane", "internal"],
  158. ["--env", "test"],
  159. ]) {
  160. const result = spawnSync(process.execPath, [cli, ...args], {
  161. ...options,
  162. encoding: "utf8",
  163. timeout: 15000,
  164. });
  165. assert.notEqual(
  166. result.status,
  167. 0,
  168. `${args.join(" ")} unexpectedly succeeded`,
  169. );
  170. assert.equal(result.error, undefined);
  171. }
  172. });
  173. test("local plugin browsing remains available with managed services offline", (t) => {
  174. const options = fixture(t);
  175. for (const args of [
  176. ["plugin", "list", "--marketplace", "local", "--json"],
  177. ["plugin", "list", "--available", "--marketplace", "local", "--json"],
  178. ]) {
  179. const result = spawnSync(process.execPath, [cli, ...args], {
  180. ...options,
  181. encoding: "utf8",
  182. timeout: 15000,
  183. });
  184. assert.equal(result.status, 0, result.stderr);
  185. const output = JSON.parse(result.stdout);
  186. assert.ok(output !== null);
  187. assert.doesNotMatch(result.stdout, /"official"/);
  188. }
  189. });
  190. test("managed commands remain discoverable without logging in", (t) => {
  191. for (const args of [["login", "--help"], ["logout", "--help"], ["update", "--help"], ["acp", "login", "--help"], ["provider", "use", "--help"]]) {
  192. const result = spawnSync(process.execPath, [cli, ...args], { ...fixture(t), encoding: "utf8", timeout: 15000 });
  193. assert.equal(result.status, 0, result.stderr);
  194. assert.match(result.stdout, /Usage:/);
  195. }
  196. });