browser_oxide 0.1.3

Stealth headless browser engine in Rust: real HTML/CSS/DOM/JS, V8 via deno_core, own BoringSSL TLS/JA4 fingerprint, no Chromium, no CDP — for anti-bot web scraping, archival, and AI agents
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
//! Integration tests for supporting Web APIs.
//!
//! Each test drives a real Page::from_html flow and verifies that the
//! API behaves as Chrome does from a black-box perspective.

use browser_oxide::Page;

// ============================================================================
// A1 — blob: URL fetch
// ============================================================================

#[tokio::test]
async fn fetch_blob_url_returns_text() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const blob = new Blob(['hello blob']);
                const url = URL.createObjectURL(blob);
                const resp = await fetch(url);
                const text = await resp.text();
                document.getElementById('out').textContent =
                    resp.status + '|' + resp.ok + '|' + text;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .expect("page builds");

    let out = page.text_of("#out").expect("script should populate #out");
    assert_eq!(out, "200|true|hello blob");
}

#[tokio::test]
async fn fetch_blob_url_preserves_content_type() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const blob = new Blob(['x,y\n1,2'], { type: 'text/csv' });
                const url = URL.createObjectURL(blob);
                const resp = await fetch(url);
                document.getElementById('out').textContent =
                    resp.headers.get('content-type') || 'none';
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("text/csv".to_string()));
}

#[tokio::test]
async fn fetch_blob_url_arraybuffer_preserves_bytes() {
    // Binary fidelity: a blob of raw non-UTF8 bytes should survive
    // round-trip through arrayBuffer() without TextEncoder corruption.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                // 0x00 0xFF 0x7F 0x80 — includes null, high-bit bytes.
                const bytes = new Uint8Array([0x00, 0xFF, 0x7F, 0x80]);
                const blob = new Blob([bytes], { type: 'application/octet-stream' });
                const url = URL.createObjectURL(blob);
                const resp = await fetch(url);
                const ab = await resp.arrayBuffer();
                const view = new Uint8Array(ab);
                const parts = [];
                for (let i = 0; i < view.length; i++) parts.push(view[i]);
                document.getElementById('out').textContent =
                    view.byteLength + ':' + parts.join(',');
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("4:0,255,127,128".to_string()));
}

#[tokio::test]
async fn fetch_unknown_blob_url_throws() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                try {
                    await fetch('blob:null/fake-nonexistent-uuid-1234');
                    document.getElementById('out').textContent = 'unexpected-ok';
                } catch (e) {
                    document.getElementById('out').textContent = 'threw:' + e.name;
                }
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    // Network error → TypeError per Fetch spec.
    let out = page.text_of("#out").unwrap_or_default();
    assert!(
        out.starts_with("threw:TypeError"),
        "expected TypeError, got {out}"
    );
}

// ============================================================================
// A2 — structuredClone
// ============================================================================

#[tokio::test]
async fn structured_clone_is_global_function() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            document.getElementById('out').textContent = typeof structuredClone;
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("function".to_string()));
}

#[tokio::test]
async fn structured_clone_primitives_and_date() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const d = new Date(1234567890000);
            const clone = structuredClone({
                n: 42,
                s: 'hello',
                b: true,
                nil: null,
                d: d,
                big: 9007199254740993n,
            });
            const parts = [];
            parts.push(clone.n === 42);
            parts.push(clone.s === 'hello');
            parts.push(clone.b === true);
            parts.push(clone.nil === null);
            parts.push(clone.d instanceof Date);
            parts.push(clone.d.getTime() === d.getTime());
            parts.push(clone.d !== d);
            parts.push(clone.big === 9007199254740993n);
            document.getElementById('out').textContent = parts.join(',');
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true,true,true,true,true,true,true,true".to_string())
    );
}

