Triton/Packages/FoundationExtensions/Tests/FoundationExtensionsTests/URLImagePreviewTests.swift
2025-12-22 11:31:33 +01:00

142 lines
4.8 KiB
Swift

import Foundation
import Testing
@testable import FoundationExtensions
@Suite("URLImagePreview Tests")
struct URLImagePreviewTests {
@Test("It should replace the file extension with .preview.jpg")
func imagePreviewURL_withStandardImageURL_insertsPreviewBeforeExtension() {
// Given
let url = URL(string: "https://cdn.some.pics/user/image.jpg")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://cdn.some.pics/user/image.preview.jpg",
"It should replace the file extension with .preview.jpg"
)
}
@Test("It should replace PNG extension with .preview.jpg")
func imagePreviewURL_withPNGImage_insertsPreviewBeforeExtension() {
// Given
let url = URL(string: "https://example.com/photos/sunset.png")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://example.com/photos/sunset.preview.jpg",
"It should replace PNG extension with .preview.jpg"
)
}
@Test("It should append .preview.jpg when there is no file extension")
func imagePreviewURL_withNoExtension_appendsPreview() {
// Given
let url = URL(string: "https://example.com/images/photo")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://example.com/images/photo.preview.jpg",
"It should append .preview.jpg when there is no file extension"
)
}
@Test("It should replace the extension with .preview.jpg for complex filenames")
func imagePreviewURL_withComplexFilename_insertsPreviewCorrectly() {
// Given
let url = URL(string: "https://cdn.example.com/user-uploads/my-vacation-photo.jpeg")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://cdn.example.com/user-uploads/my-vacation-photo.preview.jpg",
"It should replace the extension with .preview.jpg for complex filenames"
)
}
@Test("It should preserve query parameters and use .preview.jpg extension")
func imagePreviewURL_withQueryParameters_preservesQuery() {
// Given
let url = URL(string: "https://api.example.com/images/photo.jpg?size=large&quality=high")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://api.example.com/images/photo.preview.jpg?size=large&quality=high",
"It should preserve query parameters and use .preview.jpg extension"
)
}
@Test("It should maintain the full directory structure and use .preview.jpg extension")
func imagePreviewURL_withNestedDirectories_maintainsDirectoryStructure() {
// Given
let url = URL(string: "https://storage.example.com/users/123/albums/vacation/beach.gif")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://storage.example.com/users/123/albums/vacation/beach.preview.jpg",
"It should maintain the full directory structure and use .preview.jpg extension"
)
}
@Test("It should work with local file URLs and use .preview.jpg extension")
func imagePreviewURL_withLocalFileURL_insertsPreviewCorrectly() {
// Given
let url = URL(fileURLWithPath: "/Users/test/Documents/screenshot.png")
// When
let result = url.imagePreviewURL
// Then
#expect(
result.path == "/Users/test/Documents/screenshot.preview.jpg",
"It should work with local file URLs and use .preview.jpg extension"
)
}
@Test("It should replace the last extension with .preview.jpg when multiple dots exist")
func imagePreviewURL_withMultipleDots_insertsPreviewBeforeLastExtension() {
// Given
let url = URL(string: "https://example.com/files/image.backup.jpg")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://example.com/files/image.backup.preview.jpg",
"It should replace the last extension with .preview.jpg when multiple dots exist"
)
}
@Test("It should handle single character filenames and use .preview.jpg extension")
func imagePreviewURL_withSingleCharacterFilename_insertsPreviewCorrectly() {
// Given
let url = URL(string: "https://example.com/a.jpg")!
// When
let result = url.imagePreviewURL
// Then
#expect(
result.absoluteString == "https://example.com/a.preview.jpg",
"It should handle single character filenames and use .preview.jpg extension"
)
}
}