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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! StressTest - Comprehensive SDK stress test for LicenseSeat Rust SDK
//!
//! This example mimics a real Tauri/Rust application and tests all SDK functionality:
//!
//! Scenario 1:  Activation with telemetry enabled
//! Scenario 2:  Validation with telemetry
//! Scenario 3:  Heartbeat endpoint (single, rapid, spaced)
//! Scenario 4:  Enriched telemetry server acceptance
//! Scenario 5:  Telemetry disabled mode
//! Scenario 6:  Entitlement checking (comprehensive)
//! Scenario 7:  Entitlement edge cases (expiration, missing, reasons)
//! Scenario 8:  License status tracking
//! Scenario 9:  Concurrent validation stress
//! Scenario 10: Event subscription system
//! Scenario 11: Offline validation configuration
//! Scenario 12: Full lifecycle (activate -> validate -> heartbeat -> deactivate)
//!
//! Usage:
//!   LICENSESEAT_API_KEY=pk_live_xxx \
//!   LICENSESEAT_PRODUCT_SLUG=your_product \
//!   LICENSESEAT_LICENSE_KEY=your_license_key \
//!   cargo run --example stress_test
//!
//! Use a `pk_*` publishable API key for client-side examples.
//!
//! Optional:
//!   LICENSESEAT_BASE_URL - API base URL
//!   RUN_SCENARIO - Run only specific scenario (1-12)

use licenseseat::{
    Config, EntitlementReason, EventKind, LicenseSeat, LicenseStatus, OfflineFallbackMode,
};
use std::env;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};

#[derive(Clone)]
struct TestContext {
    api_key: String,
    product_slug: String,
    license_key: String,
    base_url: String,
}

impl TestContext {
    fn from_env() -> Self {
        Self {
            api_key: env::var("LICENSESEAT_API_KEY").expect("LICENSESEAT_API_KEY required"),
            product_slug: env::var("LICENSESEAT_PRODUCT_SLUG")
                .expect("LICENSESEAT_PRODUCT_SLUG required"),
            license_key: env::var("LICENSESEAT_LICENSE_KEY")
                .expect("LICENSESEAT_LICENSE_KEY required"),
            base_url: env::var("LICENSESEAT_BASE_URL")
                .unwrap_or_else(|_| "https://licenseseat.com/api/v1".to_string()),
        }
    }

    fn build_sdk(&self, telemetry: bool) -> LicenseSeat {
        LicenseSeat::new(Config {
            api_key: self.api_key.clone(),
            product_slug: self.product_slug.clone(),
            api_base_url: self.base_url.clone(),
            telemetry_enabled: telemetry,
            ..Default::default()
        })
    }

    fn build_sdk_with_version(&self, version: &str) -> LicenseSeat {
        LicenseSeat::new(Config {
            api_key: self.api_key.clone(),
            product_slug: self.product_slug.clone(),
            api_base_url: self.base_url.clone(),
            telemetry_enabled: true,
            app_version: Some(version.to_string()),
            ..Default::default()
        })
    }
}

#[derive(Clone, Copy)]
enum ScenarioResult {
    Pass,
    Fail,
}

impl ScenarioResult {
    fn symbol(&self) -> &'static str {
        match self {
            ScenarioResult::Pass => "[PASS]",
            ScenarioResult::Fail => "[FAIL]",
        }
    }
}

struct TestRunner {
    results: Vec<(String, ScenarioResult, Duration)>,
}

impl TestRunner {
    fn new() -> Self {
        Self {
            results: Vec::new(),
        }
    }

    fn record(&mut self, name: &str, result: ScenarioResult, duration: Duration) {
        println!(
            "{} {} ({:.2}s)",
            result.symbol(),
            name,
            duration.as_secs_f64()
        );
        self.results.push((name.to_string(), result, duration));
    }

