|
| 1 | +@testable import AmoreLicensing |
| 2 | +import Foundation |
| 3 | +import Testing |
| 4 | + |
| 5 | +@Suite("SubscriptionState Codable (SDK contract)") |
| 6 | +struct SubscriptionStateCodableTests { |
| 7 | + |
| 8 | + // Mirror JWTKit's coders: subscription_state only ever travels inside a JWT, |
| 9 | + // whose encoder/decoder use .secondsSince1970 for every Date. |
| 10 | + private let encoder: JSONEncoder = { |
| 11 | + let encoder = JSONEncoder() |
| 12 | + encoder.dateEncodingStrategy = .secondsSince1970 |
| 13 | + return encoder |
| 14 | + }() |
| 15 | + private let decoder: JSONDecoder = { |
| 16 | + let decoder = JSONDecoder() |
| 17 | + decoder.dateDecodingStrategy = .secondsSince1970 |
| 18 | + return decoder |
| 19 | + }() |
| 20 | + |
| 21 | + private func roundTrip(_ value: SubscriptionState) throws -> SubscriptionState { |
| 22 | + let data = try encoder.encode(value) |
| 23 | + return try decoder.decode(SubscriptionState.self, from: data) |
| 24 | + } |
| 25 | + |
| 26 | + private func jsonObject(_ value: SubscriptionState) throws -> [String: Any] { |
| 27 | + let data = try encoder.encode(value) |
| 28 | + return try JSONSerialization.jsonObject(with: data) as! [String: Any] |
| 29 | + } |
| 30 | + |
| 31 | + @Test func renewingRoundTrips() throws { |
| 32 | + let date = Date(timeIntervalSince1970: 1_780_000_000) |
| 33 | + let value: SubscriptionState = .renewing(renewsAt: date) |
| 34 | + let json = try jsonObject(value) |
| 35 | + #expect(json["state"] as? String == "renewing") |
| 36 | + #expect(json["renews_at"] as? Int == 1_780_000_000) |
| 37 | + #expect(try roundTrip(value) == value) |
| 38 | + } |
| 39 | + |
| 40 | + @Test func cancelingRoundTrips() throws { |
| 41 | + let ends = Date(timeIntervalSince1970: 1_780_000_000) |
| 42 | + let canceled = Date(timeIntervalSince1970: 1_779_000_000) |
| 43 | + let value: SubscriptionState = .canceling(endsAt: ends, canceledAt: canceled) |
| 44 | + let json = try jsonObject(value) |
| 45 | + #expect(json["state"] as? String == "canceling") |
| 46 | + #expect(json["ends_at"] as? Int == 1_780_000_000) |
| 47 | + #expect(json["canceled_at"] as? Int == 1_779_000_000) |
| 48 | + #expect(try roundTrip(value) == value) |
| 49 | + } |
| 50 | + |
| 51 | + @Test func trialingWithoutCancellationOmitsCanceledAt() throws { |
| 52 | + let trialEnd = Date(timeIntervalSince1970: 1_780_000_000) |
| 53 | + let value: SubscriptionState = .trialing(trialEndsAt: trialEnd, canceledAt: nil) |
| 54 | + let json = try jsonObject(value) |
| 55 | + #expect(json["state"] as? String == "trialing") |
| 56 | + #expect(json["trial_ends_at"] as? Int == 1_780_000_000) |
| 57 | + #expect(json["canceled_at"] == nil, "canceled_at must be omitted when nil") |
| 58 | + #expect(try roundTrip(value) == value) |
| 59 | + } |
| 60 | + |
| 61 | + @Test func trialingWithCancellationEncodesCanceledAt() throws { |
| 62 | + let trialEnd = Date(timeIntervalSince1970: 1_780_000_000) |
| 63 | + let canceled = Date(timeIntervalSince1970: 1_779_000_000) |
| 64 | + let value: SubscriptionState = .trialing(trialEndsAt: trialEnd, canceledAt: canceled) |
| 65 | + let json = try jsonObject(value) |
| 66 | + #expect(json["canceled_at"] as? Int == 1_779_000_000) |
| 67 | + #expect(try roundTrip(value) == value) |
| 68 | + } |
| 69 | + |
| 70 | + @Test func pastDueRoundTrips() throws { |
| 71 | + let grace = Date(timeIntervalSince1970: 1_780_000_000) |
| 72 | + let value: SubscriptionState = .pastDue(gracePeriodEndsAt: grace) |
| 73 | + let json = try jsonObject(value) |
| 74 | + #expect(json["state"] as? String == "past_due") |
| 75 | + #expect(json["grace_period_ends_at"] as? Int == 1_780_000_000) |
| 76 | + #expect(try roundTrip(value) == value) |
| 77 | + } |
| 78 | + |
| 79 | + @Test func pausedRoundTripsWithNoExtraKeys() throws { |
| 80 | + let value: SubscriptionState = .paused |
| 81 | + let json = try jsonObject(value) |
| 82 | + #expect(json["state"] as? String == "paused") |
| 83 | + #expect(json.count == 1) |
| 84 | + #expect(try roundTrip(value) == value) |
| 85 | + } |
| 86 | + |
| 87 | + @Test func lapsedRoundTripsWithNoExtraKeys() throws { |
| 88 | + let value: SubscriptionState = .lapsed |
| 89 | + let json = try jsonObject(value) |
| 90 | + #expect(json["state"] as? String == "lapsed") |
| 91 | + #expect(json.count == 1) |
| 92 | + #expect(try roundTrip(value) == value) |
| 93 | + } |
| 94 | + |
| 95 | + @Test func unknownStateRejected() { |
| 96 | + let json = Data(#"{"state":"flapping"}"#.utf8) |
| 97 | + #expect(throws: DecodingError.self) { |
| 98 | + _ = try self.decoder.decode(SubscriptionState.self, from: json) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test func decodesServerEncodedRenewingFixture() throws { |
| 103 | + // Byte-for-byte fixture that the server emits; must decode here. |
| 104 | + let json = Data(#"{"state":"renewing","renews_at":1780272000}"#.utf8) |
| 105 | + let decoded = try decoder.decode(SubscriptionState.self, from: json) |
| 106 | + guard case .renewing(let date) = decoded else { |
| 107 | + Issue.record("Expected .renewing, got \(decoded)") |
| 108 | + return |
| 109 | + } |
| 110 | + #expect(date == Date(timeIntervalSince1970: 1_780_272_000)) |
| 111 | + } |
| 112 | + |
| 113 | + @Test func decodesServerEncodedTrialingNullCanceledAtFixture() throws { |
| 114 | + let json = Data(#"{"state":"trialing","trial_ends_at":1780272000,"canceled_at":null}"#.utf8) |
| 115 | + let decoded = try decoder.decode(SubscriptionState.self, from: json) |
| 116 | + guard case .trialing(_, let canceledAt) = decoded else { |
| 117 | + Issue.record("Expected .trialing, got \(decoded)") |
| 118 | + return |
| 119 | + } |
| 120 | + #expect(canceledAt == nil) |
| 121 | + } |
| 122 | +} |
0 commit comments