|
| 1 | +// |
| 2 | +// VehicleMQTTLocationResponseTests.swift |
| 3 | +// KiaTests |
| 4 | +// |
| 5 | +// Created by Codex on 03/03/26. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import KiaMaps |
| 10 | + |
| 11 | +final class VehicleMQTTLocationResponseTests: XCTestCase { |
| 12 | + |
| 13 | + func testDecode_ValidMergedTimestampWithNullLocation() throws { |
| 14 | + let data = Data( |
| 15 | + """ |
| 16 | + { |
| 17 | + "latestUpdateTime": "20250901142733", |
| 18 | + "state": { |
| 19 | + "Vehicle": { |
| 20 | + "Location": null |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + """.utf8 |
| 25 | + ) |
| 26 | + |
| 27 | + let response = try JSONDecoder().decode(VehicleMQTTLocationResponse.self, from: data) |
| 28 | + XCTAssertNil(response.state.vehicle.location) |
| 29 | + |
| 30 | + guard let utcTimeZone = TimeZone(secondsFromGMT: 0) else { |
| 31 | + XCTFail("Failed to create UTC timezone") |
| 32 | + return |
| 33 | + } |
| 34 | + var utc = Calendar(identifier: .gregorian) |
| 35 | + utc.timeZone = utcTimeZone |
| 36 | + let components = utc.dateComponents( |
| 37 | + [.year, .month, .day, .hour, .minute, .second], |
| 38 | + from: response.lastUpdateTime |
| 39 | + ) |
| 40 | + |
| 41 | + XCTAssertEqual(components.year, 2025) |
| 42 | + XCTAssertEqual(components.month, 9) |
| 43 | + XCTAssertEqual(components.day, 1) |
| 44 | + XCTAssertEqual(components.hour, 14) |
| 45 | + XCTAssertEqual(components.minute, 27) |
| 46 | + XCTAssertEqual(components.second, 33) |
| 47 | + } |
| 48 | + |
| 49 | + func testDecode_InvalidMergedTimestamp_Throws() { |
| 50 | + let data = Data( |
| 51 | + """ |
| 52 | + { |
| 53 | + "latestUpdateTime": "2025-09-01T14:27:33Z", |
| 54 | + "state": { |
| 55 | + "Vehicle": { |
| 56 | + "Location": null |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + """.utf8 |
| 61 | + ) |
| 62 | + |
| 63 | + XCTAssertThrowsError(try JSONDecoder().decode(VehicleMQTTLocationResponse.self, from: data)) { error in |
| 64 | + guard let parsingError = error as? DateValue<MergedDateFormatter>.ParsingError else { |
| 65 | + XCTFail("Unexpected error type: \(type(of: error))") |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + switch parsingError { |
| 70 | + case .invalidString(let invalidValue, _): |
| 71 | + XCTAssertEqual(invalidValue, "2025-09-01T14:27:33Z") |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + func testMergedDateFormatter_RoundTrip() { |
| 77 | + let formatter = MergedDateFormatter() |
| 78 | + let source = "20241231235958" |
| 79 | + |
| 80 | + let date = try XCTUnwrap(formatter.date(from: source)) |
| 81 | + XCTAssertEqual(formatter.string(from: date), source) |
| 82 | + } |
| 83 | +} |
0 commit comments