oxirs-wasm 0.2.4

WebAssembly bindings for OxiRS - Run RDF/SPARQL in the browser
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
//! WASM/JS interop bridge type conversions.
//!
//! Provides pure-Rust representations of JavaScript values and conversion
//! utilities for transferring SPARQL results, RDF terms, and query parameters
//! across the WASM boundary without requiring `wasm_bindgen` at the type level.

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// JsValue
// ─────────────────────────────────────────────────────────────────────────────

/// A Rust-side representation of a JavaScript value.
///
/// Covers the six primitive JS types plus composite Object and Array.
#[derive(Debug, Clone, PartialEq)]
pub enum JsValue {
    /// JavaScript `undefined`.
    Undefined,
    /// JavaScript `null`.
    Null,
    /// JavaScript boolean.
    Boolean(bool),
    /// JavaScript number (always f64 in JS).
    Number(f64),
    /// JavaScript string.
    String(String),
    /// JavaScript array.
    Array(Vec<JsValue>),
    /// JavaScript plain object (string-keyed).
    Object(HashMap<String, JsValue>),
}

impl JsValue {
    /// Return the inner `bool`, or `None` if this is not a `Boolean`.
    pub fn as_bool(&self) -> Option<bool> {
        if let JsValue::Boolean(b) = self {
            Some(*b)
        } else {
            None
        }
    }

    /// Return the inner `f64`, or `None` if this is not a `Number`.
    pub fn as_f64(&self) -> Option<f64> {
        if let JsValue::Number(n) = self {
            Some(*n)
        } else {
            None
        }
    }

    /// Return a reference to the inner string slice, or `None` if not a `String`.
    pub fn as_str(&self) -> Option<&str> {
        if let JsValue::String(s) = self {
            Some(s.as_str())
        } else {
            None
        }
    }

    /// Return a slice of the inner array, or `None` if not an `Array`.
    pub fn as_array(&self) -> Option<&[JsValue]> {
        if let JsValue::Array(v) = self {
            Some(v.as_slice())
        } else {
            None
        }
    }

    /// Get a value from an `Object` variant by key, or `None` otherwise.
    pub fn get(&self, key: &str) -> Option<&JsValue> {
        if let JsValue::Object(map) = self {
            map.get(key)
        } else {
            None
        }
    }

    /// Return `true` if this value is `Null` or `Undefined`.
    pub fn is_null_or_undefined(&self) -> bool {
        matches!(self, JsValue::Null | JsValue::Undefined)
    }

    /// Return the JS type name string (mirrors `typeof` except for null).
    pub fn type_name(&self) -> &str {
        match self {
            JsValue::Undefined => "undefined",
            JsValue::Null => "null",
            JsValue::Boolean(_) => "boolean",
            JsValue::Number(_) => "number",
            JsValue::String(_) => "string",
            JsValue::Array(_) => "array",
            JsValue::Object(_) => "object",
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// BridgeError
// ─────────────────────────────────────────────────────────────────────────────

/// Errors produced by the WASM bridge converters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BridgeError {
    /// The JS value had an unexpected type.
    TypeMismatch(String),
    /// A required key was absent from an Object value.
    KeyNotFound(String),
    /// An array index was out of bounds.
    IndexOutOfBounds(usize),
}

impl std::fmt::Display for BridgeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BridgeError::TypeMismatch(msg) => write!(f, "Type mismatch: {msg}"),
            BridgeError::KeyNotFound(key) => write!(f, "Key not found: {key}"),
            BridgeError::IndexOutOfBounds(idx) => write!(f, "Index out of bounds: {idx}"),
        }
    }
}

impl std::error::Error for BridgeError {}

// ─────────────────────────────────────────────────────────────────────────────
// WasmBridge
// ─────────────────────────────────────────────────────────────────────────────

/// Stateless bridge helper for WASM↔JS type conversions.
pub struct WasmBridge;

impl WasmBridge {
    /// Convert SPARQL SELECT results to a JS-friendly `JsValue`.
    ///
    /// Produces:
    /// ```json
    /// {
    ///   "head": { "vars": ["x", "y"] },
    ///   "results": {
    ///     "bindings": [
    ///       { "x": { "type": "literal", "value": "hello" }, ... },
    ///       ...
    ///     ]
    ///   }
    /// }
    /// ```
    pub fn sparql_results_to_js(vars: &[String], bindings: &[HashMap<String, String>]) -> JsValue {
        // head.vars
        let vars_js = JsValue::Array(vars.iter().map(|v| JsValue::String(v.clone())).collect());
        let mut head = HashMap::new();
        head.insert("vars".to_string(), vars_js);

        // results.bindings
        let binding_rows: Vec<JsValue> = bindings
            .iter()
            .map(|row| {
                let mut obj = HashMap::new();
                for var in vars {
                    if let Some(val) = row.get(var) {
                        let term = Self::rdf_term_to_js(val);
                        obj.insert(var.clone(), term);
                    }
                }
                JsValue::Object(obj)
            })
            .collect();

        let mut results = HashMap::new();
        results.insert("bindings".to_string(), JsValue::Array(binding_rows));

        let mut root = HashMap::new();
        root.insert("head".to_string(), JsValue::Object(head));
        root.insert("results".to_string(), JsValue::Object(results));

        JsValue::Object(root)
    }

