ferridriver 0.3.0

Browser automation in Rust with a Playwright-compatible API. Four pluggable backends: CDP pipe, CDP WebSocket, Playwright WebKit, Firefox BiDi.
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
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
//! Playwright's utility-script tagged-union wire serializer (isomorphic variant).
//!
//! Exact Rust mirror of
//! `/tmp/playwright/packages/injected/src/utilityScriptSerializers.ts`
//! (which our injected engine ships verbatim under
//! `crates/ferridriver/src/injected/isomorphic/utilityScriptSerializers.ts`).
//! Every variant, tag letter, and wire shape matches Playwright byte-for-byte
//! so the round-trip `Rust serialize → CDP Runtime.callFunctionOn CallArgument
//! → utilityScript.evaluate → parseEvaluationResultValue → user fn(arg)` lines
//! up without a translation step.
//!
//! **Why the "isomorphic" variant and not the "channels" variant.** Playwright
//! has two wire formats: the *channels* format
//! (`playwright-core/src/protocol/serializers.ts`), with every primitive
//! wrapped as `{n: 42}` / `{b: true}` / `{s: "hi"}`, used only on the
//! Playwright client↔server WebSocket RPC; and the *isomorphic* format
//! (utilityScriptSerializers.ts) where primitives pass through raw and only
//! rich types are tagged, used on the server↔page bridge over CDP / `BiDi` /
//! `WebKit`. ferridriver has no client↔server RPC — we *are* the server — so
//! the only format we need is the isomorphic one the page's injected utility
//! script parses. Sending channels-shape primitives (`{n: 42}`) through a
//! CDP `CallArgument.value` would leave the page seeing a literal
//! `{n: 42}` JS object instead of the number 42.
//!
//! ## Wire shape
//!
//! | JS value           | Wire encoding                     |
//! |--------------------|-----------------------------------|
//! | `true` / `false`   | raw `true` / `false`              |
//! | finite `number`    | raw number (`42`, `3.14`)         |
//! | `string`           | raw JSON string                   |
//! | `null`             | `{v: "null"}`                     |
//! | `undefined`        | `{v: "undefined"}`                |
//! | `NaN`              | `{v: "NaN"}`                      |
//! | `Infinity`         | `{v: "Infinity"}`                 |
//! | `-Infinity`        | `{v: "-Infinity"}`                |
//! | `-0`               | `{v: "-0"}`                       |
//! | `Date`             | `{d: "<toJSON()>"}`               |
//! | `URL`              | `{u: "<toJSON()>"}`               |
//! | `BigInt`           | `{bi: "<toString()>"}`            |
//! | `Error`            | `{e: {m, n, s}}`                  |
//! | `RegExp`           | `{r: {p, f}}`                     |
//! | `TypedArray`       | `{ta: {b: <base64>, k: <kind>}}`  |
//! | `ArrayBuffer`      | `{ab: {b: <base64>}}`             |
//! | `Array`            | `{a: [...], id: <n>}`             |
//! | `Object` (plain)   | `{o: [{k,v}, ...], id: <n>}`      |
//! | `JSHandle` ref     | `{h: <index>}`                    |
//! | shared-subgraph    | `{ref: <id>}`                     |
//!
//! `Map` / `Set` are NOT distinct variants — Playwright walks them as plain
//! objects via `Object.keys`, matching browser iteration order.
//!
//! ## Dedup / cycles
//!
//! Arrays and objects carry a unique `id`; a second visit to the same
//! JS object (pointer-equal) is encoded as `{ref: <id>}` instead, which
//! terminates cycles and deduplicates shared subgraphs. For pure-JSON input
//! (no shared structure) every collection still gets a fresh id — the
//! deserializer ignores unreferenced ids harmlessly.

use base64::Engine;
use serde::de::{self, MapAccess, Visitor};
use serde::ser::SerializeMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

// ── SpecialValue ────────────────────────────────────────────────────────────

/// JS values that have no faithful JSON representation but still exist at
/// runtime — `undefined`, `NaN`, `±Infinity`, negative zero distinct from
/// positive zero, and `null` (Playwright wraps `null` the same way so the
/// deserializer can distinguish an intentional `null` from a missing field).
/// Encoded under the `v` tag with the exact string literal Playwright
/// emits (case matters: `NaN`, not `nan`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SpecialValue {
  #[serde(rename = "null")]
  Null,
  #[serde(rename = "undefined")]
  Undefined,
  #[serde(rename = "NaN")]
  NaN,
  #[serde(rename = "Infinity")]
  Infinity,
  #[serde(rename = "-Infinity")]
  NegInfinity,
  #[serde(rename = "-0")]
  NegZero,
}

// ── RegExpValue ─────────────────────────────────────────────────────────────

/// Wire shape of a `RegExp`: pattern + flags, matching
/// `RegExp.prototype.source` / `RegExp.prototype.flags` and the JS
/// constructor invariant `new RegExp(p, f)`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RegExpValue {
  /// Pattern. Corresponds to `RegExp.prototype.source`.
  pub p: String,
  /// Flags (e.g. `"gi"`). Corresponds to `RegExp.prototype.flags`.
  pub f: String,
}

// ── ErrorValue ──────────────────────────────────────────────────────────────

/// Wire shape of an `Error` — message + name + stack. Playwright's
/// serializer always emits all three fields (empty string for stack if
/// absent on the source) so the deserializer can unconditionally assign.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ErrorValue {
  /// `Error.prototype.message`.
  pub m: String,
  /// `Error.prototype.name` — typically `"Error"` / `"TypeError"` /
  /// `"RangeError"`, etc.
  pub n: String,
  /// `Error.prototype.stack`. May be an empty string if the source had no
  /// stack.
  pub s: String,
}

// ── TypedArrayKind ──────────────────────────────────────────────────────────

/// Enumeration of the `TypedArray` subclasses the serializer supports.
/// Encoded as the short string Playwright uses on the wire (e.g. `"i8"`
/// for `Int8Array`). Matches
/// `/tmp/playwright/packages/injected/src/utilityScriptSerializers.ts:90`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TypedArrayKind {
  #[serde(rename = "i8")]
  I8,
  #[serde(rename = "ui8")]
  U8,
  #[serde(rename = "ui8c")]
  U8Clamped,
  #[serde(rename = "i16")]
  I16,
  #[serde(rename = "ui16")]
  U16,
  #[serde(rename = "i32")]
  I32,
  #[serde(rename = "ui32")]
  U32,
  #[serde(rename = "f32")]
  F32,
  #[serde(rename = "f64")]
  F64,
  #[serde(rename = "bi64")]
  BI64,
  #[serde(rename = "bui64")]
  BUI64,
}

