import { describe, it, expect } from 'vitest';
import { validateInstagramUrl } from '$lib/server/validation/instagram-url';

describe('Instagram URL Validation', () => {
	describe('Valid URLs', () => {
		it('should accept post URLs without www', () => {
			const result = validateInstagramUrl('https://instagram.com/p/ABC123');
			expect(result.valid).toBe(true);
			expect(result.error).toBeUndefined();
		});

		it('should accept post URLs with www', () => {
			const result = validateInstagramUrl('https://www.instagram.com/p/XYZ789');
			expect(result.valid).toBe(true);
		});

		it('should accept reel URLs', () => {
			const result = validateInstagramUrl('https://instagram.com/reel/DSevV5CDcNm');
			expect(result.valid).toBe(true);
		});

		it('should accept reel URLs with query parameters', () => {
			const result = validateInstagramUrl(
				'https://www.instagram.com/reel/DSevV5CDcNm/?utm_source=ig_web_copy_link'
			);
			expect(result.valid).toBe(true);
		});

		it('should accept IGTV URLs', () => {
			const result = validateInstagramUrl('https://instagram.com/tv/ABC123');
			expect(result.valid).toBe(true);
		});

		it('should accept URLs with multiple query parameters', () => {
			const result = validateInstagramUrl(
				'https://instagram.com/p/ABC123?utm_source=share&utm_medium=social'
			);
			expect(result.valid).toBe(true);
		});

		it('should accept URLs with trailing slash', () => {
			const result = validateInstagramUrl('https://instagram.com/p/ABC123/');
			expect(result.valid).toBe(true);
		});

		it('should accept URLs with hash fragments', () => {
			const result = validateInstagramUrl('https://instagram.com/p/ABC123#section');
			expect(result.valid).toBe(true);
		});
	});

	describe('Invalid Protocol', () => {
		it('should reject HTTP URLs', () => {
			const result = validateInstagramUrl('http://instagram.com/p/ABC123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('HTTPS');
		});

		it('should reject FTP URLs', () => {
			const result = validateInstagramUrl('ftp://instagram.com/p/ABC123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('HTTPS');
		});
	});

	describe('Invalid Domain', () => {
		it('should reject non-Instagram domains', () => {
			const result = validateInstagramUrl('https://facebook.com/post/123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('instagram.com');
		});

		it('should reject malicious look-alike domains', () => {
			const result = validateInstagramUrl('https://instagram.com.evil.com/p/ABC123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('instagram.com');
		});

		it('should reject subdomains other than www', () => {
			const result = validateInstagramUrl('https://api.instagram.com/p/ABC123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('instagram.com');
		});

		it('should reject completely different domains', () => {
			const result = validateInstagramUrl('https://example.com');
			expect(result.valid).toBe(false);
		});
	});

	describe('Invalid URL Format', () => {
		it('should reject invalid URL strings', () => {
			const result = validateInstagramUrl('not-a-url');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('Invalid URL format');
		});

		it('should reject empty strings', () => {
			const result = validateInstagramUrl('');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('non-empty string');
		});

		it('should reject whitespace-only strings', () => {
			const result = validateInstagramUrl('   ');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('non-empty string');
		});

		it('should reject relative URLs', () => {
			const result = validateInstagramUrl('/p/ABC123');
			expect(result.valid).toBe(false);
			expect(result.error).toContain('Invalid URL format');
		});
	});

	describe('Edge Cases', () => {
		it('should handle URLs with Unicode characters in query params', () => {
			const result = validateInstagramUrl('https://instagram.com/p/ABC123?text=hello%20world');
			expect(result.valid).toBe(true);
		});

		it('should handle URLs with port numbers', () => {
			// Instagram doesn't use custom ports, but URL should parse
			const result = validateInstagramUrl('https://instagram.com:443/p/ABC123');
			expect(result.valid).toBe(true);
		});

		it('should accept stories URLs', () => {
			const result = validateInstagramUrl('https://instagram.com/stories/username/123456789');
			expect(result.valid).toBe(true);
		});

		it('should accept any Instagram path', () => {
			const result = validateInstagramUrl('https://instagram.com/any/path/here');
			expect(result.valid).toBe(true);
		});
	});
});
