//
//  SonyPTPCommandsTests.swift
//  suixinkanTests
//
//  Created by Codex on 2026/6/29.
//

import XCTest
@testable import suixinkan

/// Sony PTP 命令构建与解析测试。
final class SonyPTPCommandsTests: XCTestCase {
    /// 测试 OpenSession 命令包长度与 opcode。
    func testBuildOpenSessionCommand() {
        let data = SonyPTPHelper.buildCommand(
            code: SonyPTPCommand.openSession,
            transactionID: 1,
            parameters: [1]
        )

        XCTAssertEqual(data.count, 16)
        XCTAssertEqual(data[6], 0x02)
        XCTAssertEqual(data[7], 0x10)
    }

    /// 测试 SDIO Connect 命令 opcode。
    func testBuildSonyConnectCommand() {
        let data = SonyPTPHelper.buildCommand(
            code: SonyPTPCommand.sdioConnect,
            transactionID: 42,
            parameters: [1, 0, 0]
        )

        XCTAssertEqual(data.count, 24)
        XCTAssertEqual(data[6], 0x01)
        XCTAssertEqual(data[7], 0x92)
    }

    /// 测试 GetObject 压缩大小解析。
    func testParseObjectCompressedSize() {
        var data = Data(repeating: 0, count: 52)
        data[8] = 0x00
        data[9] = 0x10
        data[10] = 0x00
        data[11] = 0x00

        XCTAssertEqual(SonyPTPHelper.parseObjectCompressedSize(data), 0x1000)
    }

    /// 测试 SDIE ObjectAdded 事件解析。
    func testParseObjectAddedEvent() throws {
        var event = Data()
        event.append(contentsOf: [0x10, 0x00, 0x00, 0x00])
        event.append(contentsOf: [0x04, 0x00])
        event.append(contentsOf: [0x01, 0xC2])
        event.append(contentsOf: [0xFF, 0xFF, 0xFF, 0xFF])
        event.append(contentsOf: [0x01, 0xC0, 0xFF, 0xFF])

        let container = try SonyPTPHelper.parseContainer(event)
        XCTAssertEqual(container.code, SonyPTPCommand.sdieObjectAdded)
        XCTAssertEqual(container.params.first, SonyPTPCommand.shotObjectHandle)
    }
}
