| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
- import os from 'node:os';
- import path from 'node:path';
- import { describe, expect, it, vi } from 'vitest';
- import {
- McodeUpdateApplication,
- type McodeUpdateApplicationDependencies,
- } from '../../src/update/application.js';
- import {
- buildMcodePackageManagerCommand,
- classifyMcodeInstallPath,
- classifyNpmGlobalInstall,
- detectMcodeInstallSource,
- isInternalMcodePackageName,
- resolveMcodeNpmDistribution,
- resolveMcodeNpmDistTag,
- resolveMcodeNpmPrefixInstall,
- resolveMcodePackageName,
- resolveInstalledMcodePackageVersion,
- resolveLatestMcodeRegistryVersion,
- } from '../../src/update/install-source.js';
- import {
- resolveMcodePrefixLauncherPairs,
- resolveMcodePrefixModulesRoot,
- writeMcodePrefixUpdatePending,
- } from '../../src/update/prefix-update.js';
- describe('McodeUpdateApplication', () => {
- it('keeps signed managed-installer check and apply behind one product intent', async () => {
- const check = vi.fn(async () => ({
- status: 'available' as const,
- channel: 'stable' as const,
- currentVersion: '1.2.3',
- latestVersion: '1.2.4',
- manifest: {} as never,
- }));
- const apply = vi.fn(async () => ({
- ...(await check()),
- applied: true,
- installRoot: '/managed',
- }));
- const application = createApplication({
- detectInstallSource: async () => 'managed-installer',
- createManagedService: () => ({ check, apply }),
- });
- const plan = await application.inspect();
- expect(plan).toMatchObject({
- kind: 'available',
- source: 'managed-installer',
- currentVersion: '1.2.3',
- latestVersion: '1.2.4',
- channel: 'stable',
- });
- const signal = new AbortController().signal;
- const onPhase = vi.fn();
- await expect(application.apply(plan, { signal, onPhase })).resolves.toMatchObject({
- applied: true,
- message: 'MCode 1.2.4 is installed. Restart running MCode sessions to use it.',
- });
- expect(apply).toHaveBeenCalledWith({
- channel: 'stable',
- version: '1.2.4',
- signal,
- onPhase,
- });
- });
- it('delegates global package installations to their owning package manager', async () => {
- const runPackageManager = vi.fn(async () => undefined);
- const createManagedService = vi.fn();
- const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
- const application = createApplication(
- {
- detectInstallSource: async () => 'pnpm-global',
- createManagedService,
- runPackageManager,
- resolveLatestPackageVersion,
- },
- { packageTag: 'test' },
- );
- const plan = await application.inspect();
- expect(plan).toEqual({
- kind: 'package-manager',
- source: 'pnpm-global',
- currentVersion: '1.2.3',
- latestVersion: '1.2.4',
- packageTag: 'test',
- command: {
- executable: process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm',
- args: [
- 'add',
- '--global',
- '@minimax-ai/code@1.2.4',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- display:
- 'pnpm add --global @minimax-ai/code@1.2.4 --registry https://registry.npmjs.org/',
- },
- });
- const onOutput = vi.fn();
- const onPhase = vi.fn();
- const signal = new AbortController().signal;
- await expect(application.apply(plan, { onOutput, onPhase, signal })).resolves.toMatchObject({
- applied: true,
- message: 'MCode 1.2.4 was installed through pnpm. Restart MCode to use the installed version.',
- });
- expect(createManagedService).not.toHaveBeenCalled();
- expect(resolveLatestPackageVersion).toHaveBeenCalledWith('test');
- expect(runPackageManager).toHaveBeenCalledWith(plan.command, { onOutput, onPhase, signal });
- expect(onPhase).toHaveBeenNthCalledWith(1, { phase: 'installing', cancellable: false });
- expect(onPhase).toHaveBeenNthCalledWith(2, { phase: 'completed', cancellable: false });
- });
- it('does not reinstall a package-manager installation already at the registry version', async () => {
- const runPackageManager = vi.fn(async () => undefined);
- const application = createApplication({
- detectInstallSource: async () => 'npm-global',
- resolveLatestPackageVersion: async () => '1.2.3',
- runPackageManager,
- });
- const plan = await application.inspect();
- expect(plan).toEqual({
- kind: 'current',
- source: 'npm-global',
- currentVersion: '1.2.3',
- latestVersion: '1.2.3',
- packageTag: 'latest',
- });
- await expect(application.apply(plan)).rejects.toThrow(/cannot be applied automatically/i);
- expect(runPackageManager).not.toHaveBeenCalled();
- });
- it('does not downgrade a package-manager installation newer than the registry version', async () => {
- const application = createApplication({
- detectInstallSource: async () => 'bun-global',
- resolveLatestPackageVersion: async () => '1.2.2',
- });
- await expect(application.inspect()).resolves.toEqual({
- kind: 'ahead',
- source: 'bun-global',
- currentVersion: '1.2.3',
- latestVersion: '1.2.2',
- packageTag: 'latest',
- });
- });
- it('follows the test dist-tag when a custom prerelease sorts ahead of the tagged build', async () => {
- const resolveLatestPackageVersion = vi.fn(
- async () => '0.0.1-beta.1786162945.e3fdada2',
- );
- const application = createApplication(
- {
- detectInstallSource: async () => 'npm-global',
- resolveLatestPackageVersion,
- },
- {
- currentVersion: '0.0.1-beta.matrixfix.7069818510',
- packageTag: 'test',
- },
- );
- await expect(application.inspect()).resolves.toEqual({
- kind: 'package-manager',
- source: 'npm-global',
- currentVersion: '0.0.1-beta.matrixfix.7069818510',
- latestVersion: '0.0.1-beta.1786162945.e3fdada2',
- packageTag: 'test',
- command: {
- executable: process.platform === 'win32' ? 'npm.cmd' : 'npm',
- args: [
- 'install',
- '--global',
- '@minimax-ai/code@0.0.1-beta.1786162945.e3fdada2',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- display:
- 'npm install --global @minimax-ai/code@0.0.1-beta.1786162945.e3fdada2 ' +
- '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
- '--registry https://registry.npmjs.org/',
- },
- });
- expect(resolveLatestPackageVersion).toHaveBeenCalledWith('test');
- });
- it('follows the preview dist-tag instead of applying SemVer downgrade protection', async () => {
- const application = createApplication(
- {
- detectInstallSource: async () => 'npm-global',
- resolveLatestPackageVersion: async () => '0.2.1-previewtrain.41',
- },
- {
- currentVersion: '0.2.1-previewtrain.hotfix.42',
- packageTag: 'preview',
- },
- );
- await expect(application.inspect()).resolves.toMatchObject({
- kind: 'package-manager',
- currentVersion: '0.2.1-previewtrain.hotfix.42',
- latestVersion: '0.2.1-previewtrain.41',
- packageTag: 'preview',
- });
- });
- it('fails closed when the installation owner cannot be identified', async () => {
- const runPackageManager = vi.fn(async () => undefined);
- const createManagedService = vi.fn();
- const resolveLatestPackageVersion = vi.fn(async () => '9.9.9');
- const application = createApplication({
- detectInstallSource: async () => 'unsupported',
- createManagedService,
- runPackageManager,
- resolveLatestPackageVersion,
- });
- const plan = await application.inspect();
- expect(plan).toMatchObject({
- kind: 'manual',
- source: 'unsupported',
- command:
- 'npm install --global @minimax-ai/code@latest ' +
- '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
- '--registry https://registry.npmjs.org/',
- });
- await expect(application.apply(plan)).rejects.toThrow(/cannot be applied automatically/i);
- expect(createManagedService).not.toHaveBeenCalled();
- expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
- expect(runPackageManager).not.toHaveBeenCalled();
- });
- it.each([
- '/Users/demo/project/pnpm/global/5/source/@minimax-ai/code',
- '/Users/demo/project/.config/yarn/global/source/@minimax-ai/code',
- '/Users/demo/project/.yarn/global/source/@minimax-ai/code',
- '/Users/demo/project/.bun/install/global/source/@minimax-ai/code',
- ])(
- 'treats a local checkout containing a global-install marker as unsupported: %s',
- async (packageRoot) => {
- const createManagedService = vi.fn();
- const resolveLatestPackageVersion = vi.fn(async () => '9.9.9');
- const application = createApplication({
- detectInstallSource: () =>
- detectMcodeInstallSource({
- installRoot: '/source',
- platform: 'darwin',
- packageRoot: () => packageRoot,
- npmGlobalPrefix: async () => '/usr/local',
- managedInstall: () => false,
- }),
- createManagedService,
- resolveLatestPackageVersion,
- });
- await expect(application.inspect()).resolves.toMatchObject({
- kind: 'manual',
- source: 'unsupported',
- });
- expect(createManagedService).not.toHaveBeenCalled();
- expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
- },
- );
- it('keeps a future public package on the official npm registry', async () => {
- const application = createApplication(
- { detectInstallSource: async () => 'unsupported' },
- { packageName: '@minimax-ai/code' },
- );
- await expect(application.inspect()).resolves.toMatchObject({
- kind: 'manual',
- command:
- 'npm install --global @minimax-ai/code@latest ' +
- '--ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 ' +
- '--registry https://registry.npmjs.org/',
- });
- });
- it('keeps a China mirror prefix on the same registry during updates', async () => {
- const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
- const application = createApplication(
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion,
- },
- {
- packageName: '@minimax-ai/code',
- prefixInstall: {
- executable: '/opt/minimax/runtime/node/bin/npm',
- packageName: '@minimax-ai/code',
- prefix: '/opt/minimax',
- registry: 'https://registry.npmmirror.com/',
- },
- },
- );
- await expect(application.inspect()).resolves.toMatchObject({
- kind: 'package-manager',
- source: 'npm-prefix',
- command: {
- args: [
- 'install',
- '--global',
- '--prefix',
- '/opt/minimax',
- '@minimax-ai/code@1.2.4',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmmirror.com/',
- ],
- },
- });
- expect(resolveLatestPackageVersion).toHaveBeenCalledWith('latest');
- });
- it('reads the installed public package identity from its real entry path', () => {
- const temporaryRoot = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-public-'));
- const packageRoot = path.join(
- temporaryRoot,
- 'lib',
- 'node_modules',
- '@minimax-ai',
- 'code',
- );
- try {
- mkdirSync(packageRoot, { recursive: true });
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code' }),
- );
- const entryFile = path.join(packageRoot, 'cli.js');
- writeFileSync(entryFile, '');
- expect(resolveMcodePackageName(entryFile)).toBe('@minimax-ai/code');
- expect(resolveInstalledMcodePackageVersion(entryFile)).toBeUndefined();
- } finally {
- rmSync(temporaryRoot, { recursive: true, force: true });
- }
- });
- it('recognizes only the internal package identity as environment-selectable', () => {
- expect(isInternalMcodePackageName('@minimax/code')).toBe(true);
- expect(isInternalMcodePackageName('@minimax-ai/code')).toBe(false);
- expect(isInternalMcodePackageName(undefined)).toBe(false);
- });
- it('rejects a package-manager update when the installed exact version does not match', async () => {
- const application = createApplication({
- detectInstallSource: async () => 'npm-global',
- resolveLatestPackageVersion: async () => '1.2.4',
- runPackageManager: async () => undefined,
- readInstalledPackageVersion: () => '1.2.3',
- });
- const plan = await application.inspect();
- await expect(application.apply(plan)).rejects.toThrow(
- 'MCode update installed 1.2.3; expected 1.2.4.',
- );
- });
- });
- describe('MCode update install-source commands', () => {
- it.each([
- ['/opt/homebrew/lib/node_modules/@minimax-ai/code', 'npm-global'],
- ['C:\\Users\\demo\\AppData\\Roaming\\npm\\node_modules\\@minimax\\code', 'npm-global'],
- ['/usr/local/lib/node_modules/@minimax-ai/code', 'npm-global'],
- ['/Users/demo/.local/share/pnpm/global/5/node_modules/@minimax-ai/code', 'pnpm-global'],
- [
- '/Users/demo/Library/pnpm/global/v11/10c0afe5/node_modules/@minimax-ai/code',
- 'pnpm-global',
- ],
- [
- 'C:\\Users\\demo\\AppData\\Local\\pnpm\\global\\5\\node_modules\\@minimax\\code',
- 'pnpm-global',
- ],
- [
- 'C:\\Users\\demo\\AppData\\Local\\pnpm\\global\\v11\\10c0afe5\\node_modules\\@minimax-ai\\code',
- 'pnpm-global',
- ],
- ['/Users/demo/.config/yarn/global/node_modules/@minimax-ai/code', 'yarn-global'],
- [
- 'C:\\Users\\demo\\.config\\yarn\\global\\node_modules\\@minimax\\code',
- 'yarn-global',
- ],
- ['/Users/demo/.bun/install/global/node_modules/@minimax-ai/code', 'bun-global'],
- [
- 'C:\\Users\\demo\\.bun\\install\\global\\node_modules\\@minimax\\code',
- 'bun-global',
- ],
- ] as const)('classifies %s as %s', (packageRoot, expected) => {
- expect(classifyMcodeInstallPath(packageRoot)).toBe(expected);
- });
- it('uses Windows command shims for the Windows-aware process launcher', () => {
- expect(buildMcodePackageManagerCommand('npm-global', '1.2.4', 'win32')).toEqual({
- executable: 'npm.cmd',
- args: [
- 'install',
- '--global',
- '@minimax-ai/code@1.2.4',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- display:
- 'npm install --global @minimax-ai/code@1.2.4 --ignore-scripts=false --include=optional ' +
- '--allow-scripts=@minimax-ai/code,better-sqlite3 --registry https://registry.npmjs.org/',
- });
- expect(buildMcodePackageManagerCommand('bun-global', '1.2.4', 'win32')).toEqual({
- executable: 'bun.exe',
- args: [
- 'add',
- '--global',
- '@minimax-ai/code@1.2.4',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- display:
- 'bun add --global @minimax-ai/code@1.2.4 --registry https://registry.npmjs.org/',
- });
- });
- it('uses the installer-owned npm executable, prefix, package, and registry', () => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- const command = buildMcodePackageManagerCommand(
- 'npm-prefix',
- '1.2.4',
- 'linux',
- distribution,
- {
- executable: '/opt/minimax/runtime/node/bin/npm',
- packageName: '@minimax-ai/code',
- prefix: '/opt/minimax',
- registry: 'https://registry.npmjs.org/',
- },
- );
- expect(command).toEqual({
- executable: '/opt/minimax/runtime/node/bin/npm',
- args: [
- 'install',
- '--global',
- '--prefix',
- '/opt/minimax',
- '@minimax-ai/code@1.2.4',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- display:
- '/opt/minimax/runtime/node/bin/npm install --global --prefix /opt/minimax ' +
- '@minimax-ai/code@1.2.4 --ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3 --registry https://registry.npmjs.org/',
- });
- });
- it('keeps the explicit public mirror for installer-owned updates', () => {
- const distribution = resolveMcodeNpmDistribution(
- '@minimax-ai/code',
- 'https://registry.npmmirror.com',
- );
- const command = buildMcodePackageManagerCommand(
- 'npm-prefix',
- '1.2.4',
- 'linux',
- distribution,
- {
- executable: '/opt/minimax/runtime/node/bin/npm',
- packageName: '@minimax-ai/code',
- prefix: '/opt/minimax',
- registry: 'https://registry.npmmirror.com/',
- },
- );
- expect(command.args).toEqual([
- 'install',
- '--global',
- '--prefix',
- '/opt/minimax',
- '@minimax-ai/code@1.2.4',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmmirror.com/',
- ]);
- expect(() =>
- resolveMcodeNpmDistribution('@minimax-ai/code', 'https://registry.example.com/'),
- ).toThrow('Unsupported MCode npm registry');
- });
- it('uses the active package manifest entry when argv points at the npm bin shim', async () => {
- const runPackageManager = vi.fn(async () => undefined);
- const activateVersionedPrefix = vi.fn(() => '/opt/minimax/releases/1.2.4');
- const releasePrefixUpdateLock = vi.fn();
- const application = createApplication(
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion: async () => '1.2.4',
- runPackageManager,
- createVersionedPrefixStaging: () => '/opt/minimax/releases/.staging-1.2.4',
- prepareVersionedPrefixStaging: vi.fn(),
- readPrefixPackageMetadata: (prefix, packageName) => ({
- packageRoot: `${prefix}/lib/node_modules/${packageName}`,
- version: '1.2.4',
- binEntry: 'dist/index.js',
- mcodeToolsBinEntry: 'mcode-tools.js',
- }),
- validatePrefixPackage: vi.fn(async () => undefined),
- activateVersionedPrefix,
- acquirePrefixUpdateLock: () => releasePrefixUpdateLock,
- },
- {
- entryFile: '/opt/minimax/bin/mcode',
- packageName: '@minimax-ai/code',
- prefixInstall: {
- executable: '/opt/minimax/runtime/node/bin/npm',
- packageName: '@minimax-ai/code',
- prefix: '/opt/minimax',
- registry: 'https://registry.npmjs.org/',
- },
- },
- );
- const plan = await application.inspect();
- await expect(application.apply(plan)).resolves.toEqual({
- applied: true,
- restartRequired: false,
- message:
- 'MCode 1.2.4 is installed. New MCode sessions will use it; running sessions can continue normally.',
- });
- expect(activateVersionedPrefix).toHaveBeenCalledWith({
- stagingPrefix: '/opt/minimax/releases/.staging-1.2.4',
- activePrefix: '/opt/minimax',
- packageName: '@minimax-ai/code',
- expectedVersion: '1.2.4',
- runtimeExecutable: process.execPath,
- npmExecutable: '/opt/minimax/runtime/node/bin/npm',
- registry: 'https://registry.npmjs.org/',
- });
- expect(releasePrefixUpdateLock).toHaveBeenCalledOnce();
- });
- it('reports malformed pending metadata instead of treating it as a staged update', async () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-malformed-pending-'));
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- const pendingFile = path.join(prefix, '.mcode-update-pending.json');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
- );
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
- );
- writeFileSync(pendingFile, '{}');
- const application = new McodeUpdateApplication(
- {
- currentVersion: '1.2.3',
- entryFile,
- environment: {},
- installRoot: prefix,
- prefixInstall: {
- executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
- packageName: '@minimax-ai/code',
- prefix,
- registry: 'https://registry.npmjs.org/',
- },
- },
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion: async () => '1.2.4',
- },
- );
- await expect(application.inspect()).rejects.toThrow(
- `MCode pending update metadata is invalid at ${pendingFile}.`,
- );
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- }
- });
- it('rejects a pending journal owned by a different active prefix', async () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-foreign-pending-'));
- const foreignPrefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-foreign-owner-'));
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- const pendingFile = path.join(prefix, '.mcode-update-pending.json');
- const foreignStaging = path.join(path.dirname(foreignPrefix), '.foreign-staging');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
- );
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
- );
- writeFileSync(
- pendingFile,
- JSON.stringify({
- schemaVersion: 1,
- stagingPrefix: foreignStaging,
- activePrefix: foreignPrefix,
- activeModulesRoot: resolveMcodePrefixModulesRoot(foreignPrefix, process.platform),
- stagedModulesRoot: resolveMcodePrefixModulesRoot(foreignStaging, process.platform),
- backupModulesRoot: `${resolveMcodePrefixModulesRoot(foreignPrefix, process.platform)}.mcode-update-backup`,
- packageName: '@minimax-ai/code',
- expectedVersion: '1.2.4',
- launchers: resolveMcodePrefixLauncherPairs(foreignPrefix, foreignStaging, process.platform),
- }),
- );
- const application = new McodeUpdateApplication(
- {
- currentVersion: '1.2.3',
- entryFile,
- environment: {},
- installRoot: prefix,
- prefixInstall: {
- executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
- packageName: '@minimax-ai/code',
- prefix,
- registry: 'https://registry.npmjs.org/',
- },
- },
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion: async () => '1.2.4',
- },
- );
- await expect(application.inspect()).rejects.toThrow(
- `MCode pending update file is outside its active prefix: ${pendingFile}`,
- );
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- rmSync(foreignPrefix, { recursive: true, force: true });
- rmSync(foreignStaging, { recursive: true, force: true });
- }
- });
- it('resumes a valid staged update without checking for a newer registry version', async () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-valid-pending-'));
- const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.staging`);
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
- const stagedPackageRoot = path.join(resolveMcodePrefixModulesRoot(stagingPrefix), '@minimax-ai/code');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- mkdirSync(path.join(stagedPackageRoot, 'dist'), { recursive: true });
- mkdirSync(path.join(prefix, 'bin'), { recursive: true });
- mkdirSync(path.join(stagingPrefix, 'bin'), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3', bin: { mcode: 'dist/index.js' } }),
- );
- writeFileSync(
- path.join(stagedPackageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.4', bin: { mcode: 'dist/index.js' } }),
- );
- writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.3');
- writeFileSync(path.join(stagingPrefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.4');
- for (const pair of resolveMcodePrefixLauncherPairs(prefix, stagingPrefix)) {
- mkdirSync(path.dirname(pair.activePath), { recursive: true });
- mkdirSync(path.dirname(pair.stagedPath), { recursive: true });
- writeFileSync(pair.activePath, '1.2.3');
- writeFileSync(pair.stagedPath, '1.2.4');
- }
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
- );
- const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
- writeMcodePrefixUpdatePending({
- stagingPrefix,
- activePrefix: prefix,
- activeModulesRoot,
- stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
- backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
- packageName: '@minimax-ai/code',
- expectedVersion: '1.2.4',
- launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
- });
- const resolveLatestPackageVersion = vi.fn(async () => '1.2.5');
- const application = new McodeUpdateApplication(
- {
- currentVersion: '1.2.3',
- entryFile,
- environment: {},
- installRoot: prefix,
- platform: process.platform,
- prefixInstall: {
- executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
- packageName: '@minimax-ai/code',
- prefix,
- registry: 'https://registry.npmjs.org/',
- },
- },
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion,
- },
- );
- const plan = await application.inspect();
- expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
- await expect(application.apply(plan)).resolves.toEqual({
- applied: false,
- restartRequired: true,
- message: 'MCode 1.2.4 is already staged. It will activate after this process exits.',
- });
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- rmSync(stagingPrefix, { recursive: true, force: true });
- }
- });
- it('rejects a pending update whose staged package and launchers are missing', async () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-missing-staging-'));
- const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.missing`);
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- mkdirSync(path.join(prefix, 'bin'), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3', bin: { mcode: 'dist/index.js' } }),
- );
- writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.3');
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
- );
- const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
- const pendingFile = writeMcodePrefixUpdatePending({
- stagingPrefix,
- activePrefix: prefix,
- activeModulesRoot,
- stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
- backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
- packageName: '@minimax-ai/code',
- expectedVersion: '1.2.4',
- launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
- });
- const application = new McodeUpdateApplication(
- {
- currentVersion: '1.2.3',
- entryFile,
- environment: {},
- installRoot: prefix,
- platform: process.platform,
- prefixInstall: {
- executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
- packageName: '@minimax-ai/code',
- prefix,
- registry: 'https://registry.npmjs.org/',
- },
- },
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion: async () => '1.2.4',
- },
- );
- await expect(application.inspect()).rejects.toThrow(
- `MCode pending update artifacts are incomplete at ${pendingFile}.`,
- );
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- rmSync(stagingPrefix, { recursive: true, force: true });
- }
- });
- it('finishes cleanup when the staged version is already active', async () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-pending-cleanup-'));
- const stagingPrefix = path.join(path.dirname(prefix), `.${path.basename(prefix)}.activated`);
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai/code');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- mkdirSync(path.join(prefix, 'bin'), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.4', bin: { mcode: 'dist/index.js' } }),
- );
- writeFileSync(path.join(prefix, process.platform === 'win32' ? 'mcode' : 'bin/mcode'), '1.2.4');
- for (const pair of resolveMcodePrefixLauncherPairs(prefix, stagingPrefix)) {
- mkdirSync(path.dirname(pair.activePath), { recursive: true });
- writeFileSync(pair.activePath, '1.2.4');
- }
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({ updateOwner: 'npm-prefix', prefix }),
- );
- const activeModulesRoot = resolveMcodePrefixModulesRoot(prefix, process.platform);
- writeMcodePrefixUpdatePending({
- stagingPrefix,
- activePrefix: prefix,
- activeModulesRoot,
- stagedModulesRoot: resolveMcodePrefixModulesRoot(stagingPrefix, process.platform),
- backupModulesRoot: `${activeModulesRoot}.mcode-update-backup`,
- packageName: '@minimax-ai/code',
- expectedVersion: '1.2.4',
- launchers: resolveMcodePrefixLauncherPairs(prefix, stagingPrefix, process.platform),
- });
- const resolveLatestPackageVersion = vi.fn(async () => '1.2.4');
- const application = new McodeUpdateApplication(
- {
- currentVersion: '1.2.4',
- entryFile,
- environment: {},
- installRoot: prefix,
- platform: process.platform,
- prefixInstall: {
- executable: path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm'),
- packageName: '@minimax-ai/code',
- prefix,
- registry: 'https://registry.npmjs.org/',
- },
- },
- {
- detectInstallSource: async () => 'npm-prefix',
- resolveLatestPackageVersion,
- },
- );
- const plan = await application.inspect();
- expect(plan).toMatchObject({
- kind: 'package-manager',
- source: 'npm-prefix',
- currentVersion: '1.2.4',
- latestVersion: '1.2.4',
- });
- expect(resolveLatestPackageVersion).not.toHaveBeenCalled();
- await expect(application.apply(plan)).resolves.toEqual({
- applied: false,
- restartRequired: true,
- message: 'MCode 1.2.4 is already active. Restarting will finish update cleanup.',
- });
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- rmSync(stagingPrefix, { recursive: true, force: true });
- }
- });
- it.each(['https://registry.npmjs.org/', 'https://registry.npmmirror.com/'])(
- 'recovers installer prefix ownership from the versioned receipt using %s',
- async (registry) => {
- const temporaryRoot = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-prefix-'));
- const packageRoot = path.join(
- temporaryRoot,
- ...(process.platform === 'win32' ? [] : ['lib']),
- 'node_modules',
- '@minimax-ai',
- 'code',
- );
- const npmExecutable = path.join(temporaryRoot, 'runtime', 'node', ...(process.platform === 'win32' ? ['npm.cmd'] : ['bin', 'npm']));
- const entryFile = path.join(packageRoot, 'cli.js');
- try {
- mkdirSync(packageRoot, { recursive: true });
- mkdirSync(path.dirname(npmExecutable), { recursive: true });
- writeFileSync(npmExecutable, '');
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
- );
- writeFileSync(
- path.join(temporaryRoot, 'install.json'),
- `\uFEFF${JSON.stringify({
- schemaVersion: 1,
- product: 'minimax-code',
- updateOwner: 'npm-prefix',
- packageManager: 'npm',
- packageName: '@minimax-ai/code',
- registry,
- distTag: 'latest',
- prefix: temporaryRoot,
- npmExecutable,
- })}`,
- );
- const prefixInstall = resolveMcodeNpmPrefixInstall(entryFile, process.platform);
- expect(prefixInstall).toEqual({
- executable: npmExecutable,
- packageName: '@minimax-ai/code',
- prefix: realpathSync(temporaryRoot),
- registry,
- });
- expect(resolveInstalledMcodePackageVersion(entryFile)).toBe('1.2.3');
- await expect(
- detectMcodeInstallSource({
- installRoot: temporaryRoot,
- platform: process.platform,
- packageRoot: () => packageRoot,
- prefixInstall: () => prefixInstall,
- managedInstall: () => false,
- }),
- ).resolves.toBe('npm-prefix');
- } finally {
- rmSync(temporaryRoot, { recursive: true, force: true });
- }
- },
- );
- it('resolves versioned installer ownership from a release package entry', () => {
- const prefix = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-versioned-prefix-'));
- const packageRoot = path.join(
- prefix,
- 'releases/1.2.3',
- ...(process.platform === 'win32' ? [] : ['lib']),
- 'node_modules/@minimax-ai/code',
- );
- const npmExecutable = path.join(prefix, process.platform === 'win32' ? 'runtime/node/npm.cmd' : 'runtime/node/bin/npm');
- const entryFile = path.join(packageRoot, 'dist/index.js');
- try {
- mkdirSync(path.dirname(entryFile), { recursive: true });
- mkdirSync(path.dirname(npmExecutable), { recursive: true });
- writeFileSync(entryFile, '');
- writeFileSync(npmExecutable, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
- );
- writeFileSync(
- path.join(prefix, 'install.json'),
- JSON.stringify({
- schemaVersion: 2,
- product: 'minimax-code',
- updateOwner: 'npm-prefix',
- packageManager: 'npm',
- packageName: '@minimax-ai/code',
- registry: 'https://registry.npmjs.org/',
- distTag: 'latest',
- prefix,
- npmExecutable,
- layoutVersion: 2,
- releasesDirectory: 'releases',
- currentFile: 'current',
- }),
- );
- expect(resolveMcodeNpmPrefixInstall(entryFile, process.platform)).toEqual({
- executable: npmExecutable,
- packageName: '@minimax-ai/code',
- prefix: realpathSync(prefix),
- registry: 'https://registry.npmjs.org/',
- });
- } finally {
- rmSync(prefix, { recursive: true, force: true });
- }
- });
- it('recovers a legacy installer prefix only from its package root and adjacent npm', () => {
- const temporaryParent = mkdtempSync(path.join(os.tmpdir(), 'mcode-update-legacy-'));
- const prefix = path.join(temporaryParent, '.minimax-code');
- const packageRoot = path.join(resolveMcodePrefixModulesRoot(prefix), '@minimax-ai', 'code');
- const nodeExecutable = path.join(prefix, 'runtime', 'node', ...(process.platform === 'win32' ? ['node.exe'] : ['bin', 'node']));
- const npmExecutable = path.join(prefix, 'runtime', 'node', ...(process.platform === 'win32' ? ['npm.cmd'] : ['bin', 'npm']));
- const entryFile = path.join(packageRoot, 'cli.js');
- try {
- mkdirSync(packageRoot, { recursive: true });
- mkdirSync(path.dirname(nodeExecutable), { recursive: true });
- writeFileSync(nodeExecutable, '');
- writeFileSync(npmExecutable, '');
- writeFileSync(entryFile, '');
- writeFileSync(
- path.join(packageRoot, 'package.json'),
- JSON.stringify({ name: '@minimax-ai/code', version: '1.2.3' }),
- );
- expect(resolveMcodeNpmPrefixInstall(entryFile, process.platform, nodeExecutable)).toEqual({
- executable: npmExecutable,
- packageName: '@minimax-ai/code',
- prefix: realpathSync(prefix),
- registry: 'https://registry.npmjs.org/',
- });
- } finally {
- rmSync(temporaryParent, { recursive: true, force: true });
- }
- });
- it.each([
- ['npm-global', 'npm install --global'],
- ['pnpm-global', 'pnpm add --global'],
- ['yarn-global', 'yarn global add'],
- ['bun-global', 'bun add --global'],
- ] as const)('pins %s updates to the internal registry', (source, prefix) => {
- const command = buildMcodePackageManagerCommand(source, '1.2.4', 'linux');
- const scriptOptions =
- source === 'npm-global'
- ? ' --ignore-scripts=false --include=optional --allow-scripts=@minimax-ai/code,better-sqlite3'
- : '';
- expect(command.args.slice(-2)).toEqual([
- '--registry',
- 'https://registry.npmjs.org/',
- ]);
- expect(command.display).toBe(
- `${prefix} @minimax-ai/code@1.2.4${scriptOptions} --registry https://registry.npmjs.org/`,
- );
- });
- it('resolves the target version from the internal registry with npm shipped by Node', async () => {
- const run = vi.fn(async () => '"1.2.4"');
- await expect(
- resolveLatestMcodeRegistryVersion('test', { platform: 'linux', run }),
- ).resolves.toBe('1.2.4');
- expect(run).toHaveBeenCalledWith('npm', [
- 'view',
- '@minimax-ai/code@test',
- 'version',
- '--json',
- '--registry',
- 'https://registry.npmjs.org/',
- '--fetch-timeout',
- '30000',
- ]);
- });
- it('uses installer-owned npm for a prefix installation registry lookup', async () => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- const run = vi.fn(async () => '"1.2.4"');
- await expect(
- resolveLatestMcodeRegistryVersion('latest', {
- platform: 'linux',
- run,
- distribution,
- npmExecutable: '/opt/minimax/runtime/node/bin/npm',
- }),
- ).resolves.toBe('1.2.4');
- expect(run).toHaveBeenCalledWith('/opt/minimax/runtime/node/bin/npm', [
- 'view',
- '@minimax-ai/code@latest',
- 'version',
- '--json',
- '--registry',
- 'https://registry.npmjs.org/',
- '--fetch-timeout',
- '30000',
- ]);
- });
- it('accepts npm registry metadata returned as a single-item JSON array', async () => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- await expect(
- resolveLatestMcodeRegistryVersion('latest', {
- platform: 'linux',
- run: async () => '["1.2.4"]',
- distribution,
- }),
- ).resolves.toBe('1.2.4');
- });
- it('keeps public package lookup and installation on the official npm registry', async () => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- const run = vi.fn(async () => '"1.2.4"');
- expect(distribution).toEqual({
- packageName: '@minimax-ai/code',
- registry: 'https://registry.npmjs.org/',
- });
- await expect(
- resolveLatestMcodeRegistryVersion('latest', {
- platform: 'linux',
- run,
- distribution,
- }),
- ).resolves.toBe('1.2.4');
- expect(run).toHaveBeenCalledWith('npm', [
- 'view',
- '@minimax-ai/code@latest',
- 'version',
- '--json',
- '--registry',
- 'https://registry.npmjs.org/',
- '--fetch-timeout',
- '30000',
- ]);
- expect(
- buildMcodePackageManagerCommand('npm-global', '1.2.4', 'linux', distribution),
- ).toMatchObject({
- args: [
- 'install',
- '--global',
- '@minimax-ai/code@1.2.4',
- '--ignore-scripts=false',
- '--include=optional',
- '--allow-scripts=@minimax-ai/code,better-sqlite3',
- '--registry',
- 'https://registry.npmjs.org/',
- ],
- });
- });
- it('does not fall back to the internal registry when the public package is absent', async () => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- const run = vi.fn(async () => {
- throw new Error('E404 Not Found');
- });
- await expect(
- resolveLatestMcodeRegistryVersion('latest', {
- platform: 'linux',
- run,
- distribution,
- }),
- ).rejects.toThrow('E404 Not Found');
- expect(run).toHaveBeenCalledTimes(1);
- });
- it.each([
- ['[]', /invalid latest version/u],
- ['"1.2.4-beta.1"', /stable semantic version/u],
- ])('rejects invalid public latest metadata: %s', async (metadata, error) => {
- const distribution = resolveMcodeNpmDistribution('@minimax-ai/code');
- await expect(
- resolveLatestMcodeRegistryVersion('latest', {
- platform: 'linux',
- run: async () => metadata,
- distribution,
- }),
- ).rejects.toThrow(error);
- });
- it.each([
- ['prod', 'latest'],
- ['staging', 'preview'],
- ['test', 'test'],
- [undefined, 'latest'],
- ] as const)('maps the %s build to the %s update tag', (environment, tag) => {
- expect(resolveMcodeNpmDistTag(environment)).toBe(tag);
- });
- it('keeps an explicitly embedded preview update channel independent of the backend', () => {
- expect(resolveMcodeNpmDistTag('prod', 'preview')).toBe('preview');
- });
- it.each([
- ['/usr/local/lib/node_modules/@minimax-ai/code', '/usr/local', 'linux'],
- [
- 'C:\\Users\\demo\\AppData\\Roaming\\npm\\node_modules\\@minimax\\code',
- 'C:\\Users\\demo\\AppData\\Roaming\\npm',
- 'win32',
- ],
- ] as const)('recognizes npm global ownership for %s', (packageRoot, prefix, platform) => {
- expect(classifyNpmGlobalInstall(packageRoot, prefix, platform)).toBe('npm-global');
- });
- });
- function createApplication(
- dependencies: Partial<McodeUpdateApplicationDependencies>,
- options: {
- currentVersion?: string;
- packageTag?: 'latest' | 'test' | 'preview';
- packageName?: '@minimax-ai/code' | '@minimax-ai/code';
- prefixInstall?: {
- executable: string;
- packageName: '@minimax-ai/code' | '@minimax-ai/code';
- prefix: string;
- registry: string;
- };
- } = {},
- ): McodeUpdateApplication {
- return new McodeUpdateApplication(
- { currentVersion: '1.2.3', installRoot: '/managed', ...options },
- { readInstalledPackageVersion: () => '1.2.4', ...dependencies },
- );
- }
|