#[tokio::test]
async fn structured_clone_typed_array_survives() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const src = new Uint8Array([1, 2, 3, 255]);
            const clone = structuredClone(src);
            const isUint8 = clone instanceof Uint8Array;
            const sameValues = clone.length === 4 &&
                clone[0] === 1 && clone[3] === 255;
            // Mutating the clone must not affect the source.
            clone[0] = 99;
            const independent = src[0] === 1;
            document.getElementById('out').textContent =
                isUint8 + ',' + sameValues + ',' + independent;
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("true,true,true".to_string()));
}

#[tokio::test]
async fn structured_clone_map_set_cycle() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const m = new Map();
            m.set('a', 1);
            m.set('b', [1, 2, 3]);
            const s = new Set(['x', 'y', 'z']);
            // Cycle: object that references itself through a nested array.
            const cyclic = { name: 'root' };
            cyclic.self = cyclic;
            cyclic.ring = [cyclic];
            const bundle = { m, s, cyclic };
            const clone = structuredClone(bundle);
            const parts = [];
            parts.push(clone.m instanceof Map);
            parts.push(clone.m.get('a') === 1);
            parts.push(JSON.stringify(clone.m.get('b')) === '[1,2,3]');
            parts.push(clone.s instanceof Set);
            parts.push(clone.s.size === 3);
            parts.push(clone.s.has('y'));
            // Cycle preserved: clone.self points back at clone.
            parts.push(clone.cyclic.self === clone.cyclic);
            parts.push(clone.cyclic.ring[0] === clone.cyclic);
            // And NOT at the original.
            parts.push(clone.cyclic !== cyclic);
            document.getElementById('out').textContent = parts.join(',');
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true,true,true,true,true,true,true,true,true".to_string())
    );
}

#[tokio::test]
async fn structured_clone_regexp_preserves_source_and_flags() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const r = /foo.*bar/gim;
            const clone = structuredClone(r);
            document.getElementById('out').textContent =
                (clone instanceof RegExp) + ',' +
                clone.source + ',' + clone.flags;
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    let out = page.text_of("#out").unwrap_or_default();
    assert!(out.starts_with("true,foo.*bar,"));
    // Flags should contain g, i, m in some order.
    assert!(out.contains('g') && out.contains('i') && out.contains('m'));
}

#[tokio::test]
async fn structured_clone_function_throws() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            try {
                structuredClone({ fn: function() {} });
                document.getElementById('out').textContent = 'no-throw';
            } catch (e) {
                document.getElementById('out').textContent = 'threw:' + e.name;
            }
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    let out = page.text_of("#out").unwrap_or_default();
    assert!(
        out.starts_with("threw:DataCloneError"),
        "expected DataCloneError, got {out}"
    );
}

#[tokio::test]
async fn structured_clone_symbol_throws() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            try {
                structuredClone(Symbol('nope'));
                document.getElementById('out').textContent = 'no-throw';
            } catch (e) {
                document.getElementById('out').textContent = 'threw:' + e.name;
            }
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    let out = page.text_of("#out").unwrap_or_default();
    assert!(out.starts_with("threw:DataCloneError"), "got {out}");
}

#[tokio::test]
async fn structured_clone_array_buffer_copy() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const buf = new ArrayBuffer(8);
            new Uint8Array(buf).set([10, 20, 30, 40, 50, 60, 70, 80]);
            const clone = structuredClone(buf);
            const view = new Uint8Array(clone);
            const matches = view[0] === 10 && view[7] === 80;
            // Mutate original via a view — clone must be untouched.
            new Uint8Array(buf)[0] = 0;
            const independent = view[0] === 10;
            document.getElementById('out').textContent =
                (clone instanceof ArrayBuffer) + ',' +
                (clone !== buf) + ',' +
                matches + ',' + independent;
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true,true,true,true".to_string())
    );
}

// ============================================================================
// A4 — Streams (ReadableStream / WritableStream / TransformStream)
// ============================================================================

