import { describe, it, expect } from 'vitest';
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import { createVuetify } from 'vuetify';
import { createRouter, createMemoryHistory } from 'vue-router';
import TwoFactorView from '../../resources/js/views/auth/TwoFactorView.vue';
import { useAuthStore } from '../../resources/js/stores/auth';

const mountTwoFactor = async () => {
    setActivePinia(createPinia());
    // Эмулируем pending-2FA state — иначе onMounted редиректит на /login.
    const auth = useAuthStore();
    auth.requires2fa = true;

    const router = createRouter({
        history: createMemoryHistory(),
        routes: [
            { path: '/2fa', component: TwoFactorView },
            { path: '/login', component: { template: '<div>stub</div>' } },
            { path: '/dashboard', component: { template: '<div>stub</div>' } },
            { path: '/recovery', component: { template: '<div>stub</div>' } },
            { path: '/recovery-use', component: { template: '<div>stub</div>' } },
        ],
    });
    await router.push('/2fa');
    await router.isReady();
    return mount(TwoFactorView, {
        global: { plugins: [createVuetify(), router] },
    });
};

describe('TwoFactorView.vue', () => {
    it('монтируется и содержит заголовок', async () => {
        const wrapper = await mountTwoFactor();
        expect(wrapper.text()).toContain('Двухфакторная проверка');
    });

    it('содержит ровно 6 input-cell для кода', async () => {
        const wrapper = await mountTwoFactor();
        const cells = wrapper.findAll('.code-cell');
        expect(cells).toHaveLength(6);
        // Каждая cell — numeric inputmode + maxlength=1.
        cells.forEach((cell) => {
            expect(cell.attributes('inputmode')).toBe('numeric');
            expect(cell.attributes('maxlength')).toBe('1');
        });
    });

    it('содержит ссылку на резервный код', async () => {
        const wrapper = await mountTwoFactor();
        const links = wrapper.findAll('a').map((a) => a.text());
        expect(links.some((t) => t.includes('резервный код'))).toBe(true);
    });
});
