import { render, screen } from '@testing-library/react';
import App from './App';

const mockAllData = {
  profile: {
    nickname: 'Test User',
    avatar: null,
    introduction: 'Hello',
    showlocalbackground: false,
    footer: '© Test Footer'
  },
  techstack: null,
  projects: null,
  contacts: null
};

const mockVisitorInfo = {
  ip: '66.90.99.202',
  ipVersion: 'ipv4',
  geo: { country: 'JP', region: 'Tokyo', city: 'Ebara' }
};

beforeEach(() => {
  jest.spyOn(global, 'fetch').mockImplementation((url) => {
    if (typeof url === 'string' && url.includes('/api/all')) {
      return Promise.resolve({
        ok: true,
        json: () => Promise.resolve(mockAllData)
      });
    }
    if (url === 'https://cf-ip-geo.smyhub.com/api') {
      return Promise.resolve({
        ok: true,
        json: () => Promise.resolve(mockVisitorInfo)
      });
    }
    return Promise.resolve({
      ok: true,
      json: () => Promise.resolve({})
    });
  });
});

afterEach(() => {
  global.fetch.mockRestore();
});

test('renders visitor ip and geo in footer', async () => {
  render(<App />);
  const visitorLine = await screen.findByText(/访客 IP：66\.90\.99\.202/i);
  expect(visitorLine).toBeInTheDocument();
  expect(visitorLine).toHaveTextContent('JP');
});
