this repo has no description
1//
2// DateDecodingTests.swift
3// CoreATProtocol
4//
5// Created by Claude on 2026-01-02.
6//
7
8import Testing
9import Foundation
10@testable import CoreATProtocol
11
12@Suite("Date Decoding Tests")
13struct DateDecodingTests {
14
15 struct DateContainer: Decodable {
16 let date: Date
17 }
18
19 @Test("Decodes ISO 8601 with milliseconds and Z timezone")
20 func testMillisecondsWithZ() throws {
21 let json = """
22 {"date": "2024-01-15T10:30:00.123Z"}
23 """.data(using: .utf8)!
24
25 let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json)
26
27 let calendar = Calendar(identifier: .gregorian)
28 let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date)
29
30 #expect(components.year == 2024)
31 #expect(components.month == 1)
32 #expect(components.day == 15)
33 #expect(components.hour == 10)
34 #expect(components.minute == 30)
35 }
36
37 @Test("Decodes ISO 8601 with offset timezone")
38 func testWithOffset() throws {
39 let json = """
40 {"date": "2024-06-20T15:45:30.000+00:00"}
41 """.data(using: .utf8)!
42
43 let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json)
44
45 let calendar = Calendar(identifier: .gregorian)
46 let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date)
47
48 #expect(components.year == 2024)
49 #expect(components.month == 6)
50 #expect(components.day == 20)
51 #expect(components.hour == 15)
52 #expect(components.minute == 45)
53 }
54
55 @Test("Decodes ISO 8601 without fractional seconds")
56 func testWithoutFractional() throws {
57 let json = """
58 {"date": "2024-03-10T08:00:00Z"}
59 """.data(using: .utf8)!
60
61 let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json)
62
63 let calendar = Calendar(identifier: .gregorian)
64 let components = calendar.dateComponents(in: TimeZone(identifier: "UTC")!, from: container.date)
65
66 #expect(components.year == 2024)
67 #expect(components.month == 3)
68 #expect(components.day == 10)
69 }
70
71 @Test("Decodes microseconds precision")
72 func testMicroseconds() throws {
73 let json = """
74 {"date": "2024-12-25T12:00:00.123456+00:00"}
75 """.data(using: .utf8)!
76
77 let container = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json)
78
79 // Just verify it parses without error
80 #expect(container.date != Date.distantPast)
81 }
82
83 @Test("Multiple date formats in same response")
84 func testMultipleFormats() throws {
85 struct MultipleDates: Decodable {
86 let createdAt: Date
87 let indexedAt: Date
88 let updatedAt: Date
89 }
90
91 let json = """
92 {
93 "createdAt": "2024-01-01T00:00:00.000Z",
94 "indexedAt": "2024-01-01T00:00:00Z",
95 "updatedAt": "2024-01-01T00:00:00.000+00:00"
96 }
97 """.data(using: .utf8)!
98
99 let dates = try JSONDecoder.atDecoder.decode(MultipleDates.self, from: json)
100
101 // All should parse to the same time (within a small margin)
102 let interval1 = abs(dates.createdAt.timeIntervalSince(dates.indexedAt))
103 let interval2 = abs(dates.createdAt.timeIntervalSince(dates.updatedAt))
104
105 #expect(interval1 < 1, "Dates should be within 1 second of each other")
106 #expect(interval2 < 1, "Dates should be within 1 second of each other")
107 }
108
109 @Test("Throws on invalid date format")
110 func testInvalidFormat() {
111 let json = """
112 {"date": "not-a-date"}
113 """.data(using: .utf8)!
114
115 #expect(throws: DecodingError.self) {
116 _ = try JSONDecoder.atDecoder.decode(DateContainer.self, from: json)
117 }
118 }
119}