#[tokio::test]
async fn streams_classes_exist() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            document.getElementById('out').textContent =
                (typeof ReadableStream) + '/' +
                (typeof WritableStream) + '/' +
                (typeof TransformStream);
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("function/function/function".to_string())
    );
}

#[tokio::test]
async fn readable_stream_single_chunk_read() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const s = new ReadableStream({
                    start(c) {
                        c.enqueue('hello');
                        c.close();
                    },
                });
                const r = s.getReader();
                const first = await r.read();
                const second = await r.read();
                document.getElementById('out').textContent =
                    first.done + ':' + first.value + '|' +
                    second.done + ':' + (second.value === undefined);
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("false:hello|true:true".to_string())
    );
}

#[tokio::test]
async fn readable_stream_multi_chunk_pull() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                let n = 0;
                const s = new ReadableStream({
                    pull(c) {
                        if (n >= 3) { c.close(); return; }
                        c.enqueue('chunk-' + n);
                        n++;
                    },
                });
                const r = s.getReader();
                const chunks = [];
                while (true) {
                    const { done, value } = await r.read();
                    if (done) break;
                    chunks.push(value);
                }
                document.getElementById('out').textContent = chunks.join(',');
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("chunk-0,chunk-1,chunk-2".to_string())
    );
}

#[tokio::test]
async fn response_body_is_readable_stream() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const blob = new Blob(['body-via-stream']);
                const url = URL.createObjectURL(blob);
                const resp = await fetch(url);
                const isStream = resp.body instanceof ReadableStream;
                const r = resp.body.getReader();
                const { value, done } = await r.read();
                const decoder = new TextDecoder();
                document.getElementById('out').textContent =
                    isStream + '/' + decoder.decode(value) + '/' + done;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true/body-via-stream/false".to_string())
    );
}

#[tokio::test]
async fn readable_stream_tee_branches_independently() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const s = new ReadableStream({
                    start(c) {
                        c.enqueue('A');
                        c.enqueue('B');
                        c.close();
                    },
                });
                const [a, b] = s.tee();
                const ra = a.getReader();
                const rb = b.getReader();
                const collectA = async () => {
                    const out = [];
                    while (true) {
                        const { done, value } = await ra.read();
                        if (done) break;
                        out.push(value);
                    }
                    return out.join('+');
                };
                const collectB = async () => {
                    const out = [];
                    while (true) {
                        const { done, value } = await rb.read();
                        if (done) break;
                        out.push(value);
                    }
                    return out.join('+');
                };
                const [resA, resB] = await Promise.all([collectA(), collectB()]);
                document.getElementById('out').textContent = resA + '|' + resB;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("A+B|A+B".to_string()));
}

#[tokio::test]
async fn writable_stream_accepts_writes() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const collected = [];
                const ws = new WritableStream({
                    write(chunk) { collected.push(chunk); },
                    close() { collected.push('CLOSED'); },
                });
                const w = ws.getWriter();
                await w.write('x');
                await w.write('y');
                await w.close();
                document.getElementById('out').textContent = collected.join(',');
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("x,y,CLOSED".to_string()));
}

#[tokio::test]
async fn transform_stream_pipes_through() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                // Source: emits 1, 2, 3.
                const src = new ReadableStream({
                    start(c) { c.enqueue(1); c.enqueue(2); c.enqueue(3); c.close(); },
                });
                // Transform: doubles each chunk.
                const doubler = new TransformStream({
                    transform(chunk, c) { c.enqueue(chunk * 2); },
                });
                const out = src.pipeThrough(doubler);
                const r = out.getReader();
                const vals = [];
                while (true) {
                    const { done, value } = await r.read();
                    if (done) break;
                    vals.push(value);
                }
                document.getElementById('out').textContent = vals.join(',');
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("2,4,6".to_string()));
}

// ============================================================================
// B4 — Proxy-backed DOM prototypes
// ============================================================================

