reinhardt-test 0.1.2

Testing utilities and helpers for Reinhardt framework
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
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
#![cfg(wasm)]

//! Mock infrastructure for WASM testing.
//!
//! This module provides mock utilities for server functions, storage, and other
//! browser APIs commonly needed in WASM tests.
//!
//! # Example
//!
//! ```no_run
//! use reinhardt_test::wasm::mock::{MockStorage, MockCookies};
//!
//! // Mock localStorage
//! let storage = MockStorage::new();
//! storage.set_item("user_id", "123");
//! assert_eq!(storage.get_item("user_id"), Some("123".to_string()));
//!
//! // Mock cookies
//! let cookies = MockCookies::new();
//! cookies.set("session", "abc123");
//! ```

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use wasm_bindgen::prelude::*;

// Re-export server function mocking from reinhardt-pages
#[allow(deprecated)]
pub use reinhardt_pages::testing::{
	MockResponse, assert_server_fn_call_count, assert_server_fn_called,
	assert_server_fn_called_with, assert_server_fn_not_called, clear_mocks, get_call_history,
	mock_server_fn, mock_server_fn_error,
};

/// Mock implementation for Web Storage (localStorage/sessionStorage).
///
/// This allows testing code that uses browser storage without affecting
/// the actual browser storage.
///
/// # Example
///
/// ```no_run
/// use reinhardt_test::wasm::mock::MockStorage;
///
/// let storage = MockStorage::new();
/// storage.set_item("key", "value");
/// assert_eq!(storage.get_item("key"), Some("value".to_string()));
/// ```
#[derive(Debug, Clone, Default)]
pub struct MockStorage {
	data: Rc<RefCell<HashMap<String, String>>>,
}

impl MockStorage {
	/// Create a new empty mock storage.
	pub fn new() -> Self {
		Self::default()
	}

	/// Create a mock storage with initial data.
	pub fn with_data(data: HashMap<String, String>) -> Self {
		Self {
			data: Rc::new(RefCell::new(data)),
		}
	}

	/// Get the number of items in storage.
	pub fn length(&self) -> usize {
		self.data.borrow().len()
	}

	/// Get an item by key.
	pub fn get_item(&self, key: &str) -> Option<String> {
		self.data.borrow().get(key).cloned()
	}

	/// Set an item.
	pub fn set_item(&self, key: &str, value: &str) {
		self.data
			.borrow_mut()
			.insert(key.to_string(), value.to_string());
	}

	/// Remove an item by key.
	pub fn remove_item(&self, key: &str) {
		self.data.borrow_mut().remove(key);
	}

	/// Clear all items.
	pub fn clear(&self) {
		self.data.borrow_mut().clear();
	}

	/// Get a key by index.
	pub fn key(&self, index: usize) -> Option<String> {
		self.data.borrow().keys().nth(index).cloned()
	}

	/// Get all keys.
	pub fn keys(&self) -> Vec<String> {
		self.data.borrow().keys().cloned().collect()
	}

	/// Get all values.
	pub fn values(&self) -> Vec<String> {
		self.data.borrow().values().cloned().collect()
	}

	/// Get all entries as key-value pairs.
	pub fn entries(&self) -> Vec<(String, String)> {
		self.data
			.borrow()
			.iter()
			.map(|(k, v)| (k.clone(), v.clone()))
			.collect()
	}

	/// Check if a key exists.
	pub fn contains_key(&self, key: &str) -> bool {
		self.data.borrow().contains_key(key)
	}
}

/// Mock implementation for browser cookies.
///
/// # Example
///
/// ```no_run
/// use reinhardt_test::wasm::mock::MockCookies;
///
/// let cookies = MockCookies::new();
/// cookies.set("session_id", "abc123");
/// cookies.set_with_options("auth_token", "xyz789", CookieOptions {
///     max_age: Some(3600),
///     secure: true,
///     ..Default::default()
/// });
///
/// assert_eq!(cookies.get("session_id"), Some("abc123".to_string()));
/// ```
#[derive(Debug, Clone, Default)]
pub struct MockCookies {
	cookies: Rc<RefCell<HashMap<String, CookieEntry>>>,
}

/// Cookie entry with value and options.
#[derive(Debug, Clone)]
pub struct CookieEntry {
	/// The cookie value.
	pub value: String,
	/// Cookie options.
	pub options: CookieOptions,
}

/// Options for setting cookies.
#[derive(Debug, Clone, Default)]
pub struct CookieOptions {
	/// Max-Age in seconds.
	pub max_age: Option<i64>,
	/// Expiration date as Unix timestamp.
	pub expires: Option<i64>,
	/// Cookie path.
	pub path: Option<String>,
	/// Cookie domain.
	pub domain: Option<String>,
	/// Secure flag.
	pub secure: bool,
	/// HttpOnly flag.
	pub http_only: bool,
	/// SameSite attribute.
	pub same_site: Option<SameSite>,
}

