/**
 * Unit tests for parseLibraryId (TRUEREF-0010).
 */

import { describe, it, expect } from 'vitest';
import { parseLibraryId } from './library-id';

describe('parseLibraryId', () => {
	it('parses /owner/repo (default branch)', () => {
		const result = parseLibraryId('/facebook/react');
		expect(result.repositoryId).toBe('/facebook/react');
		expect(result.version).toBeUndefined();
	});

	it('parses /owner/repo/version', () => {
		const result = parseLibraryId('/facebook/react/v18.3.0');
		expect(result.repositoryId).toBe('/facebook/react');
		expect(result.version).toBe('v18.3.0');
	});

	it('parses /owner/repo/version with dot-separated version', () => {
		const result = parseLibraryId('/sveltejs/svelte/4.0.0');
		expect(result.repositoryId).toBe('/sveltejs/svelte');
		expect(result.version).toBe('4.0.0');
	});

	it('parses /owner/repo/branch-name as version', () => {
		const result = parseLibraryId('/vercel/next.js/canary');
		expect(result.repositoryId).toBe('/vercel/next.js');
		expect(result.version).toBe('canary');
	});

	it('throws on missing leading slash', () => {
		expect(() => parseLibraryId('facebook/react')).toThrow('Invalid libraryId');
	});

	it('throws on empty string', () => {
		expect(() => parseLibraryId('')).toThrow('Invalid libraryId');
	});

	it('throws on single-segment path', () => {
		expect(() => parseLibraryId('/react')).toThrow('Invalid libraryId');
	});

	it('throws on path with only a slash', () => {
		expect(() => parseLibraryId('/')).toThrow('Invalid libraryId');
	});
});
