import { describe, expect, it } from 'vitest';
import {
  buildCollaborationJoinUrl,
  createCollaborationRoomDescriptor,
  parseCollaborationJoinLocation,
} from './index';

describe('TX7 collaboration project links', () => {
  it('round-trips a generated UUID and 256-bit secret', () => {
    const projectId = '11111111-1111-4111-8111-111111111111';
    const room = createCollaborationRoomDescriptor(projectId);
    const url = new URL(buildCollaborationJoinUrl(room, 'https://www.learnitive.com'));
    expect(parseCollaborationJoinLocation({ pathname: url.pathname, hash: url.hash })).toEqual(room);
    expect(room.roomId).toMatch(/^[0-9a-f-]{36}$/u);
    expect(room.secret).toMatch(/^[0-9a-f]{64}$/u);
    expect(url.pathname).toBe(`/join/${room.roomId}/${projectId}`);
  });

  it('rejects malformed links and old token-shaped input', () => {
    const validRoom = '28cdf8fa-0f3f-47c0-9e20-0f38355b4ee3';
    const projectId = '11111111-1111-4111-8111-111111111111';
    const validSecret = 'a'.repeat(64);
    expect(parseCollaborationJoinLocation({ pathname: `/join/${validRoom}/${projectId}`, hash: `#${validSecret}` })).toEqual({ roomId: validRoom, secret: validSecret, projectId });
    expect(parseCollaborationJoinLocation({ pathname: '/join/room-id', hash: '#secret' })).toBeUndefined();
    expect(parseCollaborationJoinLocation({ pathname: `/join/${validRoom}`, hash: '#short' })).toBeUndefined();
    expect(parseCollaborationJoinLocation({ pathname: '/projects', hash: `#${validSecret}` })).toBeUndefined();
  });
});