/// SameSite cookie attribute.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SameSite {
	/// Strict mode - only same-site requests.
	Strict,
	/// Lax mode - same-site and top-level navigation.
	Lax,
	/// None - cross-site requests allowed (requires Secure).
	None,
}

impl MockCookies {
	/// Create a new empty mock cookies instance.
	pub fn new() -> Self {
		Self::default()
	}

	/// Create mock cookies with initial data.
	pub fn with_cookies(cookies: HashMap<String, String>) -> Self {
		let entries: HashMap<String, CookieEntry> = cookies
			.into_iter()
			.map(|(k, v)| {
				(
					k,
					CookieEntry {
						value: v,
						options: CookieOptions::default(),
					},
				)
			})
			.collect();

		Self {
			cookies: Rc::new(RefCell::new(entries)),
		}
	}

	/// Get a cookie value by name.
	pub fn get(&self, name: &str) -> Option<String> {
		self.cookies
			.borrow()
			.get(name)
			.map(|entry| entry.value.clone())
	}

	/// Get a cookie entry with options by name.
	pub fn get_entry(&self, name: &str) -> Option<CookieEntry> {
		self.cookies.borrow().get(name).cloned()
	}

	/// Set a cookie with default options.
	pub fn set(&self, name: &str, value: &str) {
		self.set_with_options(name, value, CookieOptions::default());
	}

	/// Set a cookie with custom options.
	pub fn set_with_options(&self, name: &str, value: &str, options: CookieOptions) {
		self.cookies.borrow_mut().insert(
			name.to_string(),
			CookieEntry {
				value: value.to_string(),
				options,
			},
		);
	}

	/// Remove a cookie by name.
	pub fn remove(&self, name: &str) {
		self.cookies.borrow_mut().remove(name);
	}

	/// Clear all cookies.
	pub fn clear(&self) {
		self.cookies.borrow_mut().clear();
	}

	/// Get all cookie names.
	pub fn names(&self) -> Vec<String> {
		self.cookies.borrow().keys().cloned().collect()
	}

	/// Get all cookies as name-value pairs.
	pub fn all(&self) -> HashMap<String, String> {
		self.cookies
			.borrow()
			.iter()
			.map(|(k, v)| (k.clone(), v.value.clone()))
			.collect()
	}

	/// Check if a cookie exists.
	pub fn has(&self, name: &str) -> bool {
		self.cookies.borrow().contains_key(name)
	}

	/// Get the number of cookies.
	pub fn len(&self) -> usize {
		self.cookies.borrow().len()
	}

	/// Check if there are no cookies.
	pub fn is_empty(&self) -> bool {
		self.cookies.borrow().is_empty()
	}

	/// Generate a cookie string in the format used by document.cookie.
	pub fn to_cookie_string(&self) -> String {
		self.cookies
			.borrow()
			.iter()
			.map(|(name, entry)| format!("{}={}", name, entry.value))
			.collect::<Vec<_>>()
			.join("; ")
	}
}

/// Mock implementation for fetch API.
///
/// This allows intercepting and mocking fetch requests in tests.
///
/// # Example
///
/// ```no_run
/// use reinhardt_test::wasm::mock::{MockFetch, MockFetchResponse};
///
/// let fetch = MockFetch::new();
/// fetch.mock_response("/api/users", MockFetchResponse::json(vec![
///     serde_json::json!({"id": 1, "name": "Alice"}),
/// ]));
///
/// // Later, when code calls fetch("/api/users"), it will receive the mocked response
/// ```
#[deprecated(
	since = "0.1.0-rc.16",
	note = "Use `MockServiceWorker` from `reinhardt_test::msw` instead. See issue #3283."
)]
#[derive(Debug, Clone, Default)]
pub struct MockFetch {
	responses: Rc<RefCell<HashMap<String, MockFetchResponse>>>,
	calls: Rc<RefCell<Vec<MockFetchCall>>>,
}

/// A recorded fetch call.
#[derive(Debug, Clone)]
pub struct MockFetchCall {
	/// The URL that was fetched.
	pub url: String,
	/// The HTTP method used.
	pub method: String,
	/// Request headers.
	pub headers: HashMap<String, String>,
	/// Request body if any.
	pub body: Option<String>,
}

/// A mock response for fetch.
#[derive(Debug, Clone)]
pub struct MockFetchResponse {
	/// HTTP status code.
	pub status: u16,
	/// Status text.
	pub status_text: String,
	/// Response headers.
	pub headers: HashMap<String, String>,
	/// Response body.
	pub body: String,
	/// Whether to simulate a network error.
	pub network_error: bool,
}

