// // DateDecodingTests.swift // CoreATProtocol // // Created by Claude on 2026-01-02. // import Testing import Foundation @testable import CoreATProtocol @Suite("Date Decoding Tests") struct DateDecodingTests { struct DateContainer: Decodable { let date: Date } @Test("Decodes ISO 8601 with milliseconds and Z timezone") func testMillisecondsWithZ() throws { let json = """ {"date": "2024-01-15T10:30:00.123Z"} """.data(using: .utf8)! let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json) let calendar = Calendar(identifier: .gregorian) let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date) #expect(components.year == 2024) #expect(components.month == 1) #expect(components.day == 15) #expect(components.hour == 10) #expect(components.minute == 30) } @Test("Decodes ISO 8601 with offset timezone") func testWithOffset() throws { let json = """ {"date": "2024-06-20T15:45:30.000+00:00"} """.data(using: .utf8)! let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json) let calendar = Calendar(identifier: .gregorian) let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date) #expect(components.year == 2024) #expect(components.month == 6) #expect(components.day == 20) #expect(components.hour == 15) #expect(components.minute == 45) } @Test("Decodes ISO 8601 without fractional seconds") func testWithoutFractional() throws { let json = """ {"date": "2024-03-10T08:00:00Z"} """.data(using: .utf8)! let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json) let calendar = Calendar(identifier: .gregorian) let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date) #expect(components.year == 2024) #expect(components.month == 3) #expect(components.day == 10) } @Test("Decodes microseconds precision") func testMicroseconds() throws { let json = """ {"date": "2024-12-25T12:00:00.123456+00:00"} """.data(using: .utf8)! let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json) // Just verify it parses without error #expect(container.date != Date.distantPast) } @Test("Multiple date formats in same response") func testMultipleFormats() throws { struct MultipleDates: Decodable { let createdAt: Date let indexedAt: Date let updatedAt: Date } let json = """ { "createdAt": "2024-01-01T00:00:00.000Z", "indexedAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00.000+00:00" } """.data(using: .utf8)! let dates = try JSONDecoder.atDecoder.decode(MultipleDates.self, from: json) // All should parse to the same time (within a small margin) let interval1 = abs(dates.createdAt.timeIntervalSince(dates.indexedAt)) let interval2 = abs(dates.createdAt.timeIntervalSince(dates.updatedAt)) #expect(interval1 < 1, "Dates should be within 1 second of each other") #expect(interval2 < 1, "Dates should be within 1 second of each other") } @Test("Throws on invalid date format") func testInvalidFormat() { let json = """ {"date": "not-a-date"} """.data(using: .utf8)! #expect(throws: DecodingError.self) { _ = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json) } } }