mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 02:25:20 +00:00
Expand coverage for transport, chat, and media flows (#1056)
* Expand coverage for transport, chat, and media flows * Stabilize transport and media coverage tests --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#else
|
||||
import AppKit
|
||||
#endif
|
||||
@testable import bitchat
|
||||
|
||||
private func makeTemporaryFileURL(_ name: String) -> URL {
|
||||
FileManager.default.temporaryDirectory.appendingPathComponent(name)
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
private func makePlatformImage(size: CGSize) -> UIImage {
|
||||
UIGraphicsImageRenderer(size: size).image { context in
|
||||
UIColor.systemTeal.setFill()
|
||||
context.fill(CGRect(origin: .zero, size: size))
|
||||
}
|
||||
}
|
||||
#else
|
||||
private func makePlatformImage(size: CGSize) -> NSImage {
|
||||
let image = NSImage(size: size)
|
||||
image.lockFocus()
|
||||
NSColor.systemTeal.setFill()
|
||||
NSBezierPath(rect: CGRect(origin: .zero, size: size)).fill()
|
||||
image.unlockFocus()
|
||||
return image
|
||||
}
|
||||
#endif
|
||||
|
||||
struct ImageUtilsTests {
|
||||
@Test
|
||||
func processImage_rejectsOversizedSourceFile() throws {
|
||||
let url = makeTemporaryFileURL("image-too-large.bin")
|
||||
try Data(repeating: 0xFF, count: 10 * 1024 * 1024 + 1).write(to: url, options: .atomic)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
#expect(throws: ImageUtilsError.self) {
|
||||
try ImageUtils.processImage(at: url)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func processImage_rejectsInvalidImageData() throws {
|
||||
let url = makeTemporaryFileURL("image-invalid.bin")
|
||||
try Data("not-an-image".utf8).write(to: url, options: .atomic)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
#expect(throws: ImageUtilsError.self) {
|
||||
try ImageUtils.processImage(at: url)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func processImage_writesCompressedJpeg() throws {
|
||||
let image = makePlatformImage(size: CGSize(width: 1024, height: 768))
|
||||
let outputURL = try ImageUtils.processImage(image, maxDimension: 256)
|
||||
defer { try? FileManager.default.removeItem(at: outputURL) }
|
||||
|
||||
let data = try Data(contentsOf: outputURL)
|
||||
|
||||
#expect(outputURL.pathExtension.lowercased() == "jpg")
|
||||
#expect(data.starts(with: Data([0xFF, 0xD8])))
|
||||
#expect(data.count > 0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user