#[tokio::test]
async fn canvas_methods_are_own_properties_of_prototype() {
    // Real Chrome puts getContext/toDataURL/toBlob on
    // HTMLCanvasElement.prototype directly, not on Element.prototype.
    // A probe that reads the descriptors must see them as own props.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const proto = HTMLCanvasElement.prototype;
            const parts = [];
            for (const name of ['getContext', 'toDataURL', 'toBlob']) {
                const d = Object.getOwnPropertyDescriptor(proto, name);
                parts.push(name + ':' + (d ? 'own' : 'inherited'));
            }
            document.getElementById('out').textContent = parts.join(',');
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("getContext:own,toDataURL:own,toBlob:own".to_string())
    );
}

#[tokio::test]
async fn canvas_get_context_illegal_invocation() {
    // Calling HTMLCanvasElement.prototype.getContext with a non-canvas
    // receiver must throw `TypeError: Illegal invocation` — this is
    // the fingerprint-leak the old Element-prototype patch had: a
    // probe would see getContext accept any element.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const getContext = HTMLCanvasElement.prototype.getContext;
            const fake = {};
            try {
                getContext.call(fake, '2d');
                document.getElementById('out').textContent = 'no-throw';
            } catch (e) {
                document.getElementById('out').textContent =
                    e.name + '|' + (e.message.includes('Illegal invocation') ? 'ok' : e.message);
            }
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("TypeError|ok".to_string()));
}

#[tokio::test]
async fn canvas_to_data_url_illegal_invocation() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const toDataURL = HTMLCanvasElement.prototype.toDataURL;
            try {
                toDataURL.call({ tagName: 'DIV' });
                document.getElementById('out').textContent = 'no-throw';
            } catch (e) {
                document.getElementById('out').textContent = e.name;
            }
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("TypeError".to_string()));
}

#[tokio::test]
async fn canvas_methods_still_work_on_real_canvas() {
    // Regression: after the proxy move, the normal canvas-on-element
    // path must still work for HTML-parsed canvases.
    let mut page = Page::from_html(
        r#"<html><body>
            <canvas id="c" width="50" height="50"></canvas>
            <div id="out"></div>
            <script>
                const c = document.getElementById('c');
                const ctx = c.getContext('2d');
                ctx.fillStyle = 'magenta';
                ctx.fillRect(0, 0, 50, 50);
                const url = c.toDataURL();
                document.getElementById('out').textContent =
                    (ctx !== null) + '/' + url.startsWith('data:image/png;base64,');
            </script>
        </body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("true/true".to_string()));
}

// ============================================================================
// B3 — OffscreenCanvas (main thread + workers)
// ============================================================================

#[tokio::test]
async fn offscreen_canvas_main_thread_is_functional() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            const oc = new OffscreenCanvas(100, 50);
            const ctx = oc.getContext('2d');
            const ctxIsCtx = ctx !== null && typeof ctx.fillRect === 'function';
            ctx.fillStyle = 'red';
            ctx.fillRect(0, 0, 10, 10);
            // measureText should return real values from T1.2 font stack.
            ctx.font = '16px Arial';
            const m = ctx.measureText('Hello');
            const widthOk = m.width > 0;
            document.getElementById('out').textContent =
                ctxIsCtx + '/' + widthOk;
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("true/true".to_string()));
}

#[tokio::test]
async fn offscreen_canvas_convert_to_blob_png() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const oc = new OffscreenCanvas(20, 20);
                const ctx = oc.getContext('2d');
                ctx.fillStyle = '#00ff00';
                ctx.fillRect(0, 0, 20, 20);
                const blob = await oc.convertToBlob({ type: 'image/png' });
                const ab = await blob.arrayBuffer();
                const bytes = new Uint8Array(ab);
                // PNG magic: 89 50 4E 47
                const magic = bytes[0] === 0x89 && bytes[1] === 0x50 &&
                    bytes[2] === 0x4e && bytes[3] === 0x47;
                document.getElementById('out').textContent =
                    magic + '/' + blob.type + '/' + (bytes.byteLength > 30);
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true/image/png/true".to_string())
    );
}