    /// Extract a SPARQL query string from a JS value.
    ///
    /// Accepts either a `String` value directly or an `Object` with a `"query"` key.
    ///
    /// # Errors
    /// - `TypeMismatch` if the value type is unsupported.
    /// - `KeyNotFound` if an Object is provided but has no `"query"` key.
    pub fn js_to_sparql_query(val: &JsValue) -> Result<String, BridgeError> {
        match val {
            JsValue::String(s) => Ok(s.clone()),
            JsValue::Object(map) => map
                .get("query")
                .and_then(|v| v.as_str())
                .map(str::to_string)
                .ok_or_else(|| BridgeError::KeyNotFound("query".to_string())),
            other => Err(BridgeError::TypeMismatch(format!(
                "expected string or object, got {}",
                other.type_name()
            ))),
        }
    }

    /// Convert an RDF term string to a JS object with `type` and `value`.
    ///
    /// Detection rules:
    /// - starts with `<` and ends with `>` → IRI
    /// - starts with `_:` → blank node
    /// - otherwise → literal
    pub fn rdf_term_to_js(term: &str) -> JsValue {
        let (term_type, value) = if term.starts_with('<') && term.ends_with('>') {
            ("uri", &term[1..term.len() - 1])
        } else if let Some(stripped) = term.strip_prefix("_:") {
            ("bnode", stripped)
        } else {
            ("literal", term)
        };

        let mut map = HashMap::new();
        map.insert("type".to_string(), JsValue::String(term_type.to_string()));
        map.insert("value".to_string(), JsValue::String(value.to_string()));
        JsValue::Object(map)
    }

    /// Convert a JS array of string values to `Vec<String>`.
    ///
    /// # Errors
    /// - `TypeMismatch` if the value is not an Array.
    /// - `TypeMismatch` if any element is not a String.
    pub fn js_array_to_strings(val: &JsValue) -> Result<Vec<String>, BridgeError> {
        let items = val.as_array().ok_or_else(|| {
            BridgeError::TypeMismatch(format!("expected array, got {}", val.type_name()))
        })?;

        items
            .iter()
            .enumerate()
            .map(|(i, item)| {
                item.as_str().map(str::to_string).ok_or_else(|| {
                    BridgeError::TypeMismatch(format!(
                        "element [{}] expected string, got {}",
                        i,
                        item.type_name()
                    ))
                })
            })
            .collect()
    }

    /// Convert a JS object with string values to `HashMap<String, String>`.
    ///
    /// # Errors
    /// - `TypeMismatch` if the value is not an Object.
    /// - `TypeMismatch` if any value is not a String.
    pub fn js_object_to_map(val: &JsValue) -> Result<HashMap<String, String>, BridgeError> {
        let obj = match val {
            JsValue::Object(m) => m,
            other => {
                return Err(BridgeError::TypeMismatch(format!(
                    "expected object, got {}",
                    other.type_name()
                )))
            }
        };

        let mut result = HashMap::new();
        for (k, v) in obj {
            let s = v.as_str().ok_or_else(|| {
                BridgeError::TypeMismatch(format!(
                    "value for key '{}' expected string, got {}",
                    k,
                    v.type_name()
                ))
            })?;
            result.insert(k.clone(), s.to_string());
        }
        Ok(result)
    }