impl TypedArrayKind {
  /// The byte stride of a single element in this typed array.
  ///
  /// Matches `TypedArray.BYTES_PER_ELEMENT` for the JS constructor the
  /// kind names.
  #[must_use]
  pub fn bytes_per_element(self) -> usize {
    match self {
      Self::I8 | Self::U8 | Self::U8Clamped => 1,
      Self::I16 | Self::U16 => 2,
      Self::I32 | Self::U32 | Self::F32 => 4,
      Self::F64 | Self::BI64 | Self::BUI64 => 8,
    }
  }
}

// ── TypedArrayValue ─────────────────────────────────────────────────────────

/// Wire shape of a `TypedArray`: the raw byte buffer plus a kind tag. Bytes
/// are base64-encoded on the JSON wire (matching Playwright's `btoa` /
/// `typedArrayToBase64` helper) and a `Vec<u8>` in Rust.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypedArrayValue {
  /// Underlying bytes, base64-encoded on the JSON wire.
  pub b: Vec<u8>,
  /// Typed-array constructor tag.
  pub k: TypedArrayKind,
}

#[derive(Serialize, Deserialize)]
struct TypedArrayWire<'a> {
  b: &'a str,
  k: TypedArrayKind,
}

#[derive(Deserialize)]
struct TypedArrayWireOwned {
  b: String,
  k: TypedArrayKind,
}

impl Serialize for TypedArrayValue {
  fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
    let encoded = base64::engine::general_purpose::STANDARD.encode(&self.b);
    let wire = TypedArrayWire { b: &encoded, k: self.k };
    wire.serialize(s)
  }
}

impl<'de> Deserialize<'de> for TypedArrayValue {
  fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
    let wire = TypedArrayWireOwned::deserialize(d)?;
    let b = base64::engine::general_purpose::STANDARD
      .decode(&wire.b)
      .map_err(de::Error::custom)?;
    Ok(Self { b, k: wire.k })
  }
}

// ── ArrayBufferValue ────────────────────────────────────────────────────────

/// Wire shape of a plain `ArrayBuffer` (no kind tag; it's just bytes).
/// Serialized as `{ab: {b: <base64>}}` matching Playwright's isomorphic
/// serializer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArrayBufferValue {
  /// Underlying bytes.
  pub b: Vec<u8>,
}

#[derive(Serialize, Deserialize)]
struct ArrayBufferWire<'a> {
  b: &'a str,
}

#[derive(Deserialize)]
struct ArrayBufferWireOwned {
  b: String,
}

impl Serialize for ArrayBufferValue {
  fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
    let encoded = base64::engine::general_purpose::STANDARD.encode(&self.b);
    let wire = ArrayBufferWire { b: &encoded };
    wire.serialize(s)
  }
}

impl<'de> Deserialize<'de> for ArrayBufferValue {
  fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
    let wire = ArrayBufferWireOwned::deserialize(d)?;
    let b = base64::engine::general_purpose::STANDARD
      .decode(&wire.b)
      .map_err(de::Error::custom)?;
    Ok(Self { b })
  }
}

// ── PropertyEntry ───────────────────────────────────────────────────────────

/// A single `{ k, v }` entry inside a serialized `Object` (`o`).
/// Playwright walks `Object.keys(value)` (own enumerable string-keyed
/// properties in insertion order), so entry ordering is significant and
/// we model it as an ordered `Vec`, not a map.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PropertyEntry {
  /// Property key. Always a string on the wire (Symbol keys are
  /// dropped by the JS serializer, matching `Object.keys`).
  pub k: String,
  /// Property value.
  pub v: SerializedValue,
}

// ── SerializedValue ─────────────────────────────────────────────────────────

/// The wire tagged union. Primitives (`bool` / `f64` / `String`) pass
/// through as raw JSON primitives; rich types ride inside a single-key
/// JSON object matching the [wire-shape](self#wire-shape) table.
#[derive(Debug, Clone, PartialEq)]
pub enum SerializedValue {
  /// `true` / `false` — raw JSON boolean.
  Bool(bool),
  /// Finite non-`±0`-special non-`NaN` non-`±Infinity` number.
  /// IEEE-754 specials go through [`Self::Special`].
  Number(f64),
  /// Regular JS `string` — raw JSON string on the wire.
  Str(String),
  /// `null` / `undefined` / `NaN` / `±Infinity` / `-0` — wire shape
  /// `{v: <tag>}`.
  Special(SpecialValue),
  /// `Date` — wire shape `{d: <iso-string>}`.
  Date(String),
  /// `URL` — wire shape `{u: <url>}`.
  Url(String),
  /// `BigInt` — wire shape `{bi: <decimal>}`.
  BigInt(String),
  /// `Error` — wire shape `{e: {m, n, s}}`.
  Error(ErrorValue),
  /// `RegExp` — wire shape `{r: {p, f}}`.
  RegExp(RegExpValue),
  /// `TypedArray` — wire shape `{ta: {b, k}}`.
  TypedArray(TypedArrayValue),
  /// `ArrayBuffer` — wire shape `{ab: {b}}`.
  ArrayBuffer(ArrayBufferValue),
  /// `Array` — wire shape `{a: [...], id}`. `id` enables back-references
  /// from cycles / shared subgraphs.
  Array { id: u32, items: Vec<SerializedValue> },
  /// Plain `Object` — wire shape `{o: [{k, v}, ...], id}`.
  Object { id: u32, entries: Vec<PropertyEntry> },
  /// `JSHandle` reference — wire shape `{h: <index>}` pointing into
  /// [`SerializedArgument::handles`].
  Handle(u32),
  /// Back-reference to a previously-emitted `a` / `o` by its `id`.
  Reference(u32),
}

impl SerializedValue {
  // ── Primitive builders ────────────────────────────────────────────────────

  #[must_use]
  pub fn boolean(b: bool) -> Self {
    Self::Bool(b)
  }

  #[must_use]
  pub fn number(n: f64) -> Self {
    Self::Number(n)
  }

  #[must_use]
  pub fn string(s: impl Into<String>) -> Self {
    Self::Str(s.into())
  }

  #[must_use]
  pub fn special(v: SpecialValue) -> Self {
    Self::Special(v)
  }

