type-bridge-server 1.4.4

Query-intercepting proxy server for TypeDB with validation and audit logging
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
use typedb_driver::TransactionType;
use typedb_driver::concept::Concept;

use super::backend::{DriverBackend, QueryResultKind};
use super::real_driver::RealTypeDBBackend;
use crate::config::TypeDBSection;
use crate::error::PipelineError;
use crate::executor::QueryExecutor;

/// Wrapper around the TypeDB Rust driver providing a clean async API
/// for query execution and schema retrieval.
pub struct TypeDBClient {
    backend: Box<dyn DriverBackend>,
}

impl TypeDBClient {
    /// Connect to a TypeDB server using the provided configuration.
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub async fn connect(config: &TypeDBSection) -> Result<Self, PipelineError> {
        let backend = RealTypeDBBackend::connect(config).await?;
        Ok(Self {
            backend: Box::new(backend),
        })
    }

    /// Create a TypeDBClient with a custom backend (for testing).
    #[cfg(test)]
    pub(crate) fn with_backend(backend: Box<dyn DriverBackend>) -> Self {
        Self { backend }
    }

    /// Execute a TypeQL query and return results as JSON.
    ///
    /// For read transactions, the transaction is used directly.
    /// For write and schema transactions, the transaction is committed after execution.
    pub async fn execute(
        &self,
        database: &str,
        typeql: &str,
        tx_type: &str,
    ) -> Result<serde_json::Value, PipelineError> {
        let transaction_type = parse_transaction_type(tx_type)?;

        let mut tx = self
            .backend
            .open_transaction(database, transaction_type)
            .await?;

        let answer = tx.query(typeql).await?;

        let needs_commit = matches!(
            transaction_type,
            TransactionType::Write | TransactionType::Schema
        );

        let results = match answer {
            QueryResultKind::Ok => {
                if needs_commit {
                    tx.commit().await?;
                }
                serde_json::json!({ "ok": true })
            }
            QueryResultKind::Rows(rows) => {
                if needs_commit {
                    tx.commit().await?;
                }
                serde_json::Value::Array(rows)
            }
            QueryResultKind::Documents(docs) => {
                if needs_commit {
                    let _ = tx.commit().await;
                }
                serde_json::Value::Array(docs)
            }
        };

        Ok(results)
    }

    /// Check if the driver connection is open.
    pub fn is_connected(&self) -> bool {
        self.backend.is_open()
    }
}

impl QueryExecutor for TypeDBClient {
    fn execute<'a>(
        &'a self,
        database: &'a str,
        typeql: &'a str,
        transaction_type: &'a str,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<serde_json::Value, PipelineError>> + Send + 'a>,
    > {
        Box::pin(async move { self.execute(database, typeql, transaction_type).await })
    }

    fn is_connected(&self) -> bool {
        self.is_connected()
    }
}

/// Parse a transaction type string into a TypeDB TransactionType.
pub(crate) fn parse_transaction_type(tx_type: &str) -> Result<TransactionType, PipelineError> {
    match tx_type {
        "read" => Ok(TransactionType::Read),
        "write" => Ok(TransactionType::Write),
        "schema" => Ok(TransactionType::Schema),
        other => Err(PipelineError::QueryExecution(format!(
            "Unknown transaction type: {other}"
        ))),
    }
}

/// Convert a TypeDB Concept to a serde_json::Value.
pub(crate) fn concept_to_json(concept: &Concept) -> serde_json::Value {
    let mut obj = serde_json::Map::new();

    obj.insert(
        "category".to_string(),
        serde_json::Value::String(concept.get_category().name().to_string()),
    );
    obj.insert(
        "label".to_string(),
        serde_json::Value::String(concept.get_label().to_string()),
    );

    if let Some(iid) = concept.try_get_iid() {
        obj.insert(
            "iid".to_string(),
            serde_json::Value::String(iid.to_string()),
        );
    }

    if let Some(value) = concept.try_get_value() {
        obj.insert("value".to_string(), value_to_json(value));
    }

    if let Some(value_type) = concept.try_get_value_type() {
        obj.insert(
            "value_type".to_string(),
            serde_json::Value::String(value_type.name().to_string()),
        );
    }

    serde_json::Value::Object(obj)
}

