vibesql-server 0.1.3

Network server with PostgreSQL wire protocol for VibeSQL
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
//! Tests for subscription manager functionality.

use std::collections::HashSet;

use tokio::sync::mpsc;
use vibesql_storage::Database;
use vibesql_types::SqlValue;

use super::SubscriptionManager;
use crate::subscription::{
    Subscription, SubscriptionConfig, SubscriptionError, SubscriptionId, SubscriptionUpdate,
};

fn setup_test_db() -> Database {
    let mut db = Database::new();

    // Create test tables
    let create_users = vibesql_parser::Parser::parse_sql(
        "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100), active BOOLEAN)",
    )
    .unwrap();
    if let vibesql_ast::Statement::CreateTable(stmt) = create_users {
        vibesql_executor::CreateTableExecutor::execute(&stmt, &mut db).unwrap();
    }

    let create_orders = vibesql_parser::Parser::parse_sql(
        "CREATE TABLE orders (id INT PRIMARY KEY, user_id INT, amount INT)",
    )
    .unwrap();
    if let vibesql_ast::Statement::CreateTable(stmt) = create_orders {
        vibesql_executor::CreateTableExecutor::execute(&stmt, &mut db).unwrap();
    }

    db
}

#[test]
fn test_subscribe_simple() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let result = manager.subscribe("SELECT * FROM users".to_string(), tx);
    assert!(result.is_ok());

    let _id = result.unwrap();
    assert_eq!(manager.subscription_count(), 1);

    // Check table index
    let watched = manager.watched_tables();
    assert_eq!(watched.len(), 1);
    assert!(watched.iter().any(|(t, c)| t == "users" && *c == 1));
}

#[test]
fn test_subscribe_with_join() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let result = manager
        .subscribe("SELECT * FROM users u JOIN orders o ON u.id = o.user_id".to_string(), tx);
    assert!(result.is_ok());

    // Should be indexed under both tables
    let watched = manager.watched_tables();
    assert_eq!(watched.len(), 2);
    assert!(watched.iter().any(|(t, _)| t == "users"));
    assert!(watched.iter().any(|(t, _)| t == "orders"));
}

#[test]
fn test_unsubscribe() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();
    assert_eq!(manager.subscription_count(), 1);

    manager.unsubscribe(id);
    assert_eq!(manager.subscription_count(), 0);

    // Table index should be empty
    let watched = manager.watched_tables();
    assert!(watched.iter().all(|(_, c)| *c == 0));
}

#[test]
fn test_invalid_query() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let result = manager.subscribe("SELECT * FROM".to_string(), tx);
    assert!(result.is_err());
    assert!(matches!(result, Err(SubscriptionError::ParseError(_))));
}

#[test]
fn test_query_without_tables() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    // SELECT without FROM should fail
    let result = manager.subscribe("SELECT 1 + 1".to_string(), tx);
    assert!(result.is_err());
}

#[tokio::test]
async fn test_handle_change_notifies_subscribers() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let db = setup_test_db();

    // Subscribe to users table
    let _id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Simulate a change to users table
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Insert {
                table_name: "users".to_string(),
                row_index: 0,
            },
            &db,
        )
        .await;

    // Should receive a notification (empty result since table is empty)
    let update = rx.try_recv();
    assert!(update.is_ok());

    match update.unwrap() {
        SubscriptionUpdate::Full { rows, .. } => {
            // Table is empty, so no rows
            assert!(rows.is_empty());
        }
        _ => panic!("Expected Full update"),
    }
}

#[tokio::test]
async fn test_handle_change_ignores_unrelated_tables() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let db = setup_test_db();

    // Subscribe to users table
    let _id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Simulate a change to orders table (not subscribed)
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Insert {
                table_name: "orders".to_string(),
                row_index: 0,
            },
            &db,
        )
        .await;

    // Should NOT receive a notification
    let update = rx.try_recv();
    assert!(update.is_err()); // Channel should be empty
}