    fn summary(&self) {
        println!("\n========================================");
        println!("STRESS TEST SUMMARY");
        println!("========================================\n");

        let mut passed = 0;
        let mut failed = 0;
        let mut total_time = Duration::ZERO;

        for (name, result, duration) in &self.results {
            println!(
                "  {} {} ({:.2}s)",
                result.symbol(),
                name,
                duration.as_secs_f64()
            );
            total_time += *duration;
            match result {
                ScenarioResult::Pass => passed += 1,
                ScenarioResult::Fail => failed += 1,
            }
        }

        println!("\n----------------------------------------");
        println!("Total: {} passed, {} failed", passed, failed);
        println!("Time:  {:.2}s", total_time.as_secs_f64());
        println!("========================================\n");
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive("licenseseat=info".parse()?),
        )
        .init();

    println!("\n========================================");
    println!("LICENSESEAT RUST SDK STRESS TEST");
    println!("========================================\n");

    let ctx = TestContext::from_env();
    let mut runner = TestRunner::new();

    // Check if user wants specific scenario
    let run_scenario: Option<u32> = env::var("RUN_SCENARIO").ok().and_then(|s| s.parse().ok());

    // Define scenarios
    let scenarios: &[(
        u32,
        &str,
        fn(
            TestContext,
        ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ScenarioResult> + Send>>,
    )] = &[
        (1, "Activation with telemetry", |ctx| {
            Box::pin(scenario_1_activation_telemetry(ctx))
        }),
        (2, "Validation with telemetry", |ctx| {
            Box::pin(scenario_2_validation_telemetry(ctx))
        }),
        (3, "Heartbeat patterns", |ctx| {
            Box::pin(scenario_3_heartbeat_patterns(ctx))
        }),
        (4, "Enriched telemetry", |ctx| {
            Box::pin(scenario_4_enriched_telemetry(ctx))
        }),
        (5, "Telemetry disabled", |ctx| {
            Box::pin(scenario_5_telemetry_disabled(ctx))
        }),
        (6, "Entitlement checking", |ctx| {
            Box::pin(scenario_6_entitlement_checking(ctx))
        }),
        (7, "Entitlement edge cases", |ctx| {
            Box::pin(scenario_7_entitlement_edge_cases(ctx))
        }),
        (8, "License status", |ctx| {
            Box::pin(scenario_8_license_status(ctx))
        }),
        (9, "Concurrent stress", |ctx| {
            Box::pin(scenario_9_concurrent_stress(ctx))
        }),
        (10, "Event subscription", |ctx| {
            Box::pin(scenario_10_event_subscription(ctx))
        }),
        (11, "Offline validation config", |ctx| {
            Box::pin(scenario_11_offline_validation(ctx))
        }),
        (12, "Full lifecycle", |ctx| {
            Box::pin(scenario_12_full_lifecycle(ctx))
        }),
    ];

    for (num, name, scenario_fn) in scenarios {
        if let Some(run) = run_scenario {
            if run != *num {
                continue;
            }
        }

        println!("\n--- Scenario {}: {} ---", num, name);
        let start = Instant::now();
        let result = scenario_fn(ctx.clone()).await;
        runner.record(
            &format!("Scenario {}: {}", num, name),
            result,
            start.elapsed(),
        );
    }

    runner.summary();
    Ok(())
}

// ============================================================================
// Scenario 1: Activation with telemetry enabled
// ============================================================================
async fn scenario_1_activation_telemetry(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    println!("  Activating license with telemetry...");
    match sdk.activate(&ctx.license_key).await {
        Ok(license) => {
            println!("  Activation successful");
            println!("    Device ID: {}", license.device_id);
            println!("    Activation ID: {:?}", license.activation_id);

            // Deactivate after test
            let _ = sdk.deactivate().await;
            ScenarioResult::Pass
        }
        Err(e) => {
            println!("  Activation error: {}", e);
            ScenarioResult::Fail
        }
    }
}

// ============================================================================
// Scenario 2: Validation with telemetry
// ============================================================================
async fn scenario_2_validation_telemetry(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    // First activate
    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    // Then validate
    println!("  Validating license...");
    match sdk.validate().await {
        Ok(result) => {
            if result.valid {
                println!("  Validation successful");
                println!(
                    "    Active entitlements: {}",
                    result.license.active_entitlements.len()
                );
                let _ = sdk.deactivate().await;
                ScenarioResult::Pass
            } else {
                println!("  Validation invalid: {:?}", result.code);
                let _ = sdk.deactivate().await;
                ScenarioResult::Fail
            }
        }
        Err(e) => {
            println!("  Validation error: {}", e);
            let _ = sdk.deactivate().await;
            ScenarioResult::Fail
        }
    }
}

// ============================================================================
// Scenario 3: Heartbeat patterns (single, rapid, spaced)
// ============================================================================
async fn scenario_3_heartbeat_patterns(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    // Activate first
    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    let mut success = true;

    // Single heartbeat
    println!("  Single heartbeat...");
    if let Err(e) = sdk.heartbeat().await {
        println!("    Failed: {}", e);
        success = false;
    } else {
        println!("    OK");
    }

    // Rapid heartbeats (5 in quick succession)
    println!("  Rapid heartbeats (5x)...");
    let mut rapid_failures = 0;
    for i in 1..=5 {
        if sdk.heartbeat().await.is_err() {
            rapid_failures += 1;
        }
        print!("    {}/5 ", i);
    }
    println!();
    if rapid_failures > 0 {
        println!("    {} failures", rapid_failures);
        // Allow some failures in rapid mode (rate limiting)
        if rapid_failures > 3 {
            success = false;
        }
    } else {
        println!("    All OK");
    }

    // Spaced heartbeats
    println!("  Spaced heartbeats (3x @ 1s interval)...");
    let mut spaced_failures = 0;
    for i in 1..=3 {
        tokio::time::sleep(Duration::from_secs(1)).await;
        if let Err(e) = sdk.heartbeat().await {
            println!("    {}/3 FAILED: {}", i, e);
            spaced_failures += 1;
        } else {
            println!("    {}/3 OK", i);
        }
    }
    if spaced_failures > 0 {
        success = false;
    }

    let _ = sdk.deactivate().await;

    if success {
        ScenarioResult::Pass
    } else {
        ScenarioResult::Fail
    }
}

// ============================================================================
// Scenario 4: Enriched telemetry server acceptance
// ============================================================================
async fn scenario_4_enriched_telemetry(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk_with_version("2.5.0-stress-test");

    println!("  Activating with enriched telemetry...");
    match sdk.activate(&ctx.license_key).await {
        Ok(_) => {
            println!("  Server accepted enriched telemetry");
            let _ = sdk.deactivate().await;
            ScenarioResult::Pass
        }
        Err(e) => {
            println!("  Activation error: {}", e);
            ScenarioResult::Fail
        }
    }
}

// ============================================================================
// Scenario 5: Telemetry disabled mode
// ============================================================================
async fn scenario_5_telemetry_disabled(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(false);

    println!("  Activating with telemetry disabled...");
    match sdk.activate(&ctx.license_key).await {
        Ok(_) => {
            println!("  Activation successful (no telemetry sent)");
            let _ = sdk.deactivate().await;
            ScenarioResult::Pass
        }
        Err(e) => {
            println!("  Activation error: {}", e);
            ScenarioResult::Fail
        }
    }
}

// ============================================================================
// Scenario 6: Entitlement checking (comprehensive)
// ============================================================================
async fn scenario_6_entitlement_checking(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    println!("  Validating to load entitlements...");
    let validation = match sdk.validate().await {
        Ok(v) => v,
        Err(e) => {
            println!("  Validation failed: {}", e);
            let _ = sdk.deactivate().await;
            return ScenarioResult::Fail;
        }
    };

    // List all entitlements from validation
    println!("  Entitlements from validation:");
    for ent in &validation.license.active_entitlements {
        println!("    - key: {}, expires_at: {:?}", ent.key, ent.expires_at);
    }

    let mut success = true;

    // Test 1: Check a known entitlement
    println!("  Test 1: Check known entitlement (pro-features)...");
    let status = sdk.check_entitlement("pro-features");
    println!("    active: {}", status.active);
    println!("    reason: {:?}", status.reason);
    println!("    expires_at: {:?}", status.expires_at);
    if let Some(ent) = &status.entitlement {
        println!("    entitlement.key: {}", ent.key);
    }
    if !status.active {
        println!("    WARNING: pro-features not active (may need to add to license)");
    }

    // Test 2: Check another entitlement
    println!("  Test 2: Check another entitlement (premium-support)...");
    let status = sdk.check_entitlement("premium-support");
    println!("    active: {}", status.active);
    println!("    reason: {:?}", status.reason);

    // Test 3: Check non-existent entitlement
    println!("  Test 3: Check non-existent entitlement...");
    let status = sdk.check_entitlement("non-existent-feature-xyz");
    println!("    active: {} (expected: false)", status.active);
    println!("    reason: {:?} (expected: NotFound)", status.reason);
    if status.active || status.reason != Some(EntitlementReason::NotFound) {
        println!("    FAIL: Expected NotFound reason");
        success = false;
    }

    // Test 4: has_entitlement convenience method
    println!("  Test 4: has_entitlement convenience method...");
    let has_pro = sdk.has_entitlement("pro-features");
    let has_fake = sdk.has_entitlement("fake-feature");
    println!("    has_entitlement(pro-features): {}", has_pro);
    println!(
        "    has_entitlement(fake-feature): {} (expected: false)",
        has_fake
    );
    if has_fake {
        println!("    FAIL: Should not have fake-feature");
        success = false;
    }

    // Test 5: Multiple entitlement checks in sequence
    println!("  Test 5: Sequential entitlement checks...");
    let entitlements_to_check = [
        "pro-features",
        "premium-support",
        "api-access",
        "export-pdf",
    ];
    for key in &entitlements_to_check {
        let active = sdk.has_entitlement(key);
        println!("    {}: {}", key, if active { "YES" } else { "no" });
    }

    let _ = sdk.deactivate().await;
    if success {
        ScenarioResult::Pass
    } else {
        ScenarioResult::Fail
    }
}

// ============================================================================
// Scenario 7: Entitlement edge cases (expiration, missing, reasons)
// ============================================================================
async fn scenario_7_entitlement_edge_cases(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    // Test 1: Check entitlement before activation (should fail with NoLicense)
    println!("  Test 1: Check entitlement before activation...");
    let status = sdk.check_entitlement("pro-features");
    println!("    active: {} (expected: false)", status.active);
    println!("    reason: {:?} (expected: NoLicense)", status.reason);
    if status.reason != Some(EntitlementReason::NoLicense) {
        println!("    FAIL: Expected NoLicense reason");
        return ScenarioResult::Fail;
    }

    // Activate
    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    // Test 2: Check entitlement after activation but before validation
    println!("  Test 2: Check entitlement before validation...");
    let status = sdk.check_entitlement("pro-features");
    println!("    active: {} (expected: false)", status.active);
    println!(
        "    reason: {:?} (expected: NoLicense - no validation yet)",
        status.reason
    );
    // Note: This might be NoLicense since validation hasn't populated entitlements

    // Validate
    println!("  Validating...");
    if let Err(e) = sdk.validate().await {
        println!("  Validation failed: {}", e);
        let _ = sdk.deactivate().await;
        return ScenarioResult::Fail;
    }

    // Test 3: Check entitlement after validation
    println!("  Test 3: Check entitlement after validation...");
    let status = sdk.check_entitlement("pro-features");
    println!("    active: {}", status.active);
    println!("    reason: {:?}", status.reason);
    println!("    expires_at: {:?}", status.expires_at);

    // Test 4: EntitlementStatus fields
    println!("  Test 4: Verify EntitlementStatus struct...");
    let status = sdk.check_entitlement("premium-support");
    println!("    EntitlementStatus {{");
    println!("      active: {},", status.active);
    println!("      reason: {:?},", status.reason);
    println!("      expires_at: {:?},", status.expires_at);
    println!(
        "      entitlement: {:?}",
        status.entitlement.as_ref().map(|e| &e.key)
    );
    println!("    }}");

    // Test 5: Multiple missing entitlements all return NotFound
    println!("  Test 5: Multiple missing entitlements...");
    let missing = ["a", "b", "c", "xyz-123", "fake_feature"];
    let mut all_not_found = true;
    for key in missing {
        let status = sdk.check_entitlement(key);
        if status.reason != Some(EntitlementReason::NotFound) {
            all_not_found = false;
            println!(
                "    FAIL: {} should be NotFound, got {:?}",
                key, status.reason
            );
        }
    }
    println!("    All returned NotFound: {}", all_not_found);

    let _ = sdk.deactivate().await;
    ScenarioResult::Pass
}

// ============================================================================
// Scenario 8: License status tracking
// ============================================================================
async fn scenario_8_license_status(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    // Check status before activation
    println!("  Status before activation...");
    match sdk.status() {
        LicenseStatus::Inactive { message } => {
            println!("    Status: Inactive - {}", message);
        }
        status => {
            println!("    Unexpected status: {:?}", status);
        }
    }

    // Activate
    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    // Check status after activation (before validation)
    println!("  Status after activation (before validation)...");
    match sdk.status() {
        LicenseStatus::Pending { message } => {
            println!("    Status: Pending - {}", message);
        }
        status => {
            println!("    Status: {:?}", status);
        }
    }

    // Validate
    println!("  Validating...");
    if let Err(e) = sdk.validate().await {
        println!("  Validation failed: {}", e);
        let _ = sdk.deactivate().await;
        return ScenarioResult::Fail;
    }

    // Check status after validation
    println!("  Status after validation...");
    match sdk.status() {
        LicenseStatus::Active { details } => {
            println!("    Status: Active");
            println!("    License: {}", details.license);
            println!("    Device: {}", details.device);
            println!("    Entitlements: {}", details.entitlements.len());
        }
        status => {
            println!("    Unexpected status: {:?}", status);
        }
    }

    let _ = sdk.deactivate().await;
    ScenarioResult::Pass
}

// ============================================================================
// Scenario 9: Concurrent validation stress
// ============================================================================
async fn scenario_9_concurrent_stress(ctx: TestContext) -> ScenarioResult {
    let sdk = Arc::new(ctx.build_sdk(true));

    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    // First validation to populate cache
    if let Err(e) = sdk.validate().await {
        println!("  Initial validation failed: {}", e);
        let _ = sdk.deactivate().await;
        return ScenarioResult::Fail;
    }

    println!("  Launching 10 concurrent validations...");
    let mut handles = Vec::new();

    for i in 0..10 {
        let sdk_clone = sdk.clone();
        handles.push(tokio::spawn(async move {
            let start = Instant::now();
            let result = sdk_clone.validate().await;
            let success = result.is_ok() && result.map(|r| r.valid).unwrap_or(false);
            (i, success, start.elapsed())
        }));
    }

    let mut successes = 0;
    let mut failures = 0;
    let mut total_time = Duration::ZERO;

    for handle in handles {
        if let Ok((i, success, duration)) = handle.await {
            if success {
                successes += 1;
            } else {
                failures += 1;
            }
            total_time += duration;
            println!(
                "    Validation {}: {} ({:.2}ms)",
                i,
                if success { "OK" } else { "FAIL" },
                duration.as_millis()
            );
        }
    }

    println!("  Results: {} success, {} failed", successes, failures);
    println!("  Avg time: {:.2}ms", total_time.as_millis() as f64 / 10.0);

    let _ = sdk.deactivate().await;

    // Allow up to 3 failures due to rate limiting
    if failures <= 3 {
        ScenarioResult::Pass
    } else {
        ScenarioResult::Fail
    }
}

// ============================================================================
// Scenario 10: Event subscription system
// ============================================================================
async fn scenario_10_event_subscription(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    let event_count = Arc::new(AtomicU32::new(0));
    let event_count_clone = event_count.clone();

    let mut event_rx = sdk.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = event_rx.recv().await {
            event_count_clone.fetch_add(1, Ordering::SeqCst);
            match event.kind {
                EventKind::ActivationStart => println!("    -> ActivationStart"),
                EventKind::ActivationSuccess => println!("    -> ActivationSuccess"),
                EventKind::ActivationError => println!("    -> ActivationError"),
                EventKind::ValidationStart => println!("    -> ValidationStart"),
                EventKind::ValidationSuccess => println!("    -> ValidationSuccess"),
                EventKind::HeartbeatSuccess => println!("    -> HeartbeatSuccess"),
                EventKind::DeactivationStart => println!("    -> DeactivationStart"),
                EventKind::DeactivationSuccess => println!("    -> DeactivationSuccess"),
                kind => println!("    -> {:?}", kind),
            }
        }
    });

    println!("  Performing lifecycle operations...");

    // Activate
    println!("  Activating...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("  Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    // Small delay to let events process
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Validate
    println!("  Validating...");
    let _ = sdk.validate().await;

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Heartbeat
    println!("  Sending heartbeat...");
    let _ = sdk.heartbeat().await;

    tokio::time::sleep(Duration::from_millis(100)).await;

    // Deactivate
    println!("  Deactivating...");
    let _ = sdk.deactivate().await;

    tokio::time::sleep(Duration::from_millis(100)).await;

    let count = event_count.load(Ordering::SeqCst);
    println!("  Total events received: {}", count);

    // We should have received at least activation, validation, heartbeat, deactivation events
    if count >= 4 {
        ScenarioResult::Pass
    } else {
        println!("  Expected at least 4 events");
        ScenarioResult::Fail
    }
}