  #[must_use]
  pub fn null() -> Self {
    Self::Special(SpecialValue::Null)
  }

  #[must_use]
  pub fn undefined() -> Self {
    Self::Special(SpecialValue::Undefined)
  }

  /// Build a number, auto-routing the IEEE-754 specials through the
  /// `v` tag. `-0.0` is detected via bit-equality with the IEEE-754
  /// negative-zero pattern, matching Playwright's `Object.is(value, -0)`.
  #[must_use]
  pub fn from_f64(n: f64) -> Self {
    if n.is_nan() {
      return Self::Special(SpecialValue::NaN);
    }
    if n == f64::INFINITY {
      return Self::Special(SpecialValue::Infinity);
    }
    if n == f64::NEG_INFINITY {
      return Self::Special(SpecialValue::NegInfinity);
    }
    if n == 0.0 && n.to_bits() == (-0.0_f64).to_bits() {
      return Self::Special(SpecialValue::NegZero);
    }
    Self::Number(n)
  }

  // ── Rich-type builders ────────────────────────────────────────────────────

  #[must_use]
  pub fn date(iso: impl Into<String>) -> Self {
    Self::Date(iso.into())
  }

  #[must_use]
  pub fn url(url: impl Into<String>) -> Self {
    Self::Url(url.into())
  }

  #[must_use]
  pub fn bigint(decimal: impl Into<String>) -> Self {
    Self::BigInt(decimal.into())
  }

  #[must_use]
  pub fn regexp(pattern: impl Into<String>, flags: impl Into<String>) -> Self {
    Self::RegExp(RegExpValue {
      p: pattern.into(),
      f: flags.into(),
    })
  }

  #[must_use]
  pub fn error(name: impl Into<String>, message: impl Into<String>, stack: impl Into<String>) -> Self {
    Self::Error(ErrorValue {
      n: name.into(),
      m: message.into(),
      s: stack.into(),
    })
  }

  #[must_use]
  pub fn typed_array(bytes: Vec<u8>, kind: TypedArrayKind) -> Self {
    Self::TypedArray(TypedArrayValue { b: bytes, k: kind })
  }

  #[must_use]
  pub fn array_buffer(bytes: Vec<u8>) -> Self {
    Self::ArrayBuffer(ArrayBufferValue { b: bytes })
  }

  // ── Collection builders ───────────────────────────────────────────────────

  #[must_use]
  pub fn array(id: u32, items: Vec<SerializedValue>) -> Self {
    Self::Array { id, items }
  }

  #[must_use]
  pub fn object(id: u32, entries: Vec<PropertyEntry>) -> Self {
    Self::Object { id, entries }
  }

  #[must_use]
  pub fn reference(id: u32) -> Self {
    Self::Reference(id)
  }

  #[must_use]
  pub fn handle(handle_index: u32) -> Self {
    Self::Handle(handle_index)
  }

  // ── Conversion helpers ────────────────────────────────────────────────────

  /// Convert a [`serde_json::Value`] into a `SerializedValue`, covering
  /// the JSON subset of JS types: `null` → `{v: null}`, `bool`,
  /// finite `number`, `string`, `Array`, `Object`. `ctx` supplies
  /// fresh ids for each collection. Rich JS types (`undefined` /
  /// `NaN` / `Date` / `RegExp` / `BigInt` / handles) aren't
  /// representable in JSON; callers build those via the explicit
  /// constructors.
  #[must_use]
  pub fn from_json(value: &serde_json::Value, ctx: &mut SerializationContext) -> Self {
    match value {
      serde_json::Value::Null => Self::null(),
      serde_json::Value::Bool(b) => Self::Bool(*b),
      serde_json::Value::Number(num) => num.as_f64().map_or_else(
        // Number outside f64-safe range: fall back to BigInt string
        // so no digits are lost. serde_json can hold u64/i64 that f64
        // can't express exactly.
        || Self::BigInt(num.to_string()),
        Self::from_f64,
      ),
      serde_json::Value::String(s) => Self::Str(s.clone()),
      serde_json::Value::Array(items) => {
        let id = ctx.alloc_id();
        let wire_items = items.iter().map(|v| Self::from_json(v, ctx)).collect();
        Self::Array { id, items: wire_items }
      },
      serde_json::Value::Object(map) => {
        let id = ctx.alloc_id();
        let entries = map
          .iter()
          .map(|(k, v)| PropertyEntry {
            k: k.clone(),
            v: Self::from_json(v, ctx),
          })
          .collect();
        Self::Object { id, entries }
      },
    }
  }

  /// Attempt to convert into a [`serde_json::Value`]. Succeeds for the
  /// JSON-expressible subset (`Bool` / `Number` / `Str` / `Special::Null`
  /// / `Array` / `Object`). Returns `None` for rich types (`undefined` /
  /// `NaN` / `Date` / `RegExp` / `Error` / `TypedArray` / `ArrayBuffer` /
  /// `Handle` / `Reference` / `BigInt`).
  #[must_use]
  pub fn to_json_like(&self) -> Option<serde_json::Value> {
    match self {
      Self::Bool(b) => Some(serde_json::Value::Bool(*b)),
      Self::Number(n) => {
        // Preserve integer form when the value is losslessly
        // representable within f64's safe integer range (±2^53).
        const F64_INT_MAX: f64 = 9_007_199_254_740_992.0;
        if n.is_finite() && n.fract() == 0.0 && n.abs() <= F64_INT_MAX {
          #[allow(clippy::cast_possible_truncation)]
          let as_i64 = *n as i64;
          Some(serde_json::Value::Number(as_i64.into()))
        } else {
          serde_json::Number::from_f64(*n).map(serde_json::Value::Number)
        }
      },
      Self::Str(s) => Some(serde_json::Value::String(s.clone())),
      Self::Special(SpecialValue::Null) => Some(serde_json::Value::Null),
      Self::Array { items, .. } => {
        let mut out = Vec::with_capacity(items.len());
        for v in items {
          out.push(v.to_json_like()?);
        }
        Some(serde_json::Value::Array(out))
      },
      Self::Object { entries, .. } => {
        let mut map = serde_json::Map::with_capacity(entries.len());
        for e in entries {
          map.insert(e.k.clone(), e.v.to_json_like()?);
        }
        Some(serde_json::Value::Object(map))
      },
      // Rich types + non-Null specials have no lossless JSON form.
      Self::Special(_)
      | Self::Date(_)
      | Self::Url(_)
      | Self::BigInt(_)
      | Self::Error(_)
      | Self::RegExp(_)
      | Self::TypedArray(_)
      | Self::ArrayBuffer(_)
      | Self::Handle(_)
      | Self::Reference(_) => None,
    }
  }

