Ariver
2026-06-29 1d03f5b6b433b062a098bf7899a349d550aafac7
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
import Foundation
import XCTest
@testable import AlignerCore
 
final class EntitlementPolicyTests: XCTestCase {
    private let now = Date(timeIntervalSince1970: 1_800_000_000)
 
    func testTrialIsAllowedBeforeThirtyDayBoundary() {
        let startedAt = now.addingTimeInterval(-29 * 24 * 60 * 60 - 23 * 60 * 60 - 59 * 60)
        let snapshot = EntitlementSnapshot(
            trialState: EntitlementPolicy.trialStarted(at: startedAt),
            licenseState: .none,
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proAllowed(reason: .trialActive)
        )
    }
 
    func testTrialExpiresAtThirtyDayBoundary() {
        let startedAt = now.addingTimeInterval(-EntitlementPolicy.defaultTrialDuration)
        let snapshot = EntitlementSnapshot(
            trialState: EntitlementPolicy.trialStarted(at: startedAt),
            licenseState: .none,
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .trialExpired)
        )
    }
 
    func testInvalidTrialRecordIsDeniedWhenNoLicenseExists() {
        let snapshot = EntitlementSnapshot(
            trialState: .active(startedAt: now, expiresAt: now.addingTimeInterval(-1)),
            licenseState: .none,
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .invalidLocalRecord)
        )
    }
 
    func testValidLicenseAllowsProAfterTrialExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: activeLicense(),
            validationFreshness: .valid(
                lastValidAt: now.addingTimeInterval(-60),
                nextCheckAt: now.addingTimeInterval(60)
            )
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proAllowed(reason: .licenseValid)
        )
    }
 
    func testOfflineGraceAllowsProBeforeFourteenDayBoundary() {
        let lastAttemptAt = now.addingTimeInterval(-13 * 24 * 60 * 60 - 23 * 60 * 60 - 59 * 60)
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: activeLicense(),
            validationFreshness: EntitlementPolicy.offlineGrace(lastAttemptAt: lastAttemptAt)
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proAllowed(reason: .offlineGrace)
        )
    }
 
    func testOfflineGraceExpiresAtFourteenDayBoundary() {
        let lastAttemptAt = now.addingTimeInterval(-EntitlementPolicy.defaultOfflineGraceDuration)
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: activeLicense(),
            validationFreshness: EntitlementPolicy.offlineGrace(lastAttemptAt: lastAttemptAt)
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .graceExpired)
        )
    }
 
    func testHardFailureIsDeniedWhenTrialIsExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: activeLicense(),
            validationFreshness: .hardFailure(reason: .revoked)
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .hardFailure)
        )
    }
 
    func testActiveTrialStillAllowsWhenLicenseVerificationFails() {
        let snapshot = EntitlementSnapshot(
            trialState: EntitlementPolicy.trialStarted(at: now.addingTimeInterval(-60)),
            licenseState: activeLicense(),
            validationFreshness: .hardFailure(reason: .serverRejected)
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proAllowed(reason: .trialActive)
        )
    }
 
    func testDeactivatedLicenseIsDeniedAfterTrialExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: .deactivatedLocal,
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .licenseDeactivated)
        )
    }
 
    func testDisabledLicenseIsDeniedAfterTrialExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: .activated(fingerprint: "abc123", instanceID: "instance-1", status: .disabled),
            validationFreshness: .valid(
                lastValidAt: now.addingTimeInterval(-60),
                nextCheckAt: now.addingTimeInterval(60)
            )
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .licenseDisabled)
        )
    }
 
    func testProductMismatchIsDeniedAfterTrialExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: .mismatch(reason: .product),
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .licenseMismatch)
        )
    }
 
    func testRemoteRevokedLicenseIsDeniedAfterTrialExpired() {
        let snapshot = EntitlementSnapshot(
            trialState: expiredTrial(),
            licenseState: .revokedOrExpiredRemote(reason: .revoked),
            validationFreshness: .neverChecked
        )
 
        XCTAssertEqual(
            EntitlementPolicy.evaluate(snapshot: snapshot, feature: .windowShortcutActivation, now: now),
            .proDenied(reason: .licenseRevokedOrExpired)
        )
    }
 
    func testFixtureProviderCoversRound015NamedStates() {
        let expectations: [EntitlementFixtureState: EffectiveEntitlement] = [
            .trialActive: .proAllowed(reason: .trialActive),
            .trialExpired: .proDenied(reason: .trialExpired),
            .proUnlocked: .proAllowed(reason: .licenseValid),
            .offlineGrace: .proAllowed(reason: .offlineGrace),
            .verifyFailed: .proDenied(reason: .hardFailure)
        ]
 
        for (fixtureState, expectedEntitlement) in expectations {
            let provider = FixtureEntitlementProvider(fixtureState: fixtureState, now: now)
            XCTAssertEqual(
                provider.entitlement(for: .windowShortcutActivation, at: now),
                expectedEntitlement,
                "fixture state \(fixtureState.rawValue)"
            )
        }
    }
 
    private func activeLicense() -> LicenseState {
        .activated(fingerprint: "abc123", instanceID: "instance-1", status: .active)
    }
 
    private func expiredTrial() -> TrialState {
        .expired(
            startedAt: now.addingTimeInterval(-31 * 24 * 60 * 60),
            expiredAt: now.addingTimeInterval(-24 * 60 * 60)
        )
    }
}