import {
  describe,
  it,
  expect
} from 'vitest';
import { parseInviteQuery } from './invite-query.util';

describe('parseInviteQuery', () => {
  it('returns null for empty or whitespace queries', () => {
    expect(parseInviteQuery('')).toBeNull();
    expect(parseInviteQuery('   ')).toBeNull();
  });

  it('returns null for short or space-containing queries', () => {
    expect(parseInviteQuery('abc')).toBeNull();
    expect(parseInviteQuery('hello world')).toBeNull();
  });

  it('treats a bare url-safe code as an invite without server context', () => {
    expect(parseInviteQuery('abc123')).toEqual({ inviteId: 'abc123' });
    expect(parseInviteQuery('Team-Code_9')).toEqual({ inviteId: 'Team-Code_9' });
  });

  it('extracts the id from an invite path', () => {
    expect(parseInviteQuery('/invite/xyz789')).toEqual({ inviteId: 'xyz789' });
  });

  it('extracts the id and server from a browser invite URL', () => {
    expect(parseInviteQuery('https://web.toju.app/invite/Code_42?server=https%3A%2F%2Fsignal.toju.app')).toEqual({
      inviteId: 'Code_42',
      sourceUrl: 'https://signal.toju.app'
    });
  });

  it('derives the signal server from a signal-origin invite URL', () => {
    expect(parseInviteQuery('https://localhost:3001/invite/abc123')).toEqual({
      inviteId: 'abc123',
      sourceUrl: 'https://localhost:3001'
    });
  });

  it('does not treat a web-app invite URL without server param as complete', () => {
    expect(parseInviteQuery('https://app.test/invite/Code_42?ref=1')).toEqual({ inviteId: 'Code_42' });
  });

  it('parses toju protocol invite links', () => {
    expect(parseInviteQuery('toju://invite/DeepLink_1?server=https%3A%2F%2Fsignal.toju.app')).toEqual({
      inviteId: 'DeepLink_1',
      sourceUrl: 'https://signal.toju.app'
    });
  });
});