/// Convert a TypeDB Value to a serde_json::Value.
#[cfg_attr(coverage_nightly, coverage(off))]
pub(crate) fn value_to_json(value: &typedb_driver::concept::Value) -> serde_json::Value {
    if let Some(b) = value.get_boolean() {
        return serde_json::Value::Bool(b);
    }
    if let Some(i) = value.get_integer() {
        return serde_json::json!(i);
    }
    if let Some(d) = value.get_double() {
        return serde_json::json!(d);
    }
    if let Some(s) = value.get_string() {
        return serde_json::Value::String(s.to_string());
    }
    if let Some(date) = value.get_date() {
        return serde_json::Value::String(date.to_string());
    }
    if let Some(dt) = value.get_datetime() {
        return serde_json::Value::String(dt.to_string());
    }
    if let Some(dt_tz) = value.get_datetime_tz() {
        return serde_json::Value::String(dt_tz.to_string());
    }
    if let Some(dec) = value.get_decimal() {
        return serde_json::Value::String(dec.to_string());
    }
    if let Some(dur) = value.get_duration() {
        return serde_json::Value::String(dur.to_string());
    }
    // Fallback: use Display representation
    value_to_json_fallback(value)
}

#[cfg_attr(coverage_nightly, coverage(off))]
fn value_to_json_fallback(value: &typedb_driver::concept::Value) -> serde_json::Value {
    serde_json::Value::String(value.to_string())
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    use typedb_driver::IID;
    use typedb_driver::TransactionType;
    use typedb_driver::concept::value::{Decimal, Duration, TimeZone};
    use typedb_driver::concept::{
        Attribute, AttributeType, Concept, Entity, EntityType, Value, ValueType,
    };

    use super::*;
    use crate::error::PipelineError;
    use crate::typedb::backend::TransactionOps;

    // =============================================
    // Mock infrastructure
    // =============================================

    struct MockTransaction {
        query_result: Option<QueryResultKind>,
        query_error: Option<String>,
        commit_error: Option<String>,
        committed: Arc<AtomicBool>,
        query_called: Arc<AtomicBool>,
    }

    impl MockTransaction {
        fn new(result: QueryResultKind) -> Self {
            Self {
                query_result: Some(result),
                query_error: None,
                commit_error: None,
                committed: Arc::new(AtomicBool::new(false)),
                query_called: Arc::new(AtomicBool::new(false)),
            }
        }

        fn failing_query(msg: &str) -> Self {
            Self {
                query_result: None,
                query_error: Some(msg.to_string()),
                commit_error: None,
                committed: Arc::new(AtomicBool::new(false)),
                query_called: Arc::new(AtomicBool::new(false)),
            }
        }

        fn with_commit_error(mut self, msg: &str) -> Self {
            self.commit_error = Some(msg.to_string());
            self
        }
    }

    impl TransactionOps for MockTransaction {
        fn query(
            &mut self,
            _typeql: &str,
        ) -> Pin<Box<dyn Future<Output = Result<QueryResultKind, PipelineError>> + Send + '_>>
        {
            self.query_called.store(true, Ordering::SeqCst);
            let result = self.query_result.take();
            let error = self.query_error.take();
            Box::pin(async move {
                if let Some(msg) = error {
                    return Err(PipelineError::QueryExecution(msg));
                }
                Ok(result.expect("MockTransaction::query called more than once"))
            })
        }

        fn commit(
            &mut self,
        ) -> Pin<Box<dyn Future<Output = Result<(), PipelineError>> + Send + '_>> {
            self.committed.store(true, Ordering::SeqCst);
            let error = self.commit_error.take();
            Box::pin(async move {
                if let Some(msg) = error {
                    return Err(PipelineError::QueryExecution(msg));
                }
                Ok(())
            })
        }
    }

    struct MockBackend {
        transaction: std::sync::Mutex<Option<MockTransaction>>,
        open_error: Option<String>,
        is_open: bool,
        open_called: Arc<AtomicUsize>,
    }

    impl MockBackend {
        fn new(tx: MockTransaction) -> Self {
            Self {
                transaction: std::sync::Mutex::new(Some(tx)),
                open_error: None,
                is_open: true,
                open_called: Arc::new(AtomicUsize::new(0)),
            }
        }

        fn failing(msg: &str) -> Self {
            Self {
                transaction: std::sync::Mutex::new(None),
                open_error: Some(msg.to_string()),
                is_open: true,
                open_called: Arc::new(AtomicUsize::new(0)),
            }
        }
    }

    impl DriverBackend for MockBackend {
        fn open_transaction(
            &self,
            _database: &str,
            _tx_type: TransactionType,
        ) -> Pin<Box<dyn Future<Output = Result<Box<dyn TransactionOps>, PipelineError>> + Send + '_>>
        {
            self.open_called.fetch_add(1, Ordering::SeqCst);
            let tx = self.transaction.lock().unwrap().take();
            let error = self.open_error.clone();
            Box::pin(async move {
                if let Some(msg) = error {
                    return Err(PipelineError::QueryExecution(msg));
                }
                Ok(
                    Box::new(tx.expect("MockBackend: no transaction configured"))
                        as Box<dyn TransactionOps>,
                )
            })
        }

        fn is_open(&self) -> bool {
            self.is_open
        }
    }

    fn make_client(backend: MockBackend) -> TypeDBClient {
        TypeDBClient::with_backend(Box::new(backend))
    }

    // =============================================
    // parse_transaction_type tests
    // =============================================

    #[test]
    fn parse_transaction_type_read() {
        let result = parse_transaction_type("read").unwrap();
        assert_eq!(result, TransactionType::Read);
    }

    #[test]
    fn parse_transaction_type_write() {
        let result = parse_transaction_type("write").unwrap();
        assert_eq!(result, TransactionType::Write);
    }

    #[test]
    fn parse_transaction_type_schema() {
        let result = parse_transaction_type("schema").unwrap();
        assert_eq!(result, TransactionType::Schema);
    }

    #[test]
    fn parse_transaction_type_unknown() {
        let result = parse_transaction_type("unknown");
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PipelineError::QueryExecution(msg) if msg.contains("Unknown transaction type: unknown"))
        );
    }

    #[test]
    fn parse_transaction_type_empty() {
        let result = parse_transaction_type("");
        assert!(result.is_err());
    }

    #[test]
    fn parse_transaction_type_case_sensitive() {
        let result = parse_transaction_type("Read");
        assert!(result.is_err());
    }

    // =============================================
    // execute tests (via MockBackend)
    // =============================================

    #[tokio::test]
    async fn execute_ok_read_no_commit() {
        let tx = MockTransaction::new(QueryResultKind::Ok);
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "match $x isa thing;", "read")
            .await
            .unwrap();
        assert_eq!(result, serde_json::json!({"ok": true}));
        assert!(!committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_ok_write_commits() {
        let tx = MockTransaction::new(QueryResultKind::Ok);
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "insert $x isa thing;", "write")
            .await
            .unwrap();
        assert_eq!(result, serde_json::json!({"ok": true}));
        assert!(committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_ok_schema_commits() {
        let tx = MockTransaction::new(QueryResultKind::Ok);
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "define entity thing;", "schema")
            .await
            .unwrap();
        assert_eq!(result, serde_json::json!({"ok": true}));
        assert!(committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_rows_read_no_commit() {
        let rows = vec![
            serde_json::json!({"name": "Alice"}),
            serde_json::json!({"name": "Bob"}),
        ];
        let tx = MockTransaction::new(QueryResultKind::Rows(rows.clone()));
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "match $p isa person;", "read")
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Array(rows));
        assert!(!committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_rows_write_commits() {
        let rows = vec![serde_json::json!({"id": 1})];
        let tx = MockTransaction::new(QueryResultKind::Rows(rows));
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "insert $x isa thing;", "write")
            .await
            .unwrap();
        assert!(result.is_array());
        assert!(committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_rows_data_preserved() {
        let rows = vec![
            serde_json::json!({"name": "Alice", "age": 30}),
            serde_json::json!({"name": "Bob", "age": 25}),
        ];
        let tx = MockTransaction::new(QueryResultKind::Rows(rows.clone()));
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "match $p isa person;", "read")
            .await
            .unwrap();
        let arr = result.as_array().unwrap();
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["name"], "Alice");
        assert_eq!(arr[1]["age"], 25);
    }

    #[tokio::test]
    async fn execute_docs_read_no_commit() {
        let docs = vec![serde_json::json!({"doc": "data"})];
        let tx = MockTransaction::new(QueryResultKind::Documents(docs.clone()));
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "match $p isa person; fetch {};", "read")
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Array(docs));
        assert!(!committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_docs_write_commits() {
        let docs = vec![serde_json::json!({"doc": "data"})];
        let tx = MockTransaction::new(QueryResultKind::Documents(docs));
        let committed = tx.committed.clone();
        let client = make_client(MockBackend::new(tx));

        let result = client
            .execute("db", "insert $x isa thing;", "write")
            .await
            .unwrap();
        assert!(result.is_array());
        assert!(committed.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn execute_docs_commit_error_ignored() {
        let docs = vec![serde_json::json!({"doc": "data"})];
        let tx = MockTransaction::new(QueryResultKind::Documents(docs.clone()))
            .with_commit_error("commit failed");
        let client = make_client(MockBackend::new(tx));

        // Documents + write: commit error is intentionally ignored (let _ = ...)
        let result = client
            .execute("db", "insert $x isa thing;", "write")
            .await
            .unwrap();
        assert_eq!(result, serde_json::Value::Array(docs));
    }

    #[tokio::test]
    async fn execute_transaction_open_failure() {
        let client = make_client(MockBackend::failing("connection refused"));

        let result = client.execute("db", "match $x isa thing;", "read").await;
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PipelineError::QueryExecution(msg) if msg.contains("connection refused"))
        );
    }

    #[tokio::test]
    async fn execute_query_failure() {
        let tx = MockTransaction::failing_query("syntax error");
        let client = make_client(MockBackend::new(tx));

        let result = client.execute("db", "bad query", "read").await;
        let err = result.unwrap_err();
        assert!(matches!(&err, PipelineError::QueryExecution(msg) if msg.contains("syntax error")));
    }

    #[tokio::test]
    async fn execute_commit_failure_ok_propagated() {
        let tx = MockTransaction::new(QueryResultKind::Ok).with_commit_error("commit failed");
        let client = make_client(MockBackend::new(tx));

        // Ok + write: commit error IS propagated
        let result = client.execute("db", "insert $x isa thing;", "write").await;
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PipelineError::QueryExecution(msg) if msg.contains("commit failed"))
        );
    }

    #[tokio::test]
    async fn execute_commit_failure_rows_propagated() {
        let tx =
            MockTransaction::new(QueryResultKind::Rows(vec![])).with_commit_error("commit failed");
        let client = make_client(MockBackend::new(tx));

        // Rows + write: commit error IS propagated
        let result = client.execute("db", "insert $x isa thing;", "write").await;
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PipelineError::QueryExecution(msg) if msg.contains("commit failed"))
        );
    }

    #[tokio::test]
    async fn execute_invalid_transaction_type() {
        let tx = MockTransaction::new(QueryResultKind::Ok);
        let backend = MockBackend::new(tx);
        let open_called = backend.open_called.clone();
        let client = make_client(backend);

        let result = client.execute("db", "match $x;", "invalid").await;
        assert!(result.is_err());
        // Backend should never be called if transaction type is invalid
        assert_eq!(open_called.load(Ordering::SeqCst), 0);
    }

    #[test]
    fn is_connected_delegates_to_backend() {
        let mut backend = MockBackend::new(MockTransaction::new(QueryResultKind::Ok));
        backend.is_open = true;
        let client = make_client(backend);
        assert!(client.is_connected());
    }

    #[test]
    fn is_connected_false_when_backend_closed() {
        let mut backend = MockBackend::new(MockTransaction::new(QueryResultKind::Ok));
        backend.is_open = false;
        let client = make_client(backend);
        assert!(!client.is_connected());
    }

    // =============================================
    // value_to_json tests
    // =============================================

    #[test]
    fn value_to_json_boolean_true() {
        let value = Value::Boolean(true);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::Value::Bool(true));
    }

    #[test]
    fn value_to_json_boolean_false() {
        let value = Value::Boolean(false);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::Value::Bool(false));
    }

    #[test]
    fn value_to_json_integer() {
        let value = Value::Integer(42);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::json!(42));
    }

    #[test]
    fn value_to_json_integer_negative() {
        let value = Value::Integer(-100);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::json!(-100));
    }

    #[test]
    fn value_to_json_double() {
        let value = Value::Double(3.15);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::json!(3.15));
    }

    #[test]
    fn value_to_json_string() {
        let value = Value::String("hello".to_string());
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::Value::String("hello".to_string()));
    }

    #[test]
    fn value_to_json_string_empty() {
        let value = Value::String(String::new());
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::Value::String(String::new()));
    }

    #[test]
    fn value_to_json_date() {
        let date = chrono::NaiveDate::from_ymd_opt(2024, 1, 15).unwrap();
        let value = Value::Date(date);
        let json = value_to_json(&value);
        assert_eq!(json, serde_json::Value::String("2024-01-15".to_string()));
    }

    #[test]
    fn value_to_json_datetime() {
        let dt = chrono::NaiveDate::from_ymd_opt(2024, 1, 15)
            .unwrap()
            .and_hms_opt(10, 30, 0)
            .unwrap();
        let value = Value::Datetime(dt);
        let json = value_to_json(&value);
        let s = json.as_str().unwrap();
        assert!(s.contains("2024-01-15"));
    }

    #[test]
    fn value_to_json_decimal() {
        let dec = Decimal::new(42, 0);
        let value = Value::Decimal(dec);
        let json = value_to_json(&value);
        assert!(json.is_string());
    }

    #[test]
    fn value_to_json_duration() {
        let dur = Duration::new(1, 2, 3_000_000_000);
        let value = Value::Duration(dur);
        let json = value_to_json(&value);
        assert!(json.is_string());
    }

    #[test]
    fn value_to_json_datetime_tz() {
        use chrono::TimeZone as _;
        let tz = TimeZone::Fixed(chrono::FixedOffset::east_opt(3600).unwrap());
        let dt = tz.with_ymd_and_hms(2024, 6, 15, 12, 30, 0).unwrap();
        let value = Value::DatetimeTZ(dt);
        let json = value_to_json(&value);
        let s = json.as_str().unwrap();
        assert!(s.contains("2024"));
    }

    // =============================================
    // concept_to_json tests
    // =============================================

    #[test]
    fn concept_to_json_entity_type() {
        let concept = Concept::EntityType(EntityType {
            label: "person".to_string(),
        });
        let json = concept_to_json(&concept);
        assert_eq!(json["category"], "EntityType");
        assert_eq!(json["label"], "person");
        assert!(json.get("iid").is_none());
        assert!(json.get("value").is_none());
    }

    #[test]
    fn concept_to_json_attribute_type() {
        let concept = Concept::AttributeType(AttributeType {
            label: "name".to_string(),
            value_type: Some(ValueType::String),
        });
        let json = concept_to_json(&concept);
        assert_eq!(json["category"], "AttributeType");
        assert_eq!(json["label"], "name");
        assert_eq!(json["value_type"], "string");
    }

    #[test]
    fn concept_to_json_value_boolean() {
        let concept = Concept::Value(Value::Boolean(true));
        let json = concept_to_json(&concept);
        assert_eq!(json["category"], "Value");
        assert_eq!(json["value"], true);
    }

    #[test]
    fn concept_to_json_value_integer() {
        let concept = Concept::Value(Value::Integer(42));
        let json = concept_to_json(&concept);
        assert_eq!(json["value"], 42);
    }

    #[test]
    fn concept_to_json_value_string() {
        let concept = Concept::Value(Value::String("hello".to_string()));
        let json = concept_to_json(&concept);
        assert_eq!(json["value"], "hello");
    }

    #[test]
    fn concept_to_json_entity_with_iid() {
        let iid: IID = vec![0x01, 0x02, 0x03].into();
        let concept = Concept::Entity(Entity {
            iid,
            type_: Some(EntityType {
                label: "person".to_string(),
            }),
        });
        let json = concept_to_json(&concept);
        assert_eq!(json["category"], "Entity");
        assert_eq!(json["label"], "person");
        // IID should be present
        let iid_str = json["iid"].as_str().unwrap();
        assert!(iid_str.starts_with("0x"));
    }

    #[test]
    fn concept_to_json_attribute_with_value() {
        let iid: IID = vec![0xAA, 0xBB].into();
        let concept = Concept::Attribute(Attribute {
            iid,
            value: Value::String("hello".to_string()),
            type_: Some(AttributeType {
                label: "name".to_string(),
                value_type: Some(ValueType::String),
            }),
        });
        let json = concept_to_json(&concept);
        assert_eq!(json["category"], "Attribute");
        assert_eq!(json["label"], "name");
        // Attribute IID is not exposed via try_get_iid()
        assert!(json.get("iid").is_none());
        assert_eq!(json["value"], "hello");
        assert_eq!(json["value_type"], "string");
    }

    #[test]
    fn concept_to_json_attribute_type_without_value_type() {
        let concept = Concept::AttributeType(AttributeType {
            label: "abstract_attr".to_string(),
            value_type: None,
        });
        let json = concept_to_json(&concept);
        assert_eq!(json["label"], "abstract_attr");
        assert!(json.get("value_type").is_none());
    }

    // =============================================
    // QueryExecutor trait impl tests
    // =============================================

    #[test]
    fn type_db_client_implements_query_executor() {
        fn assert_executor<T: QueryExecutor>() {}
        assert_executor::<TypeDBClient>();
    }

    #[tokio::test]
    async fn query_executor_execute_delegates_to_client() {
        let tx = MockTransaction::new(QueryResultKind::Rows(vec![serde_json::json!({"x": 1})]));
        let client = make_client(MockBackend::new(tx));
        let executor: Box<dyn QueryExecutor> = Box::new(client);

        let result = executor
            .execute("db", "match $x isa thing;", "read")
            .await
            .unwrap();
        assert!(result.is_array());
        assert_eq!(result.as_array().unwrap().len(), 1);
    }

    #[test]
    fn query_executor_is_connected_delegates_to_client() {
        let mut backend = MockBackend::new(MockTransaction::new(QueryResultKind::Ok));
        backend.is_open = true;
        let client = make_client(backend);
        let executor: Box<dyn QueryExecutor> = Box::new(client);
        assert!(executor.is_connected());
    }

    // =============================================
    // Integration tests (require running TypeDB)
    // =============================================

    #[tokio::test]
    #[ignore = "requires running TypeDB server"]
    #[cfg_attr(coverage_nightly, coverage(off))]
    async fn integration_connect_invalid_address() {
        let config = TypeDBSection {
            address: "localhost:99999".to_string(),
            database: "test".to_string(),
            username: "admin".to_string(),
            password: "password".to_string(),
        };
        let result = TypeDBClient::connect(&config).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    #[ignore = "requires running TypeDB server"]
    #[cfg_attr(coverage_nightly, coverage(off))]
    async fn integration_connect_success() {
        let config = TypeDBSection {
            address: "localhost:1729".to_string(),
            database: "test".to_string(),
            username: "admin".to_string(),
            password: "password".to_string(),
        };
        let result = TypeDBClient::connect(&config).await;
        assert!(result.is_ok());
        assert!(result.unwrap().is_connected());
    }
}