licenseseat 0.5.3

Official Rust SDK for LicenseSeat - simple, secure software licensing
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Unit tests for data models and types.

use chrono::{TimeZone, Utc};
use licenseseat::{
    ActivationOptions, Config, Entitlement, EntitlementReason, EntitlementStatus, LicenseStatus,
    LicenseStatusDetails, OfflineFallbackMode,
};

// ============================================================================
// LicenseStatus Tests
// ============================================================================

#[test]
fn test_license_status_inactive() {
    let status = LicenseStatus::Inactive {
        message: "No license activated".into(),
    };

    if let LicenseStatus::Inactive { message } = status {
        assert_eq!(message, "No license activated");
    } else {
        panic!("Expected Inactive status");
    }
}

#[test]
fn test_license_status_pending() {
    let status = LicenseStatus::Pending {
        message: "License not yet validated".into(),
    };

    if let LicenseStatus::Pending { message } = status {
        assert_eq!(message, "License not yet validated");
    } else {
        panic!("Expected Pending status");
    }
}

#[test]
fn test_license_status_active() {
    let now = Utc::now();
    let status = LicenseStatus::Active {
        details: LicenseStatusDetails {
            license: "TEST-KEY".into(),
            device: "device-123".into(),
            activated_at: now,
            last_validated: now,
            entitlements: vec![],
        },
    };

    if let LicenseStatus::Active { details } = status {
        assert_eq!(details.license, "TEST-KEY");
        assert_eq!(details.device, "device-123");
    } else {
        panic!("Expected Active status");
    }
}

#[test]
fn test_license_status_invalid() {
    let status = LicenseStatus::Invalid {
        message: "License has expired".into(),
    };

    if let LicenseStatus::Invalid { message } = status {
        assert_eq!(message, "License has expired");
    } else {
        panic!("Expected Invalid status");
    }
}

#[test]
fn test_license_status_offline_valid() {
    let now = Utc::now();
    let status = LicenseStatus::OfflineValid {
        details: LicenseStatusDetails {
            license: "OFFLINE-KEY".into(),
            device: "device-offline".into(),
            activated_at: now,
            last_validated: now,
            entitlements: vec![],
        },
    };

    if let LicenseStatus::OfflineValid { details } = status {
        assert_eq!(details.license, "OFFLINE-KEY");
    } else {
        panic!("Expected OfflineValid status");
    }
}

#[test]
fn test_license_status_offline_invalid() {
    let status = LicenseStatus::OfflineInvalid {
        message: "Offline token expired".into(),
    };

    if let LicenseStatus::OfflineInvalid { message } = status {
        assert_eq!(message, "Offline token expired");
    } else {
        panic!("Expected OfflineInvalid status");
    }
}

#[test]
fn test_license_status_is_active() {
    let now = Utc::now();

    // Active status should return true
    let active_status = LicenseStatus::Active {
        details: LicenseStatusDetails {
            license: "KEY".into(),
            device: "dev".into(),
            activated_at: now,
            last_validated: now,
            entitlements: vec![],
        },
    };
    assert!(active_status.is_active());

    // OfflineValid should return true
    let offline_valid = LicenseStatus::OfflineValid {
        details: LicenseStatusDetails {
            license: "KEY".into(),
            device: "dev".into(),
            activated_at: now,
            last_validated: now,
            entitlements: vec![],
        },
    };
    assert!(offline_valid.is_active());

    // Inactive should return false
    let inactive = LicenseStatus::Inactive {
        message: "No license".into(),
    };
    assert!(!inactive.is_active());

    // Invalid should return false
    let invalid = LicenseStatus::Invalid {
        message: "Expired".into(),
    };
    assert!(!invalid.is_active());
}

// ============================================================================
// Entitlement Tests
// ============================================================================