impl MockFetchResponse {
	/// Create a successful JSON response.
	pub fn json<T: serde::Serialize>(data: T) -> Self {
		let body = serde_json::to_string(&data).unwrap_or_default();
		Self {
			status: 200,
			status_text: "OK".to_string(),
			headers: [("content-type".to_string(), "application/json".to_string())]
				.into_iter()
				.collect(),
			body,
			network_error: false,
		}
	}

	/// Create a successful text response.
	pub fn text(body: impl Into<String>) -> Self {
		Self {
			status: 200,
			status_text: "OK".to_string(),
			headers: [("content-type".to_string(), "text/plain".to_string())]
				.into_iter()
				.collect(),
			body: body.into(),
			network_error: false,
		}
	}

	/// Create an error response.
	pub fn error(status: u16, message: impl Into<String>) -> Self {
		Self {
			status,
			status_text: http_status_text(status).to_string(),
			headers: HashMap::new(),
			body: message.into(),
			network_error: false,
		}
	}

	/// Create a network error response.
	pub fn network_error() -> Self {
		Self {
			status: 0,
			status_text: String::new(),
			headers: HashMap::new(),
			body: String::new(),
			network_error: true,
		}
	}

	/// Set a response header.
	pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
		self.headers.insert(name.into(), value.into());
		self
	}

	/// Set the response status.
	pub fn with_status(mut self, status: u16) -> Self {
		self.status = status;
		self.status_text = http_status_text(status).to_string();
		self
	}
}

impl MockFetch {
	/// Create a new mock fetch instance.
	pub fn new() -> Self {
		Self::default()
	}

	/// Register a mock response for a URL.
	pub fn mock_response(&self, url: impl Into<String>, response: MockFetchResponse) {
		self.responses.borrow_mut().insert(url.into(), response);
	}

	/// Remove a mock response.
	pub fn remove_mock(&self, url: &str) {
		self.responses.borrow_mut().remove(url);
	}

	/// Clear all mocks.
	pub fn clear_mocks(&self) {
		self.responses.borrow_mut().clear();
	}

	/// Get all recorded calls.
	pub fn calls(&self) -> Vec<MockFetchCall> {
		self.calls.borrow().clone()
	}

	/// Get calls to a specific URL.
	pub fn calls_to(&self, url: &str) -> Vec<MockFetchCall> {
		self.calls
			.borrow()
			.iter()
			.filter(|call| call.url == url)
			.cloned()
			.collect()
	}

	/// Clear recorded calls.
	pub fn clear_calls(&self) {
		self.calls.borrow_mut().clear();
	}

	/// Check if a URL was called.
	pub fn was_called(&self, url: &str) -> bool {
		self.calls.borrow().iter().any(|call| call.url == url)
	}

	/// Get the number of times a URL was called.
	pub fn call_count(&self, url: &str) -> usize {
		self.calls
			.borrow()
			.iter()
			.filter(|call| call.url == url)
			.count()
	}

	/// Record a call (for internal use).
	pub fn record_call(&self, call: MockFetchCall) {
		self.calls.borrow_mut().push(call);
	}

	/// Get a mock response for a URL if registered.
	pub fn get_response(&self, url: &str) -> Option<MockFetchResponse> {
		self.responses.borrow().get(url).cloned()
	}
}

fn http_status_text(status: u16) -> &'static str {
	match status {
		200 => "OK",
		201 => "Created",
		204 => "No Content",
		400 => "Bad Request",
		401 => "Unauthorized",
		403 => "Forbidden",
		404 => "Not Found",
		405 => "Method Not Allowed",
		409 => "Conflict",
		422 => "Unprocessable Entity",
		429 => "Too Many Requests",
		500 => "Internal Server Error",
		502 => "Bad Gateway",
		503 => "Service Unavailable",
		_ => "Unknown",
	}
}

/// Mock timer utilities for controlling time in tests.
///
/// This allows fast-forwarding time without actually waiting.
#[derive(Debug, Default)]
pub struct MockTimers {
	callbacks: Rc<RefCell<Vec<TimerCallback>>>,
	current_time: Rc<RefCell<f64>>,
}

struct TimerCallback {
	id: u32,
	callback: Box<dyn FnOnce()>,
	scheduled_time: f64,
	is_interval: bool,
	interval_ms: Option<u32>,
}

impl std::fmt::Debug for TimerCallback {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("TimerCallback")
			.field("id", &self.id)
			.field("callback", &"<FnOnce>")
			.field("scheduled_time", &self.scheduled_time)
			.field("is_interval", &self.is_interval)
			.field("interval_ms", &self.interval_ms)
			.finish()
	}
}

