import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { mount, flushPromises, VueWrapper } from '@vue/test-utils' import { createPinia, setActivePinia } from 'pinia' import FreeApiKeyCard from '@/components/FreeApiKeyCard.vue' import RatingsOrderList from '@/components/RatingsOrderList.vue' import { useAuthStore } from '@/stores/auth' import { DEFAULT_RATINGS_ORDER } from '@/lib/constants' import type { FreeKeyDefaults } from '@/lib/auth-api' vi.mock('@/stores/auth', async (importOriginal) => { const actual = await importOriginal() return actual }) /** A full FreeKeyDefaults fixture; override individual fields per test. */ function makeDefaults(overrides: Partial = {}): FreeKeyDefaults { return { image_source: 't', lang: 'en', textless: false, ratings_limit: 3, ratings_order: DEFAULT_RATINGS_ORDER, ratings_exclude: '', poster_position: 'bc', logo_ratings_limit: 5, backdrop_ratings_limit: 5, poster_badge_style: 'v', logo_badge_style: 'v', backdrop_badge_style: 'v', poster_label_style: 'o', logo_label_style: 'o', backdrop_label_style: 'o', poster_badge_direction: 'd', poster_badge_split: false, poster_fit: 'native', poster_badge_size: 'm', logo_badge_size: 'm', backdrop_badge_size: 'm', backdrop_position: 'bc', backdrop_badge_direction: 'd', backdrop_edge_inset_x: 0, backdrop_edge_inset_y: 0, episode_ratings_limit: 3, episode_badge_style: 'v', episode_label_style: 'o', episode_badge_size: 'm', episode_position: 'bc', episode_badge_direction: 'd', episode_blur: false, ...overrides, } } const SelectStub = { name: 'Select', template: '
', props: ['modelValue'], emits: ['update:modelValue'], } function mountCard(freeApiKeyEnabled = true, defaults: FreeKeyDefaults | null = makeDefaults()) { const pinia = createPinia() setActivePinia(pinia) const auth = useAuthStore() auth.freeApiKeyEnabled = freeApiKeyEnabled // Pre-seed so the card's onMounted load short-circuits (no network in tests). // Pass `null` to exercise the fetch/fallback paths explicitly. if (defaults) auth.freeKeyDefaults = defaults return mount(FreeApiKeyCard, { global: { plugins: [pinia], stubs: { Select: SelectStub, SelectTrigger: { template: '', props: ['id', 'ariaLabel', 'class'] }, SelectValue: { template: '{{ placeholder }}', props: ['placeholder'] }, SelectContent: { template: '' }, SelectItem: { template: '', props: ['value'] }, Collapsible: { template: '
', props: ['open'] }, CollapsibleTrigger: { template: '
', props: ['asChild'] }, CollapsibleContent: { template: '
' }, Input: { name: 'Input', // Mirror Vue's real v-model coercion: a `type="number"` input yields a // Number (the real shadcn Input wraps a native input, so the card's // refs receive numbers — string-only handling would break at runtime). template: '', props: ['modelValue', 'type', 'placeholder', 'required', 'id'], emits: ['update:modelValue'], methods: { onInput(e: Event) { const raw = (e.target as HTMLInputElement).value const val = this.type === 'number' && raw !== '' && !Number.isNaN(Number(raw)) ? Number(raw) : raw this.$emit('update:modelValue', val) }, }, }, Button: { template: '', props: ['disabled', 'variant', 'size', 'type'], }, Checkbox: { name: 'Checkbox', template: '