#[tokio::test]
async fn test_send_initial_results() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let mut db = setup_test_db();

    // Insert some data
    let insert =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (1, 'Alice', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Subscribe
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Send initial results
    manager.send_initial_results(id, &db).await.unwrap();

    // Should receive initial data
    let update = rx.recv().await.unwrap();
    match update {
        SubscriptionUpdate::Full { rows, .. } => {
            assert_eq!(rows.len(), 1);
            assert_eq!(rows[0].values[0], SqlValue::Integer(1));
        }
        _ => panic!("Expected Full update"),
    }
}

#[tokio::test]
async fn test_results_changed_detection() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let mut db = setup_test_db();

    // Subscribe before any data
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Send initial (empty) results
    manager.send_initial_results(id, &db).await.unwrap();
    let _ = rx.recv().await; // Consume initial

    // Insert data
    let insert =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (1, 'Alice', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Trigger change notification
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Insert {
                table_name: "users".to_string(),
                row_index: 0,
            },
            &db,
        )
        .await;

    // Should receive update with new data (as Delta since we have previous results)
    let update = rx.recv().await.unwrap();
    match update {
        SubscriptionUpdate::Delta { inserts, updates, deletes, .. } => {
            // The inserted row should appear as an insert
            assert_eq!(inserts.len(), 1);
            assert!(updates.is_empty());
            assert!(deletes.is_empty());
        }
        SubscriptionUpdate::Full { rows, .. } => {
            // Also acceptable if Full is sent
            assert_eq!(rows.len(), 1);
        }
        _ => panic!("Expected Delta or Full update"),
    }
}

#[tokio::test]
async fn test_no_notification_when_unchanged() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let db = setup_test_db();

    // Subscribe (empty table)
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Send initial results
    manager.send_initial_results(id, &db).await.unwrap();
    let _ = rx.recv().await; // Consume initial

    // Trigger change (but data didn't actually change since we didn't insert)
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Insert {
                table_name: "users".to_string(),
                row_index: 0,
            },
            &db,
        )
        .await;

    // Should NOT receive notification (results haven't changed)
    let update = rx.try_recv();
    assert!(update.is_err()); // Channel should be empty
}

#[test]
fn test_multiple_subscriptions_same_table() {
    let manager = SubscriptionManager::new();
    let (tx1, _rx1) = mpsc::channel(16);
    let (tx2, _rx2) = mpsc::channel(16);

    let _id1 = manager.subscribe("SELECT * FROM users".to_string(), tx1).unwrap();
    let _id2 =
        manager.subscribe("SELECT * FROM users WHERE active = TRUE".to_string(), tx2).unwrap();

    assert_eq!(manager.subscription_count(), 2);

    // Both should be indexed under users
    let watched = manager.watched_tables();
    let users_entry = watched.iter().find(|(t, _)| t == "users").unwrap();
    assert_eq!(users_entry.1, 2);
}

