Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions src/tui/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Gradient from 'ink-gradient';
import React, {
useCallback,
useEffect,
useMemo,
useRef,
useState
} from 'react';
Expand Down Expand Up @@ -319,14 +320,15 @@ function clearInstallationMetadata(settings: Settings | null): Settings | null {
return next;
}

export function getCurrentInstallation(
isClaudeInstalled: boolean,
// Claude Code runs ccstatusline, so until its status line command is read, the saved
// metadata describes the install
export function classifyInstalledStatusLine(
existingStatusLine: string | null,
settings: Settings
savedInstallation: InstallationMetadata | undefined
): InstallationMetadata {
return isClaudeInstalled && !existingStatusLine && settings.installation
? settings.installation
: classifyInstallation(existingStatusLine, settings.installation);
return !existingStatusLine && savedInstallation
? savedInstallation
: classifyInstallation(existingStatusLine, savedInstallation);
}

function trimTrailingSeparators(filePath: string): string {
Expand Down Expand Up @@ -583,7 +585,9 @@ export const App: React.FC = () => {
}
// Global save shortcut
if (key.ctrl && input === 's' && settings && screen !== 'confirm') {
const installation = getCurrentInstallation(isClaudeInstalled, existingStatusLine, settings);
const installation = isClaudeInstalled
? classifyInstalledStatusLine(existingStatusLine, settings.installation)
: classifyInstallation(existingStatusLine, settings.installation);
const activeCommand = installation.method === 'pinned' || installation.method === 'self-managed'
? inspectActiveGlobalCommand({ commandAvailability })
: null;
Expand Down Expand Up @@ -742,9 +746,9 @@ export const App: React.FC = () => {

const handleUpdateCheck = useCallback(() => {
setUpdateCheckerState({ status: 'checking' });
const installation = settings
? getCurrentInstallation(isClaudeInstalled, existingStatusLine, settings)
: classifyInstallation(existingStatusLine, undefined);
const installation = isClaudeInstalled
? classifyInstalledStatusLine(existingStatusLine, settings?.installation)
: classifyInstallation(existingStatusLine, settings?.installation);
const activeCommand = installation.method === 'pinned' || installation.method === 'self-managed'
? inspectActiveGlobalCommand({ commandAvailability })
: null;
Expand Down Expand Up @@ -867,22 +871,44 @@ export const App: React.FC = () => {
setScreen('main');
}, [importValidation, settings]);

if (!settings || !hasLoadedClaudeStatus || !hasLoadedInstalledState) {
const savedInstallation = settings?.installation;
const hasLoaded = settings !== null && hasLoadedClaudeStatus && hasLoadedInstalledState;
// Looking up the global ccstatusline command runs `which -a`, `npm prefix -g` and
// `bun pm bin -g`, which block for a few hundred ms. Editor keys redraw App (the live
// preview, each widget edit), so the lookups run only when the installation changes:
// once loading finishes, and after an install, update or uninstall here, since each
// changes the installation metadata or Claude's status line. Null while loading.
const installationState = useMemo(() => {
if (!hasLoaded) {
return null;
}

const currentInstallation = isClaudeInstalled
? classifyInstalledStatusLine(existingStatusLine, savedInstallation)
: classifyInstallation(existingStatusLine, savedInstallation);
const activeGlobalCommand = currentInstallation.method === 'pinned' || currentInstallation.method === 'self-managed'
? inspectActiveGlobalCommand({ commandAvailability })
: null;
const effectiveInstallation = getPathInferredInstallation(currentInstallation, activeGlobalCommand);
const pinnedRelaunchCommand = effectiveInstallation.method === 'pinned'
&& effectiveInstallation.packageManager !== 'unknown'
? getPinnedGlobalRelaunchCommand(effectiveInstallation.packageManager)
: null;

return { activeGlobalCommand, effectiveInstallation, pinnedRelaunchCommand };
}, [commandAvailability, existingStatusLine, hasLoaded, isClaudeInstalled, savedInstallation]);

if (!settings || !installationState) {
return <Text>Loading settings...</Text>;
}

const runningVersion = getPackageVersion();
const currentInstallation = getCurrentInstallation(isClaudeInstalled, existingStatusLine, settings);
const activeGlobalCommand = currentInstallation.method === 'pinned' || currentInstallation.method === 'self-managed'
? inspectActiveGlobalCommand({ commandAvailability })
: null;
const effectiveInstallation = getPathInferredInstallation(currentInstallation, activeGlobalCommand);
const pinnedVersionMismatch = effectiveInstallation.method === 'pinned'
&& effectiveInstallation.packageManager !== 'unknown'
const { activeGlobalCommand, effectiveInstallation, pinnedRelaunchCommand } = installationState;
const pinnedVersionMismatch = pinnedRelaunchCommand
? getPinnedVersionMismatch(
effectiveInstallation,
runningVersion,
getPinnedGlobalRelaunchCommand(effectiveInstallation.packageManager),
pinnedRelaunchCommand,
versionInstalledThisSession
)
: null;
Expand Down
101 changes: 96 additions & 5 deletions src/tui/__tests__/App.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import {
type InstallationMetadata
} from '../../types/Settings';
import * as claudeSettings from '../../utils/claude-settings';
import * as globalCommandResolution from '../../utils/global-command-resolution';
import * as globalPackageManager from '../../utils/global-package-manager';
import { getPackageVersion } from '../../utils/terminal';
import * as updateChecker from '../../utils/update-checker';
import {
applyTuiImport,
buildConfigLoadWarning,
buildInvalidConfigSaveConfirm,
classifyInstalledStatusLine,
clearInstallMenuSelection,
getConfirmCancelScreen,
getCurrentInstallation,
getPathInferredInstallation,
getPinnedVersionMismatch
} from '../App';
Expand Down Expand Up @@ -113,10 +114,7 @@ describe('Pinned version mismatch guard', () => {
installedVersion: '2.2.13'
};

expect(getCurrentInstallation(true, null, {
...DEFAULT_SETTINGS,
installation
})).toEqual(installation);
expect(classifyInstalledStatusLine(null, installation)).toEqual(installation);
});

it('does not block auto-update or matching pinned installs', () => {
Expand Down Expand Up @@ -502,6 +500,99 @@ describe('App live label preview', () => {
});
});

describe('App lookup of the global ccstatusline command', () => {
let sandbox: AppSandbox;

beforeEach(() => {
sandbox = setUpAppSandbox();
vi.spyOn(claudeStatus, 'loadClaudeStatusLineState').mockResolvedValue({ existingStatusLine: 'ccstatusline', refreshInterval: null });
vi.spyOn(claudeSettings, 'isInstalled').mockResolvedValue(true);
});

afterEach(() => {
sandbox.restore();
});

// The real lookups run `which -a`, `npm prefix -g` and `bun pm bin -g`
it.each(['pinned', 'self-managed'] as const)('looks up a %s install once, not again for each editor key', async (method) => {
const runningVersion = getPackageVersion();
fs.writeFileSync(sandbox.settingsPath, JSON.stringify({
...DEFAULT_SETTINGS,
lines: [[{ id: 'model', type: 'model', metadata: { label: '7d' } }]],
...(method === 'pinned' && { installation: { method: 'pinned', installedVersion: runningVersion } })
}));
const inspectActiveCommand = vi.spyOn(globalPackageManager, 'inspectActiveGlobalCommand').mockReturnValue({
packageManager: 'npm',
resolvedPath: '/usr/local/bin/ccstatusline',
resolvedPaths: ['/usr/local/bin/ccstatusline'],
binDir: '/usr/local/bin',
version: runningVersion,
warning: null
});
const inspectResolution = vi.spyOn(globalCommandResolution, 'inspectGlobalCommandResolution').mockReturnValue({
resolvedPaths: ['/usr/local/bin/ccstatusline'],
firstResolvedPath: '/usr/local/bin/ccstatusline',
expectedBinDir: '/usr/local/bin',
warning: null
});
const rendered = renderApp();

try {
await waitFor(() => {
expect(rendered.getFrame()).toContain('🧰 Manage Installation');
});
await pressKey(rendered, KEYS.enter, 'Select Line to Edit Items');
await pressKey(rendered, KEYS.enter, 'Edit Line 1');
await pressKey(rendered, 'e', '(default: "Model: ")');
await pressKey(rendered, 'X', '7dXClaude');
await pressKey(rendered, 'Y', '7dXYClaude');
await pressKey(rendered, 'Z', '7dXYZClaude');
await pressKey(rendered, KEYS.enter, 'Edit Line 1');
await pressKey(rendered, 'r', '(raw value)');
await pressKey(rendered, KEYS.escape, 'Select Line to Edit Items');
await pressKey(rendered, KEYS.escape, '🧰 Manage Installation');

expect(inspectActiveCommand).toHaveBeenCalledOnce();
// Pinned installs also look up the relaunch path for the version-mismatch screen
expect(inspectResolution).toHaveBeenCalledTimes(method === 'pinned' ? 1 : 0);
} finally {
rendered.cleanup();
}
});

it('looks again after the TUI updates the pinned global install', async () => {
fs.writeFileSync(sandbox.settingsPath, JSON.stringify({
...DEFAULT_SETTINGS,
installation: { method: 'pinned', installedVersion: '2.0.0' }
}));
let versionOnDisk = '2.0.0';
const inspectActiveCommand = vi.spyOn(globalPackageManager, 'inspectActiveGlobalCommand').mockImplementation(() => ({
packageManager: 'npm',
resolvedPath: '/usr/local/bin/ccstatusline',
resolvedPaths: ['/usr/local/bin/ccstatusline'],
binDir: '/usr/local/bin',
version: versionOnDisk,
warning: null
}));
vi.spyOn(updateChecker, 'runGlobalPackageInstall').mockImplementation((packageManager, version) => {
versionOnDisk = version;
return Promise.resolve();
});
const rendered = renderApp();

try {
await waitFor(() => {
expect(rendered.getFrame()).toContain('Pinned Install Version Mismatch');
});
await pressKey(rendered, KEYS.enter, '✓ Global package updated');
expect(rendered.getFrame()).toContain('Main Menu');
expect(inspectActiveCommand).toHaveBeenCalledTimes(2);
} finally {
rendered.cleanup();
}
});
});

describe('App save guard for an invalid settings.json', () => {
let sandbox: AppSandbox;

Expand Down
Loading