  /// Boolean projection for call sites that expect a `Bool` result. Non-bool
  /// values return `None` — callers typically `.unwrap_or(false)`.
  #[must_use]
  pub fn as_bool(&self) -> Option<bool> {
    match self {
      Self::Bool(b) => Some(*b),
      _ => None,
    }
  }

  /// String projection for call sites that expect a `Str` result. Non-string
  /// values return `None`.
  #[must_use]
  pub fn as_str(&self) -> Option<&str> {
    match self {
      Self::Str(s) => Some(s.as_str()),
      _ => None,
    }
  }

  /// Number projection. Returns `None` for non-number tags (including `BigInt`).
  #[must_use]
  pub fn as_number(&self) -> Option<f64> {
    match self {
      Self::Number(n) => Some(*n),
      _ => None,
    }
  }

  /// Array projection — borrows the item slice when this is an `Array`,
  /// `None` otherwise.
  #[must_use]
  pub fn as_array(&self) -> Option<&[SerializedValue]> {
    match self {
      Self::Array { items, .. } => Some(items),
      _ => None,
    }
  }

  /// Lossy text projection: `Str` → its content, numbers / booleans /
  /// `Date` / `Url` / `BigInt` / `RegExp` / `null` → their human-readable
  /// form, everything else (`undefined`, arrays, objects, handles) →
  /// empty string. Intended as a migration shim for call sites that
  /// used to call `Page::evaluate_str(expr)` and expected the raw string
  /// content without the surrounding JSON quoting.
  #[must_use]
  pub fn as_string_lossy(&self) -> String {
    match self {
      Self::Str(s) | Self::Date(s) | Self::Url(s) | Self::BigInt(s) => s.clone(),
      Self::Number(n) => n.to_string(),
      Self::Bool(b) => b.to_string(),
      Self::Special(SpecialValue::Null) => "null".to_string(),
      Self::RegExp(RegExpValue { p, .. }) => p.clone(),
      _ => String::new(),
    }
  }
}

// ── SerializedValue wire serialization ──────────────────────────────────────

impl Serialize for SerializedValue {
  fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
    match self {
      // Primitives pass through as raw JSON primitives.
      Self::Bool(b) => s.serialize_bool(*b),
      Self::Number(n) => s.serialize_f64(*n),
      Self::Str(v) => s.serialize_str(v),
      // Single-key object shapes.
      Self::Special(v) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("v", v)?;
        m.end()
      },
      Self::Date(d) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("d", d)?;
        m.end()
      },
      Self::Url(u) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("u", u)?;
        m.end()
      },
      Self::BigInt(bi) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("bi", bi)?;
        m.end()
      },
      Self::Error(e) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("e", e)?;
        m.end()
      },
      Self::RegExp(r) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("r", r)?;
        m.end()
      },
      Self::TypedArray(ta) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("ta", ta)?;
        m.end()
      },
      Self::ArrayBuffer(ab) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("ab", ab)?;
        m.end()
      },
      Self::Array { id, items } => {
        let mut m = s.serialize_map(Some(2))?;
        m.serialize_entry("a", items)?;
        m.serialize_entry("id", id)?;
        m.end()
      },
      Self::Object { id, entries } => {
        let mut m = s.serialize_map(Some(2))?;
        m.serialize_entry("o", entries)?;
        m.serialize_entry("id", id)?;
        m.end()
      },
      Self::Handle(idx) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("h", idx)?;
        m.end()
      },
      Self::Reference(id) => {
        let mut m = s.serialize_map(Some(1))?;
        m.serialize_entry("ref", id)?;
        m.end()
      },
    }
  }
}

impl<'de> Deserialize<'de> for SerializedValue {
  fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
    d.deserialize_any(SerializedValueVisitor)
  }
}

/// `serde_json`'s internal key for an `arbitrary_precision` number when
/// it surfaces through a `deserialize_any` visitor as a map.
const AP_NUMBER_SENTINEL: &str = "$serde_json::private::Number";

struct SerializedValueVisitor;

impl<'de> Visitor<'de> for SerializedValueVisitor {
  type Value = SerializedValue;

  fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str("a Playwright isomorphic SerializedValue: raw bool/number/string, or single-key tagged object")
  }

  // Primitives come in as raw JSON values.

  fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
    Ok(SerializedValue::Bool(v))
  }

  fn visit_i64<E: de::Error>(self, v: i64) -> Result<Self::Value, E> {
    #[allow(clippy::cast_precision_loss)]
    let as_f64 = v as f64;
    Ok(SerializedValue::Number(as_f64))
  }

  fn visit_u64<E: de::Error>(self, v: u64) -> Result<Self::Value, E> {
    #[allow(clippy::cast_precision_loss)]
    let as_f64 = v as f64;
    Ok(SerializedValue::Number(as_f64))
  }

  fn visit_f64<E: de::Error>(self, v: f64) -> Result<Self::Value, E> {
    Ok(SerializedValue::Number(v))
  }

  fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
    Ok(SerializedValue::Str(v.to_string()))
  }

  fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
    Ok(SerializedValue::Str(v))
  }

  fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
    // `undefined` over the JSON wire has no native form; some
    // encoders emit it as absent-field / `null`. Treat as `undefined`.
    Ok(SerializedValue::Special(SpecialValue::Undefined))
  }

  fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
    // Same as above — bare JSON `null` is NOT what Playwright emits
    // (it uses `{v: "null"}`), but be tolerant so callers can pass
    // plain JSON graphs too.
    Ok(SerializedValue::Special(SpecialValue::Null))
  }

  fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
    // Playwright's tagged objects always have either one primary key
    // (plus `id` companion for `a` / `o`). We collect the raw key/value
    // pairs into a `serde_json::Map` first, then dispatch on the
    // primary tag. This keeps the impl compact at the cost of one
    // extra allocation per object — cheap compared to the wire-
    // decoding cost.
    let mut bag = serde_json::Map::new();
    while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
      bag.insert(k, v);
    }
    // `serde_json`'s `arbitrary_precision` feature (force-enabled
    // workspace-wide by a transitive dep) makes a bare JSON number
    // reach a `deserialize_any` visitor as a one-key map
    // `{"$serde_json::private::Number": "<literal>"}` instead of
    // `visit_f64`. Decode that back to a number before tag dispatch so
    // the wire stays AP-agnostic.
    if bag.len() == 1 {
      if let Some(serde_json::Value::String(lit)) = bag.get(AP_NUMBER_SENTINEL) {
        if let Ok(n) = lit.parse::<f64>() {
          return Ok(SerializedValue::Number(n));
        }
      }
    }
    decode_tagged_object(bag).map_err(de::Error::custom)
  }
}