// ============================================================================
// Scenario 11: Offline validation configuration
// ============================================================================
async fn scenario_11_offline_validation(ctx: TestContext) -> ScenarioResult {
    println!("  Testing offline validation configuration...");

    // Test 1: Default offline fallback mode
    println!("  Test 1: Default offline fallback mode...");
    let default_config = Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        ..Default::default()
    };
    println!(
        "    offline_fallback_mode: {:?} (expected: NetworkOnly)",
        default_config.offline_fallback_mode
    );
    if default_config.offline_fallback_mode != OfflineFallbackMode::NetworkOnly {
        println!("    FAIL: Expected NetworkOnly as default");
        return ScenarioResult::Fail;
    }

    // Test 2: Configure offline fallback mode to Always
    println!("  Test 2: Configure offline fallback to Always...");
    let always_config = Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        offline_fallback_mode: OfflineFallbackMode::Always,
        ..Default::default()
    };
    println!(
        "    offline_fallback_mode: {:?}",
        always_config.offline_fallback_mode
    );

    // Test 3: Max offline days configuration
    println!("  Test 3: Max offline days configuration...");
    let offline_config = Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        max_offline_days: 7,
        ..Default::default()
    };
    println!(
        "    max_offline_days: {} (set to 7)",
        offline_config.max_offline_days
    );
    if offline_config.max_offline_days != 7 {
        println!("    FAIL: Expected max_offline_days = 7");
        return ScenarioResult::Fail;
    }

    // Test 4: Max clock skew configuration
    println!("  Test 4: Max clock skew configuration...");
    let skew_config = Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        max_clock_skew: Duration::from_secs(600), // 10 minutes
        ..Default::default()
    };
    println!("    max_clock_skew: {:?}", skew_config.max_clock_skew);

    // Test 5: Offline token refresh interval
    println!("  Test 5: Offline token refresh interval...");
    let refresh_config = Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        offline_token_refresh_interval: Duration::from_secs(24 * 3600), // 24 hours
        ..Default::default()
    };
    println!(
        "    offline_token_refresh_interval: {:?}",
        refresh_config.offline_token_refresh_interval
    );

    // Test 6: SDK with offline config
    println!("  Test 6: Create SDK with offline configuration...");
    let sdk = LicenseSeat::new(Config {
        api_key: ctx.api_key.clone(),
        product_slug: ctx.product_slug.clone(),
        api_base_url: ctx.base_url.clone(),
        offline_fallback_mode: OfflineFallbackMode::Always,
        max_offline_days: 7,
        max_clock_skew: Duration::from_secs(300),
        ..Default::default()
    });

    // Activate and validate to ensure SDK works with offline config
    println!("  Activating with offline-enabled config...");
    if let Err(e) = sdk.activate(&ctx.license_key).await {
        println!("    Activation failed: {}", e);
        return ScenarioResult::Fail;
    }

    println!("  Validating...");
    match sdk.validate().await {
        Ok(result) => {
            println!("    Validation: valid={}", result.valid);
        }
        Err(e) => {
            println!("    Validation failed: {}", e);
        }
    }

    let _ = sdk.deactivate().await;

    // Note about offline feature
    println!("\n  Note: Full offline validation (Ed25519 token verification) requires:");
    println!("    cargo run --example stress_test --features offline");
    println!("  The 'offline' feature enables cryptographic verification of cached tokens.");

    ScenarioResult::Pass
}

