jsdet-chrome-ext 0.1.0

Chrome Extension API bridges for jsdet — chrome.tabs, chrome.cookies, chrome.webRequest, etc.
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
//! Simulated Chrome extension state.
//!
//! Provides fake but realistic state for the chrome.* APIs:
//! tabs, cookies, storage, alarms. The state is controllable by
//! the security researcher — inject specific cookies, tabs, or
//! storage values to test how the extension handles them.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use jsdet_core::observation::Value;

/// Simulated browser state that chrome.* APIs operate on.
#[derive(Clone, Debug)]
pub struct ExtensionState {
    /// Simulated tab list.
    pub tabs: Arc<Mutex<Vec<Tab>>>,
    /// Simulated cookie jar.
    pub cookies: Arc<Mutex<Vec<Cookie>>>,
    /// Simulated chrome.storage.local.
    pub storage_local: Arc<Mutex<HashMap<String, String>>>,
    /// Simulated chrome.storage.sync.
    pub storage_sync: Arc<Mutex<HashMap<String, String>>>,
    /// Simulated alarms.
    pub alarms: Arc<Mutex<Vec<Alarm>>>,
    /// Message queue (from content scripts or external websites).
    pub pending_messages: Arc<Mutex<Vec<PendingMessage>>>,
    /// Extension's own ID.
    pub extension_id: String,
}

/// A simulated browser tab.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Tab {
    pub id: u32,
    pub url: String,
    pub title: String,
    pub active: bool,
    pub index: u32,
}

/// A simulated cookie.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Cookie {
    pub name: String,
    pub value: String,
    pub domain: String,
    pub path: String,
    pub secure: bool,
    pub http_only: bool,
}

/// A simulated alarm.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Alarm {
    pub name: String,
    pub scheduled_time: f64,
    pub period_in_minutes: Option<f64>,
}

/// A message waiting to be delivered to the extension.
#[derive(Clone, Debug)]
pub struct PendingMessage {
    /// Message payload.
    pub data: Value,
    /// Sender info (for origin checking).
    pub sender_origin: Option<String>,
    /// Whether this is from an external website (vs content script).
    pub is_external: bool,
    /// Sender tab ID (if from a content script).
    pub sender_tab_id: Option<u32>,
}

impl Default for ExtensionState {
    fn default() -> Self {
        Self {
            tabs: Arc::new(Mutex::new(vec![Tab {
                id: 1,
                url: "https://example.com".into(),
                title: "Example".into(),
                active: true,
                index: 0,
            }])),
            cookies: Arc::new(Mutex::new(vec![Cookie {
                name: "session".into(),
                value: "abc123".into(),
                domain: ".example.com".into(),
                path: "/".into(),
                secure: true,
                http_only: true,
            }])),
            storage_local: Arc::new(Mutex::new(HashMap::new())),
            storage_sync: Arc::new(Mutex::new(HashMap::new())),
            alarms: Arc::new(Mutex::new(Vec::new())),
            pending_messages: Arc::new(Mutex::new(Vec::new())),
            extension_id: "test-extension-id".into(),
        }
    }
}

impl ExtensionState {
    /// Create default state with a specific extension ID.
    pub fn default_with_id(id: &str) -> Self {
        Self {
            extension_id: id.to_string(),
            ..Self::default()
        }
    }

    /// Create state with custom tab list.
    pub fn with_tabs(mut self, tabs: Vec<Tab>) -> Self {
        self.tabs = Arc::new(Mutex::new(tabs));
        self
    }

    /// Create state with custom cookies.
    pub fn with_cookies(mut self, cookies: Vec<Cookie>) -> Self {
        self.cookies = Arc::new(Mutex::new(cookies));
        self
    }

    /// Pre-load storage with values.
    pub fn with_storage(mut self, data: HashMap<String, String>) -> Self {
        self.storage_local = Arc::new(Mutex::new(data));
        self
    }

    /// Queue a message for delivery to the extension.
    /// CRITICAL FIX: Handle poisoned mutex gracefully - drop message if mutex is poisoned.
    pub fn queue_message(&self, msg: PendingMessage) {
        if let Ok(mut guard) = self.pending_messages.lock() {
            guard.push(msg);
        }
    }

