diff --git a/src/main/codex-accounts/account-management/codex-account-registration.ts b/src/main/codex-accounts/account-management/codex-account-registration.ts index e132f024815..c9117fe6532 100644 --- a/src/main/codex-accounts/account-management/codex-account-registration.ts +++ b/src/main/codex-accounts/account-management/codex-account-registration.ts @@ -147,7 +147,7 @@ export class CodexAccountRegistration { ) // Restore this runtime's selection while retaining current validation of other runtimes. - this.dependencies.store.updateSettings({ + this.dependencies.selection.saveSettings({ codexManagedAccounts: updatedAccounts, activeCodexManagedAccountId: activeSelection.host, activeCodexManagedAccountIdsByRuntime: activeSelection @@ -232,7 +232,7 @@ export class CodexAccountRegistration { const settings = this.dependencies.store.getSettings() const selection = normalizeCodexRuntimeSelection(settings) const targetSelection = getCodexSelectionTargetForAccount(account) - this.dependencies.store.updateSettings({ + this.dependencies.selection.saveSettings({ codexManagedAccounts: [...settings.codexManagedAccounts, account], activeCodexManagedAccountId: targetSelection.runtime === 'host' ? account.id : selection.host, activeCodexManagedAccountIdsByRuntime: setSelectedCodexAccountIdForTarget( @@ -247,7 +247,7 @@ export class CodexAccountRegistration { this.dependencies.runtimeHome.syncForCurrentSelection(targetSelection) } catch (error) { // Restore settings before caller cleanup removes the failed account's home. - this.dependencies.store.updateSettings({ + this.dependencies.selection.saveSettings({ codexManagedAccounts: settings.codexManagedAccounts, activeCodexManagedAccountId: settings.activeCodexManagedAccountId, activeCodexManagedAccountIdsByRuntime: settings.activeCodexManagedAccountIdsByRuntime diff --git a/src/main/codex-accounts/account-management/codex-account-selection-broadcast.test.ts b/src/main/codex-accounts/account-management/codex-account-selection-broadcast.test.ts new file mode 100644 index 00000000000..57b66c4b705 --- /dev/null +++ b/src/main/codex-accounts/account-management/codex-account-selection-broadcast.test.ts @@ -0,0 +1,92 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { installFakeAppEnvironment } from '../../../../config/scripts/test-runtime/environment/vitest-host-ports-setup' +import type { GlobalSettings } from '../../../shared/settings/global-settings-types' +import type { CodexAccountService as CodexAccountServiceInstance } from '../service' +import { + createManagedHome, + createRateLimits, + createRuntimeHome, + createSettings, + createStore, + registerCodexAccountsTestHomes, + testState +} from './service-test-harness' +import { createCodexAccountRecord } from '../runtime-home/runtime-home-service-test-harness' + +vi.mock('electron', () => ({ + app: { + getPath: () => testState.userDataDir + } +})) +beforeEach(() => installFakeAppEnvironment({ getPath: () => testState.userDataDir })) + +vi.mock(import('node:os'), async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + homedir: () => testState.fakeHomeDir + } +}) + +describe('Codex account selection broadcast', () => { + registerCodexAccountsTestHomes() + + // #43107: a switch from the CLI or a paired phone must reach every window's status bar. + it.each([ + { + change: 'select', + apply: (service: CodexAccountServiceInstance) => service.selectAccount('b'), + expected: { + activeCodexManagedAccountId: 'b', + activeCodexManagedAccountIdsByRuntime: { host: 'b', wsl: {} } + } + }, + { + change: 'remove', + apply: (service: CodexAccountServiceInstance) => service.removeAccount('a'), + expected: { + activeCodexManagedAccountId: null, + activeCodexManagedAccountIdsByRuntime: { host: null, wsl: {} } + } + } + ])( + 'tells settings listeners about a $change made outside the window', + async ({ apply, expected }) => { + const store = createStore( + createSettings({ + codexManagedAccounts: [ + createCodexAccountRecord( + 'a', + 'a@example.com', + 'acct-a', + createManagedHome(testState.userDataDir, 'a') + ), + createCodexAccountRecord( + 'b', + 'b@example.com', + 'acct-b', + createManagedHome(testState.userDataDir, 'b') + ) + ], + activeCodexManagedAccountId: 'a', + activeCodexManagedAccountIdsByRuntime: { host: 'a', wsl: {} } + }) + ) + const broadcasts: Partial[] = [] + store.onSettingsChanged((updates) => broadcasts.push(updates)) + const { CodexAccountService } = await import('../service') + const service = new CodexAccountService( + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: select/remove read only getSettings/updateSettings, which the harness store implements. + store as never, + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the harness stub implements the rate-limit members the service calls on a switch. + createRateLimits() as never, + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the harness stub implements the runtime-home members the service calls on a switch. + createRuntimeHome() as never + ) + + await apply(service) + + expect(broadcasts).toContainEqual(expect.objectContaining(expected)) + } + ) +}) diff --git a/src/main/codex-accounts/account-management/codex-account-selection.ts b/src/main/codex-accounts/account-management/codex-account-selection.ts index 8b3895dacdc..2d3b54d38e9 100644 --- a/src/main/codex-accounts/account-management/codex-account-selection.ts +++ b/src/main/codex-accounts/account-management/codex-account-selection.ts @@ -3,6 +3,7 @@ import type { CodexRateLimitAccountsState, CodexSystemDefaultIdentity } from '../../../shared/agent-accounts/managed-account-types' +import type { GlobalSettings } from '../../../shared/settings/global-settings-types' import { parseWslUncPath } from '../../../shared/file-access/wsl-paths' import type { Store } from '../../persistence/loading-store/store' import type { RateLimitService } from '../../rate-limits/service/rate-limit-service' @@ -95,7 +96,7 @@ export class CodexAccountSelection { null ) - this.dependencies.store.updateSettings({ + this.saveSettings({ codexManagedAccounts: nextAccounts, activeCodexManagedAccountId: nextActiveId, activeCodexManagedAccountIdsByRuntime: nextSelection, @@ -134,11 +135,7 @@ export class CodexAccountSelection { normalizeManagedAccountDisplayName(displayName) ) if (names) { - // Notify windows: their status-bar rows read names from settings. - this.dependencies.store.updateSettings( - { managedAccountDisplayNames: names }, - { notifyListeners: true } - ) + this.saveSettings({ managedAccountDisplayNames: names }) } return this.snapshot() } @@ -169,7 +166,7 @@ export class CodexAccountSelection { accountId, effectiveTarget ) - this.dependencies.store.updateSettings({ + this.saveSettings({ activeCodexManagedAccountId: effectiveTarget?.runtime === 'wsl' ? nextSelection.host : accountId, activeCodexManagedAccountIdsByRuntime: nextSelection @@ -187,6 +184,11 @@ export class CodexAccountSelection { return this.snapshot() } + // Notify every window of changes made by the CLI, paired clients, or other windows. + saveSettings(patch: Partial): void { + this.dependencies.store.updateSettings(patch, { notifyListeners: true }) + } + // A `codex login` run inside a managed home swaps its account; label the row with what actually runs (#22284). private adoptManagedHomeIdentities(): void { const settings = this.dependencies.store.getSettings() diff --git a/src/main/codex-accounts/account-management/service-test-harness.ts b/src/main/codex-accounts/account-management/service-test-harness.ts index 1213b8ed1ac..4f7f010934d 100644 --- a/src/main/codex-accounts/account-management/service-test-harness.ts +++ b/src/main/codex-accounts/account-management/service-test-harness.ts @@ -41,19 +41,29 @@ export function createSettings(overrides: Partial = {}): GlobalS export function createStore(settings: GlobalSettings) { let resetLedger: CodexResetCreditAttemptLedger = { version: 1, attempts: [] } + const settingsListeners = new Set<(updates: Partial) => void>() return { getSettings: vi.fn(() => settings), - updateSettings: vi.fn((updates: Partial) => { - settings = { - ...settings, - ...updates, - notifications: { - ...settings.notifications, - ...updates.notifications + updateSettings: vi.fn( + (updates: Partial, options?: { notifyListeners?: boolean }) => { + settings = { + ...settings, + ...updates, + notifications: { + ...settings.notifications, + ...updates.notifications + } } + if (options?.notifyListeners) { + settingsListeners.forEach((listener) => listener(updates)) + } + return settings } - return settings - }), + ), + onSettingsChanged: (listener: (updates: Partial) => void) => { + settingsListeners.add(listener) + return () => settingsListeners.delete(listener) + }, getCodexResetCreditAttemptLedger: vi.fn(() => structuredClone(resetLedger)), replaceCodexResetCreditAttemptLedgerAndFlush: vi.fn( async (next: CodexResetCreditAttemptLedger) => {