// ============================================================================
// Scenario 12: Full lifecycle (activate -> validate -> heartbeat -> deactivate)
// ============================================================================
async fn scenario_12_full_lifecycle(ctx: TestContext) -> ScenarioResult {
    let sdk = ctx.build_sdk(true);

    let events = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
    let events_clone = events.clone();

    let mut event_rx = sdk.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = event_rx.recv().await {
            let event_name = format!("{}", event.kind);
            events_clone.lock().unwrap().push(event_name);
        }
    });

    // Step 1: Activate
    println!("  Step 1: Activate...");
    match sdk.activate(&ctx.license_key).await {
        Ok(license) => {
            println!("    OK - Device: {}", license.device_id);
        }
        Err(e) => {
            println!("    Failed: {}", e);
            return ScenarioResult::Fail;
        }
    }

    // Step 2: Validate
    println!("  Step 2: Validate...");
    match sdk.validate().await {
        Ok(result) => {
            if !result.valid {
                println!("    Validation invalid");
                let _ = sdk.deactivate().await;
                return ScenarioResult::Fail;
            }
            println!(
                "    OK - {} entitlements",
                result.license.active_entitlements.len()
            );
        }
        Err(e) => {
            println!("    Failed: {}", e);
            let _ = sdk.deactivate().await;
            return ScenarioResult::Fail;
        }
    }

    // Step 3: Heartbeat
    println!("  Step 3: Heartbeat...");
    match sdk.heartbeat().await {
        Ok(response) => {
            println!("    OK (received_at: {})", response.received_at);
        }
        Err(e) => {
            println!("    Failed: {}", e);
            let _ = sdk.deactivate().await;
            return ScenarioResult::Fail;
        }
    }

    // Step 4: Check cached license
    println!("  Step 4: Check cached license...");
    match sdk.current_license() {
        Some(license) => {
            println!("    Cached license found: {}", license.license_key);
        }
        None => {
            println!("    No cached license (unexpected but not fatal)");
        }
    }

    // Step 5: Deactivate
    println!("  Step 5: Deactivate...");
    match sdk.deactivate().await {
        Ok(_) => {
            println!("    OK");
        }
        Err(e) => {
            println!("    Failed: {}", e);
            return ScenarioResult::Fail;
        }
    }

    // Summary
    tokio::time::sleep(Duration::from_millis(100)).await;
    let captured_events = events.lock().unwrap();
    println!("  Events captured: {}", captured_events.len());
    for event in captured_events.iter().take(8) {
        println!("    - {}", event);
    }
    if captured_events.len() > 8 {
        println!("    ... and {} more", captured_events.len() - 8);
    }

    ScenarioResult::Pass
}