import { afterEach, describe, expect, it, vi } from 'vitest';
import { openExternalUrl } from './links';

describe('openExternalUrl', () => {
  afterEach(() => vi.unstubAllGlobals());

  it('routes through the VK away redirect inside the Android VK client', () => {
    const open = vi.fn();
    vi.stubGlobal('location', {
      pathname: '/vk/',
      search: '?vk_user_id=1&vk_platform=mobile_android&sign=x',
      href: 'https://app.example/vk/',
      origin: 'https://app.example',
    });
    vi.stubGlobal('window', { open });
    openExternalUrl('https://t.me/some_bot');
    expect(open).toHaveBeenCalledWith(
      `https://vk.com/away.php?to=${encodeURIComponent('https://t.me/some_bot')}`,
      '_blank',
    );
  });

  it('opens a plain new tab elsewhere', () => {
    const open = vi.fn();
    vi.stubGlobal('window', { open });
    openExternalUrl('https://t.me/some_bot');
    expect(open).toHaveBeenCalledWith('https://t.me/some_bot', '_blank');
  });
});