#[tokio::test]
async fn offscreen_canvas_in_worker() {
    // Workers now run canvas_bootstrap too — `new OffscreenCanvas(w,h)
    // .getContext('2d')` should return a real CanvasRenderingContext2D
    // in the worker scope.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    self.onmessage = () => {
                        const hasOC = typeof OffscreenCanvas === 'function';
                        if (!hasOC) { self.postMessage('no-offscreen'); return; }
                        const oc = new OffscreenCanvas(40, 40);
                        const ctx = oc.getContext('2d');
                        if (!ctx) { self.postMessage('no-context'); return; }
                        ctx.fillStyle = 'blue';
                        ctx.fillRect(0, 0, 40, 40);
                        const img = ctx.getImageData(5, 5, 1, 1);
                        const px = img.data;
                        self.postMessage(
                            px[0] + ',' + px[1] + ',' + px[2] + ',' + px[3]
                        );
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                const result = await new Promise((r) => {
                    w.onmessage = (e) => r(e.data);
                    setTimeout(() => w.postMessage('go'), 100);
                });
                w.terminate();
                document.getElementById('out').textContent = result;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    // Blue at (5,5) = (0, 0, 255, 255) from our skia-safe canvas.
    assert_eq!(page.text_of("#out"), Some("0,0,255,255".to_string()));
}

// ============================================================================
// B2 — Worker transferables + binary-safe postMessage
// ============================================================================

#[tokio::test]
async fn worker_post_message_typed_array_round_trip() {
    // Previously, JSON serialization would silently drop TypedArrays
    // in Worker.postMessage (receiver got `{}`). With the wire
    // serializer they survive round-trip with byte-exact content.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    self.onmessage = (e) => {
                        const u = e.data.u8;
                        const isU8 = u instanceof Uint8Array;
                        const bytes = isU8
                            ? Array.from(u).join(',')
                            : 'NOT_U8:' + Object.prototype.toString.call(u);
                        self.postMessage({ isU8, bytes });
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                const sendBytes = new Uint8Array([1, 2, 3, 250, 255]);
                const result = await new Promise((r) => {
                    w.onmessage = (e) => r(e.data);
                    setTimeout(() => w.postMessage({ u8: sendBytes }), 100);
                });
                w.terminate();
                document.getElementById('out').textContent =
                    result.isU8 + '|' + result.bytes;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("true|1,2,3,250,255".to_string()));
}

#[tokio::test]
async fn worker_post_message_arraybuffer_round_trip() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    self.onmessage = (e) => {
                        const buf = e.data;
                        const isAB = buf instanceof ArrayBuffer;
                        const byteLen = isAB ? buf.byteLength : -1;
                        const view = isAB ? new Uint8Array(buf) : null;
                        const first = view ? view[0] : -1;
                        const last = view ? view[view.length - 1] : -1;
                        self.postMessage({ isAB, byteLen, first, last });
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                const buf = new ArrayBuffer(4);
                new Uint8Array(buf).set([0x10, 0x20, 0x30, 0x40]);
                const result = await new Promise((r) => {
                    w.onmessage = (e) => r(e.data);
                    setTimeout(() => w.postMessage(buf, [buf]), 100);
                });
                w.terminate();
                document.getElementById('out').textContent =
                    result.isAB + '|' + result.byteLen + '|' +
                    result.first + '|' + result.last;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("true|4|16|64".to_string()));
}

