import { describe, expect, it } from 'vitest';
import { parse } from './routeparse';

describe('parse', () => {
  it('maps the empty hash and the root to the lobby', () => {
    expect(parse('').name).toBe('lobby');
    expect(parse('#/').name).toBe('lobby');
  });

  it('maps known paths to their routes', () => {
    expect(parse('#/login').name).toBe('login');
    expect(parse('#/new').name).toBe('new');
    expect(parse('#/settings').name).toBe('settings');
    expect(parse('#/stats').name).toBe('stats');
    expect(parse('#/game/abc')).toEqual({ name: 'game', params: { id: 'abc' } });
    expect(parse('#/game/abc/chat')).toEqual({ name: 'gameChat', params: { id: 'abc' } });
    expect(parse('#/game/abc/check')).toEqual({ name: 'gameCheck', params: { id: 'abc' } });
  });

  it('maps an unknown path and a game without an id to notfound', () => {
    expect(parse('#/bogus').name).toBe('notfound');
    expect(parse('#/game').name).toBe('notfound');
  });

  it('treats a Telegram launch fragment as the lobby root, not notfound', () => {
    // Telegram appends its Mini App launch params to the URL fragment on a cold launch; they
    // are launch metadata, not a route. Parsing them as notfound made bootstrap's navigate('/')
    // re-key the route pane (notfound -> lobby) and slide the lobby in on launch.
    expect(parse('#tgWebAppData=query_id%3Dx&tgWebAppVersion=7.0&tgWebAppPlatform=ios').name).toBe(
      'lobby',
    );
    expect(parse('#tgWebAppStartParam=foo').name).toBe('lobby');
  });
});