impl MockTimers {
	/// Create a new mock timers instance.
	pub fn new() -> Self {
		Self::default()
	}

	/// Get the current mock time.
	pub fn now(&self) -> f64 {
		*self.current_time.borrow()
	}

	/// Advance time by the specified milliseconds and run due callbacks.
	pub fn advance_by(&self, ms: u32) {
		let new_time = *self.current_time.borrow() + ms as f64;
		*self.current_time.borrow_mut() = new_time;
		self.run_due_callbacks();
	}

	/// Run all pending timers immediately.
	pub fn run_all(&self) {
		let max_time = self
			.callbacks
			.borrow()
			.iter()
			.map(|cb| cb.scheduled_time)
			.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
			.unwrap_or(0.0);

		if max_time > *self.current_time.borrow() {
			*self.current_time.borrow_mut() = max_time;
			self.run_due_callbacks();
		}
	}

	/// Get the number of pending timers.
	pub fn pending_count(&self) -> usize {
		self.callbacks.borrow().len()
	}

	/// Clear all pending timers.
	pub fn clear_all(&self) {
		self.callbacks.borrow_mut().clear();
	}

	// Fixes #879
	fn run_due_callbacks(&self) {
		let current = *self.current_time.borrow();

		// Drain all callbacks from the list, then partition into due and remaining
		let all_callbacks: Vec<TimerCallback> = self.callbacks.borrow_mut().drain(..).collect();

		let mut remaining = Vec::new();
		let mut due = Vec::new();

		for cb in all_callbacks {
			if cb.scheduled_time <= current {
				due.push(cb);
			} else {
				remaining.push(cb);
			}
		}

		// Restore non-due callbacks
		*self.callbacks.borrow_mut() = remaining;

		// Execute due callbacks in scheduled order
		due.sort_by(|a, b| {
			a.scheduled_time
				.partial_cmp(&b.scheduled_time)
				.unwrap_or(std::cmp::Ordering::Equal)
		});

		for cb in due {
			(cb.callback)();
		}
	}
}

/// Test helper for tracking element mutations.
///
/// This uses MutationObserver to track DOM changes.
#[derive(Debug)]
pub struct MutationTracker {
	mutations: Rc<RefCell<Vec<MutationRecord>>>,
}

/// A recorded DOM mutation.
#[derive(Debug, Clone)]
pub struct MutationRecord {
	/// Type of mutation (childList, attributes, characterData).
	pub mutation_type: String,
	/// Target element tag name.
	pub target: String,
	/// Attribute name for attribute mutations.
	pub attribute_name: Option<String>,
	/// Old value if available.
	pub old_value: Option<String>,
	/// Number of added nodes.
	pub added_nodes_count: usize,
	/// Number of removed nodes.
	pub removed_nodes_count: usize,
}

impl MutationTracker {
	/// Create a new mutation tracker for the given element.
	///
	/// # Panics
	///
	/// Always panics because `MutationObserver` requires a live WASM runtime
	/// with access to the browser DOM API. This type cannot be used outside
	/// of an actual browser environment.
	// Fixes #879
	pub fn new(_element: &web_sys::Element) -> Self {
		unimplemented!(
			"MutationTracker requires a WASM runtime with browser DOM access. \
			 MutationObserver cannot be set up outside of an actual browser environment."
		)
	}

	/// Get all recorded mutations.
	pub fn mutations(&self) -> Vec<MutationRecord> {
		self.mutations.borrow().clone()
	}

	/// Clear recorded mutations.
	pub fn clear(&self) {
		self.mutations.borrow_mut().clear();
	}

	/// Check if any mutations occurred.
	pub fn has_mutations(&self) -> bool {
		!self.mutations.borrow().is_empty()
	}