/// Typed decode-error for the Playwright tagged-object wire shape. Lives
/// private to the serializer because the only consumer is a
/// `serde::Deserialize` Visitor which forwards through
/// `de::Error::custom(...)` and only needs `Display`. Using a dedicated
/// enum (rather than `String`) gives matchable variants if a future
/// caller wants to programmatically distinguish "missing id companion"
/// from "unknown tag".
#[derive(Debug, thiserror::Error)]
enum DecodeError {
  #[error("invalid tag {tag:?}: {reason}")]
  InvalidTag {
    tag: &'static str,
    reason: std::string::String,
  },
  #[error("id companion out of range: {0}")]
  IdRange(#[from] std::num::TryFromIntError),
  #[error("nested decode: {0}")]
  Nested(#[from] serde_json::Error),
  #[error("no recognised tag in object (keys: {0:?})")]
  UnknownShape(Vec<std::string::String>),
}

impl DecodeError {
  fn tag(tag: &'static str, reason: impl Into<std::string::String>) -> Self {
    Self::InvalidTag {
      tag,
      reason: reason.into(),
    }
  }
}

fn decode_tagged_object(mut bag: serde_json::Map<String, serde_json::Value>) -> Result<SerializedValue, DecodeError> {
  // The tag precedence mirrors Playwright's own deserializer
  // (`ref` > `v` > rich types > `a` / `o` > `h` > typed / array
  // buffer). See
  // `/tmp/playwright/packages/injected/src/utilityScriptSerializers.ts:124`.
  if let Some(v) = bag.remove("ref") {
    let id = v
      .as_u64()
      .ok_or_else(|| DecodeError::tag("ref", format!("must be a u64, got {v}")))?;
    return Ok(SerializedValue::Reference(u32::try_from(id)?));
  }
  if let Some(v) = bag.remove("v") {
    let special: SpecialValue = serde_json::from_value(v)?;
    return Ok(SerializedValue::Special(special));
  }
  if let Some(v) = bag.remove("d") {
    let iso = v
      .as_str()
      .ok_or_else(|| DecodeError::tag("d", "must be string"))?
      .to_string();
    return Ok(SerializedValue::Date(iso));
  }
  if let Some(v) = bag.remove("u") {
    let s = v
      .as_str()
      .ok_or_else(|| DecodeError::tag("u", "must be string"))?
      .to_string();
    return Ok(SerializedValue::Url(s));
  }
  if let Some(v) = bag.remove("bi") {
    let s = v
      .as_str()
      .ok_or_else(|| DecodeError::tag("bi", "must be string"))?
      .to_string();
    return Ok(SerializedValue::BigInt(s));
  }
  if let Some(v) = bag.remove("e") {
    let e: ErrorValue = serde_json::from_value(v)?;
    return Ok(SerializedValue::Error(e));
  }
  if let Some(v) = bag.remove("r") {
    let r: RegExpValue = serde_json::from_value(v)?;
    return Ok(SerializedValue::RegExp(r));
  }
  if let Some(v) = bag.remove("a") {
    let items: Vec<SerializedValue> = serde_json::from_value(v)?;
    let id = bag
      .remove("id")
      .and_then(|v| v.as_u64())
      .ok_or_else(|| DecodeError::tag("a", "must be paired with numeric id"))?;
    return Ok(SerializedValue::Array {
      id: u32::try_from(id)?,
      items,
    });
  }
  if let Some(v) = bag.remove("o") {
    let entries: Vec<PropertyEntry> = serde_json::from_value(v)?;
    let id = bag
      .remove("id")
      .and_then(|v| v.as_u64())
      .ok_or_else(|| DecodeError::tag("o", "must be paired with numeric id"))?;
    return Ok(SerializedValue::Object {
      id: u32::try_from(id)?,
      entries,
    });
  }
  if let Some(v) = bag.remove("h") {
    let idx = v.as_u64().ok_or_else(|| DecodeError::tag("h", "must be u64"))?;
    return Ok(SerializedValue::Handle(u32::try_from(idx)?));
  }
  if let Some(v) = bag.remove("ta") {
    let ta: TypedArrayValue = serde_json::from_value(v)?;
    return Ok(SerializedValue::TypedArray(ta));
  }
  if let Some(v) = bag.remove("ab") {
    let ab: ArrayBufferValue = serde_json::from_value(v)?;
    return Ok(SerializedValue::ArrayBuffer(ab));
  }
  Err(DecodeError::UnknownShape(bag.keys().cloned().collect()))
}

// ── SerializationContext ────────────────────────────────────────────────────

/// Tracks the next unique `id` to hand out when building `a` / `o` values.
/// Held by the caller across a `from_json` invocation (or a manual build)
/// so every collection in the resulting graph has a distinct id.
#[derive(Debug, Clone, Default)]
pub struct SerializationContext {
  next_id: u32,
}

impl SerializationContext {
  /// Allocate a fresh id. First id handed out is `1` — Playwright's
  /// `lastId` starts at `0` and the first `++lastId` produces `1`.
  pub fn alloc_id(&mut self) -> u32 {
    self.next_id = self.next_id.saturating_add(1);
    self.next_id
  }
}

// ── SerializedArgument ──────────────────────────────────────────────────────

/// The full `{ value, handles }` envelope ferridriver passes to the
/// injected utility script when a user calls `page.evaluate(fn, arg)`.
/// `value` carries the serialized arg tree (possibly containing
/// `h: N` refs into `handles`); `handles` carries the backend object
/// references those refs resolve to. [`HandleId`] models the cross-
/// backend variant (CDP `RemoteObjectId` string / `BiDi`
/// `{shared_id, handle}` / `WebKit` `__wr[]` index); the backend
/// marshaler layer converts each entry into the protocol's native
/// remote-object reference (CDP `CallArgument.objectId`, etc.).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SerializedArgument {
  pub value: SerializedValue,
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub handles: Vec<HandleId>,
}

impl Default for SerializedValue {
  fn default() -> Self {
    Self::Special(SpecialValue::Undefined)
  }
}

/// Backend-agnostic handle identifier used in
/// [`SerializedArgument::handles`]. Each backend maps this to its own
/// native remote-object reference on marshal.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HandleId {
  /// CDP `Runtime.RemoteObjectId` — an opaque string.
  Cdp(String),
  /// `WebDriver` `BiDi` shared reference — `{ sharedId, handle? }`.
  Bidi { shared_id: String, handle: Option<String> },
  /// Playwright `WebKit` `Runtime.RemoteObjectId` — an opaque string.
  WebKit(String),
}

