
import XCTest
@testable import NewtonTranslators

class NewtonPackageTests: XCTestCase {

    func parseDate(_ dateString: String) -> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.timeZone = TimeZone(abbreviation: "UTC")
        return formatter.date(from: dateString)
    }

    func testParsePackage1() throws {

        let data = Data(bytes: [
            0x70, 0x61, 0x63, 0x6B, 0x61, 0x67, 0x65, 0x31,
            0x78, 0x78, 0x78, 0x78, 0x10, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x58,
            0x00, 0x58, 0x00, 0x1A, 0x00, 0x02, 0xA9, 0x50,
            0xB1, 0x2E, 0xA0, 0xBF, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x34,
            0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x02, 0xA8, 0x1C, 0x00, 0x02, 0xA8, 0x1C,
            0x66, 0x6F, 0x72, 0x6D, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x81, 0x00, 0xB8, 0x00, 0x25,
            0x00, 0x00, 0x00, 0x00, 0x00, 0xA9, 0x00, 0x31,
            0x00, 0x39, 0x00, 0x39, 0x00, 0x36, 0x00, 0x2D,
            0x00, 0x39, 0x00, 0x37, 0x00, 0x20, 0x00, 0x53,
            0x00, 0x63, 0x00, 0x72, 0x00, 0x61, 0x00, 0x77,
            0x00, 0x6C, 0x00, 0x2C, 0x00, 0x20, 0x00, 0x4C,
            0x00, 0x4C, 0x00, 0x43, 0x00, 0x2E, 0x00, 0x20,
            0x00, 0x20, 0x00, 0x41, 0x00, 0x6C, 0x00, 0x6C,
            0x00, 0x20, 0x00, 0x72, 0x00, 0x69, 0x00, 0x67,
            0x00, 0x68, 0x00, 0x74, 0x00, 0x73, 0x00, 0x20,
            0x00, 0x72, 0x00, 0x65, 0x00, 0x73, 0x00, 0x65,
            0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x64,
            0x00, 0x2E, 0x00, 0x00, 0x00, 0x50, 0x00, 0x54,
            0x00, 0x31, 0x00, 0x30, 0x00, 0x30, 0x00, 0x3A,
            0x00, 0x53, 0x00, 0x63, 0x00, 0x72, 0x00, 0x61,
            0x00, 0x77, 0x00, 0x6C, 0x00, 0x00, 0x4E, 0x65,
            0x77, 0x74, 0x6F, 0x6E, 0x20, 0x54, 0x6F, 0x6F,
            0x6C, 0x6B, 0x69, 0x74, 0x20, 0x31, 0x2E, 0x36,
            0x2E, 0x34, 0x3B, 0x20, 0x70, 0x6C, 0x61, 0x74,
            0x66, 0x6F, 0x72, 0x6D, 0x20, 0x66, 0x69, 0x6C,
            0x65, 0x20, 0x4E, 0x65, 0x77, 0x74, 0x6F, 0x6E,
            0x20, 0x32, 0x2E, 0x31, 0x20, 0x76, 0x35, 0x4E,
            0x65, 0x77, 0x74, 0x6F, 0x6E, 0x20, 0x54, 0x6F,
            0x6F, 0x6C, 0x6B, 0x69, 0x74, 0x20, 0x31, 0x2E,
            0x36, 0x2E, 0x34, 0x00, 0x3A, 0x4F, 0x62, 0x6A,
            0x65, 0x63, 0x74, 0x73, 0x4E, 0x6F, 0x44, 0x65,
            0x62, 0x75, 0x67, 0x3A, 0x43, 0x4C, 0x65, 0x78,
            0x65, 0x72, 0x2E, 0x73, 0x79, 0x6D, 0x40, 0x30,
            0x78, 0x30, 0x30, 0x30, 0x30, 0x31, 0x41, 0x41,
            0x38, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x10, 0x41,
            0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
            0x00, 0x00, 0x01, 0x45, 0x00, 0x00, 0x24, 0x43
        ])

        let packageInfo = try NewtonPackageInfo(data: data)

        XCTAssertEqual(packageInfo.version, 10)
        XCTAssertEqual(packageInfo.size, 174416)
        XCTAssertEqual(packageInfo.creationDate, parseDate("1998-03-13 08:37:19")!)
        XCTAssertEqual(packageInfo.copyright, "©1996-97 Scrawl, LLC.  All rights reserved.")
        XCTAssertEqual(packageInfo.name, "PT100:Scrawl")
    }

    func testParsePackage2() throws {

        let data = Data(bytes: [
            0x70, 0x61, 0x63, 0x6B, 0x61, 0x67, 0x65, 0x30,
            0x78, 0x78, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x54,
            0x00, 0x54, 0x00, 0x26, 0x00, 0x02, 0x14, 0x94,
            0xAC, 0x05, 0xD7, 0x44, 0xAC, 0x05, 0xD7, 0x44,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x24,
            0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x02, 0x13, 0x70, 0x00, 0x02, 0x13, 0x70,
            0x66, 0x6F, 0x72, 0x6D, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x81, 0x00, 0x7A, 0x00, 0x1C,
            0x00, 0x00, 0x00, 0x00, 0xFF, 0xA9, 0x00, 0x31,
            0x00, 0x39, 0x00, 0x39, 0x00, 0x33, 0x00, 0x2D,
            0x00, 0x31, 0x00, 0x39, 0x00, 0x39, 0x00, 0x35,
            0x00, 0x20, 0x00, 0x52, 0x00, 0x6F, 0x00, 0x6E,
            0x00, 0x20, 0x00, 0x46, 0x00, 0x6C, 0x00, 0x61,
            0x00, 0x78, 0x00, 0x2E, 0x00, 0x20, 0x00, 0x41,
            0x00, 0x6C, 0x00, 0x6C, 0x00, 0x20, 0x00, 0x72,
            0x00, 0x69, 0x00, 0x67, 0x00, 0x68, 0x00, 0x74,
            0x00, 0x73, 0x00, 0x20, 0x00, 0x72, 0x00, 0x65,
            0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x76,
            0x00, 0x65, 0x00, 0x64, 0x00, 0x2E, 0x00, 0x00,
            0x00, 0x50, 0x00, 0x61, 0x00, 0x67, 0x00, 0x65,
            0x00, 0x58, 0x00, 0x70, 0x00, 0x72, 0x00, 0x65,
            0x00, 0x73, 0x00, 0x73, 0x00, 0x3A, 0x00, 0x52,
            0x00, 0x4F, 0x00, 0x4E, 0x00, 0x46, 0x00, 0x4C,
            0x00, 0x41, 0x00, 0x58, 0x00, 0x00, 0x41, 0x20,
            0x4E, 0x65, 0x77, 0x74, 0x6F, 0x6E, 0x20, 0x54,
            0x6F, 0x6F, 0x6C, 0x6B, 0x69, 0x74, 0x20, 0x61,
            0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69,
            0x6F, 0x6E, 0x4E, 0x65, 0x77, 0x74, 0x6F, 0x6E,
            0xAA, 0x20, 0x54, 0x6F, 0x6F, 0x6C, 0x4B, 0x69,
            0x74, 0x20, 0x50, 0x61, 0x63, 0x6B, 0x61, 0x67,
            0x65, 0x20, 0xA9, 0x20, 0x31, 0x39, 0x39, 0x32,
            0x2D, 0x31, 0x39, 0x39, 0x34, 0x2C, 0x20, 0x41,
            0x70, 0x70, 0x6C, 0x65, 0x20, 0x43, 0x6F, 0x6D,
            0x70, 0x75, 0x74, 0x65, 0x72, 0x2C, 0x20, 0x49,
            0x6E, 0x63, 0x2E, 0x00, 0x00, 0x00, 0x10, 0x41,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
            0x00, 0x00, 0x01, 0x35, 0x00, 0x00, 0x28, 0x43,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5D,
            0x00, 0x00, 0x02, 0x4D, 0x00, 0x00, 0x02, 0x75,
            0x00, 0x00, 0x02, 0xB5, 0x00, 0x00, 0x04, 0xD5,
            0x00, 0x02, 0x10, 0x15, 0x00, 0x02, 0x10, 0xC5,
            0x00, 0x02, 0x13, 0xED, 0x00, 0x00, 0x2C, 0x41,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        ])

        let packageInfo = try NewtonPackageInfo(data: data)

        XCTAssertEqual(packageInfo.version, 3)
        XCTAssertEqual(packageInfo.size, 136340)
        XCTAssertEqual(packageInfo.creationDate, parseDate("1995-06-15 12:25:40")!)
        XCTAssertEqual(packageInfo.copyright, "ﾩ1993-1995 Ron Flax. All rights reserved.")
        XCTAssertEqual(packageInfo.name, "PageXpress:RONFLAX")
    }

    func testInvalidSignature() {
        let data = Data(bytes: [0x70, 0x61, 0xFF, 0xFF, 0x61, 0x67, 0x65])
        XCTAssertThrowsError(try NewtonPackageInfo(data: data))
    }

    func testTooShort() {
        let data = Data(bytes: [0x70])
        XCTAssertThrowsError(try NewtonPackageInfo(data: data))
    }

    static var allTests : [(String, (NewtonPackageTests) -> () throws -> Void)] {
        return [
            ("testParsePackage1", testParsePackage1),
            ("testParsePackage2", testParsePackage2),
            ("testInvalidSignature", testInvalidSignature),
            ("testTooShort", testTooShort)
        ]
    }
}