	/// Get the number of mutations.
	pub fn mutation_count(&self) -> usize {
		self.mutations.borrow().len()
	}
}

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

	// ==================== MockStorage tests ====================

	#[rstest]
	fn test_mock_storage_operations() {
		// Arrange
		let storage = MockStorage::new();

		// Act & Assert
		assert_eq!(storage.length(), 0);

		storage.set_item("key1", "value1");
		storage.set_item("key2", "value2");

		assert_eq!(storage.length(), 2);
		assert_eq!(storage.get_item("key1"), Some("value1".to_string()));
		assert_eq!(storage.get_item("key2"), Some("value2".to_string()));
		assert_eq!(storage.get_item("key3"), None);

		storage.remove_item("key1");
		assert_eq!(storage.length(), 1);
		assert_eq!(storage.get_item("key1"), None);

		storage.clear();
		assert_eq!(storage.length(), 0);
	}

	#[rstest]
	fn test_mock_storage_with_data() {
		// Arrange
		let mut data = HashMap::new();
		data.insert("a".to_string(), "1".to_string());
		data.insert("b".to_string(), "2".to_string());

		// Act
		let storage = MockStorage::with_data(data);

		// Assert
		assert_eq!(storage.length(), 2);
		assert_eq!(storage.get_item("a"), Some("1".to_string()));
		assert_eq!(storage.get_item("b"), Some("2".to_string()));
	}

	#[rstest]
	fn test_mock_storage_key_by_index() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("alpha", "val");

		// Act
		let key_0 = storage.key(0);
		let key_out_of_bounds = storage.key(99);

		// Assert
		assert_eq!(key_0, Some("alpha".to_string()));
		assert_eq!(key_out_of_bounds, None);
	}

	#[rstest]
	fn test_mock_storage_keys_values_entries() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("x", "10");
		storage.set_item("y", "20");

		// Act
		let keys = storage.keys();
		let values = storage.values();
		let entries = storage.entries();

		// Assert
		assert_eq!(keys.len(), 2);
		assert!(keys.contains(&"x".to_string()));
		assert!(keys.contains(&"y".to_string()));
		assert_eq!(values.len(), 2);
		assert!(values.contains(&"10".to_string()));
		assert!(values.contains(&"20".to_string()));
		assert_eq!(entries.len(), 2);
	}

	#[rstest]
	fn test_mock_storage_contains_key() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("present", "yes");

		// Act & Assert
		assert!(storage.contains_key("present"));
		assert!(!storage.contains_key("absent"));
	}

	#[rstest]
	fn test_mock_storage_length_after_ops() {
		// Arrange
		let storage = MockStorage::new();

		// Act & Assert
		assert_eq!(storage.length(), 0);
		storage.set_item("a", "1");
		assert_eq!(storage.length(), 1);
		storage.set_item("b", "2");
		assert_eq!(storage.length(), 2);
		storage.remove_item("a");
		assert_eq!(storage.length(), 1);
		storage.clear();
		assert_eq!(storage.length(), 0);
	}

	#[rstest]
	fn test_mock_storage_overwrite() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("key", "old_value");

		// Act
		storage.set_item("key", "new_value");

		// Assert
		assert_eq!(storage.length(), 1);
		assert_eq!(storage.get_item("key"), Some("new_value".to_string()));
	}

	#[rstest]
	fn test_mock_storage_remove_nonexistent() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("key", "value");

		// Act
		storage.remove_item("nonexistent");

		// Assert
		assert_eq!(storage.length(), 1);
		assert_eq!(storage.get_item("key"), Some("value".to_string()));
	}

	#[rstest]
	fn test_mock_storage_empty_key_and_value() {
		// Arrange
		let storage = MockStorage::new();

		// Act
		storage.set_item("", "empty_key");
		storage.set_item("empty_value", "");

		// Assert
		assert_eq!(storage.get_item(""), Some("empty_key".to_string()));
		assert_eq!(storage.get_item("empty_value"), Some(String::new()));
		assert_eq!(storage.length(), 2);
	}

	#[rstest]
	fn test_mock_storage_clone_independence() {
		// Arrange
		let storage = MockStorage::new();
		storage.set_item("shared", "data");
		let cloned = storage.clone();

		// Act
		cloned.set_item("extra", "value");

		// Assert: Rc<RefCell> means clones share the same data
		assert_eq!(storage.length(), 2);
		assert_eq!(storage.get_item("extra"), Some("value".to_string()));
	}

	// ==================== MockCookies tests ====================

	#[rstest]
	fn test_mock_cookies_operations() {
		// Arrange
		let cookies = MockCookies::new();

		// Act & Assert
		assert!(cookies.is_empty());

		cookies.set("session", "abc123");
		cookies.set_with_options(
			"auth",
			"xyz",
			CookieOptions {
				secure: true,
				http_only: true,
				..Default::default()
			},
		);

		assert_eq!(cookies.len(), 2);
		assert_eq!(cookies.get("session"), Some("abc123".to_string()));
		assert_eq!(cookies.get("auth"), Some("xyz".to_string()));

		let entry = cookies.get_entry("auth").unwrap();
		assert!(entry.options.secure);
		assert!(entry.options.http_only);

		cookies.remove("session");
		assert!(!cookies.has("session"));
		assert!(cookies.has("auth"));
	}

	#[rstest]
	fn test_mock_cookies_with_cookies_constructor() {
		// Arrange
		let mut initial = HashMap::new();
		initial.insert("a".to_string(), "1".to_string());
		initial.insert("b".to_string(), "2".to_string());

		// Act
		let cookies = MockCookies::with_cookies(initial);

		// Assert
		assert_eq!(cookies.len(), 2);
		assert_eq!(cookies.get("a"), Some("1".to_string()));
		assert_eq!(cookies.get("b"), Some("2".to_string()));
	}

	#[rstest]
	fn test_mock_cookies_names_and_all() {
		// Arrange
		let cookies = MockCookies::new();
		cookies.set("x", "10");
		cookies.set("y", "20");

		// Act
		let names = cookies.names();
		let all = cookies.all();

		// Assert
		assert_eq!(names.len(), 2);
		assert!(names.contains(&"x".to_string()));
		assert!(names.contains(&"y".to_string()));
		assert_eq!(all.len(), 2);
		assert_eq!(all.get("x"), Some(&"10".to_string()));
		assert_eq!(all.get("y"), Some(&"20".to_string()));
	}

	#[rstest]
	fn test_mock_cookies_is_empty_transitions() {
		// Arrange
		let cookies = MockCookies::new();

		// Assert: initially empty
		assert!(cookies.is_empty());

		// Act: add a cookie
		cookies.set("k", "v");

		// Assert: no longer empty
		assert!(!cookies.is_empty());

		// Act: remove the cookie
		cookies.remove("k");

		// Assert: empty again
		assert!(cookies.is_empty());
	}

	#[rstest]
	fn test_mock_cookies_get_entry_with_options() {
		// Arrange
		let cookies = MockCookies::new();
		cookies.set_with_options(
			"tracking",
			"abc",
			CookieOptions {
				max_age: Some(3600),
				path: Some("/app".to_string()),
				domain: Some("example.com".to_string()),
				secure: true,
				http_only: false,
				same_site: Some(SameSite::Lax),
				expires: None,
			},
		);

		// Act
		let entry = cookies.get_entry("tracking").unwrap();

		// Assert
		assert_eq!(entry.value, "abc");
		assert_eq!(entry.options.max_age, Some(3600));
		assert_eq!(entry.options.path, Some("/app".to_string()));
		assert_eq!(entry.options.domain, Some("example.com".to_string()));
		assert!(entry.options.secure);
		assert!(!entry.options.http_only);
		assert_eq!(entry.options.same_site, Some(SameSite::Lax));
	}

	#[rstest]
	fn test_mock_cookies_get_entry_nonexistent() {
		// Arrange
		let cookies = MockCookies::new();

		// Act
		let entry = cookies.get_entry("missing");

		// Assert
		assert!(entry.is_none());
	}

	#[rstest]
	fn test_mock_cookies_to_cookie_string_format() {
		// Arrange
		let mut initial = HashMap::new();
		initial.insert("a".to_string(), "1".to_string());
		let cookies = MockCookies::with_cookies(initial);

		// Act
		let cookie_str = cookies.to_cookie_string();

		// Assert
		assert_eq!(cookie_str, "a=1");
	}

	#[rstest]
	fn test_mock_cookies_to_cookie_string_empty() {
		// Arrange
		let cookies = MockCookies::new();

		// Act
		let cookie_str = cookies.to_cookie_string();

		// Assert
		assert_eq!(cookie_str, "");
	}

	#[rstest]
	fn test_mock_cookies_to_cookie_string_multiple() {
		// Arrange
		let cookies = MockCookies::new();
		cookies.set("a", "1");
		cookies.set("b", "2");

		// Act
		let cookie_str = cookies.to_cookie_string();

		// Assert: order is not guaranteed, but format should be "key=value; key=value"
		assert!(cookie_str.contains("a=1"));
		assert!(cookie_str.contains("b=2"));
		assert!(cookie_str.contains("; "));
	}

	#[rstest]
	fn test_cookie_options_default() {
		// Arrange & Act
		let opts = CookieOptions::default();

		// Assert
		assert_eq!(opts.max_age, None);
		assert_eq!(opts.expires, None);
		assert_eq!(opts.path, None);
		assert_eq!(opts.domain, None);
		assert!(!opts.secure);
		assert!(!opts.http_only);
		assert!(opts.same_site.is_none());
	}

	#[rstest]
	fn test_same_site_variants() {
		// Arrange & Act & Assert
		assert_eq!(SameSite::Strict, SameSite::Strict);
		assert_eq!(SameSite::Lax, SameSite::Lax);
		assert_eq!(SameSite::None, SameSite::None);
		assert_ne!(SameSite::Strict, SameSite::Lax);
		assert_ne!(SameSite::Lax, SameSite::None);
		assert_ne!(SameSite::Strict, SameSite::None);
	}

	#[rstest]
	fn test_mock_cookies_clear() {
		// Arrange
		let cookies = MockCookies::new();
		cookies.set("a", "1");
		cookies.set("b", "2");
		assert_eq!(cookies.len(), 2);

		// Act
		cookies.clear();

		// Assert
		assert_eq!(cookies.len(), 0);
		assert!(cookies.is_empty());
	}

	// ==================== MockFetch tests ====================

	#[rstest]
	fn test_mock_fetch_response_builders() {
		// Arrange & Act
		let json_response = MockFetchResponse::json(serde_json::json!({"status": "ok"}));

		// Assert
		assert_eq!(json_response.status, 200);
		assert!(json_response.body.contains("status"));

		// Arrange & Act
		let text_response = MockFetchResponse::text("Hello, World!");

		// Assert
		assert_eq!(text_response.body, "Hello, World!");

		// Arrange & Act
		let error_response = MockFetchResponse::error(404, "Not found");

		// Assert
		assert_eq!(error_response.status, 404);
		assert_eq!(error_response.status_text, "Not Found");

		// Arrange & Act
		let network_error = MockFetchResponse::network_error();

		// Assert
		assert!(network_error.network_error);
	}

	#[rstest]
	fn test_mock_fetch_recording() {
		// Arrange
		let fetch = MockFetch::new();

		// Act
		fetch.record_call(MockFetchCall {
			url: "/api/users".to_string(),
			method: "GET".to_string(),
			headers: HashMap::new(),
			body: None,
		});

		fetch.record_call(MockFetchCall {
			url: "/api/posts".to_string(),
			method: "POST".to_string(),
			headers: HashMap::new(),
			body: Some(r#"{"title":"Test"}"#.to_string()),
		});

		// Assert
		assert!(fetch.was_called("/api/users"));
		assert!(fetch.was_called("/api/posts"));
		assert!(!fetch.was_called("/api/comments"));

		assert_eq!(fetch.call_count("/api/users"), 1);
		assert_eq!(fetch.calls().len(), 2);
	}

	#[rstest]
	fn test_mock_fetch_get_response_matching() {
		// Arrange
		let fetch = MockFetch::new();
		let response = MockFetchResponse::json(serde_json::json!({"id": 1}));
		fetch.mock_response("/api/data", response);

		// Act
		let result = fetch.get_response("/api/data");

		// Assert
		assert!(result.is_some());
		let resp = result.unwrap();
		assert_eq!(resp.status, 200);
		assert!(resp.body.contains("id"));
	}

	#[rstest]
	fn test_mock_fetch_get_response_unregistered_returns_none() {
		// Arrange
		let fetch = MockFetch::new();

		// Act
		let result = fetch.get_response("/unknown");

		// Assert
		assert!(result.is_none());
	}

	#[rstest]
	fn test_mock_fetch_remove_mock() {
		// Arrange
		let fetch = MockFetch::new();
		fetch.mock_response("/api/x", MockFetchResponse::text("hello"));
		assert!(fetch.get_response("/api/x").is_some());

		// Act
		fetch.remove_mock("/api/x");

		// Assert
		assert!(fetch.get_response("/api/x").is_none());
	}

	#[rstest]
	fn test_mock_fetch_clear_mocks() {
		// Arrange
		let fetch = MockFetch::new();
		fetch.mock_response("/a", MockFetchResponse::text("1"));
		fetch.mock_response("/b", MockFetchResponse::text("2"));

		// Act
		fetch.clear_mocks();

		// Assert
		assert!(fetch.get_response("/a").is_none());
		assert!(fetch.get_response("/b").is_none());
	}

	#[rstest]
	fn test_mock_fetch_calls_to_filtering() {
		// Arrange
		let fetch = MockFetch::new();
		fetch.record_call(MockFetchCall {
			url: "/api/a".to_string(),
			method: "GET".to_string(),
			headers: HashMap::new(),
			body: None,
		});
		fetch.record_call(MockFetchCall {
			url: "/api/b".to_string(),
			method: "GET".to_string(),
			headers: HashMap::new(),
			body: None,
		});
		fetch.record_call(MockFetchCall {
			url: "/api/a".to_string(),
			method: "POST".to_string(),
			headers: HashMap::new(),
			body: Some("data".to_string()),
		});

		// Act
		let calls_a = fetch.calls_to("/api/a");
		let calls_b = fetch.calls_to("/api/b");
		let calls_c = fetch.calls_to("/api/c");

		// Assert
		assert_eq!(calls_a.len(), 2);
		assert_eq!(calls_b.len(), 1);
		assert_eq!(calls_c.len(), 0);
	}

	#[rstest]
	fn test_mock_fetch_clear_calls() {
		// Arrange
		let fetch = MockFetch::new();
		fetch.record_call(MockFetchCall {
			url: "/api/x".to_string(),
			method: "GET".to_string(),
			headers: HashMap::new(),
			body: None,
		});
		assert_eq!(fetch.calls().len(), 1);

		// Act
		fetch.clear_calls();

		// Assert
		assert_eq!(fetch.calls().len(), 0);
		assert!(!fetch.was_called("/api/x"));
	}

	#[rstest]
	fn test_mock_fetch_call_count_multiple() {
		// Arrange
		let fetch = MockFetch::new();
		for _ in 0..5 {
			fetch.record_call(MockFetchCall {
				url: "/api/repeat".to_string(),
				method: "GET".to_string(),
				headers: HashMap::new(),
				body: None,
			});
		}
		fetch.record_call(MockFetchCall {
			url: "/api/other".to_string(),
			method: "GET".to_string(),
			headers: HashMap::new(),
			body: None,
		});

		// Act & Assert
		assert_eq!(fetch.call_count("/api/repeat"), 5);
		assert_eq!(fetch.call_count("/api/other"), 1);
		assert_eq!(fetch.call_count("/api/none"), 0);
	}

	// ==================== MockFetchResponse tests ====================

	#[rstest]
	fn test_mock_fetch_response_json_content_type() {
		// Arrange & Act
		let resp = MockFetchResponse::json(serde_json::json!([1, 2, 3]));

		// Assert
		assert_eq!(
			resp.headers.get("content-type"),
			Some(&"application/json".to_string())
		);
		assert_eq!(resp.status, 200);
		assert_eq!(resp.status_text, "OK");
		assert!(!resp.network_error);
	}

	#[rstest]
	fn test_mock_fetch_response_text_content_type() {
		// Arrange & Act
		let resp = MockFetchResponse::text("plain text");

		// Assert
		assert_eq!(
			resp.headers.get("content-type"),
			Some(&"text/plain".to_string())
		);
		assert_eq!(resp.body, "plain text");
		assert_eq!(resp.status, 200);
		assert!(!resp.network_error);
	}

	#[rstest]
	fn test_mock_fetch_response_with_status() {
		// Arrange
		let resp = MockFetchResponse::text("created");

		// Act
		let resp = resp.with_status(201);

		// Assert
		assert_eq!(resp.status, 201);
		assert_eq!(resp.status_text, "Created");
	}

	#[rstest]
	fn test_mock_fetch_response_with_header() {
		// Arrange
		let resp = MockFetchResponse::text("body");

		// Act
		let resp = resp
			.with_header("X-Custom", "value1")
			.with_header("X-Another", "value2");

		// Assert
		assert_eq!(resp.headers.get("X-Custom"), Some(&"value1".to_string()));
		assert_eq!(resp.headers.get("X-Another"), Some(&"value2".to_string()));
		// Original content-type header should still be present
		assert_eq!(
			resp.headers.get("content-type"),
			Some(&"text/plain".to_string())
		);
	}

	#[rstest]
	fn test_mock_fetch_response_error_various_statuses() {
		// Arrange & Act & Assert
		let resp_400 = MockFetchResponse::error(400, "bad");
		assert_eq!(resp_400.status, 400);
		assert_eq!(resp_400.status_text, "Bad Request");

		let resp_500 = MockFetchResponse::error(500, "fail");
		assert_eq!(resp_500.status, 500);
		assert_eq!(resp_500.status_text, "Internal Server Error");

		let resp_unknown = MockFetchResponse::error(999, "mystery");
		assert_eq!(resp_unknown.status, 999);
		assert_eq!(resp_unknown.status_text, "Unknown");
	}

	#[rstest]
	fn test_mock_fetch_response_network_error_fields() {
		// Arrange & Act
		let resp = MockFetchResponse::network_error();

		// Assert
		assert!(resp.network_error);
		assert_eq!(resp.status, 0);
		assert_eq!(resp.status_text, "");
		assert!(resp.headers.is_empty());
		assert_eq!(resp.body, "");
	}

	// ==================== MockTimers tests ====================

	#[rstest]
	fn test_mock_timers_initial_state() {
		// Arrange & Act
		let timers = MockTimers::new();

		// Assert
		assert_eq!(timers.now(), 0.0);
		assert_eq!(timers.pending_count(), 0);
	}

	#[rstest]
	fn test_mock_timers_advance_by() {
		// Arrange
		let timers = MockTimers::new();

		// Act
		timers.advance_by(100);

		// Assert
		assert_eq!(timers.now(), 100.0);

		// Act
		timers.advance_by(50);

		// Assert
		assert_eq!(timers.now(), 150.0);
	}

	#[rstest]
	fn test_mock_timers_clear_all() {
		// Arrange
		let timers = MockTimers::new();

		// Act
		timers.clear_all();

		// Assert
		assert_eq!(timers.pending_count(), 0);
	}
}