// ── base64 helpers (stand-alone) ────────────────────────────────────────────

/// Emit the same base64 encoding the `ta.b` / `ab.b` serializers use.
/// Handy for backend marshalers that need to produce or consume the raw
/// bytes outside the serde path.
#[must_use]
pub fn encode_typed_array_bytes(bytes: &[u8]) -> String {
  base64::engine::general_purpose::STANDARD.encode(bytes)
}

/// Inverse of [`encode_typed_array_bytes`].
///
/// # Errors
///
/// Returns `base64::DecodeError` if the string is not valid standard
/// base64.
pub fn decode_typed_array_bytes(encoded: &str) -> Result<Vec<u8>, base64::DecodeError> {
  base64::engine::general_purpose::STANDARD.decode(encoded)
}

// ── Tests ───────────────────────────────────────────────────────────────────

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

  // ── Primitive wire shapes ────────────────────────────────────────────────

  #[test]
  fn serializes_bool_raw() {
    assert_eq!(serde_json::to_value(SerializedValue::Bool(true)).unwrap(), json!(true));
    assert_eq!(
      serde_json::to_value(SerializedValue::Bool(false)).unwrap(),
      json!(false)
    );
  }

  #[test]
  fn serializes_number_raw() {
    // Playwright's isomorphic format emits primitives raw — `42.0`, not
    // `{n: 42.0}`. That's what the page's utilityScript expects.
    assert_eq!(serde_json::to_value(SerializedValue::Number(1.5)).unwrap(), json!(1.5));
  }

  #[test]
  fn serializes_string_raw() {
    assert_eq!(
      serde_json::to_value(SerializedValue::Str("hi".into())).unwrap(),
      json!("hi")
    );
  }

  // ── Special values ───────────────────────────────────────────────────────

  #[test]
  fn serializes_special_values() {
    for (special, expected) in [
      (SpecialValue::Null, json!({"v": "null"})),
      (SpecialValue::Undefined, json!({"v": "undefined"})),
      (SpecialValue::NaN, json!({"v": "NaN"})),
      (SpecialValue::Infinity, json!({"v": "Infinity"})),
      (SpecialValue::NegInfinity, json!({"v": "-Infinity"})),
      (SpecialValue::NegZero, json!({"v": "-0"})),
    ] {
      assert_eq!(
        serde_json::to_value(SerializedValue::Special(special)).unwrap(),
        expected,
        "variant {special:?}"
      );
    }
  }

  #[test]
  fn from_f64_routes_ieee_specials() {
    assert!(matches!(
      SerializedValue::from_f64(f64::NAN),
      SerializedValue::Special(SpecialValue::NaN)
    ));
    assert!(matches!(
      SerializedValue::from_f64(f64::INFINITY),
      SerializedValue::Special(SpecialValue::Infinity)
    ));
    assert!(matches!(
      SerializedValue::from_f64(f64::NEG_INFINITY),
      SerializedValue::Special(SpecialValue::NegInfinity)
    ));
    assert!(matches!(
      SerializedValue::from_f64(-0.0_f64),
      SerializedValue::Special(SpecialValue::NegZero)
    ));
    assert!(matches!(
      SerializedValue::from_f64(0.0_f64),
      SerializedValue::Number(n) if (n - 0.0_f64).abs() < f64::EPSILON
    ));
    assert!(matches!(
      SerializedValue::from_f64(1.5),
      SerializedValue::Number(n) if (n - 1.5_f64).abs() < f64::EPSILON
    ));
  }

  // ── Rich types ──────────────────────────────────────────────────────────

  #[test]
  fn serializes_date_url_bigint() {
    assert_eq!(
      serde_json::to_value(SerializedValue::date("2024-01-01T00:00:00.000Z")).unwrap(),
      json!({"d": "2024-01-01T00:00:00.000Z"})
    );
    assert_eq!(
      serde_json::to_value(SerializedValue::url("https://example.com/")).unwrap(),
      json!({"u": "https://example.com/"})
    );
    assert_eq!(
      serde_json::to_value(SerializedValue::bigint("9007199254740993")).unwrap(),
      json!({"bi": "9007199254740993"})
    );
  }

  #[test]
  fn serializes_regexp() {
    let wire = serde_json::to_value(SerializedValue::regexp("foo.*bar", "gi")).unwrap();
    assert_eq!(wire, json!({"r": {"p": "foo.*bar", "f": "gi"}}));
  }

  #[test]
  fn serializes_error() {
    let wire = serde_json::to_value(SerializedValue::error(
      "TypeError",
      "nope",
      "TypeError: nope\n    at foo",
    ))
    .unwrap();
    assert_eq!(
      wire,
      json!({"e": {"n": "TypeError", "m": "nope", "s": "TypeError: nope\n    at foo"}})
    );
  }

  #[test]
  fn serializes_typed_array_with_base64() {
    let wire = serde_json::to_value(SerializedValue::typed_array(vec![1, 2, 3, 4], TypedArrayKind::U8)).unwrap();
    assert_eq!(wire, json!({"ta": {"b": "AQIDBA==", "k": "ui8"}}));
  }

  #[test]
  fn serializes_array_buffer_with_base64() {
    let wire = serde_json::to_value(SerializedValue::array_buffer(vec![0xca, 0xfe, 0xba, 0xbe])).unwrap();
    assert_eq!(wire, json!({"ab": {"b": "yv66vg=="}}));
  }

  #[test]
  fn typed_array_bytes_per_element_matches_js() {
    assert_eq!(TypedArrayKind::I8.bytes_per_element(), 1);
    assert_eq!(TypedArrayKind::U8.bytes_per_element(), 1);
    assert_eq!(TypedArrayKind::U8Clamped.bytes_per_element(), 1);
    assert_eq!(TypedArrayKind::I16.bytes_per_element(), 2);
    assert_eq!(TypedArrayKind::U16.bytes_per_element(), 2);
    assert_eq!(TypedArrayKind::I32.bytes_per_element(), 4);
    assert_eq!(TypedArrayKind::U32.bytes_per_element(), 4);
    assert_eq!(TypedArrayKind::F32.bytes_per_element(), 4);
    assert_eq!(TypedArrayKind::F64.bytes_per_element(), 8);
    assert_eq!(TypedArrayKind::BI64.bytes_per_element(), 8);
    assert_eq!(TypedArrayKind::BUI64.bytes_per_element(), 8);
  }

  // ── Collections ──────────────────────────────────────────────────────────

  #[test]
  fn serializes_array_with_id() {
    let wire = serde_json::to_value(SerializedValue::array(
      1,
      vec![SerializedValue::Number(1.0), SerializedValue::Number(2.0)],
    ))
    .unwrap();
    assert_eq!(wire, json!({"a": [1.0, 2.0], "id": 1}));
  }

  #[test]
  fn serializes_object_with_id_preserves_order() {
    let wire = serde_json::to_value(SerializedValue::object(
      2,
      vec![
        PropertyEntry {
          k: "first".into(),
          v: SerializedValue::Number(1.0),
        },
        PropertyEntry {
          k: "second".into(),
          v: SerializedValue::Str("two".into()),
        },
      ],
    ))
    .unwrap();
    assert_eq!(
      wire,
      json!({"o": [{"k": "first", "v": 1.0}, {"k": "second", "v": "two"}], "id": 2})
    );
  }

  #[test]
  fn serializes_handle_and_reference() {
    assert_eq!(
      serde_json::to_value(SerializedValue::handle(3)).unwrap(),
      json!({"h": 3})
    );
    assert_eq!(
      serde_json::to_value(SerializedValue::reference(5)).unwrap(),
      json!({"ref": 5})
    );
  }

  // ── Deserialization ─────────────────────────────────────────────────────

  #[test]
  fn deserializes_primitives_raw() {
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!(true)).unwrap(),
      SerializedValue::Bool(true)
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!(42)).unwrap(),
      SerializedValue::Number(42.0)
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!("hi")).unwrap(),
      SerializedValue::Str("hi".into())
    );
  }

  #[test]
  fn deserializes_special_tag() {
    for (wire, expected) in [
      (json!({"v": "null"}), SpecialValue::Null),
      (json!({"v": "undefined"}), SpecialValue::Undefined),
      (json!({"v": "NaN"}), SpecialValue::NaN),
      (json!({"v": "Infinity"}), SpecialValue::Infinity),
      (json!({"v": "-Infinity"}), SpecialValue::NegInfinity),
      (json!({"v": "-0"}), SpecialValue::NegZero),
    ] {
      let parsed = serde_json::from_value::<SerializedValue>(wire.clone()).unwrap();
      assert_eq!(parsed, SerializedValue::Special(expected), "wire: {wire}");
    }
  }

  #[test]
  fn deserializes_rich_tagged_forms() {
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"d": "2024"})).unwrap(),
      SerializedValue::Date("2024".into())
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"u": "https://a/"})).unwrap(),
      SerializedValue::Url("https://a/".into())
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"bi": "-42"})).unwrap(),
      SerializedValue::BigInt("-42".into())
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"r": {"p": "a", "f": "g"}})).unwrap(),
      SerializedValue::RegExp(RegExpValue {
        p: "a".into(),
        f: "g".into()
      })
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"e": {"n": "E", "m": "m", "s": "s"}})).unwrap(),
      SerializedValue::Error(ErrorValue {
        n: "E".into(),
        m: "m".into(),
        s: "s".into()
      })
    );
  }

  #[test]
  fn deserializes_array_and_object_with_id() {
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"a": [1, 2], "id": 1})).unwrap(),
      SerializedValue::Array {
        id: 1,
        items: vec![SerializedValue::Number(1.0), SerializedValue::Number(2.0)],
      }
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"o": [{"k": "x", "v": true}], "id": 2})).unwrap(),
      SerializedValue::Object {
        id: 2,
        entries: vec![PropertyEntry {
          k: "x".into(),
          v: SerializedValue::Bool(true),
        }],
      }
    );
  }

  #[test]
  fn deserializes_handle_and_reference() {
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"h": 0})).unwrap(),
      SerializedValue::Handle(0)
    );
    assert_eq!(
      serde_json::from_value::<SerializedValue>(json!({"ref": 5})).unwrap(),
      SerializedValue::Reference(5)
    );
  }

  #[test]
  fn deserializes_typed_array_base64() {
    let parsed: SerializedValue = serde_json::from_value(json!({"ta": {"b": "AQIDBA==", "k": "ui8"}})).unwrap();
    assert_eq!(
      parsed,
      SerializedValue::TypedArray(TypedArrayValue {
        b: vec![1, 2, 3, 4],
        k: TypedArrayKind::U8
      })
    );
  }

  #[test]
  fn deserializes_array_buffer_base64() {
    let parsed: SerializedValue = serde_json::from_value(json!({"ab": {"b": "yv66vg=="}})).unwrap();
    assert_eq!(
      parsed,
      SerializedValue::ArrayBuffer(ArrayBufferValue {
        b: vec![0xca, 0xfe, 0xba, 0xbe],
      })
    );
  }

  #[test]
  fn rejects_empty_tagged_object() {
    let err = serde_json::from_value::<SerializedValue>(json!({})).unwrap_err();
    assert!(
      err.to_string().contains("no recognised tag"),
      "unexpected error message: {err}"
    );
  }

  // ── Round-trip ──────────────────────────────────────────────────────────

  #[test]
  fn roundtrips_every_variant_via_serde() {
    let values = vec![
      SerializedValue::Bool(true),
      SerializedValue::Bool(false),
      SerializedValue::Number(1.5),
      SerializedValue::Number(0.0),
      SerializedValue::Str("hello".into()),
      SerializedValue::Special(SpecialValue::Null),
      SerializedValue::Special(SpecialValue::Undefined),
      SerializedValue::Special(SpecialValue::NaN),
      SerializedValue::Special(SpecialValue::Infinity),
      SerializedValue::Special(SpecialValue::NegInfinity),
      SerializedValue::Special(SpecialValue::NegZero),
      SerializedValue::date("2024-06-01T12:00:00.000Z"),
      SerializedValue::url("https://a.test/"),
      SerializedValue::bigint("-12345678901234567890"),
      SerializedValue::regexp("a|b", "i"),
      SerializedValue::error("Error", "boom", ""),
      SerializedValue::typed_array(vec![0xde, 0xad, 0xbe, 0xef], TypedArrayKind::U32),
      SerializedValue::array_buffer(vec![0x01, 0x02]),
      SerializedValue::array(1, vec![SerializedValue::Number(1.0)]),
      SerializedValue::object(
        2,
        vec![PropertyEntry {
          k: "x".into(),
          v: SerializedValue::Bool(true),
        }],
      ),
      SerializedValue::handle(0),
      SerializedValue::reference(1),
    ];
    for v in values {
      let wire = serde_json::to_string(&v).unwrap();
      let back: SerializedValue = serde_json::from_str(&wire).unwrap();
      assert_eq!(v, back, "roundtrip drift for {wire}");
    }
  }

  // ── JSON conversion helpers ─────────────────────────────────────────────

  #[test]
  fn from_json_maps_scalars() {
    let mut ctx = SerializationContext::default();
    assert_eq!(
      SerializedValue::from_json(&json!(null), &mut ctx),
      SerializedValue::Special(SpecialValue::Null)
    );
    assert_eq!(
      SerializedValue::from_json(&json!(true), &mut ctx),
      SerializedValue::Bool(true)
    );
    assert_eq!(
      SerializedValue::from_json(&json!("hi"), &mut ctx),
      SerializedValue::Str("hi".into())
    );
    assert!(matches!(
      SerializedValue::from_json(&json!(42), &mut ctx),
      SerializedValue::Number(n) if (n - 42.0).abs() < f64::EPSILON
    ));
  }

  #[test]
  fn from_json_allocates_ids_for_collections() {
    let mut ctx = SerializationContext::default();
    let val = SerializedValue::from_json(&json!({"a": [1, 2], "b": null}), &mut ctx);
    let (outer_id, entries) = match &val {
      SerializedValue::Object { id, entries } => (*id, entries),
      _ => panic!("expected Object"),
    };
    let a_entry = entries.iter().find(|e| e.k == "a").expect("has key `a`");
    let a_id = match &a_entry.v {
      SerializedValue::Array { id, .. } => *id,
      _ => panic!("expected Array"),
    };
    assert_ne!(outer_id, a_id, "ids are distinct");
  }

  #[test]
  fn to_json_like_roundtrips_json_subset() {
    let original = json!({"arr": [1, true, "x", null], "n": 2.5});
    let mut ctx = SerializationContext::default();
    let wire = SerializedValue::from_json(&original, &mut ctx);
    let back = wire.to_json_like().expect("JSON subset should round-trip");
    assert_eq!(back, original);
  }

  #[test]
  fn to_json_like_returns_none_for_rich_types() {
    assert_eq!(SerializedValue::undefined().to_json_like(), None);
    assert_eq!(SerializedValue::date("2024-01-01T00:00:00Z").to_json_like(), None);
    assert_eq!(SerializedValue::handle(0).to_json_like(), None);
    assert_eq!(SerializedValue::reference(1).to_json_like(), None);
    assert_eq!(
      SerializedValue::from_f64(f64::NAN).to_json_like(),
      None,
      "NaN has no JSON form"
    );
  }

  #[test]
  fn serialization_context_allocates_distinct_ids() {
    let mut ctx = SerializationContext::default();
    let ids: Vec<u32> = (0..5).map(|_| ctx.alloc_id()).collect();
    assert_eq!(ids, vec![1, 2, 3, 4, 5]);
  }

  // ── SerializedArgument ──────────────────────────────────────────────────

  #[test]
  fn serialized_argument_omits_empty_handles() {
    let arg = SerializedArgument {
      value: SerializedValue::Number(1.0),
      handles: vec![],
    };
    let s = serde_json::to_string(&arg).unwrap();
    assert_eq!(s, r#"{"value":1.0}"#);
  }

  #[test]
  fn serialized_argument_carries_handle_list() {
    let arg = SerializedArgument {
      value: SerializedValue::array(1, vec![SerializedValue::handle(0), SerializedValue::handle(1)]),
      handles: vec![
        HandleId::Cdp("obj-1".into()),
        HandleId::Bidi {
          shared_id: "shared-1".into(),
          handle: None,
        },
      ],
    };
    let wire = serde_json::to_value(&arg).unwrap();
    assert_eq!(
      wire,
      json!({
        "value": {"a": [{"h": 0}, {"h": 1}], "id": 1},
        "handles": [
          {"Cdp": "obj-1"},
          {"Bidi": {"shared_id": "shared-1", "handle": null}}
        ]
      })
    );
  }

  // ── Playwright-wire-exact parity spot checks ────────────────────────────
  //
  // Each case mirrors a value Playwright's own `serializeAsCallArgument` in
  // `/tmp/playwright/packages/injected/src/utilityScriptSerializers.ts`
  // would produce for the corresponding JS value. If we ever drift from
  // these strings the page-side `parseEvaluationResultValue` would
  // misinterpret the CDP arguments.

  #[test]
  fn parity_array_of_mixed_primitives() {
    let v = SerializedValue::array(
      1,
      vec![
        SerializedValue::Number(1.0),
        SerializedValue::Bool(true),
        SerializedValue::Str("x".into()),
        SerializedValue::Special(SpecialValue::Null),
        SerializedValue::Special(SpecialValue::NaN),
      ],
    );
    assert_eq!(
      serde_json::to_value(&v).unwrap(),
      json!({"a": [1.0, true, "x", {"v": "null"}, {"v": "NaN"}], "id": 1})
    );
  }

  #[test]
  fn parity_nested_object_with_handle_refs() {
    let v = SerializedValue::object(
      1,
      vec![
        PropertyEntry {
          k: "el".into(),
          v: SerializedValue::handle(0),
        },
        PropertyEntry {
          k: "meta".into(),
          v: SerializedValue::object(
            2,
            vec![PropertyEntry {
              k: "count".into(),
              v: SerializedValue::Number(3.0),
            }],
          ),
        },
      ],
    );
    assert_eq!(
      serde_json::to_value(&v).unwrap(),
      json!({
        "o": [
          {"k": "el", "v": {"h": 0}},
          {"k": "meta", "v": {"o": [{"k": "count", "v": 3.0}], "id": 2}}
        ],
        "id": 1
      })
    );
  }
}