Ariver
2026-06-28 9ba7cd3b47fb91eaaa8d0c3df8b7286a89be1756
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import Foundation
 
public enum ProFeature: String, CaseIterable, Codable, Equatable, Sendable {
    case windowShortcutActivation
}
 
public enum TrialInvalidReason: String, Codable, Equatable, Sendable {
    case corruptedRecord
    case expiresBeforeStart
    case startedInFuture
    case timeRollback
}
 
public enum TrialState: Codable, Equatable, Sendable {
    case notInitialized
    case active(startedAt: Date, expiresAt: Date)
    case expired(startedAt: Date, expiredAt: Date)
    case invalidLocalRecord(reason: TrialInvalidReason)
}
 
public enum LicenseActivationStatus: String, Codable, Equatable, Sendable {
    case active
    case disabled
    case expired
}
 
public enum LicenseRemoteFailureReason: String, Codable, Equatable, Sendable {
    case invalid
    case revoked
    case disabled
    case expired
    case unknown
}
 
public enum LicenseMismatchReason: String, Codable, Equatable, Sendable {
    case store
    case product
    case variant
}
 
public enum LicenseState: Codable, Equatable, Sendable {
    case none
    case activated(fingerprint: String, instanceID: String, status: LicenseActivationStatus)
    case deactivatedLocal
    case revokedOrExpiredRemote(reason: LicenseRemoteFailureReason)
    case mismatch(reason: LicenseMismatchReason)
}
 
public enum ValidationHardFailureReason: String, Codable, Equatable, Sendable {
    case invalid
    case revoked
    case disabled
    case expired
    case productMismatch
    case storeMismatch
    case variantMismatch
    case serverRejected
    case unknown
}
 
public enum ValidationFreshness: Codable, Equatable, Sendable {
    case neverChecked
    case valid(lastValidAt: Date, nextCheckAt: Date)
    case temporaryFailure(lastAttemptAt: Date, graceUntil: Date)
    case hardFailure(reason: ValidationHardFailureReason)
}
 
public enum EntitlementAllowedReason: String, Codable, Equatable, Sendable {
    case trialActive
    case licenseValid
    case offlineGrace
}
 
public enum EntitlementDeniedReason: String, Codable, Equatable, Sendable {
    case trialNotInitialized
    case trialExpired
    case noLicense
    case licenseDeactivated
    case licenseRevokedOrExpired
    case licenseMismatch
    case licenseDisabled
    case licenseExpired
    case verificationRequired
    case graceExpired
    case hardFailure
    case invalidLocalRecord
    case unsupportedFeature
}
 
public enum EffectiveEntitlement: Codable, Equatable, Sendable {
    case proAllowed(reason: EntitlementAllowedReason)
    case proDenied(reason: EntitlementDeniedReason)
 
    public var isProAllowed: Bool {
        switch self {
        case .proAllowed:
            return true
        case .proDenied:
            return false
        }
    }
}
 
public struct EntitlementSnapshot: Codable, Equatable, Sendable {
    public var trialState: TrialState
    public var licenseState: LicenseState
    public var validationFreshness: ValidationFreshness
 
    public init(
        trialState: TrialState,
        licenseState: LicenseState,
        validationFreshness: ValidationFreshness
    ) {
        self.trialState = trialState
        self.licenseState = licenseState
        self.validationFreshness = validationFreshness
    }
}
 
public protocol ClockProtocol: Sendable {
    var now: Date { get }
}
 
public struct SystemClock: ClockProtocol {
    public init() {}
 
    public var now: Date {
        Date()
    }
}
 
public protocol LicenseStoreProtocol: Sendable {
    func loadEntitlementSnapshot() throws -> EntitlementSnapshot
    func saveEntitlementSnapshot(_ snapshot: EntitlementSnapshot) throws
}
 
public enum LicenseValidationResult: Equatable, Sendable {
    case success(lastValidAt: Date, nextCheckAt: Date)
    case temporaryFailure(lastAttemptAt: Date, graceUntil: Date)
    case hardFailure(reason: ValidationHardFailureReason)
}
 
public protocol LicenseValidationClientProtocol: Sendable {
    func validateLicense(fingerprint: String, instanceID: String) async throws -> LicenseValidationResult
}
 
public protocol EntitlementProviderProtocol: Sendable {
    func entitlement(for feature: ProFeature, at now: Date) -> EffectiveEntitlement
}
 
public enum EntitlementPolicy {
    public static let defaultTrialDuration: TimeInterval = 30 * 24 * 60 * 60
    public static let defaultOfflineGraceDuration: TimeInterval = 14 * 24 * 60 * 60
 
    public static func trialStarted(at startedAt: Date, duration: TimeInterval = defaultTrialDuration) -> TrialState {
        .active(startedAt: startedAt, expiresAt: startedAt.addingTimeInterval(duration))
    }
 
    public static func offlineGrace(
        lastAttemptAt: Date,
        duration: TimeInterval = defaultOfflineGraceDuration
    ) -> ValidationFreshness {
        .temporaryFailure(lastAttemptAt: lastAttemptAt, graceUntil: lastAttemptAt.addingTimeInterval(duration))
    }
 
