import { render, screen } from '@testing-library/react';
import { expect, test } from 'vitest';
import { MemoryRouter } from 'react-router-dom';
import { ROUTER_FUTURE } from '../routing/routerConfig';
import { withMonitorReturn } from '../routing/monitorContext';
import { MonitorReturnBar } from './MonitorReturnBar';

test('shows a deterministic monitor return only for monitor-originated child routes', () => {
  const child = withMonitorReturn('/history?vin=LTEST000000000001', '/monitor?selectedVin=LTEST000000000001&detail=collapsed&zoom=13&bounds=113.1%2C23%2C113.4%2C23.3');
  const view = render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={[child]}><MonitorReturnBar /></MemoryRouter>);

  expect(screen.getByRole('link', { name: /返回全局监控/ })).toHaveAttribute('href', expect.stringContaining('/monitor?'));
  expect(screen.getByText(/LTEST000000000001/)).toBeInTheDocument();
  expect(screen.getByText(/Zoom 13 · 详情收起/)).toBeInTheDocument();

  view.unmount();
  render(<MemoryRouter future={ROUTER_FUTURE} initialEntries={['/history?vin=LTEST000000000001']}><MonitorReturnBar /></MemoryRouter>);
  expect(screen.queryByRole('link', { name: /返回全局监控/ })).not.toBeInTheDocument();
});
