update-application.test.ts 44 KB

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