//
//  ProfileAvatarCropTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

@MainActor
/// 空间设置头像裁剪输出测试。
final class ProfileAvatarCropTests: XCTestCase {
    func testCropOutputIs480PixelSquare() throws {
        let source = UIGraphicsImageRenderer(size: CGSize(width: 800, height: 600)).image { context in
            UIColor.red.setFill()
            context.fill(CGRect(x: 0, y: 0, width: 800, height: 600))
        }
        let controller = ProfileAvatarCropViewController(image: source) { _ in }
        controller.loadViewIfNeeded()
        controller.view.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
        controller.view.setNeedsLayout()
        controller.view.layoutIfNeeded()

        let data = try XCTUnwrap(controller.renderedCropData())
        let image = try XCTUnwrap(UIImage(data: data))
        XCTAssertEqual(image.size.width * image.scale, 480)
        XCTAssertEqual(image.size.height * image.scale, 480)
    }

    func testCropNormalizesImageOrientation() throws {
        let base = UIGraphicsImageRenderer(size: CGSize(width: 600, height: 800)).image { context in
            UIColor.blue.setFill()
            context.fill(CGRect(x: 0, y: 0, width: 600, height: 800))
        }
        let cgImage = try XCTUnwrap(base.cgImage)
        let rotated = UIImage(cgImage: cgImage, scale: 1, orientation: .left)
        let controller = ProfileAvatarCropViewController(image: rotated) { _ in }
        controller.loadViewIfNeeded()
        controller.view.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
        controller.view.layoutIfNeeded()

        let data = try XCTUnwrap(controller.renderedCropData())
        let output = try XCTUnwrap(UIImage(data: data))
        XCTAssertEqual(output.imageOrientation, .up)
        XCTAssertEqual(output.size.width * output.scale, 480)
        XCTAssertEqual(output.size.height * output.scale, 480)
    }
}
