/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { beforeEach, describe, expect, it, vi } from 'vitest';
const profileBucketsRef = vi.hoisted(() => ({
    current: ['bucket-a', 'bucket-b'],
}));
vi.mock('../runtime/runtimeSettings.js', async () => {
    const actual = await vi.importActual('../runtime/runtimeSettings.js');
    return {
        ...actual,
        getCliRuntimeServices: vi.fn(() => ({
            settingsService: {
                getCurrentProfileName: () => 'dualclaude',
                get: () => 'dualclaude',
            },
        })),
    };
});
vi.mock('@vybestack/llxprt-code-core', async () => {
    const actual = await vi.importActual('@vybestack/llxprt-code-core');
    class MockProfileManager {
        async loadProfile() {
            return {
                auth: {
                    type: 'oauth',
                    buckets: profileBucketsRef.current,
                },
            };
        }
    }
    return {
        ...actual,
        ProfileManager: MockProfileManager,
    };
});
import { OAuthManager } from './oauth-manager.js';
function makeToken(accessToken) {
    return {
        access_token: accessToken,
        refresh_token: `refresh-${accessToken}`,
        expiry: Math.floor(Date.now() / 1000) + 3600,
        token_type: 'Bearer',
        scope: '',
    };
}
describe('OAuthManager bucket failover integration (CLI)', () => {
    let tokenStore;
    let oauthManager;
    beforeEach(() => {
        tokenStore = {
            saveToken: vi.fn(),
            getToken: vi.fn(async (_provider, bucket) => {
                if (bucket === 'bucket-a') {
                    return makeToken('token-bucket-a');
                }
                return null;
            }),
            removeToken: vi.fn(),
            listProviders: vi.fn(async () => []),
            listBuckets: vi.fn(async () => profileBucketsRef.current),
            getBucketStats: vi.fn(async () => null),
        };
        oauthManager = new OAuthManager(tokenStore);
        const provider = {
            name: 'anthropic',
            initiateAuth: vi.fn(async () => undefined),
            getToken: vi.fn(async () => null),
            refreshIfNeeded: vi.fn(async () => null),
        };
        oauthManager.registerProvider(provider);
    });
    it('defaults to the first profile bucket and configures a failover handler', async () => {
        const config = {
            getBucketFailoverHandler: vi.fn(() => undefined),
            setBucketFailoverHandler: vi.fn(),
        };
        oauthManager.setConfigGetter(() => config);
        await oauthManager.toggleOAuthEnabled('anthropic');
        const token = await oauthManager.getToken('anthropic');
        expect(token).toBe('token-bucket-a');
        expect(oauthManager.getSessionBucket('anthropic')).toBe('bucket-a');
        expect(config.setBucketFailoverHandler).toHaveBeenCalledTimes(1);
        expect(tokenStore.getToken).toHaveBeenCalledWith('anthropic', 'bucket-a');
    });
});
//# sourceMappingURL=oauth-manager.bucketFailover.spec.js.map