import { describe, it, expect } from 'vitest';
import { characterSchema, type Character } from '@/lib/schemas';
import { encodeClaim, decodeClaim } from './playerLink';

const char: Character = characterSchema.parse({
  id: 'ch1', campaignId: 'cmp1', system: 'pf2e', name: 'Pïp Ünderbough',
  abilities: { str: 8, dex: 16, con: 12, int: 10, wis: 14, cha: 13 },
  hp: { current: 22, max: 22, temp: 0 }, createdAt: 't', updatedAt: 't',
});

describe('player claim link', () => {
  it('round-trips a payload through base64url (incl. unicode names)', () => {
    const encoded = encodeClaim({ character: char, campaignName: 'Lost Mine', campaignSystem: 'pf2e' });
    expect(encoded).not.toMatch(/[+/=]/); // url-safe
    const decoded = decodeClaim(encoded);
    expect(decoded?.character.name).toBe('Pïp Ünderbough');
    expect(decoded?.campaignName).toBe('Lost Mine');
    expect(decoded?.character.id).toBe('ch1');
  });

  it('returns null for garbage', () => {
    expect(decodeClaim('not-base64!!')).toBeNull();
    expect(decodeClaim(encodeClaim.length ? 'eyJ4IjoxfQ' : '')).toBeNull(); // valid b64 but wrong shape
  });
});
