diff --git a/src/auth/profile-detector.ts b/src/auth/profile-detector.ts index 8fa35701..48948a8e 100644 --- a/src/auth/profile-detector.ts +++ b/src/auth/profile-detector.ts @@ -427,6 +427,14 @@ class ProfileDetector { // Check if account-based default exists (legacy) const profiles = this.readProfiles(); + if (unifiedConfig?.default && profiles.profiles[unifiedConfig.default]) { + return { + type: 'account', + name: unifiedConfig.default, + profile: profiles.profiles[unifiedConfig.default], + }; + } + if (profiles.default && profiles.profiles[profiles.default]) { return { type: 'account', diff --git a/tests/unit/auth/profile-detector.test.ts b/tests/unit/auth/profile-detector.test.ts index 7bdb344a..8769615b 100644 --- a/tests/unit/auth/profile-detector.test.ts +++ b/tests/unit/auth/profile-detector.test.ts @@ -190,6 +190,51 @@ describe('ProfileDetector', () => { } }); + it('should resolve a unified default that points to a legacy-only account profile', () => { + const originalCcsHome = process.env.CCS_HOME; + process.env.CCS_HOME = tempDir; + const ccsDir = path.join(tempDir, '.ccs'); + fs.mkdirSync(ccsDir, { recursive: true }); + fs.writeFileSync( + path.join(ccsDir, 'profiles.json'), + JSON.stringify({ + default: 'oldDefault', + profiles: { + oldDefault: { created: '2025-01-01', last_used: '2025-01-02' }, + legacyOnly: { created: '2025-02-01', last_used: '2025-02-02' }, + }, + }) + ); + + const mockUnifiedConfig = { + version: 2, + default: 'legacyOnly', + profiles: {}, + accounts: {}, + }; + + const isUnifiedModeSpy = spyOn(unifiedConfigLoader, 'isUnifiedMode').mockReturnValue(true); + const loadUnifiedConfigSpy = spyOn(unifiedConfigLoader, 'loadUnifiedConfig').mockReturnValue( + mockUnifiedConfig as any + ); + + try { + const localDetector = new ProfileDetector(); + const result = localDetector.resolveDefaultProfileResult(); + expect(result.type).toBe('account'); + expect(result.name).toBe('legacyOnly'); + expect(result.profile).toEqual({ created: '2025-02-01', last_used: '2025-02-02' }); + } finally { + isUnifiedModeSpy.mockRestore(); + loadUnifiedConfigSpy.mockRestore(); + if (originalCcsHome !== undefined) { + process.env.CCS_HOME = originalCcsHome; + } else { + delete process.env.CCS_HOME; + } + } + }); + it('should return null for unknown profile (throws error)', () => { const isUnifiedModeSpy = spyOn(unifiedConfigLoader, 'isUnifiedMode').mockReturnValue(false); // Mock readConfig/readProfiles to return empty