Skip to content

Commit

Permalink
Test the files to make sure their mime types match the info.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Oct 15, 2024
1 parent 46a63c9 commit ec509a5
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions UnitTests/Sources/MediaUploadingPreprocessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Please see LICENSE in the repository root for full details.
//

import UniformTypeIdentifiers
import XCTest

@testable import ElementX
Expand Down Expand Up @@ -257,11 +258,14 @@ final class MediaUploadingPreprocessorTests: XCTestCase {
}

guard case let .success(result) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(_, _, imageInfo) = result else {
case let .image(convertedImageURL, _, imageInfo) = result else {
XCTFail("Failed processing asset")
return
}

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: convertedImageURL), "image/png")

// Check resulting image info
XCTAssertEqual(imageInfo.mimetype, "image/png")
XCTAssertEqual(imageInfo.blurhash, "K0TSUA~qfQ~qj[fQfQfQfQ")
Expand All @@ -279,11 +283,14 @@ final class MediaUploadingPreprocessorTests: XCTestCase {
appSettings.optimizeMediaUploads = true

guard case let .success(optimizedResult) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(_, _, optimizedImageInfo) = optimizedResult else {
case let .image(optimizedImageURL, _, optimizedImageInfo) = optimizedResult else {
XCTFail("Failed processing asset")
return
}

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: optimizedImageURL), "image/png")

// Check optimised image info
XCTAssertEqual(optimizedImageInfo.mimetype, "image/png")
XCTAssertEqual(optimizedImageInfo.blurhash, "K0TSUA~qfQ~qj[fQfQfQfQ")
Expand All @@ -307,6 +314,9 @@ final class MediaUploadingPreprocessorTests: XCTestCase {

compare(originalImageAt: url, toConvertedImageAt: convertedImageURL, withThumbnailAt: thumbnailURL)

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: convertedImageURL), "image/heic")

// Check resulting image info
XCTAssertEqual(imageInfo.mimetype, "image/heic")
XCTAssertEqual(imageInfo.blurhash, "KGD]3ns:T00$kWxFXmt6xv")
Expand All @@ -331,6 +341,9 @@ final class MediaUploadingPreprocessorTests: XCTestCase {

compare(originalImageAt: url, toConvertedImageAt: optimizedImageURL, withThumbnailAt: thumbnailURL)

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: optimizedImageURL), "image/jpeg")

// Check optimised image info
XCTAssertEqual(optimizedImageInfo.mimetype, "image/jpeg")
XCTAssertEqual(optimizedImageInfo.blurhash, "KGD]3ns:T00#kWxFb^s:xv")
Expand All @@ -350,11 +363,14 @@ final class MediaUploadingPreprocessorTests: XCTestCase {
}

guard case let .success(result) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(_, _, imageInfo) = result else {
case let .image(convertedImageURL, _, imageInfo) = result else {
XCTFail("Failed processing asset")
return
}

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: convertedImageURL), "image/gif")

// Check resulting image info
XCTAssertEqual(imageInfo.mimetype, "image/gif")
XCTAssertEqual(imageInfo.blurhash, "K7SY{qs;%NxuRjof~qozIU")
Expand All @@ -372,11 +388,14 @@ final class MediaUploadingPreprocessorTests: XCTestCase {
appSettings.optimizeMediaUploads = true

guard case let .success(optimizedResult) = await mediaUploadingPreprocessor.processMedia(at: url),
case let .image(_, _, optimizedImageInfo) = optimizedResult else {
case let .image(optimizedImageURL, _, optimizedImageInfo) = optimizedResult else {
XCTFail("Failed processing asset")
return
}

// Make sure the output file matches the image info.
XCTAssertEqual(mimeType(from: optimizedImageURL), "image/gif")

// Ensure optimised image is still the same as the original image.
XCTAssertEqual(optimizedImageInfo.mimetype, "image/gif")
XCTAssertEqual(optimizedImageInfo.blurhash, "K7SY{qs;%NxuRjof~qozIU")
Expand Down Expand Up @@ -446,4 +465,15 @@ final class MediaUploadingPreprocessorTests: XCTestCase {

return convertedMetadata
}

private func mimeType(from url: URL) -> String? {
guard let imageSource = CGImageSourceCreateWithURL(url as NSURL, nil),
let typeIdentifier = CGImageSourceGetType(imageSource),
let type = UTType(typeIdentifier as String),
let mimeType = type.preferredMIMEType else {
XCTFail("Failed to get mimetype from URL.")
return nil
}
return mimeType
}
}

0 comments on commit ec509a5

Please sign in to comment.