#[tokio::test]
async fn worker_post_message_map_set_date_survive() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    self.onmessage = (e) => {
                        const {m, s, d, re} = e.data;
                        const parts = [];
                        parts.push(m instanceof Map);
                        parts.push(m.get('k') === 42);
                        parts.push(s instanceof Set);
                        parts.push(s.has('x'));
                        parts.push(d instanceof Date);
                        parts.push(d.getTime() === 1000);
                        parts.push(re instanceof RegExp);
                        parts.push(re.source === 'foo');
                        self.postMessage(parts.join(','));
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                const m = new Map([['k', 42]]);
                const s = new Set(['x', 'y']);
                const d = new Date(1000);
                const re = /foo/gi;
                const result = await new Promise((r) => {
                    w.onmessage = (e) => r(e.data);
                    setTimeout(() => w.postMessage({m, s, d, re}), 100);
                });
                w.terminate();
                document.getElementById('out').textContent = result;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(
        page.text_of("#out"),
        Some("true,true,true,true,true,true,true,true".to_string())
    );
}

#[tokio::test]
async fn worker_post_message_transferable_list_accepted() {
    // The transferables list must be accepted as an array of
    // ArrayBuffers/views without throwing. (We don't actually detach
    // the source — that requires V8 internals — but the shape check
    // that fingerprint probes do for `postMessage(buf, [buf])` passes.)
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    self.onmessage = () => self.postMessage('received');
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                const buf = new ArrayBuffer(8);
                try {
                    w.postMessage({ buf }, [buf]);
                    const ok = await new Promise((r) => { w.onmessage = e => r(e.data); });
                    document.getElementById('out').textContent = 'ok:' + ok;
                } catch (e) {
                    document.getElementById('out').textContent = 'threw:' + e.message;
                }
                w.terminate();
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("ok:received".to_string()));
}

#[tokio::test]
async fn worker_post_message_rejects_non_transferable() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `self.onmessage = () => self.postMessage('x');`;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url);
                try {
                    w.postMessage('hi', [{}]);
                    document.getElementById('out').textContent = 'no-throw';
                } catch (e) {
                    document.getElementById('out').textContent = 'threw:' + e.name;
                }
                w.terminate();
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("threw:TypeError".to_string()));
}

// ============================================================================
// B1 — Module workers
// ============================================================================

