update-application.test.ts 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
  2. import os from 'node:os';
  3. import path from 'node:path';
  4. import { describe, expect, it, vi } from 'vitest';
  5. import {
  6. McodeUpdateApplication,
  7. type McodeUpdateApplicationDependencies,
  8. } from '../../src/update/application.js';
  9. import {
  10. bindMcodeNpmCommandToRuntime,
  11. buildMcodePackageManagerCommand,
  12. classifyMcodeInstallPath,
  13. classifyNpmGlobalInstall,
  14. detectMcodeInstallSource,
  15. isInternalMcodePackageName,
  16. resolveMcodeNpmDistribution,
  17. resolveMcodeNpmDistTag,
  18. resolveMcodeNpmPrefixInstall,
  19. resolveMcodePackageName,
  20. resolveInstalledMcodePackageVersion,
  21. resolveLatestMcodeRegistryVersion,
  22. } from '../../src/update/install-source.js';
  23. import {
  24. resolveMcodePrefixLauncherPairs,
  25. resolveMcodePrefixModulesRoot,
  26. writeMcodePrefixUpdatePending,
  27. } from '../../src/update/prefix-update.js';
  28. describe('installer-owned npm runtime binding', () => {
  29. const target = 'nodejs/26.3.1/lib/node_modules/npm/bin/npm-cli.js';
  30. const shim = (cli = target) =>
  31. [
  32. '#!/bin/sh',
  33. 'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
  34. 'if [ -x "$basedir/node" ]; then',
  35. ` exec "$basedir/node" "$basedir/${cli}" "$@"`,
  36. 'else',
  37. ` exec node "$basedir/${cli}" "$@"`,
  38. 'fi',
  39. ].join('\n');
  40. async function withLayout(
  41. run: (root: string, write: (file: string, text?: string) => string) => void | Promise<void>,
  42. ) {
  43. const root = realpathSync(mkdtempSync(path.join(os.tmpdir(), 'mcode-npm-shim-')));
  44. const write = (file: string, text = '') => {
  45. const fullPath = path.join(root, file);
  46. mkdirSync(path.dirname(fullPath), { recursive: true });
  47. writeFileSync(fullPath, text);
  48. return fullPath;
  49. };
  50. try {
  51. await run(root, write);
  52. } finally {
  53. rmSync(root, { recursive: true, force: true });
  54. }
  55. }
  56. function bind(executable: string) {
  57. return bindMcodeNpmCommandToRuntime(
  58. { executable, args: ['view', '@minimax-ai/code@latest', '--json'], display: 'owned npm' },
  59. process.execPath,
  60. );
  61. }
  62. it('resolves the reported pnpm shell shim and runs its CLI with the selected Node', async () => {
  63. await withLayout(async (root, write) => {
  64. const marker = path.join(root, 'shim-was-executed');
  65. const npm = write(
  66. 'pnpm home/npm',
  67. shim().replace('#!/bin/sh', `#!/bin/sh\ntouch "${marker}"`),
  68. );
  69. const cli = write(`pnpm home/${target}`, 'console.log(JSON.stringify("1.2.4"));');
  70. // A different installed pnpm Node version must not influence the selection.
  71. write(
  72. 'pnpm home/nodejs/24.2.0/lib/node_modules/npm/bin/npm-cli.js',
  73. 'throw new Error("wrong npm");',
  74. );
  75. expect(bind(npm)).toEqual({
  76. executable: process.execPath,
  77. args: [cli, 'view', '@minimax-ai/code@latest', '--json'],
  78. display: 'owned npm',
  79. });
  80. await expect(
  81. resolveLatestMcodeRegistryVersion('latest', {
  82. npmExecutable: npm,
  83. runtimeExecutable: process.execPath,
  84. distribution: resolveMcodeNpmDistribution('@minimax-ai/code'),
  85. }),
  86. ).resolves.toBe('1.2.4');
  87. expect(existsSync(marker)).toBe(false);
  88. });
  89. });
  90. it.skipIf(process.platform === 'win32')(
  91. 'preserves regular npm symlinks and resolves symlinked pnpm shims',
  92. async () => {
  93. await withLayout((root, write) => {
  94. const cli = write(`pnpm home/${target}`);
  95. const npm = write('pnpm home/npm', shim());
  96. symlinkSync(npm, path.join(root, 'npm'));
  97. expect(bind(path.join(root, 'npm')).args[0]).toBe(cli);
  98. const regularCli = write('node/lib/node_modules/npm/bin/npm-cli.js');
  99. mkdirSync(path.join(root, 'node/bin'), { recursive: true });
  100. symlinkSync('../lib/node_modules/npm/bin/npm-cli.js', path.join(root, 'node/bin/npm'));
  101. expect(bind(path.join(root, 'node/bin/npm')).args[0]).toBe(regularCli);
  102. });
  103. },
  104. );
  105. it.each([
  106. ['node/npm.cmd', 'node/node_modules/npm/bin/npm-cli.js'],
  107. ['node/bin/npm', 'node/lib/node_modules/npm/bin/npm-cli.js'],
  108. ])('preserves conventional npm layout for %s', async (executable, cliPath) => {
  109. await withLayout((_root, write) => {
  110. const npm = write(executable, '@ECHO OFF\r\n');
  111. const cli = write(cliPath);
  112. expect(bind(npm).args[0]).toBe(cli);
  113. });
  114. });
  115. it.each([
  116. ['missing target', shim()],
  117. ['directory target', shim()],
  118. ['traversal', shim(`../${target}`)],
  119. ['dynamic version', shim('nodejs/$(node -v)/lib/node_modules/npm/bin/npm-cli.js')],
  120. [
  121. 'ambiguous branches',
  122. shim().replace('exec node "$basedir/nodejs/26.3.1/', 'exec node "$basedir/nodejs/24.2.0/'),
  123. ],
  124. ['comment-only target', `#!/bin/sh\n# exec node "$basedir/${target}" "$@"`],
  125. ['unknown wrapper', '#!/bin/sh\nexec npm "$@"'],
  126. ])('rejects %s without searching other installed versions', async (kind, content) => {
  127. await withLayout((root, write) => {
  128. const npm = write('pnpm/npm', content);
  129. write('pnpm/nodejs/24.2.0/lib/node_modules/npm/bin/npm-cli.js');
  130. if (kind === 'directory target')
  131. mkdirSync(path.join(root, 'pnpm', target), { recursive: true });
  132. else if (kind !== 'missing target') write(`pnpm/${target}`);
  133. write(target);
  134. expect(() => bind(npm)).toThrow('Cannot locate npm-cli.js for the owned npm executable');
  135. });
  136. });
  137. it.skipIf(process.platform === 'win32').each(['file', 'version directory', 'dangling file'])(
  138. 'rejects a pnpm %s symlink escaping its version directory',
  139. async (kind) => {
  140. await withLayout((root, write) => {
  141. const npm = write('pnpm/npm', shim());
  142. const outside = write('outside/lib/node_modules/npm/bin/npm-cli.js');
  143. const cli = path.join(root, 'pnpm', target);
  144. if (kind === 'version directory') {
  145. mkdirSync(path.join(root, 'pnpm/nodejs'), { recursive: true });
  146. symlinkSync(path.join(root, 'outside'), path.join(root, 'pnpm/nodejs/26.3.1'));
  147. } else {
  148. mkdirSync(path.dirname(cli), { recursive: true });
  149. symlinkSync(kind === 'dangling file' ? `${outside}.missing` : outside, cli);
  150. }
  151. expect(() => bind(npm)).toThrow('Cannot locate npm-cli.js for the owned npm executable');
  152. });
  153. },
  154. );
  155. it('rejects a missing owned executable even when a conventional CLI exists', async () => {
  156. await withLayout((root, write) => {
  157. write('node_modules/npm/bin/npm-cli.js');
  158. expect(() => bind(path.join(root, 'npm'))).toThrow();
  159. });
  160. });
  161. });
  162. describe('McodeUpdateApplication', () => {
  163. it('keeps signed managed-installer check and apply behind one product intent', async () => {
  164. const check = vi.fn(async () => ({
  165. status: 'available' as const,
  166. channel: 'stable' as const,
  167. currentVersion: '1.2.3',
  168. latestVersion: '1.2.4',
  169. manifest: {} as never,
  170. }));
  171. const apply = vi.fn(async () => ({
  172. ...(await check()),
  173. applied: true,
  174. installRoot: '/managed',
  175. }));
  176. const application = createApplication({
  177. detectInstallSource: async () => 'managed-installer',
  178. createManagedService: () => ({ check, apply }),
  179. });
  180. const plan = await application.inspect();
  181. expect(plan).toMatchObject({
  182. kind: 'available',
  183. source: 'managed-installer',
  184. currentVersion: '1.2.3',
  185. latestVersion: '1.2.4',
  186. channel: 'stable',
  187. });
  188. const signal = new AbortController().signal;
  189. const onPhase = vi.fn();
  190. await expect(application.apply(plan, { signal, onPhase })).resolves.toMatchObject({
  191. applied: true,
  192. message: 'MCode 1.2.4 is installed. Restart running MCode sessions to use it.',
  193. });
  194. expect(apply).toHaveBeenCalledWith({
  195. channel: 'stable',
  196. version: '1.2.4',
  197. signal,
  198. onPhase,
  199. });
  200. });
  201. it('delegates global package installations to their owning package manager', async () => {
  202. const runPackageManager = vi.fn(async () => undefined);
  203. const createManagedService = vi.fn();
  204. const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
  205. const application = createApplication(
  206. {
  207. detectInstallSource: async () => 'pnpm-global',
  208. createManagedService,
  209. runPackageManager,
  210. resolveLatestPackageVersion,
  211. },
  212. { packageTag: 'test' },
  213. );
  214. const plan = await application.inspect();
  215. expect(plan).toEqual({
  216. kind: 'package-manager',
  217. source: 'pnpm-global',
  218. currentVersion: '1.2.3',
  219. latestVersion: '1.2.4',
  220. packageTag: 'test',
  221. command: {
  222. executable: process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
  223. args: [
  224. 'add',
  225. '--global',
  226. '@minimax-ai/code@1.2.4',
  227. '--registry',
  228. 'https://registry.npmjs.org/',
  229. ],
  230. display:
  231. 'pnpm add --global @minimax-ai/code@1.2.4 --registry https://registry.npmjs.org/',
  232. },
  233. });
  234. const onOutput = vi.fn();
  235. const onPhase = vi.fn();
  236. const signal = new AbortController().signal;
  237. await expect(application.apply(plan, { onOutput, onPhase, signal })).resolves.toMatchObject({
  238. applied: true,
  239. message: 'MCode 1.2.4 was installed through pnpm. Restart MCode to use the installed version.',
  240. });
  241. expect(createManagedService).not.toHaveBeenCalled();
  242. expect(resolveLatestPackageVersion).toHaveBeenCalledWith('test');
  243. expect(runPackageManager).toHaveBeenCalledWith(plan.command, { onOutput, onPhase, signal });
  244. expect(onPhase).toHaveBeenNthCalledWith(1, { phase: 'installing', cancellable: false });
  245. expect(onPhase).toHaveBeenNthCalledWith(2, { phase: 'completed', cancellable: false });
  246. });
  247. it('does not reinstall a package-manager installation already at the registry version', async () => {
  248. const runPackageManager = vi.fn(async () => undefined);
  249. const application = createApplication({
  250. detectInstallSource: async () => 'npm-global',
  251. resolveLatestPackageVersion: async () => '1.2.3',
  252. runPackageManager,
  253. });
  254. const plan = await application.inspect();
  255. expect(plan).toEqual({
  256. kind: 'current',
  257. source: 'npm-global',
  258. currentVersion: '1.2.3',
  259. latestVersion: '1.2.3',
  260. packageTag: 'latest',
  261. });
  262. await expect(application.apply(plan)).rejects.toThrow(/cannot be applied automatically/i);
  263. expect(runPackageManager).not.toHaveBeenCalled();
  264. });
  265. it('does not downgrade a package-manager installation newer than the registry version', async () => {
  266. const application = createApplication({
  267. detectInstallSource: async () => 'bun-global',
  268. resolveLatestPackageVersion: async () => '1.2.2',
  269. });
  270. await expect(application.inspect()).resolves.toEqual({
  271. kind: 'ahead',
  272. source: 'bun-global',
  273. currentVersion: '1.2.3',
  274. latestVersion: '1.2.2',
  275. packageTag: 'latest',
  276. });
  277. });
  278. it('follows the test dist-tag when a custom prerelease sorts ahead of the tagged build', async () => {
  279. const resolveLatestPackageVersion = vi.fn(
  280. async () => '0.0.1-beta.1786162945.e3fdada2',
  281. );
  282. const application = createApplication(
  283. {
  284. detectInstallSource: async () => 'npm-global',
  285. resolveLatestPackageVersion,
  286. },
  287. {
  288. currentVersion: '0.0.1-beta.matrixfix.7069818510',
  289. packageTag: 'test',
  290. },
  291. );
  292. await expect(application.inspect()).resolves.toEqual({
  293. kind: 'package-manager',
  294. source: 'npm-global',
  295. currentVersion: '0.0.1-beta.matrixfix.7069818510',
  296. latestVersion: '0.0.1-beta.1786162945.e3fdada2',
  297. packageTag: 'test',
  298. command: {
  299. executable: process.platform === 'win32' ? 'npm.cmd' : 'npm',
  300. args: [
  301. 'install',
  302. '--global',
  303. '@minimax-ai/code@0.0.1-beta.1786162945.e3fdada2',
  304. '--ignore-scripts=false',
  305. '--include=optional',
  306. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  307. '--registry',
  308. 'https://registry.npmjs.org/',
  309. ],
  310. display:
  311. 'npm install --global @minimax-ai/code@0.0.1-beta.1786162945.e3fdada2 ' +
  312. '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
  313. '--registry https://registry.npmjs.org/',
  314. },
  315. });
  316. expect(resolveLatestPackageVersion).toHaveBeenCalledWith('test');
  317. });
  318. it('follows the preview dist-tag instead of applying SemVer downgrade protection', async () => {
  319. const application = createApplication(
  320. {
  321. detectInstallSource: async () => 'npm-global',
  322. resolveLatestPackageVersion: async () => '0.2.1-previewtrain.41',
  323. },
  324. {
  325. currentVersion: '0.2.1-previewtrain.hotfix.42',
  326. packageTag: 'preview',
  327. },
  328. );
  329. await expect(application.inspect()).resolves.toMatchObject({
  330. kind: 'package-manager',
  331. currentVersion: '0.2.1-previewtrain.hotfix.42',
  332. latestVersion: '0.2.1-previewtrain.41',
  333. packageTag: 'preview',
  334. });
  335. });
  336. it('fails closed when the installation owner cannot be identified', async () => {
  337. const runPackageManager = vi.fn(async () => undefined);
  338. const createManagedService = vi.fn();
  339. const resolveLatestPackageVersion = vi.fn(async () => '9.9.9');
  340. const application = createApplication({
  341. detectInstallSource: async () => 'unsupported',
  342. createManagedService,
  343. runPackageManager,
  344. resolveLatestPackageVersion,
  345. });
  346. const plan = await application.inspect();
  347. expect(plan).toMatchObject({
  348. kind: 'manual',
  349. source: 'unsupported',
  350. command:
  351. 'npm install --global @minimax-ai/code@latest ' +
  352. '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
  353. '--registry https://registry.npmjs.org/',
  354. });
  355. await expect(application.apply(plan)).rejects.toThrow(/cannot be applied automatically/i);
  356. expect(createManagedService).not.toHaveBeenCalled();
  357. expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
  358. expect(runPackageManager).not.toHaveBeenCalled();
  359. });
  360. it.each([
  361. '/Users/demo/project/pnpm/global/5/source/@minimax-ai/code',
  362. '/Users/demo/project/.config/yarn/global/source/@minimax-ai/code',
  363. '/Users/demo/project/.yarn/global/source/@minimax-ai/code',
  364. '/Users/demo/project/.bun/install/global/source/@minimax-ai/code',
  365. ])(
  366. 'treats a local checkout containing a global-install marker as unsupported: %s',
  367. async (packageRoot) => {
  368. const createManagedService = vi.fn();
  369. const resolveLatestPackageVersion = vi.fn(async () => '9.9.9');
  370. const application = createApplication({
  371. detectInstallSource: () =>
  372. detectMcodeInstallSource({
  373. installRoot: '/source',
  374. platform: 'darwin',
  375. packageRoot: () => packageRoot,
  376. npmGlobalPrefix: async () => '/usr/local',
  377. managedInstall: () => false,
  378. }),
  379. createManagedService,
  380. resolveLatestPackageVersion,
  381. });
  382. await expect(application.inspect()).resolves.toMatchObject({
  383. kind: 'manual',
  384. source: 'unsupported',
  385. });
  386. expect(createManagedService).not.toHaveBeenCalled();
  387. expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
  388. },
  389. );
  390. it('keeps a future public package on the official npm registry', async () => {
  391. const application = createApplication(
  392. { detectInstallSource: async () => 'unsupported' },
  393. { packageName: '@minimax-ai/code' },
  394. );
  395. await expect(application.inspect()).resolves.toMatchObject({
  396. kind: 'manual',
  397. command:
  398. 'npm install --global @minimax-ai/code@latest ' +
  399. '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
  400. '--registry https://registry.npmjs.org/',
  401. });
  402. });
  403. it('keeps a China mirror prefix on the same registry during updates', async () => {
  404. const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
  405. const application = createApplication(
  406. {
  407. detectInstallSource: async () => 'npm-prefix',
  408. resolveLatestPackageVersion,
  409. },
  410. {
  411. packageName: '@minimax-ai/code',
  412. prefixInstall: {
  413. executable: '/opt/minimax/runtime/node/bin/npm',
  414. packageName: '@minimax-ai/code',
  415. prefix: '/opt/minimax',
  416. registry: 'https://registry.npmmirror.com/',
  417. },
  418. },
  419. );
  420. await expect(application.inspect()).resolves.toMatchObject({
  421. kind: 'package-manager',
  422. source: 'npm-prefix',
  423. command: {
  424. args: [
  425. 'install',
  426. '--global',
  427. '--prefix',
  428. '/opt/minimax',
  429. '@minimax-ai/code@1.2.4',
  430. '--ignore-scripts=false',
  431. '--include=optional',
  432. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  433. '--registry',
  434. 'https://registry.npmmirror.com/',
  435. ],
  436. },
  437. });
  438. expect(resolveLatestPackageVersion).toHaveBeenCalledWith('latest');
  439. });
  440. it('reads the installed public package identity from its real entry path', () => {
  441. const temporaryRoot = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-public-'));
  442. const packageRoot = path.join(
  443. temporaryRoot,
  444. 'lib',
  445. 'node_modules',
  446. '@minimax-ai',
  447. 'code',
  448. );
  449. try {
  450. mkdirSync(packageRoot, { recursive: true });
  451. writeFileSync(
  452. path.join(packageRoot, 'package.json'),
  453. JSON.stringify({ name: '@minimax-ai/code' }),
  454. );
  455. const entryFile = path.join(packageRoot, 'cli.js');
  456. writeFileSync(entryFile, '');
  457. expect(resolveMcodePackageName(entryFile)).toBe('@minimax-ai/code');
  458. expect(resolveInstalledMcodePackageVersion(entryFile)).toBeUndefined();
  459. } finally {
  460. rmSync(temporaryRoot, { recursive: true, force: true });
  461. }
  462. });
  463. it('recognizes only the internal package identity as environment-selectable', () => {
  464. expect(isInternalMcodePackageName('@minimax/code')).toBe(true);
  465. expect(isInternalMcodePackageName('@minimax-ai/code')).toBe(false);
  466. expect(isInternalMcodePackageName(undefined)).toBe(false);
  467. });
  468. it('rejects a package-manager update when the installed exact version does not match', async () => {
  469. const application = createApplication({
  470. detectInstallSource: async () => 'npm-global',
  471. resolveLatestPackageVersion: async () => '1.2.4',
  472. runPackageManager: async () => undefined,
  473. readInstalledPackageVersion: () => '1.2.3',
  474. });
  475. const plan = await application.inspect();
  476. await expect(application.apply(plan)).rejects.toThrow(
  477. 'MCode update installed 1.2.3; expected 1.2.4.',
  478. );
  479. });
  480. });
  481. describe('MCode update install-source commands', () => {
  482. it.each([
  483. ['/opt/homebrew/lib/node_modules/@minimax-ai/code', 'npm-global'],
  484. ['C:\\Users\\demo\\AppData\\Roaming\\npm\\node_modules\\@minimax\\code', 'npm-global'],
  485. ['/usr/local/lib/node_modules/@minimax-ai/code', 'npm-global'],
  486. ['/Users/demo/.local/share/pnpm/global/5/node_modules/@minimax-ai/code', 'pnpm-global'],
  487. [
  488. '/Users/demo/Library/pnpm/global/v11/10c0afe5/node_modules/@minimax-ai/code',
  489. 'pnpm-global',
  490. ],
  491. [
  492. 'C:\\Users\\demo\\AppData\\Local\\pnpm\\global\\5\\node_modules\\@minimax\\code',
  493. 'pnpm-global',
  494. ],
  495. [
  496. 'C:\\Users\\demo\\AppData\\Local\\pnpm\\global\\v11\\10c0afe5\\node_modules\\@minimax-ai\\code',
  497. 'pnpm-global',
  498. ],
  499. ['/Users/demo/.config/yarn/global/node_modules/@minimax-ai/code', 'yarn-global'],
  500. [
  501. 'C:\\Users\\demo\\.config\\yarn\\global\\node_modules\\@minimax\\code',
  502. 'yarn-global',
  503. ],
  504. ['/Users/demo/.bun/install/global/node_modules/@minimax-ai/code', 'bun-global'],
  505. [
  506. 'C:\\Users\\demo\\.bun\\install\\global\\node_modules\\@minimax\\code',
  507. 'bun-global',
  508. ],
  509. ] as const)('classifies %s as %s', (packageRoot, expected) => {
  510. expect(classifyMcodeInstallPath(packageRoot)).toBe(expected);
  511. });
  512. it('uses Windows command shims for the Windows-aware process launcher', () => {
  513. expect(buildMcodePackageManagerCommand('npm-global', '1.2.4', 'win32')).toEqual({
  514. executable: 'npm.cmd',
  515. args: [
  516. 'install',
  517. '--global',
  518. '@minimax-ai/code@1.2.4',
  519. '--ignore-scripts=false',
  520. '--include=optional',
  521. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  522. '--registry',
  523. 'https://registry.npmjs.org/',
  524. ],
  525. display:
  526. 'npm install --global @minimax-ai/code@1.2.4 --ignore-scripts=false --include=optional ' +
  527. '--allow-scripts=@minimax-ai/code,better-sqlite3 --registry https://registry.npmjs.org/',
  528. });
  529. expect(buildMcodePackageManagerCommand('bun-global', '1.2.4', 'win32')).toEqual({
  530. executable: 'bun.exe',
  531. args: [
  532. 'add',
  533. '--global',
  534. '@minimax-ai/code@1.2.4',
  535. '--registry',
  536. 'https://registry.npmjs.org/',
  537. ],
  538. display:
  539. 'bun add --global @minimax-ai/code@1.2.4 --registry https://registry.npmjs.org/',
  540. });
  541. });
  542. it('uses the installer-owned npm executable, prefix, package, and registry', () => {
  543. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  544. const command = buildMcodePackageManagerCommand(
  545. 'npm-prefix',
  546. '1.2.4',
  547. 'linux',
  548. distribution,
  549. {
  550. executable: '/opt/minimax/runtime/node/bin/npm',
  551. packageName: '@minimax-ai/code',
  552. prefix: '/opt/minimax',
  553. registry: 'https://registry.npmjs.org/',
  554. },
  555. );
  556. expect(command).toEqual({
  557. executable: '/opt/minimax/runtime/node/bin/npm',
  558. args: [
  559. 'install',
  560. '--global',
  561. '--prefix',
  562. '/opt/minimax',
  563. '@minimax-ai/code@1.2.4',
  564. '--ignore-scripts=false',
  565. '--include=optional',
  566. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  567. '--registry',
  568. 'https://registry.npmjs.org/',
  569. ],
  570. display:
  571. '/opt/minimax/runtime/node/bin/npm install --global --prefix /opt/minimax ' +
  572. '@minimax-ai/code@1.2.4 --ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 --registry https://registry.npmjs.org/',
  573. });
  574. });
  575. it('keeps the explicit public mirror for installer-owned updates', () => {
  576. const distribution = resolveMcodeNpmDistribution(
  577. '@minimax-ai/code',
  578. 'https://registry.npmmirror.com',
  579. );
  580. const command = buildMcodePackageManagerCommand(
  581. 'npm-prefix',
  582. '1.2.4',
  583. 'linux',
  584. distribution,
  585. {
  586. executable: '/opt/minimax/runtime/node/bin/npm',
  587. packageName: '@minimax-ai/code',
  588. prefix: '/opt/minimax',
  589. registry: 'https://registry.npmmirror.com/',
  590. },
  591. );
  592. expect(command.args).toEqual([
  593. 'install',
  594. '--global',
  595. '--prefix',
  596. '/opt/minimax',
  597. '@minimax-ai/code@1.2.4',
  598. '--ignore-scripts=false',
  599. '--include=optional',
  600. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  601. '--registry',
  602. 'https://registry.npmmirror.com/',
  603. ]);
  604. expect(() =>
  605. resolveMcodeNpmDistribution('@minimax-ai/code', 'https://registry.example.com/'),
  606. ).toThrow('Unsupported MCode npm registry');
  607. });
  608. it('uses the active package manifest entry when argv points at the npm bin shim', async () => {
  609. const runPackageManager = vi.fn(async () => undefined);
  610. const activateVersionedPrefix = vi.fn(() => '/opt/minimax/releases/1.2.4');
  611. const releasePrefixUpdateLock = vi.fn();
  612. const application = createApplication(
  613. {
  614. detectInstallSource: async () => 'npm-prefix',
  615. resolveLatestPackageVersion: async () => '1.2.4',
  616. runPackageManager,
  617. createVersionedPrefixStaging: () => '/opt/minimax/releases/.staging-1.2.4',
  618. prepareVersionedPrefixStaging: vi.fn(),
  619. readPrefixPackageMetadata: (prefix, packageName) => ({
  620. packageRoot: `${prefix}/lib/node_modules/${packageName}`,
  621. version: '1.2.4',
  622. binEntry: 'dist/index.js',
  623. mcodeToolsBinEntry: 'mcode-tools.js',
  624. }),
  625. validatePrefixPackage: vi.fn(async () => undefined),
  626. activateVersionedPrefix,
  627. acquirePrefixUpdateLock: () => releasePrefixUpdateLock,
  628. },
  629. {
  630. entryFile: '/opt/minimax/bin/mcode',
  631. packageName: '@minimax-ai/code',
  632. prefixInstall: {
  633. executable: '/opt/minimax/runtime/node/bin/npm',
  634. packageName: '@minimax-ai/code',
  635. prefix: '/opt/minimax',
  636. registry: 'https://registry.npmjs.org/',
  637. },
  638. },
  639. );
  640. const plan = await application.inspect();
  641. await expect(application.apply(plan)).resolves.toEqual({
  642. applied: true,
  643. restartRequired: false,
  644. message:
  645. 'MCode 1.2.4 is installed. New MCode sessions will use it; running sessions can continue normally.',
  646. });
  647. expect(activateVersionedPrefix).toHaveBeenCalledWith({
  648. stagingPrefix: '/opt/minimax/releases/.staging-1.2.4',
  649. activePrefix: '/opt/minimax',
  650. packageName: '@minimax-ai/code',
  651. expectedVersion: '1.2.4',
  652. runtimeExecutable: process.execPath,
  653. npmExecutable: '/opt/minimax/runtime/node/bin/npm',
  654. registry: 'https://registry.npmjs.org/',
  655. });
  656. expect(releasePrefixUpdateLock).toHaveBeenCalledOnce();
  657. });
  658. it('reports malformed pending metadata instead of treating it as a staged update', async () => {
  659. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-malformed-pending-'));
  660. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
  661. const entryFile = path.join(packageRoot, 'dist/index.js');
  662. const pendingFile = path.join(prefix, '.mcode-update-pending.json');
  663. try {
  664. mkdirSync(path.dirname(entryFile), { recursive: true });
  665. writeFileSync(entryFile, '');
  666. writeFileSync(
  667. path.join(packageRoot, 'package.json'),
  668. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
  669. );
  670. writeFileSync(
  671. path.join(prefix, 'install.json'),
  672. JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
  673. );
  674. writeFileSync(pendingFile, '{}');
  675. const application = new McodeUpdateApplication(
  676. {
  677. currentVersion: '1.2.3',
  678. entryFile,
  679. environment: {},
  680. installRoot: prefix,
  681. prefixInstall: {
  682. executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
  683. packageName: '@minimax-ai/code',
  684. prefix,
  685. registry: 'https://registry.npmjs.org/',
  686. },
  687. },
  688. {
  689. detectInstallSource: async () => 'npm-prefix',
  690. resolveLatestPackageVersion: async () => '1.2.4',
  691. },
  692. );
  693. await expect(application.inspect()).rejects.toThrow(
  694. `MCode pending update metadata is invalid at ${pendingFile}.`,
  695. );
  696. } finally {
  697. rmSync(prefix, { recursive: true, force: true });
  698. }
  699. });
  700. it('rejects a pending journal owned by a different active prefix', async () => {
  701. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-foreign-pending-'));
  702. const foreignPrefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-foreign-owner-'));
  703. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
  704. const entryFile = path.join(packageRoot, 'dist/index.js');
  705. const pendingFile = path.join(prefix, '.mcode-update-pending.json');
  706. const foreignStaging = path.join(path.dirname(foreignPrefix), '.foreign-staging');
  707. try {
  708. mkdirSync(path.dirname(entryFile), { recursive: true });
  709. writeFileSync(entryFile, '');
  710. writeFileSync(
  711. path.join(packageRoot, 'package.json'),
  712. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
  713. );
  714. writeFileSync(
  715. path.join(prefix, 'install.json'),
  716. JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
  717. );
  718. writeFileSync(
  719. pendingFile,
  720. JSON.stringify({
  721. schemaVersion: 1,
  722. stagingPrefix: foreignStaging,
  723. activePrefix: foreignPrefix,
  724. activeModulesRoot: resolveMcodePrefixModulesRoot(foreignPrefix, process.platform),
  725. stagedModulesRoot: resolveMcodePrefixModulesRoot(foreignStaging, process.platform),
  726. backupModulesRoot: `${resolveMcodePrefixModulesRoot(foreignPrefix, process.platform)}.mcode-update-backup`,
  727. packageName: '@minimax-ai/code',
  728. expectedVersion: '1.2.4',
  729. launchers: resolveMcodePrefixLauncherPairs(foreignPrefix, foreignStaging, process.platform),
  730. }),
  731. );
  732. const application = new McodeUpdateApplication(
  733. {
  734. currentVersion: '1.2.3',
  735. entryFile,
  736. environment: {},
  737. installRoot: prefix,
  738. prefixInstall: {
  739. executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
  740. packageName: '@minimax-ai/code',
  741. prefix,
  742. registry: 'https://registry.npmjs.org/',
  743. },
  744. },
  745. {
  746. detectInstallSource: async () => 'npm-prefix',
  747. resolveLatestPackageVersion: async () => '1.2.4',
  748. },
  749. );
  750. await expect(application.inspect()).rejects.toThrow(
  751. `MCode pending update file is outside its active prefix: ${pendingFile}`,
  752. );
  753. } finally {
  754. rmSync(prefix, { recursive: true, force: true });
  755. rmSync(foreignPrefix, { recursive: true, force: true });
  756. rmSync(foreignStaging, { recursive: true, force: true });
  757. }
  758. });
  759. it('resumes a valid staged update without checking for a newer registry version', async () => {
  760. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-valid-pending-'));
  761. const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.staging`);
  762. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
  763. const stagedPackageRoot = path.join(resolveMcodePrefixModulesRoot(stagingPrefix), '@minimax-ai/code');
  764. const entryFile = path.join(packageRoot, 'dist/index.js');
  765. try {
  766. mkdirSync(path.dirname(entryFile), { recursive: true });
  767. mkdirSync(path.join(stagedPackageRoot, 'dist'), { recursive: true });
  768. mkdirSync(path.join(prefix, 'bin'), { recursive: true });
  769. mkdirSync(path.join(stagingPrefix, 'bin'), { recursive: true });
  770. writeFileSync(entryFile, '');
  771. writeFileSync(
  772. path.join(packageRoot, 'package.json'),
  773. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3', bin: { mcode: 'dist/index.js' } }),
  774. );
  775. writeFileSync(
  776. path.join(stagedPackageRoot, 'package.json'),
  777. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.4', bin: { mcode: 'dist/index.js' } }),
  778. );
  779. writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.3');
  780. writeFileSync(path.join(stagingPrefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.4');
  781. for (const pair of resolveMcodePrefixLauncherPairs(prefix, stagingPrefix)) {
  782. mkdirSync(path.dirname(pair.activePath), { recursive: true });
  783. mkdirSync(path.dirname(pair.stagedPath), { recursive: true });
  784. writeFileSync(pair.activePath, '1.2.3');
  785. writeFileSync(pair.stagedPath, '1.2.4');
  786. }
  787. writeFileSync(
  788. path.join(prefix, 'install.json'),
  789. JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
  790. );
  791. const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
  792. writeMcodePrefixUpdatePending({
  793. stagingPrefix,
  794. activePrefix: prefix,
  795. activeModulesRoot,
  796. stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
  797. backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
  798. packageName: '@minimax-ai/code',
  799. expectedVersion: '1.2.4',
  800. launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
  801. });
  802. const resolveLatestPackageVersion = vi.fn(async () => '1.2.5');
  803. const application = new McodeUpdateApplication(
  804. {
  805. currentVersion: '1.2.3',
  806. entryFile,
  807. environment: {},
  808. installRoot: prefix,
  809. platform: process.platform,
  810. prefixInstall: {
  811. executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
  812. packageName: '@minimax-ai/code',
  813. prefix,
  814. registry: 'https://registry.npmjs.org/',
  815. },
  816. },
  817. {
  818. detectInstallSource: async () => 'npm-prefix',
  819. resolveLatestPackageVersion,
  820. },
  821. );
  822. const plan = await application.inspect();
  823. expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
  824. await expect(application.apply(plan)).resolves.toEqual({
  825. applied: false,
  826. restartRequired: true,
  827. message: 'MCode 1.2.4 is already staged. It will activate after this process exits.',
  828. });
  829. } finally {
  830. rmSync(prefix, { recursive: true, force: true });
  831. rmSync(stagingPrefix, { recursive: true, force: true });
  832. }
  833. });
  834. it('rejects a pending update whose staged package and launchers are missing', async () => {
  835. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-missing-staging-'));
  836. const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.missing`);
  837. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
  838. const entryFile = path.join(packageRoot, 'dist/index.js');
  839. try {
  840. mkdirSync(path.dirname(entryFile), { recursive: true });
  841. mkdirSync(path.join(prefix, 'bin'), { recursive: true });
  842. writeFileSync(entryFile, '');
  843. writeFileSync(
  844. path.join(packageRoot, 'package.json'),
  845. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3', bin: { mcode: 'dist/index.js' } }),
  846. );
  847. writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.3');
  848. writeFileSync(
  849. path.join(prefix, 'install.json'),
  850. JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
  851. );
  852. const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
  853. const pendingFile = writeMcodePrefixUpdatePending({
  854. stagingPrefix,
  855. activePrefix: prefix,
  856. activeModulesRoot,
  857. stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
  858. backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
  859. packageName: '@minimax-ai/code',
  860. expectedVersion: '1.2.4',
  861. launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
  862. });
  863. const application = new McodeUpdateApplication(
  864. {
  865. currentVersion: '1.2.3',
  866. entryFile,
  867. environment: {},
  868. installRoot: prefix,
  869. platform: process.platform,
  870. prefixInstall: {
  871. executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
  872. packageName: '@minimax-ai/code',
  873. prefix,
  874. registry: 'https://registry.npmjs.org/',
  875. },
  876. },
  877. {
  878. detectInstallSource: async () => 'npm-prefix',
  879. resolveLatestPackageVersion: async () => '1.2.4',
  880. },
  881. );
  882. await expect(application.inspect()).rejects.toThrow(
  883. `MCode pending update artifacts are incomplete at ${pendingFile}.`,
  884. );
  885. } finally {
  886. rmSync(prefix, { recursive: true, force: true });
  887. rmSync(stagingPrefix, { recursive: true, force: true });
  888. }
  889. });
  890. it('finishes cleanup when the staged version is already active', async () => {
  891. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-pending-cleanup-'));
  892. const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.activated`);
  893. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
  894. const entryFile = path.join(packageRoot, 'dist/index.js');
  895. try {
  896. mkdirSync(path.dirname(entryFile), { recursive: true });
  897. mkdirSync(path.join(prefix, 'bin'), { recursive: true });
  898. writeFileSync(entryFile, '');
  899. writeFileSync(
  900. path.join(packageRoot, 'package.json'),
  901. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.4', bin: { mcode: 'dist/index.js' } }),
  902. );
  903. writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.4');
  904. for (const pair of resolveMcodePrefixLauncherPairs(prefix, stagingPrefix)) {
  905. mkdirSync(path.dirname(pair.activePath), { recursive: true });
  906. writeFileSync(pair.activePath, '1.2.4');
  907. }
  908. writeFileSync(
  909. path.join(prefix, 'install.json'),
  910. JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
  911. );
  912. const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
  913. writeMcodePrefixUpdatePending({
  914. stagingPrefix,
  915. activePrefix: prefix,
  916. activeModulesRoot,
  917. stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
  918. backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
  919. packageName: '@minimax-ai/code',
  920. expectedVersion: '1.2.4',
  921. launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
  922. });
  923. const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
  924. const application = new McodeUpdateApplication(
  925. {
  926. currentVersion: '1.2.4',
  927. entryFile,
  928. environment: {},
  929. installRoot: prefix,
  930. platform: process.platform,
  931. prefixInstall: {
  932. executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
  933. packageName: '@minimax-ai/code',
  934. prefix,
  935. registry: 'https://registry.npmjs.org/',
  936. },
  937. },
  938. {
  939. detectInstallSource: async () => 'npm-prefix',
  940. resolveLatestPackageVersion,
  941. },
  942. );
  943. const plan = await application.inspect();
  944. expect(plan).toMatchObject({
  945. kind: 'package-manager',
  946. source: 'npm-prefix',
  947. currentVersion: '1.2.4',
  948. latestVersion: '1.2.4',
  949. });
  950. expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
  951. await expect(application.apply(plan)).resolves.toEqual({
  952. applied: false,
  953. restartRequired: true,
  954. message: 'MCode 1.2.4 is already active. Restarting will finish update cleanup.',
  955. });
  956. } finally {
  957. rmSync(prefix, { recursive: true, force: true });
  958. rmSync(stagingPrefix, { recursive: true, force: true });
  959. }
  960. });
  961. it.each(['https://registry.npmjs.org/', 'https://registry.npmmirror.com/'])(
  962. 'recovers installer prefix ownership from the versioned receipt using %s',
  963. async (registry) => {
  964. const temporaryRoot = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-prefix-'));
  965. const packageRoot = path.join(
  966. temporaryRoot,
  967. ...(process.platform === 'win32' ? [] : ['lib']),
  968. 'node_modules',
  969. '@minimax-ai',
  970. 'code',
  971. );
  972. const npmExecutable = path.join(temporaryRoot, 'runtime', 'node', ...(process.platform === 'win32' ? ['npm.cmd'] : ['bin', 'npm']));
  973. const entryFile = path.join(packageRoot, 'cli.js');
  974. try {
  975. mkdirSync(packageRoot, { recursive: true });
  976. mkdirSync(path.dirname(npmExecutable), { recursive: true });
  977. writeFileSync(npmExecutable, '');
  978. writeFileSync(entryFile, '');
  979. writeFileSync(
  980. path.join(packageRoot, 'package.json'),
  981. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
  982. );
  983. writeFileSync(
  984. path.join(temporaryRoot, 'install.json'),
  985. `\uFEFF${JSON.stringify({
  986. schemaVersion: 1,
  987. product: 'minimax-code',
  988. updateOwner: 'npm-prefix',
  989. packageManager: 'npm',
  990. packageName: '@minimax-ai/code',
  991. registry,
  992. distTag: 'latest',
  993. prefix: temporaryRoot,
  994. npmExecutable,
  995. })}`,
  996. );
  997. const prefixInstall = resolveMcodeNpmPrefixInstall(entryFile, process.platform);
  998. expect(prefixInstall).toEqual({
  999. executable: npmExecutable,
  1000. packageName: '@minimax-ai/code',
  1001. prefix: realpathSync(temporaryRoot),
  1002. registry,
  1003. });
  1004. expect(resolveInstalledMcodePackageVersion(entryFile)).toBe('1.2.3');
  1005. await expect(
  1006. detectMcodeInstallSource({
  1007. installRoot: temporaryRoot,
  1008. platform: process.platform,
  1009. packageRoot: () => packageRoot,
  1010. prefixInstall: () => prefixInstall,
  1011. managedInstall: () => false,
  1012. }),
  1013. ).resolves.toBe('npm-prefix');
  1014. } finally {
  1015. rmSync(temporaryRoot, { recursive: true, force: true });
  1016. }
  1017. },
  1018. );
  1019. it('resolves versioned installer ownership from a release package entry', () => {
  1020. const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-versioned-prefix-'));
  1021. const packageRoot = path.join(
  1022. prefix,
  1023. 'releases/1.2.3',
  1024. ...(process.platform === 'win32' ? [] : ['lib']),
  1025. 'node_modules/@minimax-ai/code',
  1026. );
  1027. const npmExecutable = path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm');
  1028. const entryFile = path.join(packageRoot, 'dist/index.js');
  1029. try {
  1030. mkdirSync(path.dirname(entryFile), { recursive: true });
  1031. mkdirSync(path.dirname(npmExecutable), { recursive: true });
  1032. writeFileSync(entryFile, '');
  1033. writeFileSync(npmExecutable, '');
  1034. writeFileSync(
  1035. path.join(packageRoot, 'package.json'),
  1036. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
  1037. );
  1038. writeFileSync(
  1039. path.join(prefix, 'install.json'),
  1040. JSON.stringify({
  1041. schemaVersion: 2,
  1042. product: 'minimax-code',
  1043. updateOwner: 'npm-prefix',
  1044. packageManager: 'npm',
  1045. packageName: '@minimax-ai/code',
  1046. registry: 'https://registry.npmjs.org/',
  1047. distTag: 'latest',
  1048. prefix,
  1049. npmExecutable,
  1050. layoutVersion: 2,
  1051. releasesDirectory: 'releases',
  1052. currentFile: 'current',
  1053. }),
  1054. );
  1055. expect(resolveMcodeNpmPrefixInstall(entryFile, process.platform)).toEqual({
  1056. executable: npmExecutable,
  1057. packageName: '@minimax-ai/code',
  1058. prefix: realpathSync(prefix),
  1059. registry: 'https://registry.npmjs.org/',
  1060. });
  1061. } finally {
  1062. rmSync(prefix, { recursive: true, force: true });
  1063. }
  1064. });
  1065. it('recovers a legacy installer prefix only from its package root and adjacent npm', () => {
  1066. const temporaryParent = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-legacy-'));
  1067. const prefix = path.join(temporaryParent, '.minimax-code');
  1068. const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai', 'code');
  1069. const nodeExecutable = path.join(prefix, 'runtime', 'node', ...(process.platform === 'win32' ? ['node.exe'] : ['bin', 'node']));
  1070. const npmExecutable = path.join(prefix, 'runtime', 'node', ...(process.platform === 'win32' ? ['npm.cmd'] : ['bin', 'npm']));
  1071. const entryFile = path.join(packageRoot, 'cli.js');
  1072. try {
  1073. mkdirSync(packageRoot, { recursive: true });
  1074. mkdirSync(path.dirname(nodeExecutable), { recursive: true });
  1075. writeFileSync(nodeExecutable, '');
  1076. writeFileSync(npmExecutable, '');
  1077. writeFileSync(entryFile, '');
  1078. writeFileSync(
  1079. path.join(packageRoot, 'package.json'),
  1080. JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
  1081. );
  1082. expect(resolveMcodeNpmPrefixInstall(entryFile, process.platform, nodeExecutable)).toEqual({
  1083. executable: npmExecutable,
  1084. packageName: '@minimax-ai/code',
  1085. prefix: realpathSync(prefix),
  1086. registry: 'https://registry.npmjs.org/',
  1087. });
  1088. } finally {
  1089. rmSync(temporaryParent, { recursive: true, force: true });
  1090. }
  1091. });
  1092. it.each([
  1093. ['npm-global', 'npm install --global'],
  1094. ['pnpm-global', 'pnpm add --global'],
  1095. ['yarn-global', 'yarn global add'],
  1096. ['bun-global', 'bun add --global'],
  1097. ] as const)('pins %s updates to the internal registry', (source, prefix) => {
  1098. const command = buildMcodePackageManagerCommand(source, '1.2.4', 'linux');
  1099. const scriptOptions =
  1100. source === 'npm-global'
  1101. ? ' --ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3'
  1102. : '';
  1103. expect(command.args.slice(-2)).toEqual([
  1104. '--registry',
  1105. 'https://registry.npmjs.org/',
  1106. ]);
  1107. expect(command.display).toBe(
  1108. `${prefix} @minimax-ai/code@1.2.4${scriptOptions} --registry https://registry.npmjs.org/`,
  1109. );
  1110. });
  1111. it('resolves the target version from the internal registry with npm shipped by Node', async () => {
  1112. const run = vi.fn(async () => '"1.2.4"');
  1113. await expect(
  1114. resolveLatestMcodeRegistryVersion('test', { platform: 'linux', run }),
  1115. ).resolves.toBe('1.2.4');
  1116. expect(run).toHaveBeenCalledWith('npm', [
  1117. 'view',
  1118. '@minimax-ai/code@test',
  1119. 'version',
  1120. '--json',
  1121. '--registry',
  1122. 'https://registry.npmjs.org/',
  1123. '--fetch-timeout',
  1124. '30000',
  1125. ]);
  1126. });
  1127. it('uses installer-owned npm for a prefix installation registry lookup', async () => {
  1128. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  1129. const run = vi.fn(async () => '"1.2.4"');
  1130. await expect(
  1131. resolveLatestMcodeRegistryVersion('latest', {
  1132. platform: 'linux',
  1133. run,
  1134. distribution,
  1135. npmExecutable: '/opt/minimax/runtime/node/bin/npm',
  1136. }),
  1137. ).resolves.toBe('1.2.4');
  1138. expect(run).toHaveBeenCalledWith('/opt/minimax/runtime/node/bin/npm', [
  1139. 'view',
  1140. '@minimax-ai/code@latest',
  1141. 'version',
  1142. '--json',
  1143. '--registry',
  1144. 'https://registry.npmjs.org/',
  1145. '--fetch-timeout',
  1146. '30000',
  1147. ]);
  1148. });
  1149. it('accepts npm registry metadata returned as a single-item JSON array', async () => {
  1150. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  1151. await expect(
  1152. resolveLatestMcodeRegistryVersion('latest', {
  1153. platform: 'linux',
  1154. run: async () => '["1.2.4"]',
  1155. distribution,
  1156. }),
  1157. ).resolves.toBe('1.2.4');
  1158. });
  1159. it('keeps public package lookup and installation on the official npm registry', async () => {
  1160. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  1161. const run = vi.fn(async () => '"1.2.4"');
  1162. expect(distribution).toEqual({
  1163. packageName: '@minimax-ai/code',
  1164. registry: 'https://registry.npmjs.org/',
  1165. });
  1166. await expect(
  1167. resolveLatestMcodeRegistryVersion('latest', {
  1168. platform: 'linux',
  1169. run,
  1170. distribution,
  1171. }),
  1172. ).resolves.toBe('1.2.4');
  1173. expect(run).toHaveBeenCalledWith('npm', [
  1174. 'view',
  1175. '@minimax-ai/code@latest',
  1176. 'version',
  1177. '--json',
  1178. '--registry',
  1179. 'https://registry.npmjs.org/',
  1180. '--fetch-timeout',
  1181. '30000',
  1182. ]);
  1183. expect(
  1184. buildMcodePackageManagerCommand('npm-global', '1.2.4', 'linux', distribution),
  1185. ).toMatchObject({
  1186. args: [
  1187. 'install',
  1188. '--global',
  1189. '@minimax-ai/code@1.2.4',
  1190. '--ignore-scripts=false',
  1191. '--include=optional',
  1192. '--allow-scripts=@minimax-ai/code,better-sqlite3',
  1193. '--registry',
  1194. 'https://registry.npmjs.org/',
  1195. ],
  1196. });
  1197. });
  1198. it('does not fall back to the internal registry when the public package is absent', async () => {
  1199. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  1200. const run = vi.fn(async () => {
  1201. throw new Error('E404 Not Found');
  1202. });
  1203. await expect(
  1204. resolveLatestMcodeRegistryVersion('latest', {
  1205. platform: 'linux',
  1206. run,
  1207. distribution,
  1208. }),
  1209. ).rejects.toThrow('E404 Not Found');
  1210. expect(run).toHaveBeenCalledTimes(1);
  1211. });
  1212. it.each([
  1213. ['[]', /invalid latest version/u],
  1214. ['"1.2.4-beta.1"', /stable semantic version/u],
  1215. ])('rejects invalid public latest metadata: %s', async (metadata, error) => {
  1216. const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
  1217. await expect(
  1218. resolveLatestMcodeRegistryVersion('latest', {
  1219. platform: 'linux',
  1220. run: async () => metadata,
  1221. distribution,
  1222. }),
  1223. ).rejects.toThrow(error);
  1224. });
  1225. it.each([
  1226. ['prod', 'latest'],
  1227. ['staging', 'preview'],
  1228. ['test', 'test'],
  1229. [undefined, 'latest'],
  1230. ] as const)('maps the %s build to the %s update tag', (environment, tag) => {
  1231. expect(resolveMcodeNpmDistTag(environment)).toBe(tag);
  1232. });
  1233. it('keeps an explicitly embedded preview update channel independent of the backend', () => {
  1234. expect(resolveMcodeNpmDistTag('prod', 'preview')).toBe('preview');
  1235. });
  1236. it.each([
  1237. ['/usr/local/lib/node_modules/@minimax-ai/code', '/usr/local', 'linux'],
  1238. [
  1239. 'C:\\Users\\demo\\AppData\\Roaming\\npm\\node_modules\\@minimax\\code',
  1240. 'C:\\Users\\demo\\AppData\\Roaming\\npm',
  1241. 'win32',
  1242. ],
  1243. ] as const)('recognizes npm global ownership for %s', (packageRoot, prefix, platform) => {
  1244. expect(classifyNpmGlobalInstall(packageRoot, prefix, platform)).toBe('npm-global');
  1245. });
  1246. });
  1247. function createApplication(
  1248. dependencies: Partial<McodeUpdateApplicationDependencies>,
  1249. options: {
  1250. currentVersion?: string;
  1251. packageTag?: 'latest' | 'test' | 'preview';
  1252. packageName?: '@minimax-ai/code' | '@minimax-ai/code';
  1253. prefixInstall?: {
  1254. executable: string;
  1255. packageName: '@minimax-ai/code' | '@minimax-ai/code';
  1256. prefix: string;
  1257. registry: string;
  1258. };
  1259. } = {},
  1260. ): McodeUpdateApplication {
  1261. return new McodeUpdateApplication(
  1262. { currentVersion: '1.2.3', installRoot: '/managed', ...options },
  1263. { readInstalledPackageVersion: () => '1.2.4', ...dependencies },
  1264. );
  1265. }