import { describe, expect, it } from 'vitest';
import { generateCodeChallenge, generateRandomString } from './pkce';

describe('pkce', () => {
  it('computes the RFC 7636 Appendix B S256 test vector', async () => {
    // https://datatracker.ietf.org/doc/html/rfc7636#appendix-B
    const codeVerifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
    const challenge = await generateCodeChallenge(codeVerifier);
    expect(challenge).toBe('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
  });

  it('generates a URL-safe random string of the requested length family', () => {
    const value = generateRandomString();
    expect(value).toMatch(/^[A-Za-z0-9_-]+$/);
    expect(value.length).toBeGreaterThanOrEqual(43);
  });

  it('generates different values on each call', () => {
    expect(generateRandomString()).not.toBe(generateRandomString());
  });
});