#[test]
fn test_entitlement_creation() {
    let expiry = Utc.with_ymd_and_hms(2025, 12, 31, 23, 59, 59).unwrap();
    let entitlement = Entitlement {
        key: "pro-features".into(),
        expires_at: Some(expiry),
        metadata: None,
    };

    assert_eq!(entitlement.key, "pro-features");
    assert!(entitlement.expires_at.is_some());
    assert!(entitlement.metadata.is_none());
}

#[test]
fn test_entitlement_permanent() {
    let entitlement = Entitlement {
        key: "lifetime".into(),
        expires_at: None, // No expiration = permanent
        metadata: None,
    };

    assert_eq!(entitlement.key, "lifetime");
    assert!(entitlement.expires_at.is_none());
}

#[test]
fn test_entitlement_is_expired() {
    let past = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    let entitlement = Entitlement {
        key: "expired-feature".into(),
        expires_at: Some(past),
        metadata: None,
    };

    // Check if expired (expires_at is in the past)
    assert!(entitlement.expires_at.unwrap() < Utc::now());
}

#[test]
fn test_entitlement_is_active() {
    let future = Utc.with_ymd_and_hms(2030, 12, 31, 23, 59, 59).unwrap();
    let entitlement = Entitlement {
        key: "active-feature".into(),
        expires_at: Some(future),
        metadata: None,
    };

    // Check if active (expires_at is in the future)
    assert!(entitlement.expires_at.unwrap() > Utc::now());
}

// ============================================================================
// EntitlementStatus Tests
// ============================================================================

#[test]
fn test_entitlement_status_active() {
    let status = EntitlementStatus {
        active: true,
        reason: None,
        entitlement: Some(Entitlement {
            key: "pro".into(),
            expires_at: None,
            metadata: None,
        }),
        expires_at: None,
    };

    assert!(status.active);
    assert!(status.reason.is_none());
    assert!(status.entitlement.is_some());
}

#[test]
fn test_entitlement_status_not_found() {
    let status = EntitlementStatus {
        active: false,
        reason: Some(EntitlementReason::NotFound),
        entitlement: None,
        expires_at: None,
    };

    assert!(!status.active);
    assert_eq!(status.reason, Some(EntitlementReason::NotFound));
}

#[test]
fn test_entitlement_status_expired() {
    let past = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
    let status = EntitlementStatus {
        active: false,
        reason: Some(EntitlementReason::Expired),
        entitlement: Some(Entitlement {
            key: "trial".into(),
            expires_at: Some(past),
            metadata: None,
        }),
        expires_at: Some(past),
    };

    assert!(!status.active);
    assert_eq!(status.reason, Some(EntitlementReason::Expired));
    assert!(status.expires_at.is_some());
}

#[test]
fn test_entitlement_status_no_license() {
    let status = EntitlementStatus {
        active: false,
        reason: Some(EntitlementReason::NoLicense),
        entitlement: None,
        expires_at: None,
    };

    assert!(!status.active);
    assert_eq!(status.reason, Some(EntitlementReason::NoLicense));
}

#[test]
fn test_entitlement_reason_variants() {
    let reasons = [
        EntitlementReason::NotFound,
        EntitlementReason::Expired,
        EntitlementReason::NoLicense,
    ];

    // All reasons should be distinct
    for (i, r1) in reasons.iter().enumerate() {
        for (j, r2) in reasons.iter().enumerate() {
            if i != j {
                assert_ne!(r1, r2);
            }
        }
    }
}

// ============================================================================
// ActivationOptions Tests
// ============================================================================

#[test]
fn test_activation_options_default() {
    let opts = ActivationOptions::default();

    assert!(opts.device_id.is_none());
    assert!(opts.device_fingerprint.is_none());
    assert!(opts.device_name.is_none());
    assert!(opts.metadata.is_none());
}

#[test]
fn test_activation_options_with_device_name() {
    let opts = ActivationOptions::with_device_name("My MacBook Pro");

    assert!(opts.device_id.is_none());
    assert!(opts.device_fingerprint.is_none());
    assert_eq!(opts.device_name.as_deref(), Some("My MacBook Pro"));
    assert!(opts.metadata.is_none());
}

