/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, expect, it, vi } from 'vitest';
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 concurrency', () => {
    it('serializes bucket resolution for concurrent getOAuthToken calls (same provider)', async () => {
        const 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 () => ['bucket-a', 'bucket-b']),
            getBucketStats: vi.fn(async () => null),
        };
        const 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);
        let inProgress = false;
        vi.spyOn(oauthManager, 'getProfileBuckets').mockImplementation(async () => {
            if (inProgress) {
                throw new Error('getProfileBuckets called concurrently');
            }
            inProgress = true;
            await new Promise((resolve) => setTimeout(resolve, 10));
            inProgress = false;
            return ['bucket-a', 'bucket-b'];
        });
        let storedFailoverHandler;
        const config = {
            getBucketFailoverHandler: vi.fn(() => storedFailoverHandler),
            setBucketFailoverHandler: vi.fn((handler) => {
                storedFailoverHandler = handler;
            }),
        };
        oauthManager.setConfigGetter(() => config);
        const [tokenA, tokenB] = await Promise.all([
            oauthManager.getOAuthToken('anthropic'),
            oauthManager.getOAuthToken('anthropic'),
        ]);
        expect(tokenA?.access_token).toBe('token-bucket-a');
        expect(tokenB?.access_token).toBe('token-bucket-a');
        expect(config.setBucketFailoverHandler).toHaveBeenCalledTimes(1);
    });
});
//# sourceMappingURL=oauth-manager.concurrency.spec.js.map