    public static func evaluate(
        snapshot: EntitlementSnapshot,
        feature: ProFeature,
        now: Date
    ) -> EffectiveEntitlement {
        switch feature {
        case .windowShortcutActivation:
            break
        }
 
        let licenseDecision = decisionForLicense(
            state: snapshot.licenseState,
            freshness: snapshot.validationFreshness,
            now: now
        )
        if case .proAllowed = licenseDecision {
            return licenseDecision
        }
 
        let trialDecision = decisionForTrial(state: snapshot.trialState, now: now)
        if case .proAllowed = trialDecision {
            return trialDecision
        }
 
        if case .none = snapshot.licenseState {
            if case .notInitialized = snapshot.trialState {
                return .proDenied(reason: .noLicense)
            }
            return trialDecision
        }
 
        return licenseDecision
    }
 
    private static func decisionForTrial(state: TrialState, now: Date) -> EffectiveEntitlement {
        switch state {
        case .notInitialized:
            return .proDenied(reason: .trialNotInitialized)
        case let .active(startedAt, expiresAt):
            guard expiresAt > startedAt else {
                return .proDenied(reason: .invalidLocalRecord)
            }
            guard now >= startedAt else {
                return .proDenied(reason: .invalidLocalRecord)
            }
            guard now < expiresAt else {
                return .proDenied(reason: .trialExpired)
            }
            return .proAllowed(reason: .trialActive)
        case .expired:
            return .proDenied(reason: .trialExpired)
        case .invalidLocalRecord:
            return .proDenied(reason: .invalidLocalRecord)
        }
    }
 
    private static func decisionForLicense(
        state: LicenseState,
        freshness: ValidationFreshness,
        now: Date
    ) -> EffectiveEntitlement {
        switch state {
        case .none:
            return .proDenied(reason: .noLicense)
        case .deactivatedLocal:
            return .proDenied(reason: .licenseDeactivated)
        case .revokedOrExpiredRemote:
            return .proDenied(reason: .licenseRevokedOrExpired)
        case .mismatch:
            return .proDenied(reason: .licenseMismatch)
        case let .activated(_, _, status):
            switch status {
            case .active:
                return decisionForValidationFreshness(freshness, now: now)
            case .disabled:
                return .proDenied(reason: .licenseDisabled)
            case .expired:
                return .proDenied(reason: .licenseExpired)
            }
        }
    }
 
    private static func decisionForValidationFreshness(
        _ freshness: ValidationFreshness,
        now: Date
    ) -> EffectiveEntitlement {
        switch freshness {
        case .neverChecked:
            return .proDenied(reason: .verificationRequired)
        case let .valid(_, nextCheckAt):
            return now <= nextCheckAt
                ? .proAllowed(reason: .licenseValid)
                : .proDenied(reason: .verificationRequired)
        case let .temporaryFailure(_, graceUntil):
            return now < graceUntil
                ? .proAllowed(reason: .offlineGrace)
                : .proDenied(reason: .graceExpired)
        case .hardFailure:
            return .proDenied(reason: .hardFailure)
        }
    }
}
 
public enum EntitlementFixtureState: String, CaseIterable, Codable, Equatable, Sendable {
    case trialActive
    case trialExpired
    case proUnlocked
    case offlineGrace
    case verifyFailed
}
 
public struct FixtureEntitlementProvider: EntitlementProviderProtocol {
    public let snapshot: EntitlementSnapshot
 
    public init(fixtureState: EntitlementFixtureState, now: Date) {
        let startedAt = now.addingTimeInterval(-24 * 60 * 60)
        let expiredAt = now.addingTimeInterval(-24 * 60 * 60)
        let expiredTrial = TrialState.expired(
            startedAt: now.addingTimeInterval(-31 * 24 * 60 * 60),
            expiredAt: expiredAt
        )
        let activeLicense = LicenseState.activated(
            fingerprint: "fixture-fingerprint",
            instanceID: "fixture-instance",
            status: .active
        )
 
        switch fixtureState {
        case .trialActive:
            snapshot = EntitlementSnapshot(
                trialState: .active(
                    startedAt: startedAt,
                    expiresAt: now.addingTimeInterval(29 * 24 * 60 * 60)
                ),
                licenseState: .none,
                validationFreshness: .neverChecked
            )
        case .trialExpired:
            snapshot = EntitlementSnapshot(
                trialState: expiredTrial,
                licenseState: .none,
                validationFreshness: .neverChecked
            )
        case .proUnlocked:
            snapshot = EntitlementSnapshot(
                trialState: expiredTrial,
                licenseState: activeLicense,
                validationFreshness: .valid(
                    lastValidAt: now,
                    nextCheckAt: now.addingTimeInterval(24 * 60 * 60)
                )
            )
        case .offlineGrace:
            snapshot = EntitlementSnapshot(
                trialState: expiredTrial,
                licenseState: activeLicense,
                validationFreshness: EntitlementPolicy.offlineGrace(lastAttemptAt: now)
            )
        case .verifyFailed:
            snapshot = EntitlementSnapshot(
                trialState: expiredTrial,
                licenseState: activeLicense,
                validationFreshness: .hardFailure(reason: .serverRejected)
            )
        }
    }
 
    public func entitlement(for feature: ProFeature, at now: Date) -> EffectiveEntitlement {
        EntitlementPolicy.evaluate(snapshot: snapshot, feature: feature, now: now)
    }
}