#[test]
fn test_activation_options_full() {
    use std::collections::HashMap;

    let mut metadata = HashMap::new();
    metadata.insert("env".into(), serde_json::json!("production"));

    let opts = ActivationOptions {
        fingerprint: None,
        device_id: Some("custom-device-id".into()),
        device_fingerprint: None,
        device_name: Some("Production Server".into()),
        metadata: Some(metadata),
    };

    assert_eq!(opts.device_id.as_deref(), Some("custom-device-id"));
    assert_eq!(opts.device_name.as_deref(), Some("Production Server"));
    assert!(opts.metadata.is_some());
}

// ============================================================================
// Config Tests
// ============================================================================

#[test]
fn test_config_default_values() {
    let config = Config::default();

    assert_eq!(config.api_base_url, "https://licenseseat.com/api/v1");
    assert_eq!(
        config.auto_validate_interval,
        std::time::Duration::from_secs(3600)
    );
    assert_eq!(
        config.heartbeat_interval,
        std::time::Duration::from_secs(300)
    );
    assert!(!config.debug);
    assert!(config.telemetry_enabled);
    assert_eq!(config.max_offline_days, 0);
}

#[test]
fn test_config_new() {
    let config = Config::new("my-api-key", "my-product");

    assert_eq!(config.api_key, "my-api-key");
    assert_eq!(config.product_slug, "my-product");
}

#[test]
fn test_offline_fallback_mode_variants() {
    let modes = [
        OfflineFallbackMode::NetworkOnly,
        OfflineFallbackMode::Always,
    ];

    // All modes should be distinct
    for (i, m1) in modes.iter().enumerate() {
        for (j, m2) in modes.iter().enumerate() {
            if i != j {
                assert_ne!(m1, m2);
            }
        }
    }
}

#[test]
fn test_config_builder_methods() {
    let config = Config::new("key", "product")
        .with_debug(true)
        .with_auto_validate_interval(std::time::Duration::from_secs(1800))
        .with_offline_fallback(OfflineFallbackMode::Always)
        .with_max_offline_days(7);

    assert!(config.debug);
    assert_eq!(
        config.auto_validate_interval,
        std::time::Duration::from_secs(1800)
    );
    assert!(matches!(
        config.offline_fallback_mode,
        OfflineFallbackMode::Always
    ));
    assert_eq!(config.max_offline_days, 7);
}

#[test]
fn test_config_custom_values() {
    let config = Config {
        api_key: "custom-key".into(),
        product_slug: "custom-product".into(),
        api_base_url: "https://custom.api.com".into(),
        auto_validate_interval: std::time::Duration::from_secs(1800),
        heartbeat_interval: std::time::Duration::from_secs(60),
        offline_fallback_mode: OfflineFallbackMode::Always,
        max_offline_days: 7,
        debug: true,
        telemetry_enabled: false,
        device_identifier: Some("my-device".into()),
        app_version: Some("1.0.0".into()),
        app_build: Some("42".into()),
        ..Default::default()
    };

    assert_eq!(config.api_key, "custom-key");
    assert_eq!(config.product_slug, "custom-product");
    assert_eq!(config.api_base_url, "https://custom.api.com");
    assert_eq!(
        config.auto_validate_interval,
        std::time::Duration::from_secs(1800)
    );
    assert_eq!(
        config.heartbeat_interval,
        std::time::Duration::from_secs(60)
    );
    assert!(matches!(
        config.offline_fallback_mode,
        OfflineFallbackMode::Always
    ));
    assert_eq!(config.max_offline_days, 7);
    assert!(config.debug);
    assert!(!config.telemetry_enabled);
    assert_eq!(config.device_identifier.as_deref(), Some("my-device"));
    assert_eq!(config.app_version.as_deref(), Some("1.0.0"));
    assert_eq!(config.app_build.as_deref(), Some("42"));
}