#[tokio::test]
async fn test_delta_update_on_insert() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let mut db = setup_test_db();

    // Insert initial data
    let insert =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (1, 'Alice', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Subscribe and get initial results
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();
    manager.send_initial_results(id, &db).await.unwrap();

    // Consume initial Full update
    let initial = rx.recv().await.unwrap();
    match initial {
        SubscriptionUpdate::Full { rows, .. } => {
            assert_eq!(rows.len(), 1);
        }
        _ => panic!("Expected Full update for initial results"),
    }

    // Insert another row
    let insert2 =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (2, 'Bob', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert2 {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Trigger change notification
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Insert {
                table_name: "users".to_string(),
                row_index: 1,
            },
            &db,
        )
        .await;

    // Should receive a Delta update (not Full)
    let update = rx.recv().await.unwrap();
    match update {
        SubscriptionUpdate::Delta { inserts, updates, deletes, .. } => {
            assert_eq!(inserts.len(), 1);
            assert_eq!(inserts[0].values[0], SqlValue::Integer(2));
            assert!(updates.is_empty());
            assert!(deletes.is_empty());
        }
        SubscriptionUpdate::Full { .. } => {
            panic!("Expected Delta update, got Full");
        }
        _ => panic!("Unexpected update type"),
    }
}

#[tokio::test]
async fn test_delta_update_on_delete() {
    let manager = SubscriptionManager::new();
    let (tx, mut rx) = mpsc::channel(16);
    let mut db = setup_test_db();

    // Insert initial data
    let insert1 =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (1, 'Alice', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert1 {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }
    let insert2 =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (2, 'Bob', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert2 {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Subscribe and get initial results
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();
    manager.send_initial_results(id, &db).await.unwrap();

    // Consume initial Full update
    let initial = rx.recv().await.unwrap();
    match initial {
        SubscriptionUpdate::Full { rows, .. } => {
            assert_eq!(rows.len(), 2);
        }
        _ => panic!("Expected Full update for initial results"),
    }

    // Delete a row
    let delete = vibesql_parser::Parser::parse_sql("DELETE FROM users WHERE id = 2").unwrap();
    if let vibesql_ast::Statement::Delete(stmt) = delete {
        vibesql_executor::DeleteExecutor::execute(&stmt, &mut db).unwrap();
    }

    // Trigger change notification
    manager
        .handle_change(
            vibesql_storage::ChangeEvent::Delete {
                table_name: "users".to_string(),
                row_index: 1,
            },
            &db,
        )
        .await;

    // Should receive a Delta update with delete
    let update = rx.recv().await.unwrap();
    match update {
        SubscriptionUpdate::Delta { inserts, updates, deletes, .. } => {
            assert!(inserts.is_empty());
            assert!(updates.is_empty());
            assert_eq!(deletes.len(), 1);
            assert_eq!(deletes[0].values[0], SqlValue::Integer(2));
        }
        SubscriptionUpdate::Full { .. } => {
            panic!("Expected Delta update, got Full");
        }
        _ => panic!("Unexpected update type"),
    }
}

#[test]
fn test_global_limit_exceeded() {
    // Create manager with very low global limit for testing
    let config = SubscriptionConfig {
        max_per_connection: 100,
        max_global: 2,
        max_result_rows: 10000,
        rate_limit_per_second: 100,
        ..Default::default()
    };
    let manager = SubscriptionManager::with_config(config);

    // First two subscriptions should succeed
    let (tx1, _rx1) = mpsc::channel(16);
    let (tx2, _rx2) = mpsc::channel(16);
    let (tx3, _rx3) = mpsc::channel(16);

    manager.subscribe("SELECT * FROM users".to_string(), tx1).unwrap();
    manager.subscribe("SELECT * FROM users WHERE id = 1".to_string(), tx2).unwrap();

    // Third subscription should fail with global limit exceeded
    let result = manager.subscribe("SELECT * FROM users WHERE id = 2".to_string(), tx3);
    assert!(matches!(
        result,
        Err(SubscriptionError::GlobalLimitExceeded { current: 2, max: 2 })
    ));

    // Metrics should reflect the limit exceeded event
    assert_eq!(manager.limit_exceeded_count(), 1);
}

#[tokio::test]
async fn test_result_set_too_large() {
    // Create manager with very low result limit for testing
    let config = SubscriptionConfig {
        max_per_connection: 100,
        max_global: 10000,
        max_result_rows: 0, // No rows allowed
        rate_limit_per_second: 100,
        ..Default::default()
    };
    let manager = SubscriptionManager::with_config(config);
    let mut db = setup_test_db();

    // Insert some data
    let insert =
        vibesql_parser::Parser::parse_sql("INSERT INTO users VALUES (1, 'Alice', TRUE)").unwrap();
    if let vibesql_ast::Statement::Insert(stmt) = insert {
        vibesql_executor::InsertExecutor::execute(&mut db, &stmt).unwrap();
    }

    // Subscribe
    let (tx, _rx) = mpsc::channel(16);
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Send initial results should fail due to result set too large
    let result = manager.send_initial_results(id, &db).await;
    assert!(matches!(result, Err(SubscriptionError::ResultSetTooLarge { rows: 1, max: 0 })));

    // Metrics should reflect the result set exceeded event
    assert_eq!(manager.result_set_exceeded_count(), 1);
}

#[test]
fn test_retry_policy_backoff_calculation() {
    use crate::subscription::SubscriptionRetryPolicy;

    let policy = SubscriptionRetryPolicy {
        max_retries: 3,
        base_delay_ms: 1000,
        max_delay_ms: 30000,
        backoff_multiplier: 2.0,
    };

    // First retry: 1000ms
    let backoff0 = policy.calculate_backoff(0);
    assert_eq!(backoff0.as_millis(), 1000);

    // Second retry: 2000ms
    let backoff1 = policy.calculate_backoff(1);
    assert_eq!(backoff1.as_millis(), 2000);

    // Third retry: 4000ms
    let backoff2 = policy.calculate_backoff(2);
    assert_eq!(backoff2.as_millis(), 4000);

    // Fourth retry: 8000ms
    let backoff3 = policy.calculate_backoff(3);
    assert_eq!(backoff3.as_millis(), 8000);

    // High attempt should be capped at max_delay_ms
    let backoff10 = policy.calculate_backoff(10);
    assert_eq!(backoff10.as_millis(), 30000);
}

#[test]
fn test_subscription_retry_count_initialization() {
    let (tx, _rx) = mpsc::channel(16);
    let tables: std::collections::HashSet<_> = vec!["users".to_string()].into_iter().collect();

    let sub = Subscription::new("SELECT * FROM users".to_string(), tables, tx);
    assert_eq!(sub.retry_count, 0);
}

#[test]
fn test_subscription_with_custom_policy() {
    use crate::subscription::SubscriptionRetryPolicy;

    let (tx, _rx) = mpsc::channel(16);
    let tables: std::collections::HashSet<_> = vec!["users".to_string()].into_iter().collect();

    let policy = SubscriptionRetryPolicy {
        max_retries: 5,
        base_delay_ms: 500,
        max_delay_ms: 60000,
        backoff_multiplier: 3.0,
    };

    let sub =
        Subscription::with_policy("SELECT * FROM users".to_string(), tables, tx, policy.clone());

    assert_eq!(sub.retry_count, 0);
    assert_eq!(sub.retry_policy.max_retries, 5);
    assert_eq!(sub.retry_policy.base_delay_ms, 500);
}

#[test]
fn test_subscription_backpressure_fields_initialization() {
    let (tx, _rx) = mpsc::channel(16);
    let tables: std::collections::HashSet<_> = vec!["users".to_string()].into_iter().collect();

    let sub = Subscription::new("SELECT * FROM users".to_string(), tables, tx);

    // Verify backpressure tracking fields are initialized
    assert_eq!(sub.updates_sent, 0);
    assert_eq!(sub.updates_dropped, 0);
    assert_eq!(sub.channel_buffer_size, 64); // default
    assert_eq!(sub.slow_consumer_threshold_percent, 80); // default
}

#[test]
fn test_subscription_with_config_backpressure() {
    use crate::subscription::SubscriptionConfig;

    let (tx, _rx) = mpsc::channel(16);
    let tables: std::collections::HashSet<_> = vec!["users".to_string()].into_iter().collect();

    let config = SubscriptionConfig {
        max_per_connection: 100,
        max_global: 10000,
        max_result_rows: 10000,
        rate_limit_per_second: 100,
        channel_buffer_size: 128,
        slow_consumer_threshold_percent: 90,
        selective_updates: Default::default(),
    };

    let sub = Subscription::with_config("SELECT * FROM users".to_string(), tables, tx, &config);

    // Verify config values are applied
    assert_eq!(sub.updates_sent, 0);
    assert_eq!(sub.updates_dropped, 0);
    assert_eq!(sub.channel_buffer_size, 128);
    assert_eq!(sub.slow_consumer_threshold_percent, 90);
}

#[test]
fn test_get_subscription_metrics() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Get metrics for the subscription
    let metrics = manager.get_subscription_metrics(id);
    assert!(metrics.is_some());

    let metrics = metrics.unwrap();
    assert_eq!(metrics.subscription_id, Some(id));
    assert_eq!(metrics.updates_sent, 0);
    assert_eq!(metrics.updates_dropped, 0);
    assert_eq!(metrics.channel_buffer_size, 64);
    assert_eq!(metrics.slow_consumer_threshold_percent, 80);
}

#[test]
fn test_get_subscription_metrics_not_found() {
    let manager = SubscriptionManager::new();

    // Get metrics for a non-existent subscription
    let fake_id = SubscriptionId::new();
    let metrics = manager.get_subscription_metrics(fake_id);
    assert!(metrics.is_none());
}

#[test]
fn test_get_all_metrics() {
    let manager = SubscriptionManager::new();
    let (tx1, _rx1) = mpsc::channel(16);
    let (tx2, _rx2) = mpsc::channel(16);

    manager.subscribe("SELECT * FROM users".to_string(), tx1).unwrap();
    manager.subscribe("SELECT * FROM orders".to_string(), tx2).unwrap();

    // Get all metrics
    let all_metrics = manager.get_all_metrics();
    assert_eq!(all_metrics.len(), 2);

    // Verify each subscription has metrics
    for metrics in all_metrics {
        assert!(metrics.subscription_id.is_some());
        assert_eq!(metrics.updates_sent, 0);
        assert_eq!(metrics.updates_dropped, 0);
    }
}

#[test]
fn test_get_all_metrics_empty() {
    let manager = SubscriptionManager::new();

    // Get all metrics when no subscriptions exist
    let all_metrics = manager.get_all_metrics();
    assert!(all_metrics.is_empty());
}

// ========================================================================
// Tests for connection tracking methods
// ========================================================================

#[test]
fn test_subscribe_for_connection() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let connection_id = "conn-1".to_string();
    let wire_id: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    let result = manager.subscribe_for_connection(
        "SELECT * FROM users".to_string(),
        tx,
        connection_id.clone(),
        wire_id,
        tables,
        None, // no filter
    );

    assert!(result.is_ok());
    let id = result.unwrap();

    // Verify subscription was registered
    assert_eq!(manager.subscription_count(), 1);
    assert_eq!(manager.connection_subscription_count(&connection_id), 1);

    // Verify wire ID index works
    let found_id = manager.find_subscription_by_wire_id(&wire_id);
    assert_eq!(found_id, Some(id));
}

#[test]
fn test_subscribe_for_connection_limit_enforcement() {
    use crate::subscription::SubscriptionConfig;

    let config = SubscriptionConfig { max_per_connection: 2, max_global: 10, ..Default::default() };
    let manager = SubscriptionManager::with_config(config);
    let connection_id = "conn-1".to_string();

    // Create 2 subscriptions (at the limit)
    for i in 0..2 {
        let (tx, _rx) = mpsc::channel(16);
        let mut wire_id = [0u8; 16];
        wire_id[0] = i;
        let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

        let result = manager.subscribe_for_connection(
            format!("SELECT {} FROM users", i),
            tx,
            connection_id.clone(),
            wire_id,
            tables,
            None,
        );
        assert!(result.is_ok(), "Subscription {} should succeed", i);
    }

    assert_eq!(manager.connection_subscription_count(&connection_id), 2);

    // Third subscription should fail (connection limit)
    let (tx, _rx) = mpsc::channel(16);
    let wire_id = [2u8; 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    let result = manager.subscribe_for_connection(
        "SELECT 3 FROM users".to_string(),
        tx,
        connection_id.clone(),
        wire_id,
        tables,
        None,
    );

    assert!(result.is_err());
    match result.unwrap_err() {
        crate::subscription::SubscriptionError::ConnectionLimitExceeded { current, max } => {
            assert_eq!(current, 2);
            assert_eq!(max, 2);
        }
        e => panic!("Expected ConnectionLimitExceeded, got {:?}", e),
    }
}

#[test]
fn test_unsubscribe_by_wire_id() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let connection_id = "conn-1".to_string();
    let wire_id: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    manager
        .subscribe_for_connection(
            "SELECT * FROM users".to_string(),
            tx,
            connection_id.clone(),
            wire_id,
            tables,
            None,
        )
        .unwrap();

    assert_eq!(manager.subscription_count(), 1);
    assert_eq!(manager.connection_subscription_count(&connection_id), 1);

    // Unsubscribe by wire ID
    manager.unsubscribe_by_wire_id(&wire_id);

    assert_eq!(manager.subscription_count(), 0);
    assert_eq!(manager.connection_subscription_count(&connection_id), 0);

    // Wire ID should no longer be found
    assert!(manager.find_subscription_by_wire_id(&wire_id).is_none());
}

#[test]
fn test_unsubscribe_all_for_connection() {
    let manager = SubscriptionManager::new();
    let connection_id1 = "conn-1".to_string();
    let connection_id2 = "conn-2".to_string();

    // Create subscriptions for connection 1
    for i in 0..3 {
        let (tx, _rx) = mpsc::channel(16);
        let mut wire_id = [0u8; 16];
        wire_id[0] = i;
        let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

        manager
            .subscribe_for_connection(
                format!("SELECT {} FROM users", i),
                tx,
                connection_id1.clone(),
                wire_id,
                tables,
                None,
            )
            .unwrap();
    }

    // Create subscriptions for connection 2
    for i in 0..2 {
        let (tx, _rx) = mpsc::channel(16);
        let mut wire_id = [10u8; 16];
        wire_id[0] = i + 10;
        let tables: HashSet<String> = ["orders".to_string()].into_iter().collect();

        manager
            .subscribe_for_connection(
                format!("SELECT {} FROM orders", i),
                tx,
                connection_id2.clone(),
                wire_id,
                tables,
                None,
            )
            .unwrap();
    }

    assert_eq!(manager.subscription_count(), 5);
    assert_eq!(manager.connection_subscription_count(&connection_id1), 3);
    assert_eq!(manager.connection_subscription_count(&connection_id2), 2);

    // Unsubscribe all for connection 1
    manager.unsubscribe_all_for_connection(&connection_id1);

    assert_eq!(manager.subscription_count(), 2);
    assert_eq!(manager.connection_subscription_count(&connection_id1), 0);
    assert_eq!(manager.connection_subscription_count(&connection_id2), 2);
}

#[test]
fn test_connection_subscription_count() {
    let manager = SubscriptionManager::new();
    let connection_id = "conn-1".to_string();

    // Initially zero
    assert_eq!(manager.connection_subscription_count(&connection_id), 0);

    // Add a subscription
    let (tx, _rx) = mpsc::channel(16);
    let wire_id: [u8; 16] = [1u8; 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    manager
        .subscribe_for_connection(
            "SELECT * FROM users".to_string(),
            tx,
            connection_id.clone(),
            wire_id,
            tables,
            None,
        )
        .unwrap();

    assert_eq!(manager.connection_subscription_count(&connection_id), 1);

    // Non-existent connection should return 0
    assert_eq!(manager.connection_subscription_count("non-existent"), 0);
}

#[test]
fn test_get_affected_subscriptions_for_wire_protocol() {
    let manager = SubscriptionManager::new();
    let connection_id = "conn-1".to_string();

    // Create a subscription for "users" table
    let (tx, _rx) = mpsc::channel(16);
    let wire_id: [u8; 16] = [1u8; 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    manager
        .subscribe_for_connection(
            "SELECT * FROM users".to_string(),
            tx,
            connection_id.clone(),
            wire_id,
            tables,
            None,
        )
        .unwrap();

    // Query for affected subscriptions
    let affected = manager.get_affected_subscriptions_for_wire_protocol("users");
    assert_eq!(affected.len(), 1);
    assert_eq!(affected[0].0, wire_id);
    assert_eq!(affected[0].1, "SELECT * FROM users");

    // Query for non-existent table
    let affected = manager.get_affected_subscriptions_for_wire_protocol("orders");
    assert!(affected.is_empty());
}

#[test]
fn test_update_result_by_wire_id() {
    let manager = SubscriptionManager::new();
    let connection_id = "conn-1".to_string();

    let (tx, _rx) = mpsc::channel(16);
    let wire_id: [u8; 16] = [1u8; 16];
    let tables: HashSet<String> = ["users".to_string()].into_iter().collect();

    manager
        .subscribe_for_connection(
            "SELECT * FROM users".to_string(),
            tx,
            connection_id.clone(),
            wire_id,
            tables,
            None,
        )
        .unwrap();

    // Update the result
    let rows = vec![crate::Row { values: vec![vibesql_types::SqlValue::Integer(42)] }];
    manager.update_result_by_wire_id(&wire_id, 12345, rows.clone());

    // Verify the result was stored
    let affected = manager.get_affected_subscriptions_for_wire_protocol("users");
    assert_eq!(affected.len(), 1);
    assert_eq!(affected[0].2, 12345); // last_result_hash
    assert!(affected[0].3.is_some()); // last_result
    assert_eq!(affected[0].3.as_ref().unwrap().len(), 1);
}

#[test]
fn test_update_pk_columns_with_eligibility() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    // Create a basic subscription (non-connection based)
    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Initially not selective eligible
    let sub = manager.subscriptions.get(&id).unwrap();
    assert!(!sub.selective_eligible);
    assert_eq!(sub.pk_columns, vec![0]); // Default
    drop(sub);

    // Update PK columns with confident detection
    let newly_eligible = manager.update_pk_columns_with_eligibility(id, vec![0, 1], true);
    assert!(newly_eligible, "Should be newly eligible");

    // Verify changes
    let sub = manager.subscriptions.get(&id).unwrap();
    assert!(sub.selective_eligible);
    assert_eq!(sub.pk_columns, vec![0, 1]);
    drop(sub);

    // Updating again should return false (not newly eligible)
    let newly_eligible2 = manager.update_pk_columns_with_eligibility(id, vec![0, 1], true);
    assert!(!newly_eligible2, "Should not be newly eligible since already was");

    // Unsubscribe should return true (was selective eligible)
    let was_eligible = manager.unsubscribe(id);
    assert!(was_eligible);
}

#[test]
fn test_update_pk_columns_with_eligibility_not_confident() {
    let manager = SubscriptionManager::new();
    let (tx, _rx) = mpsc::channel(16);

    let id = manager.subscribe("SELECT * FROM users".to_string(), tx).unwrap();

    // Update with non-confident detection
    let newly_eligible = manager.update_pk_columns_with_eligibility(id, vec![0, 2], false);
    assert!(!newly_eligible, "Should not be eligible when not confident");

    // Verify PK columns updated but not selective eligible
    let sub = manager.subscriptions.get(&id).unwrap();
    assert!(!sub.selective_eligible);
    assert_eq!(sub.pk_columns, vec![0, 2]);
    drop(sub);

    // Unsubscribe should return false (was not selective eligible)
    let was_eligible = manager.unsubscribe(id);
    assert!(!was_eligible);
}