    /// Take all pending messages (drains the queue).
    /// CRITICAL FIX: Handle poisoned mutex - recover data even if poisoned.
    pub fn take_messages(&self) -> Vec<PendingMessage> {
        let mut guard = self
            .pending_messages
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        std::mem::take(&mut *guard)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================
    // DEFAULT STATE TESTS
    // ============================================================

    #[test]
    fn default_state_has_one_tab() {
        let state = ExtensionState::default();
        let tabs = state.tabs.lock().unwrap();
        assert_eq!(tabs.len(), 1);
        assert!(tabs[0].active);
    }

    #[test]
    fn default_state_has_session_cookie() {
        let state = ExtensionState::default();
        let cookies = state.cookies.lock().unwrap();
        assert_eq!(cookies.len(), 1);
        assert_eq!(cookies[0].name, "session");
    }

    #[test]
    fn default_state_cookie_properties() {
        let state = ExtensionState::default();
        let cookies = state.cookies.lock().unwrap();
        let cookie = &cookies[0];
        assert_eq!(cookie.value, "abc123");
        assert_eq!(cookie.domain, ".example.com");
        assert_eq!(cookie.path, "/");
        assert!(cookie.secure);
        assert!(cookie.http_only);
    }

    #[test]
    fn default_state_tab_properties() {
        let state = ExtensionState::default();
        let tabs = state.tabs.lock().unwrap();
        let tab = &tabs[0];
        assert_eq!(tab.id, 1);
        assert_eq!(tab.url, "https://example.com");
        assert_eq!(tab.title, "Example");
        assert_eq!(tab.index, 0);
        assert!(tab.active);
    }

    #[test]
    fn default_state_storage_is_empty() {
        let state = ExtensionState::default();
        assert!(state.storage_local.lock().unwrap().is_empty());
        assert!(state.storage_sync.lock().unwrap().is_empty());
    }

    #[test]
    fn default_state_alarms_is_empty() {
        let state = ExtensionState::default();
        assert!(state.alarms.lock().unwrap().is_empty());
    }

    #[test]
    fn default_state_pending_messages_is_empty() {
        let state = ExtensionState::default();
        assert!(state.pending_messages.lock().unwrap().is_empty());
    }

    #[test]
    fn default_state_extension_id() {
        let state = ExtensionState::default();
        assert_eq!(state.extension_id, "test-extension-id");
    }

    // ============================================================
    // MESSAGE QUEUE TESTS
    // ============================================================

    #[test]
    fn message_queue() {
        let state = ExtensionState::default();
        state.queue_message(PendingMessage {
            data: Value::string("hello"),
            sender_origin: Some("https://evil.com".into()),
            is_external: true,
            sender_tab_id: None,
        });

        let msgs = state.take_messages();
        assert_eq!(msgs.len(), 1);
        assert!(msgs[0].is_external);

        // Queue should be empty after take.
        assert!(state.take_messages().is_empty());
    }

    #[test]
    fn message_queue_multiple_messages() {
        let state = ExtensionState::default();

        state.queue_message(PendingMessage {
            data: Value::string("msg1"),
            sender_origin: Some("https://a.com".into()),
            is_external: true,
            sender_tab_id: None,
        });
        state.queue_message(PendingMessage {
            data: Value::string("msg2"),
            sender_origin: Some("https://b.com".into()),
            is_external: false,
            sender_tab_id: Some(1),
        });
        state.queue_message(PendingMessage {
            data: Value::json(r#"{"action": "test"}"#),
            sender_origin: None,
            is_external: false,
            sender_tab_id: Some(2),
        });

        let msgs = state.take_messages();
        assert_eq!(msgs.len(), 3);
        assert!(msgs[0].is_external);
        assert!(!msgs[1].is_external);
        assert_eq!(msgs[1].sender_tab_id, Some(1));
    }

    #[test]
    fn message_queue_take_drains_queue() {
        let state = ExtensionState::default();

        for i in 0..5 {
            state.queue_message(PendingMessage {
                data: Value::string(format!("msg{}", i)),
                sender_origin: None,
                is_external: false,
                sender_tab_id: None,
            });
        }

        let msgs1 = state.take_messages();
        assert_eq!(msgs1.len(), 5);

        let msgs2 = state.take_messages();
        assert!(msgs2.is_empty());

        let msgs3 = state.take_messages();
        assert!(msgs3.is_empty());
    }

    #[test]
    fn message_queue_order_preserved() {
        let state = ExtensionState::default();

        state.queue_message(PendingMessage {
            data: Value::string("first"),
            sender_origin: None,
            is_external: false,
            sender_tab_id: None,
        });
        state.queue_message(PendingMessage {
            data: Value::string("second"),
            sender_origin: None,
            is_external: false,
            sender_tab_id: None,
        });
        state.queue_message(PendingMessage {
            data: Value::string("third"),
            sender_origin: None,
            is_external: false,
            sender_tab_id: None,
        });

        let msgs = state.take_messages();
        assert_eq!(msgs[0].data, Value::string("first"));
        assert_eq!(msgs[1].data, Value::string("second"));
        assert_eq!(msgs[2].data, Value::string("third"));
    }

    #[test]
    fn message_queue_external_vs_internal() {
        let state = ExtensionState::default();

        state.queue_message(PendingMessage {
            data: Value::Null,
            sender_origin: Some("https://external.com".into()),
            is_external: true,
            sender_tab_id: None,
        });
        state.queue_message(PendingMessage {
            data: Value::Null,
            sender_origin: None,
            is_external: false,
            sender_tab_id: Some(1),
        });

        let msgs = state.take_messages();
        assert!(msgs[0].is_external);
        assert!(!msgs[1].is_external);
        assert_eq!(msgs[0].sender_origin, Some("https://external.com".into()));
        assert_eq!(msgs[1].sender_tab_id, Some(1));
    }

    // ============================================================
    // CUSTOM STATE BUILDER TESTS
    // ============================================================

    #[test]
    fn custom_state() {
        let state = ExtensionState::default()
            .with_tabs(vec![
                Tab {
                    id: 1,
                    url: "https://a.com".into(),
                    title: "A".into(),
                    active: true,
                    index: 0,
                },
                Tab {
                    id: 2,
                    url: "https://b.com".into(),
                    title: "B".into(),
                    active: false,
                    index: 1,
                },
            ])
            .with_storage(HashMap::from([("key".into(), "value".into())]));

        assert_eq!(state.tabs.lock().unwrap().len(), 2);
        assert_eq!(
            state.storage_local.lock().unwrap().get("key").unwrap(),
            "value"
        );
    }

    #[test]
    fn with_tabs_empty() {
        let state = ExtensionState::default().with_tabs(vec![]);
        assert!(state.tabs.lock().unwrap().is_empty());
    }

    #[test]
    fn with_tabs_single() {
        let state = ExtensionState::default().with_tabs(vec![Tab {
            id: 42,
            url: "https://test.com".into(),
            title: "Test".into(),
            active: true,
            index: 0,
        }]);
        let tabs = state.tabs.lock().unwrap();
        assert_eq!(tabs.len(), 1);
        assert_eq!(tabs[0].id, 42);
    }

    #[test]
    fn with_tabs_multiple() {
        let tabs_vec: Vec<Tab> = (0..10)
            .map(|i| Tab {
                id: i,
                url: format!("https://site{}.com", i),
                title: format!("Site {}", i),
                active: i == 0,
                index: i as u32,
            })
            .collect();

        let state = ExtensionState::default().with_tabs(tabs_vec);
        assert_eq!(state.tabs.lock().unwrap().len(), 10);
    }

    #[test]
    fn with_cookies_empty() {
        let state = ExtensionState::default().with_cookies(vec![]);
        assert!(state.cookies.lock().unwrap().is_empty());
    }

    #[test]
    fn with_cookies_single() {
        let state = ExtensionState::default().with_cookies(vec![Cookie {
            name: "custom".into(),
            value: "value".into(),
            domain: ".custom.com".into(),
            path: "/path".into(),
            secure: false,
            http_only: false,
        }]);
        let cookies = state.cookies.lock().unwrap();
        assert_eq!(cookies.len(), 1);
        assert_eq!(cookies[0].name, "custom");
    }

    #[test]
    fn with_cookies_multiple() {
        let cookies_vec: Vec<Cookie> = (0..10)
            .map(|i| Cookie {
                name: format!("cookie{}", i),
                value: format!("value{}", i),
                domain: format!(".site{}.com", i),
                path: "/".into(),
                secure: i % 2 == 0,
                http_only: i % 2 == 1,
            })
            .collect();

        let state = ExtensionState::default().with_cookies(cookies_vec);
        assert_eq!(state.cookies.lock().unwrap().len(), 10);
    }

    #[test]
    fn with_storage_empty() {
        let state = ExtensionState::default().with_storage(HashMap::new());
        assert!(state.storage_local.lock().unwrap().is_empty());
    }

    #[test]
    fn with_storage_single() {
        let mut map = HashMap::new();
        map.insert("key1".into(), "value1".into());
        let state = ExtensionState::default().with_storage(map);
        assert_eq!(
            state.storage_local.lock().unwrap().get("key1"),
            Some(&"value1".into())
        );
    }

    #[test]
    fn with_storage_multiple() {
        let map: HashMap<String, String> = (0..10)
            .map(|i| (format!("key{}", i), format!("value{}", i)))
            .collect();

        let state = ExtensionState::default().with_storage(map);
        let storage = state.storage_local.lock().unwrap();
        assert_eq!(storage.len(), 10);
        assert_eq!(storage.get("key5"), Some(&"value5".into()));
    }

    #[test]
    fn with_storage_overwrites_default() {
        let mut map = HashMap::new();
        map.insert("custom".into(), "data".into());
        let state = ExtensionState::default().with_storage(map);
        let storage = state.storage_local.lock().unwrap();
        assert!(storage.get("custom").is_some());
        assert_eq!(storage.len(), 1); // Only the custom data
    }

    #[test]
    fn chained_builders() {
        let state = ExtensionState::default()
            .with_tabs(vec![Tab {
                id: 1,
                url: "https://a.com".into(),
                title: "A".into(),
                active: true,
                index: 0,
            }])
            .with_cookies(vec![Cookie {
                name: "c".into(),
                value: "v".into(),
                domain: "d".into(),
                path: "/".into(),
                secure: true,
                http_only: true,
            }])
            .with_storage(HashMap::from([("k".into(), "v".into())]));

        assert_eq!(state.tabs.lock().unwrap().len(), 1);
        assert_eq!(state.cookies.lock().unwrap().len(), 1);
        assert_eq!(state.storage_local.lock().unwrap().len(), 1);
    }

    // ============================================================
    // EMPTY STATE TESTS
    // ============================================================

    #[test]
    fn empty_state_tabs() {
        let state = ExtensionState::default().with_tabs(vec![]);
        assert!(state.tabs.lock().unwrap().is_empty());
        assert_eq!(state.tabs.lock().unwrap().len(), 0);
    }

    #[test]
    fn empty_state_cookies() {
        let state = ExtensionState::default().with_cookies(vec![]);
        assert!(state.cookies.lock().unwrap().is_empty());
    }

    #[test]
    fn empty_state_storage() {
        let state = ExtensionState::default().with_storage(HashMap::new());
        assert!(state.storage_local.lock().unwrap().is_empty());
    }

    #[test]
    fn empty_state_all() {
        let state = ExtensionState::default()
            .with_tabs(vec![])
            .with_cookies(vec![])
            .with_storage(HashMap::new());

        assert!(state.tabs.lock().unwrap().is_empty());
        assert!(state.cookies.lock().unwrap().is_empty());
        assert!(state.storage_local.lock().unwrap().is_empty());
        assert!(state.storage_sync.lock().unwrap().is_empty());
        assert!(state.alarms.lock().unwrap().is_empty());
        assert!(state.pending_messages.lock().unwrap().is_empty());
    }

    // ============================================================
    // LARGE STATE TESTS
    // ============================================================

    #[test]
    fn large_state_many_tabs() {
        let tabs: Vec<Tab> = (0..100)
            .map(|i| Tab {
                id: i,
                url: format!("https://site{}.com/page", i),
                title: format!("Tab {} Title", i),
                active: i == 0,
                index: i as u32,
            })
            .collect();

        let state = ExtensionState::default().with_tabs(tabs);
        assert_eq!(state.tabs.lock().unwrap().len(), 100);
    }

    #[test]
    fn large_state_many_cookies() {
        let cookies: Vec<Cookie> = (0..1000)
            .map(|i| Cookie {
                name: format!("cookie_{}", i),
                value: format!("value_{}_{}", i, "x".repeat(50)),
                domain: format!(".domain{}.com", i % 100),
                path: "/".into(),
                secure: i % 3 == 0,
                http_only: i % 5 == 0,
            })
            .collect();

        let state = ExtensionState::default().with_cookies(cookies);
        assert_eq!(state.cookies.lock().unwrap().len(), 1000);
    }

    #[test]
    fn large_state_large_storage() {
        let storage: HashMap<String, String> = (0..1000)
            .map(|i| (format!("key_{}", i), format!("value_{}", i)))
            .collect();

        let state = ExtensionState::default().with_storage(storage);
        assert_eq!(state.storage_local.lock().unwrap().len(), 1000);
    }

    // ============================================================
    // CONCURRENT ACCESS TESTS (via Arc)
    // ============================================================

    #[test]
    fn concurrent_access_tabs() {
        let state = Arc::new(ExtensionState::default());
        let state2 = state.clone();

        // Modify from one reference
        state.tabs.lock().unwrap().push(Tab {
            id: 999,
            url: "https://new.com".into(),
            title: "New".into(),
            active: false,
            index: 1,
        });

        // Should be visible from other reference
        assert_eq!(state2.tabs.lock().unwrap().len(), 2);
    }

    #[test]
    fn concurrent_access_storage() {
        let state = Arc::new(ExtensionState::default());
        let state2 = state.clone();

        state
            .storage_local
            .lock()
            .unwrap()
            .insert("key".into(), "value".into());

        assert_eq!(
            state2.storage_local.lock().unwrap().get("key"),
            Some(&"value".into())
        );
    }

    #[test]
    fn concurrent_access_messages() {
        let state = Arc::new(ExtensionState::default());
        let state2 = state.clone();

        state.queue_message(PendingMessage {
            data: Value::string("test"),
            sender_origin: None,
            is_external: false,
            sender_tab_id: None,
        });

        let msgs = state2.take_messages();
        assert_eq!(msgs.len(), 1);
    }

    #[test]
    fn state_clone_shares_data() {
        // ExtensionState uses Arc<Mutex<...>> so clones share the same data
        let state1 = ExtensionState::default();
        let state2 = state1.clone();

        // Modify state1
        state1.tabs.lock().unwrap().clear();

        // state2 sees the same change because they share the Arc
        // NOTE: This is the actual behavior - clones share state via Arc
        assert!(state2.tabs.lock().unwrap().is_empty());
        assert!(state1.tabs.lock().unwrap().is_empty());
    }

    // ============================================================
    // TAB STRUCT TESTS
    // ============================================================

    #[test]
    fn tab_struct_all_fields() {
        let tab = Tab {
            id: 123,
            url: "https://example.com/path".into(),
            title: "Example Title".into(),
            active: true,
            index: 5,
        };
        assert_eq!(tab.id, 123);
        assert_eq!(tab.url, "https://example.com/path");
        assert_eq!(tab.title, "Example Title");
        assert!(tab.active);
        assert_eq!(tab.index, 5);
    }

    #[test]
    fn tab_inactive() {
        let tab = Tab {
            id: 1,
            url: "https://example.com".into(),
            title: "Example".into(),
            active: false,
            index: 0,
        };
        assert!(!tab.active);
    }

    // ============================================================
    // COOKIE STRUCT TESTS
    // ============================================================

    #[test]
    fn cookie_struct_all_fields() {
        let cookie = Cookie {
            name: "session_id".into(),
            value: "abc123xyz".into(),
            domain: ".example.com".into(),
            path: "/api".into(),
            secure: true,
            http_only: true,
        };
        assert_eq!(cookie.name, "session_id");
        assert_eq!(cookie.value, "abc123xyz");
        assert_eq!(cookie.domain, ".example.com");
        assert_eq!(cookie.path, "/api");
        assert!(cookie.secure);
        assert!(cookie.http_only);
    }

    #[test]
    fn cookie_non_secure() {
        let cookie = Cookie {
            name: "test".into(),
            value: "value".into(),
            domain: "example.com".into(),
            path: "/".into(),
            secure: false,
            http_only: false,
        };
        assert!(!cookie.secure);
        assert!(!cookie.http_only);
    }

    // ============================================================
    // ALARM STRUCT TESTS
    // ============================================================

    #[test]
    fn alarm_struct_all_fields() {
        let alarm = Alarm {
            name: "alarm1".into(),
            scheduled_time: 1234567890.5,
            period_in_minutes: Some(5.0),
        };
        assert_eq!(alarm.name, "alarm1");
        assert_eq!(alarm.scheduled_time, 1234567890.5);
        assert_eq!(alarm.period_in_minutes, Some(5.0));
    }

    #[test]
    fn alarm_one_time() {
        let alarm = Alarm {
            name: "once".into(),
            scheduled_time: 1234567890.0,
            period_in_minutes: None,
        };
        assert!(alarm.period_in_minutes.is_none());
    }

    // ============================================================
    // PENDING MESSAGE STRUCT TESTS
    // ============================================================

    #[test]
    fn pending_message_all_fields() {
        let msg = PendingMessage {
            data: Value::json(r#"{"action": "test"}"#),
            sender_origin: Some("https://sender.com".into()),
            is_external: true,
            sender_tab_id: Some(42),
        };
        assert_eq!(msg.data, Value::json(r#"{"action": "test"}"#));
        assert_eq!(msg.sender_origin, Some("https://sender.com".into()));
        assert!(msg.is_external);
        assert_eq!(msg.sender_tab_id, Some(42));
    }

    #[test]
    fn pending_message_minimal() {
        let msg = PendingMessage {
            data: Value::Null,
            sender_origin: None,
            is_external: false,
            sender_tab_id: None,
        };
        assert_eq!(msg.data, Value::Null);
        assert!(msg.sender_origin.is_none());
        assert!(!msg.is_external);
        assert!(msg.sender_tab_id.is_none());
    }
}