fix: restore update command helpers (#1857)

Exported package-manager helpers used by openchamber update
Added regression coverage for the update-available path
This commit is contained in:
Bohdan Triapitsyn
2026-06-28 09:58:45 +03:00
parent 5034ad27a8
commit b72230bc24
3 changed files with 65 additions and 3 deletions
@@ -0,0 +1,50 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { describe, expect, it, vi } from 'vitest';
import { createUpdateCommand } from './commands-update.js';
async function withTempOpenChamberDataDir(fn) {
const previous = process.env.OPENCHAMBER_DATA_DIR;
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'openchamber-update-test-'));
process.env.OPENCHAMBER_DATA_DIR = dir;
try {
return await fn(dir);
} finally {
if (typeof previous === 'string') {
process.env.OPENCHAMBER_DATA_DIR = previous;
} else {
delete process.env.OPENCHAMBER_DATA_DIR;
}
fs.rmSync(dir, { recursive: true, force: true });
}
}
describe('update command', () => {
it('uses the package-manager helpers on the update-available path', async () => {
await withTempOpenChamberDataDir(async () => {
const originalWrite = process.stdout.write;
process.stdout.write = vi.fn(() => true);
const executeUpdate = vi.fn(() => ({ success: true, exitCode: 0 }));
const updateCommand = createUpdateCommand({
packageManagerPath: '/fake/package-manager.js',
serveCommand: vi.fn(),
importFromFilePath: vi.fn(async () => ({
checkForUpdates: vi.fn(async () => ({ available: true, version: '9.9.9' })),
detectPackageManager: vi.fn(() => 'npm'),
executeUpdate,
getCurrentVersion: vi.fn(() => '1.0.0'),
})),
});
try {
await updateCommand({ json: true });
expect(executeUpdate).toHaveBeenCalledWith('npm', { silent: true });
} finally {
process.stdout.write = originalWrite;
}
});
});
});
+2 -2
View File
@@ -483,7 +483,7 @@ export function detectPackageManagerDetails() {
};
}
function detectPackageManager() {
export function detectPackageManager() {
return detectPackageManagerDetails().packageManager;
}
@@ -769,7 +769,7 @@ export async function checkForUpdates(options = {}) {
/**
* Execute the update (used by CLI)
*/
function executeUpdate(pm = detectPackageManager(), options = {}) {
export function executeUpdate(pm = detectPackageManager(), options = {}) {
const command = getUpdateCommand(pm);
if (!options?.silent) {
console.log(`Updating ${PACKAGE_NAME} using ${pm}...`);
@@ -6,7 +6,12 @@ vi.mock('node:child_process', () => ({
spawnSync: vi.fn(() => ({ status: 0, stdout: '/usr/local/bin', stderr: '' })),
}));
const { checkForUpdates, getCurrentVersion } = await import('./package-manager.js');
const {
checkForUpdates,
detectPackageManager,
executeUpdate,
getCurrentVersion,
} = await import('./package-manager.js');
/** Helper: create a fetch mock that routes by URL pattern */
function createFetchMock() {
@@ -251,3 +256,10 @@ describe('getCurrentVersion', () => {
expect(getCurrentVersion()).toMatch(/^\d+\.\d+\.\d+|unknown$/);
});
});
describe('CLI update exports', () => {
it('exports package-manager helpers used by the update command', () => {
expect(typeof detectPackageManager).toBe('function');
expect(typeof executeUpdate).toBe('function');
});
});