#[tokio::test]
async fn module_worker_option_accepted() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                // Sanity: a module-type worker body with no imports
                // should run the same way as a classic worker and be
                // able to post a message back.
                const src = `
                    self.onmessage = function(e) {
                        self.postMessage('module-says:' + e.data);
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url, { type: 'module' });
                const result = await new Promise((resolve) => {
                    w.onmessage = (e) => resolve(e.data);
                    setTimeout(() => w.postMessage('hi'), 100);
                });
                w.terminate();
                document.getElementById('out').textContent = result;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("module-says:hi".to_string()));
}

#[tokio::test]
async fn module_worker_import_meta_url_available() {
    // Module workers expose `import.meta` — a classic worker does not.
    // Even if the URL value is our synthetic worker-oxide scheme, the
    // presence of import.meta (without a ReferenceError) proves the
    // body was loaded in module mode.
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = `
                    const hasImportMeta = typeof import.meta === 'object';
                    self.onmessage = () => {
                        self.postMessage(hasImportMeta ? 'module' : 'not-module');
                    };
                `;
                const url = URL.createObjectURL(new Blob([src]));
                const w = new Worker(url, { type: 'module' });
                const result = await new Promise((resolve) => {
                    w.onmessage = (e) => resolve(e.data);
                    setTimeout(() => w.postMessage('q'), 100);
                });
                w.terminate();
                document.getElementById('out').textContent = result;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("module".to_string()));
}

// ============================================================================
// A5 — IndexedDB
// ============================================================================

#[tokio::test]
async fn indexeddb_open_put_get_round_trip() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const req = indexedDB.open('test-db', 1);
                req.onupgradeneeded = () => {
                    req.result.createObjectStore('items', { keyPath: 'id' });
                };
                await new Promise((resolve, reject) => {
                    req.onsuccess = resolve;
                    req.onerror = reject;
                });
                const db = req.result;
                const tx = db.transaction('items', 'readwrite');
                const store = tx.objectStore('items');
                store.put({ id: 1, name: 'alpha' });
                store.put({ id: 2, name: 'beta' });
                await new Promise((resolve) => { tx.oncomplete = resolve; });
                const tx2 = db.transaction('items', 'readonly');
                const getReq = tx2.objectStore('items').get(1);
                await new Promise((r) => { getReq.onsuccess = r; });
                document.getElementById('out').textContent =
                    getReq.result.id + '/' + getReq.result.name;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("1/alpha".to_string()));
}

#[tokio::test]
async fn indexeddb_deep_clone_isolates_stored_values() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const req = indexedDB.open('isolation-db', 1);
                req.onupgradeneeded = () => {
                    req.result.createObjectStore('s', { keyPath: 'id' });
                };
                await new Promise((r) => { req.onsuccess = r; });
                const db = req.result;
                const obj = { id: 1, nested: { value: 'original' } };
                const tx = db.transaction('s', 'readwrite');
                tx.objectStore('s').put(obj);
                await new Promise((r) => { tx.oncomplete = r; });
                // Mutate the source AFTER put — the stored copy must
                // NOT reflect the mutation.
                obj.nested.value = 'mutated';
                const getReq = db.transaction('s').objectStore('s').get(1);
                await new Promise((r) => { getReq.onsuccess = r; });
                const stored = getReq.result.nested.value;
                // And mutating the retrieved value must NOT affect
                // the store (second get returns original).
                getReq.result.nested.value = 'leaked';
                const getReq2 = db.transaction('s').objectStore('s').get(1);
                await new Promise((r) => { getReq2.onsuccess = r; });
                document.getElementById('out').textContent =
                    stored + '/' + getReq2.result.nested.value;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("original/original".to_string()));
}

#[tokio::test]
async fn indexeddb_key_range_query() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const req = indexedDB.open('range-db', 1);
                req.onupgradeneeded = () => {
                    req.result.createObjectStore('n', { keyPath: 'i' });
                };
                await new Promise((r) => { req.onsuccess = r; });
                const db = req.result;
                const tx = db.transaction('n', 'readwrite');
                const store = tx.objectStore('n');
                for (let i = 1; i <= 5; i++) store.put({ i });
                await new Promise((r) => { tx.oncomplete = r; });
                const range = IDBKeyRange.bound(2, 4);
                const getAllReq = db.transaction('n').objectStore('n').getAll(range);
                await new Promise((r) => { getAllReq.onsuccess = r; });
                const ids = getAllReq.result.map((o) => o.i).join(',');
                document.getElementById('out').textContent = ids;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("2,3,4".to_string()));
}

#[tokio::test]
async fn indexeddb_cursor_iterates_in_key_order() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const req = indexedDB.open('cursor-db', 1);
                req.onupgradeneeded = () => {
                    req.result.createObjectStore('c', { keyPath: 'k' });
                };
                await new Promise((r) => { req.onsuccess = r; });
                const db = req.result;
                const tx = db.transaction('c', 'readwrite');
                const store = tx.objectStore('c');
                // Insert out of order — cursor should still come back
                // in sorted key order.
                store.put({ k: 3 });
                store.put({ k: 1 });
                store.put({ k: 2 });
                await new Promise((r) => { tx.oncomplete = r; });
                const curReq = db.transaction('c').objectStore('c').openCursor();
                const keys = [];
                await new Promise((resolve) => {
                    curReq.onsuccess = () => {
                        const cur = curReq.result;
                        if (!cur) { resolve(); return; }
                        keys.push(cur.key);
                        cur.continue();
                    };
                });
                document.getElementById('out').textContent = keys.join(',');
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("1,2,3".to_string()));
}

#[tokio::test]
async fn indexeddb_version_upgrade_flow() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                // Open v1: create 'old' store.
                const r1 = indexedDB.open('upgrade-db', 1);
                r1.onupgradeneeded = () => {
                    r1.result.createObjectStore('old');
                };
                await new Promise((r) => { r1.onsuccess = r; });
                r1.result.close();
                // Reopen at v2: onupgradeneeded should fire with
                // oldVersion=1 newVersion=2.
                let upgradeEvent = null;
                const r2 = indexedDB.open('upgrade-db', 2);
                r2.onupgradeneeded = (e) => {
                    upgradeEvent = { old: e.oldVersion, new_: e.newVersion };
                    r2.result.createObjectStore('new');
                };
                await new Promise((r) => { r2.onsuccess = r; });
                const db = r2.result;
                const stores = [...db.objectStoreNames].sort().join(',');
                document.getElementById('out').textContent =
                    upgradeEvent.old + '->' + upgradeEvent.new_ + '/' + stores;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("1->2/new,old".to_string()));
}

// ============================================================================
// A3 — Worker importScripts()
// ============================================================================

#[tokio::test]
async fn worker_import_scripts_from_blob_url() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const importSrc = `
                    self._imported_value = 123;
                    self.onmessage = function(e) {
                        self.postMessage({ got: e.data, imported: self._imported_value });
                    };
                `;
                const importURL = URL.createObjectURL(new Blob([importSrc]));

                const workerSrc = `
                    importScripts(${JSON.stringify(importURL)});
                `;
                const workerURL = URL.createObjectURL(new Blob([workerSrc]));

                const w = new Worker(workerURL);
                const result = await new Promise((resolve) => {
                    w.onmessage = (e) => resolve(e.data);
                    setTimeout(() => w.postMessage('ping'), 100);
                });
                w.terminate();
                document.getElementById('out').textContent =
                    result.got + '/' + result.imported;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("ping/123".to_string()));
}

#[tokio::test]
async fn worker_import_scripts_from_data_url() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const src = btoa("self._data_imported = true;");
                const dataURL = "data:application/javascript;base64," + src;

                const workerSrc = `
                    importScripts(${JSON.stringify(dataURL)});
                    self.postMessage(self._data_imported === true ? 'yes' : 'no');
                `;
                const workerURL = URL.createObjectURL(new Blob([workerSrc]));

                const w = new Worker(workerURL);
                const result = await new Promise((resolve) => {
                    w.onmessage = (e) => resolve(e.data);
                });
                w.terminate();
                document.getElementById('out').textContent = result;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("yes".to_string()));
}

#[tokio::test]
async fn worker_import_scripts_unknown_blob_throws() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const workerSrc = `
                    try {
                        importScripts('blob:null/does-not-exist-uuid');
                        self.postMessage('no-throw');
                    } catch (e) {
                        self.postMessage('threw:' + e.message);
                    }
                `;
                const url = URL.createObjectURL(new Blob([workerSrc]));
                const w = new Worker(url);
                const msg = await new Promise((res) => { w.onmessage = e => res(e.data); });
                w.terminate();
                document.getElementById('out').textContent = msg;
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    let out = page.text_of("#out").unwrap_or_default();
    assert!(out.starts_with("threw:"), "got {out}");
}

#[tokio::test]
async fn blob_revoke_invalidates_url() {
    let mut page = Page::from_html(
        r#"<html><body><div id="out"></div><script>
            (async () => {
                const url = URL.createObjectURL(new Blob(['revoked']));
                URL.revokeObjectURL(url);
                try {
                    await fetch(url);
                    document.getElementById('out').textContent = 'fetched';
                } catch (e) {
                    document.getElementById('out').textContent = 'error';
                }
            })();
        </script></body></html>"#,
        None::<browser_oxide::stealth::StealthProfile>,
    )
    .await
    .unwrap();
    assert_eq!(page.text_of("#out"), Some("error".to_string()));
}