/** * useChallongeClient Composable Tests */ import { describe, it, expect, vi } from 'vitest'; import { ref } from 'vue'; import { ScopeType } from '../../../src/services/challonge.service.js'; // Mock dependencies before importing composable vi.mock('../../../src/composables/useChallongeApiKey.js', () => ({ useChallongeApiKey: () => ({ getApiKey: () => 'test-api-key-1234567890' }) })); vi.mock('../../../src/composables/useChallongeOAuth.js', () => ({ useChallongeOAuth: () => ({ isAuthenticated: ref(false), accessToken: ref(null) }) })); vi.mock('../../../src/composables/useChallongeClientCredentials.js', () => ({ useChallongeClientCredentials: () => ({ isAuthenticated: ref(false), accessToken: ref(null) }) })); vi.mock('../../../src/services/challonge.service.js', async () => { const actual = await vi.importActual('../../../src/services/challonge.service.js'); return { ...actual, createChallongeV1Client: vi.fn(() => ({ version: 'v1', mock: true })), createChallongeV2Client: vi.fn(() => ({ version: 'v2.1', mock: true })) }; }); import { useChallongeClient } from '../../../src/composables/useChallongeClient.js'; describe('useChallongeClient', () => { it('creates composable with default values', () => { const { apiVersion, tournamentScope, debugMode } = useChallongeClient(); expect(apiVersion.value).toBe('v2.1'); expect(tournamentScope.value).toBe(ScopeType.USER); expect(debugMode.value).toBe(false); }); it('creates client successfully', () => { const { client } = useChallongeClient(); expect(client.value).toBeDefined(); expect(client.value).toHaveProperty('version'); }); it('switches API version', () => { const { apiVersion, switchVersion } = useChallongeClient(); expect(apiVersion.value).toBe('v2.1'); switchVersion('v1'); expect(apiVersion.value).toBe('v1'); }); it('throws error for invalid API version', () => { const { switchVersion } = useChallongeClient(); expect(() => switchVersion('v3')).toThrow('Invalid API version'); }); it('changes tournament scope', () => { const { tournamentScope, setScope } = useChallongeClient(); expect(tournamentScope.value).toBe(ScopeType.USER); setScope(ScopeType.APPLICATION); expect(tournamentScope.value).toBe(ScopeType.APPLICATION); }); it('throws error for invalid scope', () => { const { setScope } = useChallongeClient(); expect(() => setScope('invalid')).toThrow('Invalid scope type'); }); it('masks API key correctly', () => { const { maskedApiKey } = useChallongeClient(); const masked = maskedApiKey.value; expect(masked).toContain('•••'); expect(masked.startsWith('test')).toBe(true); expect(masked.endsWith('7890')).toBe(true); }); it('returns auth type', () => { const { authType } = useChallongeClient(); expect(authType.value).toBe('API Key'); }); it('toggles debug mode', () => { const { debugMode, setDebugMode } = useChallongeClient(); expect(debugMode.value).toBe(false); setDebugMode(true); expect(debugMode.value).toBe(true); }); it('exposes constants', () => { const { ScopeType: ST, AuthType: AT } = useChallongeClient(); expect(ST).toBeDefined(); expect(AT).toBeDefined(); expect(ST.USER).toBe('user'); expect(ST.APPLICATION).toBe('application'); }); });