import { describe, it, expect, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import { ProvinceProvider, useProvince } from '@/context/ProvinceProvider';

function Probe() {
    const { cityId, isProvinceInclude } = useProvince();
    return <span data-testid="out">{cityId ? `${cityId}:${isProvinceInclude.name}` : 'none'}</span>;
}

function setHostname(hostname) {
    Object.defineProperty(window, 'location', {
        value: { hostname },
        writable: true,
        configurable: true,
    });
}

describe('ProvinceProvider / useProvince', () => {
    it('host شهر شناخته‌شده → cityId و شهر را از useEffect ست می‌کند', async () => {
        setHostname('arak-nobat.ir');
        render(
            <ProvinceProvider>
                <Probe />
            </ProvinceProvider>,
        );
        await waitFor(() => expect(screen.getByTestId('out')).toHaveTextContent('129:اراک'));
    });

    it('host ناشناخته → cityId null', async () => {
        setHostname('example.com');
        render(
            <ProvinceProvider>
                <Probe />
            </ProvinceProvider>,
        );
        await waitFor(() => expect(screen.getByTestId('out')).toHaveTextContent('none'));
    });
});