    /// Serialise a `JsValue` to a JSON-like string.
    ///
    /// Produces valid JSON for all variants.
    pub fn serialize_js_value(val: &JsValue) -> String {
        match val {
            JsValue::Undefined => "undefined".to_string(),
            JsValue::Null => "null".to_string(),
            JsValue::Boolean(b) => b.to_string(),
            JsValue::Number(n) => {
                if n.fract() == 0.0 && n.is_finite() {
                    format!("{}", *n as i64)
                } else {
                    format!("{n}")
                }
            }
            JsValue::String(s) => {
                // Simple JSON-string escaping
                let escaped = s
                    .replace('\\', "\\\\")
                    .replace('"', "\\\"")
                    .replace('\n', "\\n")
                    .replace('\r', "\\r")
                    .replace('\t', "\\t");
                format!("\"{escaped}\"")
            }
            JsValue::Array(items) => {
                let inner: Vec<String> = items.iter().map(Self::serialize_js_value).collect();
                format!("[{}]", inner.join(","))
            }
            JsValue::Object(map) => {
                let mut pairs: Vec<String> = map
                    .iter()
                    .map(|(k, v)| {
                        let ek = k.replace('\\', "\\\\").replace('"', "\\\"");
                        format!("\"{}\":{}", ek, Self::serialize_js_value(v))
                    })
                    .collect();
                pairs.sort(); // deterministic output for tests
                format!("{{{}}}", pairs.join(","))
            }
        }
    }
}

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

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

    // ── JsValue::as_bool ─────────────────────────────────────────────────────

    #[test]
    fn test_as_bool_true() {
        assert_eq!(JsValue::Boolean(true).as_bool(), Some(true));
    }

    #[test]
    fn test_as_bool_false() {
        assert_eq!(JsValue::Boolean(false).as_bool(), Some(false));
    }

    #[test]
    fn test_as_bool_non_bool() {
        assert_eq!(JsValue::Number(1.0).as_bool(), None);
    }

    // ── JsValue::as_f64 ──────────────────────────────────────────────────────

    #[test]
    fn test_as_f64_present() {
        assert_eq!(JsValue::Number(2.71).as_f64(), Some(2.71));
    }

    #[test]
    fn test_as_f64_absent() {
        assert_eq!(JsValue::String("x".to_string()).as_f64(), None);
    }

    // ── JsValue::as_str ──────────────────────────────────────────────────────

    #[test]
    fn test_as_str_present() {
        assert_eq!(JsValue::String("hello".to_string()).as_str(), Some("hello"));
    }

    #[test]
    fn test_as_str_absent() {
        assert_eq!(JsValue::Null.as_str(), None);
    }

    // ── JsValue::as_array ────────────────────────────────────────────────────

    #[test]
    fn test_as_array_present() {
        let arr = JsValue::Array(vec![JsValue::Number(1.0)]);
        assert_eq!(arr.as_array().map(|a| a.len()), Some(1));
    }

    #[test]
    fn test_as_array_absent() {
        assert!(JsValue::Null.as_array().is_none());
    }

    // ── JsValue::get ─────────────────────────────────────────────────────────

    #[test]
    fn test_get_existing_key() {
        let mut map = HashMap::new();
        map.insert("foo".to_string(), JsValue::Boolean(true));
        let obj = JsValue::Object(map);
        assert_eq!(obj.get("foo"), Some(&JsValue::Boolean(true)));
    }

    #[test]
    fn test_get_missing_key() {
        let obj = JsValue::Object(HashMap::new());
        assert!(obj.get("bar").is_none());
    }

    #[test]
    fn test_get_non_object() {
        assert!(JsValue::Null.get("key").is_none());
    }

    // ── JsValue::is_null_or_undefined ────────────────────────────────────────

    #[test]
    fn test_null_is_null_or_undefined() {
        assert!(JsValue::Null.is_null_or_undefined());
    }

    #[test]
    fn test_undefined_is_null_or_undefined() {
        assert!(JsValue::Undefined.is_null_or_undefined());
    }

    #[test]
    fn test_number_is_not_null_or_undefined() {
        assert!(!JsValue::Number(0.0).is_null_or_undefined());
    }

    // ── JsValue::type_name ───────────────────────────────────────────────────

    #[test]
    fn test_type_name_undefined() {
        assert_eq!(JsValue::Undefined.type_name(), "undefined");
    }

    #[test]
    fn test_type_name_null() {
        assert_eq!(JsValue::Null.type_name(), "null");
    }

    #[test]
    fn test_type_name_boolean() {
        assert_eq!(JsValue::Boolean(false).type_name(), "boolean");
    }

    #[test]
    fn test_type_name_number() {
        assert_eq!(JsValue::Number(0.0).type_name(), "number");
    }

    #[test]
    fn test_type_name_string() {
        assert_eq!(JsValue::String("x".to_string()).type_name(), "string");
    }

    #[test]
    fn test_type_name_array() {
        assert_eq!(JsValue::Array(vec![]).type_name(), "array");
    }

    #[test]
    fn test_type_name_object() {
        assert_eq!(JsValue::Object(HashMap::new()).type_name(), "object");
    }

    // ── sparql_results_to_js ─────────────────────────────────────────────────

    #[test]
    fn test_sparql_results_has_head() {
        let vars = vec!["x".to_string()];
        let bindings = vec![];
        let result = WasmBridge::sparql_results_to_js(&vars, &bindings);
        assert!(result.get("head").is_some());
    }

    #[test]
    fn test_sparql_results_has_results() {
        let vars = vec!["x".to_string()];
        let result = WasmBridge::sparql_results_to_js(&vars, &[]);
        assert!(result.get("results").is_some());
    }

    #[test]
    fn test_sparql_results_vars_count() {
        let vars = vec!["s".to_string(), "p".to_string(), "o".to_string()];
        let result = WasmBridge::sparql_results_to_js(&vars, &[]);
        let head = result.get("head").expect("should succeed");
        let head_vars = head
            .get("vars")
            .expect("should succeed")
            .as_array()
            .expect("should succeed");
        assert_eq!(head_vars.len(), 3);
    }

    #[test]
    fn test_sparql_results_binding_row() {
        let vars = vec!["x".to_string()];
        let mut row = HashMap::new();
        row.insert("x".to_string(), "<http://example.org/a>".to_string());
        let result = WasmBridge::sparql_results_to_js(&vars, &[row]);
        let results = result.get("results").expect("should succeed");
        let rows = results
            .get("bindings")
            .expect("should succeed")
            .as_array()
            .expect("should succeed");
        assert_eq!(rows.len(), 1);
    }

    // ── js_to_sparql_query ───────────────────────────────────────────────────

    #[test]
    fn test_js_to_sparql_query_string() {
        let val = JsValue::String("SELECT * WHERE { ?s ?p ?o }".to_string());
        let q = WasmBridge::js_to_sparql_query(&val).expect("should succeed");
        assert!(q.contains("SELECT"));
    }

    #[test]
    fn test_js_to_sparql_query_object() {
        let mut map = HashMap::new();
        map.insert("query".to_string(), JsValue::String("ASK { }".to_string()));
        let val = JsValue::Object(map);
        let q = WasmBridge::js_to_sparql_query(&val).expect("should succeed");
        assert_eq!(q, "ASK { }");
    }

    #[test]
    fn test_js_to_sparql_query_object_missing_key() {
        let val = JsValue::Object(HashMap::new());
        assert!(matches!(
            WasmBridge::js_to_sparql_query(&val),
            Err(BridgeError::KeyNotFound(_))
        ));
    }

    #[test]
    fn test_js_to_sparql_query_type_mismatch() {
        let val = JsValue::Number(42.0);
        assert!(matches!(
            WasmBridge::js_to_sparql_query(&val),
            Err(BridgeError::TypeMismatch(_))
        ));
    }

    // ── rdf_term_to_js ───────────────────────────────────────────────────────

    #[test]
    fn test_rdf_term_iri() {
        let js = WasmBridge::rdf_term_to_js("<http://example.org/a>");
        let ty = js
            .get("type")
            .expect("should succeed")
            .as_str()
            .expect("should succeed");
        assert_eq!(ty, "uri");
    }

    #[test]
    fn test_rdf_term_blank() {
        let js = WasmBridge::rdf_term_to_js("_:b0");
        let ty = js
            .get("type")
            .expect("should succeed")
            .as_str()
            .expect("should succeed");
        assert_eq!(ty, "bnode");
        let val = js
            .get("value")
            .expect("should succeed")
            .as_str()
            .expect("should succeed");
        assert_eq!(val, "b0");
    }

    #[test]
    fn test_rdf_term_literal() {
        let js = WasmBridge::rdf_term_to_js("hello world");
        let ty = js
            .get("type")
            .expect("should succeed")
            .as_str()
            .expect("should succeed");
        assert_eq!(ty, "literal");
    }

    #[test]
    fn test_rdf_term_iri_strips_brackets() {
        let js = WasmBridge::rdf_term_to_js("<http://x.org/>");
        let val = js
            .get("value")
            .expect("should succeed")
            .as_str()
            .expect("should succeed");
        assert_eq!(val, "http://x.org/");
    }

    // ── js_array_to_strings ──────────────────────────────────────────────────

    #[test]
    fn test_js_array_to_strings_ok() {
        let val = JsValue::Array(vec![
            JsValue::String("a".to_string()),
            JsValue::String("b".to_string()),
        ]);
        let result = WasmBridge::js_array_to_strings(&val).expect("should succeed");
        assert_eq!(result, vec!["a", "b"]);
    }

    #[test]
    fn test_js_array_to_strings_not_array() {
        let val = JsValue::Null;
        assert!(matches!(
            WasmBridge::js_array_to_strings(&val),
            Err(BridgeError::TypeMismatch(_))
        ));
    }

    #[test]
    fn test_js_array_to_strings_non_string_element() {
        let val = JsValue::Array(vec![JsValue::Number(1.0)]);
        assert!(matches!(
            WasmBridge::js_array_to_strings(&val),
            Err(BridgeError::TypeMismatch(_))
        ));
    }

    #[test]
    fn test_js_array_to_strings_empty() {
        let val = JsValue::Array(vec![]);
        assert_eq!(
            WasmBridge::js_array_to_strings(&val).expect("should succeed"),
            Vec::<String>::new()
        );
    }

    // ── js_object_to_map ─────────────────────────────────────────────────────

    #[test]
    fn test_js_object_to_map_ok() {
        let mut map = HashMap::new();
        map.insert("k".to_string(), JsValue::String("v".to_string()));
        let val = JsValue::Object(map);
        let result = WasmBridge::js_object_to_map(&val).expect("should succeed");
        assert_eq!(result.get("k").map(String::as_str), Some("v"));
    }

    #[test]
    fn test_js_object_to_map_not_object() {
        let val = JsValue::Array(vec![]);
        assert!(matches!(
            WasmBridge::js_object_to_map(&val),
            Err(BridgeError::TypeMismatch(_))
        ));
    }

    #[test]
    fn test_js_object_to_map_non_string_value() {
        let mut map = HashMap::new();
        map.insert("k".to_string(), JsValue::Number(1.0));
        let val = JsValue::Object(map);
        assert!(matches!(
            WasmBridge::js_object_to_map(&val),
            Err(BridgeError::TypeMismatch(_))
        ));
    }

    // ── serialize_js_value ───────────────────────────────────────────────────

    #[test]
    fn test_serialize_undefined() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::Undefined),
            "undefined"
        );
    }

    #[test]
    fn test_serialize_null() {
        assert_eq!(WasmBridge::serialize_js_value(&JsValue::Null), "null");
    }

    #[test]
    fn test_serialize_boolean_true() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::Boolean(true)),
            "true"
        );
    }

    #[test]
    fn test_serialize_boolean_false() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::Boolean(false)),
            "false"
        );
    }

    #[test]
    fn test_serialize_integer_number() {
        assert_eq!(WasmBridge::serialize_js_value(&JsValue::Number(42.0)), "42");
    }

    #[test]
    fn test_serialize_float_number() {
        let s = WasmBridge::serialize_js_value(&JsValue::Number(2.71));
        assert!(s.contains("2.71"));
    }

    #[test]
    fn test_serialize_string() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::String("hello".to_string())),
            "\"hello\""
        );
    }

    #[test]
    fn test_serialize_string_with_quotes() {
        let s = WasmBridge::serialize_js_value(&JsValue::String("say \"hi\"".to_string()));
        assert!(s.contains("\\\""));
    }

    #[test]
    fn test_serialize_empty_array() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::Array(vec![])),
            "[]"
        );
    }

    #[test]
    fn test_serialize_array() {
        let val = JsValue::Array(vec![JsValue::Number(1.0), JsValue::Number(2.0)]);
        let s = WasmBridge::serialize_js_value(&val);
        assert!(s.starts_with('[') && s.ends_with(']'));
    }

    #[test]
    fn test_serialize_empty_object() {
        assert_eq!(
            WasmBridge::serialize_js_value(&JsValue::Object(HashMap::new())),
            "{}"
        );
    }

    #[test]
    fn test_serialize_object_with_field() {
        let mut map = HashMap::new();
        map.insert("x".to_string(), JsValue::Number(1.0));
        let val = JsValue::Object(map);
        let s = WasmBridge::serialize_js_value(&val);
        assert!(s.contains("\"x\""));
        assert!(s.contains('1'));
    }

    // ── BridgeError display ──────────────────────────────────────────────────

    #[test]
    fn test_bridge_error_type_mismatch_display() {
        let e = BridgeError::TypeMismatch("bad type".to_string());
        assert!(e.to_string().contains("Type mismatch"));
    }

    #[test]
    fn test_bridge_error_key_not_found_display() {
        let e = BridgeError::KeyNotFound("query".to_string());
        assert!(e.to_string().contains("Key not found"));
    }

    #[test]
    fn test_bridge_error_index_out_of_bounds_display() {
        let e = BridgeError::IndexOutOfBounds(5);
        assert!(e.to_string().contains("5"));
    }

    #[test]
    fn test_bridge_error_is_std_error() {
        let e: Box<dyn std::error::Error> = Box::new(BridgeError::TypeMismatch("x".to_string()));
        assert!(!e.to_string().is_empty());
    }
}