import { ImageUrlNormalizer } from './ImageUrlNormalizer.js';
import { Url } from './Url.js';
describe('ImageUrlNormalizer', () => {
const baseUrl = Url.create('https://example.com/blog/post');
describe('Should_NormalizeToAbsolute_When_RelativeUrl', () => {
it('converts relative path to absolute', () => {
const imageUrl = 'images/pic.jpg';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://example.com/blog/post/images/pic.jpg');
});
it('converts relative path with ./ prefix to absolute', () => {
const imageUrl = './images/pic.jpg';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://example.com/blog/post/images/pic.jpg');
});
it('converts absolute path to absolute URL', () => {
const imageUrl = '/assets/logo.png';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://example.com/assets/logo.png');
});
it('converts protocol-relative URL to absolute', () => {
const imageUrl = '//cdn.example.com/image.jpg';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://cdn.example.com/image.jpg');
});
});
describe('Should_ReturnAsIs_When_AlreadyAbsolute', () => {
it('returns https URL unchanged', () => {
const imageUrl = 'https://other.com/image.jpg';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://other.com/image.jpg');
});
it('returns http URL as https', () => {
const imageUrl = 'http://other.com/image.jpg';
const result = ImageUrlNormalizer.normalizeImageUrl(imageUrl, baseUrl);
expect(result).toBe('https://other.com/image.jpg');
});
});
describe('Should_NormalizeArray_When_MultipleUrls', () => {
it('normalizes array of mixed URL types', () => {
const images = [
'https://absolute.com/pic1.jpg',
'/assets/pic2.png',
'images/pic3.gif',
'//cdn.example.com/pic4.jpg'
];
const result = ImageUrlNormalizer.normalizeImageUrls(images, baseUrl);
expect(result).toEqual([
'https://absolute.com/pic1.jpg',
'https://example.com/assets/pic2.png',
'https://example.com/blog/post/images/pic3.gif',
'https://cdn.example.com/pic4.jpg'
]);
});
});
describe('Should_ValidateImageUrls_When_Filtering', () => {
it('identifies valid image URLs', () => {
expect(ImageUrlNormalizer.isValidImageUrl('https://example.com/pic.jpg')).toBe(true);
expect(ImageUrlNormalizer.isValidImageUrl('http://example.com/pic.jpg')).toBe(true);
});
it('identifies invalid image URLs', () => {
expect(ImageUrlNormalizer.isValidImageUrl('ftp://example.com/pic.jpg')).toBe(false);
expect(ImageUrlNormalizer.isValidImageUrl('not-a-url')).toBe(false);
expect(ImageUrlNormalizer.isValidImageUrl('')).toBe(false);
});
it('filters out invalid URLs', () => {
const urls = [
'https://valid.com/pic.jpg',
'ftp://invalid.com/pic.jpg',
'not-a-url',
'https://another-valid.com/image.png'
];
const result = ImageUrlNormalizer.filterValidImageUrls(urls);
expect(result).toEqual([
'https://valid.com/pic.jpg',
'https://another-valid.com/image.png'
]);
});
});
describe('Should_HandleError_When_InvalidUrl', () => {
it('returns original URL when normalization fails', () => {
const invalidUrl = 'definitely-not-a-valid-url';
const result = ImageUrlNormalizer.normalizeImageUrl(invalidUrl, baseUrl);
expect(result).toBe(invalidUrl);
});
});
});