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

describe('parseRepoUrl', () => {
  it('parses "owner/name"', () => {
    expect(parseRepoUrl('vercel/next.js')).toEqual({ owner: 'vercel', name: 'next.js' });
  });
  it('parses an https URL', () => {
    expect(parseRepoUrl('https://github.com/torvalds/linux')).toEqual({
      owner: 'torvalds',
      name: 'linux',
    });
  });
  it('parses an ssh URL', () => {
    expect(parseRepoUrl('git@github.com:ignaciolagosruiz/releasekit.git')).toEqual({
      owner: 'ignaciolagosruiz',
      name: 'releasekit',
    });
  });
  it('lowercases the owner and name', () => {
    expect(parseRepoUrl('IgnacioLagosRuiz/REPO-STATS')).toEqual({
      owner: 'ignaciolagosruiz',
      name: 'repo-stats',
    });
  });
  it('strips a trailing .git', () => {
    expect(parseRepoUrl('https://github.com/x/y.git/')).toEqual({ owner: 'x', name: 'y' });
  });
  it('throws on garbage', () => {
    expect(() => parseRepoUrl('not a repo')).toThrow(/Could not parse/);
  });
});

describe('normalizeRepo', () => {
  it('lowercases and strips .git', () => {
    expect(normalizeRepo({ owner: 'X', name: 'Y.GIT' })).toEqual({ owner: 'x', name: 'y' });
  });
});
