kalam-client 0.5.2-rc.2

Official Rust SDK for KalamDB — SQL, materialized live rows, and optional topic consumers
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
//! Integration tests for the Kafka-style TopicConsumer.
//!
//! These tests require a running KalamDB server with topic pub/sub support.
//! Set KALAMDB_SERVER_URL and KALAMDB_ROOT_PASSWORD environment variables.
//!
//! Run with: cargo test --test test_consumer -- --nocapture

use std::{collections::HashMap, time::Duration};

use kalam_client::{
    consumer::{AutoOffsetReset, TopicConsumer},
    AuthProvider, KalamLinkClient,
};

fn get_server_url() -> String {
    std::env::var("KALAMDB_SERVER_URL").unwrap_or_else(|_| "http://localhost:3000".to_string())
}

fn get_auth() -> AuthProvider {
    let password =
        std::env::var("KALAMDB_ROOT_PASSWORD").unwrap_or_else(|_| "admin123".to_string());
    AuthProvider::system_user_auth(password)
}

fn unique_id() -> String {
    format!(
        "test_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    )
}

async fn create_client() -> KalamLinkClient {
    KalamLinkClient::builder()
        .base_url(get_server_url())
        .auth(get_auth())
        .timeout(Duration::from_secs(30))
        .build()
        .expect("Failed to create client")
}

async fn setup_topic_and_table(client: &KalamLinkClient, topic_name: &str, table_name: &str) {
    // Create namespace if needed
    let ns = topic_name.split('.').next().unwrap_or("test");
    let _ = client
        .execute_query(&format!("CREATE NAMESPACE IF NOT EXISTS {}", ns), None, None, None)
        .await;

    // Create table
    let create_table = format!(
        "CREATE TABLE IF NOT EXISTS {} (id INT PRIMARY KEY, message TEXT, created_at TIMESTAMP \
         DEFAULT NOW())",
        table_name
    );
    let _ = client.execute_query(&create_table, None, None, None).await;

    // Create topic
    let create_topic = format!("CREATE TOPIC IF NOT EXISTS {}", topic_name);
    let _ = client.execute_query(&create_topic, None, None, None).await;

    // Add route from table to topic
    let add_route = format!(
        "ALTER TOPIC {} ADD SOURCE {} ON INSERT WITH (payload = 'full')",
        topic_name, table_name
    );
    let _ = client.execute_query(&add_route, None, None, None).await;
}

async fn insert_messages(client: &KalamLinkClient, table_name: &str, count: usize) {
    for i in 0..count {
        let sql =
            format!("INSERT INTO {} (id, message) VALUES ({}, 'Message {}')", table_name, i, i);
        client.execute_query(&sql, None, None, None).await.expect("Insert failed");
    }
}

// =============================================================================
// Unit-style tests (no server required)
// =============================================================================

#[test]
fn test_consumer_builder_requires_group_id() {
    let result = TopicConsumer::builder()
        .base_url("http://localhost:3000")
        .topic("test.topic")
        .build();

    let err = match result {
        Ok(_) => panic!("Expected error when group_id is missing"),
        Err(err) => err,
    };
    assert!(err.to_string().contains("group_id"));
}

#[test]
fn test_consumer_builder_requires_topic() {
    let result = TopicConsumer::builder()
        .base_url("http://localhost:3000")
        .group_id("test-group")
        .build();

    assert!(result.is_err());
    let err = match result {
        Ok(_) => panic!("Expected error when topic is missing"),
        Err(err) => err,
    };
    assert!(err.to_string().contains("topic"));
}

#[test]
fn test_consumer_builder_requires_base_url_without_client() {
    let result = TopicConsumer::builder().group_id("test-group").topic("test.topic").build();

    assert!(result.is_err());
    let err = match result {
        Ok(_) => panic!("Expected error when base_url is missing"),
        Err(err) => err,
    };
    assert!(err.to_string().contains("base_url"));
}

#[test]
fn test_consumer_builder_from_properties() {
    let mut props = HashMap::new();
    props.insert("group.id".to_string(), "kafka-group".to_string());
    props.insert("topic".to_string(), "kafka-topic".to_string());
    props.insert("auto.offset.reset".to_string(), "earliest".to_string());
    props.insert("enable.auto.commit".to_string(), "false".to_string());
    props.insert("max.poll.records".to_string(), "50".to_string());

    let builder = TopicConsumer::builder()
        .base_url("http://localhost:3000")
        .from_properties(&props)
        .expect("from_properties failed");

    // The builder should have absorbed the properties
    let result = builder.build();
    assert!(result.is_ok());
}

#[test]
fn test_consumer_builder_all_options() {
    let result = TopicConsumer::builder()
        .base_url("http://localhost:3000")
        .group_id("my-group")
        .topic("my.topic")
        .client_id("my-client")
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .auto_commit_interval(Duration::from_secs(10))
        .max_poll_records(50)
        .poll_timeout(Duration::from_secs(15))
        .partition_id(1)
        .request_timeout(Duration::from_secs(20))
        .retry_backoff(Duration::from_millis(200))
        .build();

    assert!(result.is_ok());
}

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

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_basic_consume_manual_commit() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 5).await;

    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .max_poll_records(10)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    // Poll for messages
    let records = consumer.poll().await.expect("Poll failed");
    assert!(!records.is_empty(), "Expected some records");

    // Mark all as processed
    for record in &records {
        consumer.mark_processed(record);
    }

    // Commit
    let commit_result = consumer.commit_sync().await.expect("Commit failed");
    assert!(commit_result.acknowledged_offset > 0);
    assert_eq!(commit_result.group_id, group_id);

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_auto_commit() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 3).await;

    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(true)
        .auto_commit_interval(Duration::from_millis(100))
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    // Poll and mark processed
    let records = consumer.poll().await.expect("Poll failed");
    for record in &records {
        consumer.mark_processed(record);
    }

    // Wait for auto-commit interval
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Trigger auto-commit via another poll
    let _ = consumer.poll_with_timeout(Duration::from_secs(1)).await;

    // Check offsets were committed
    let offsets = consumer.offsets();
    assert!(offsets.last_committed.is_some());

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_auto_offset_reset_earliest() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 3).await;

    // New group, should start from earliest
    let mut consumer = client
        .consumer()
        .group_id(format!("new_group_{}", id))
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    let records = consumer.poll().await.expect("Poll failed");
    assert!(!records.is_empty(), "Expected records from earliest");

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_auto_offset_reset_latest() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 3).await;

    // New group with latest, should get no existing messages
    let mut consumer = client
        .consumer()
        .group_id(format!("latest_group_{}", id))
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Latest)
        .enable_auto_commit(false)
        .poll_timeout(Duration::from_secs(2))
        .build()
        .expect("Consumer build failed");

    let _records = consumer.poll().await.expect("Poll failed");
    // Latest should return empty or only new messages
    // (depends on timing, but typically empty)

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_poll_with_timeout_returns_empty() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    // Don't insert any messages

    let mut consumer = client
        .consumer()
        .group_id(format!("timeout_group_{}", id))
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Latest)
        .enable_auto_commit(false)
        .build()
        .expect("Consumer build failed");

    let start = std::time::Instant::now();
    let records = consumer.poll_with_timeout(Duration::from_secs(2)).await.expect("Poll failed");

    assert!(records.is_empty());
    assert!(start.elapsed() >= Duration::from_secs(1));

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_seek_changes_position() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("seek_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 10).await;

    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .max_poll_records(5)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    // Poll first batch
    let records = consumer.poll().await.expect("Poll failed");
    assert!(!records.is_empty());
    let _first_position = consumer.position();

    // Seek back to beginning
    consumer.seek(0);
    assert_eq!(consumer.position(), 0);

    // Poll again should get same records
    let records2 = consumer.poll().await.expect("Poll failed");
    assert!(!records2.is_empty());

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_commit_persistence_across_consumers() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("persist_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 5).await;

    // First consumer
    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(false)
            .max_poll_records(3)
            .poll_timeout(Duration::from_secs(5))
            .build()
            .expect("Consumer build failed");

        let records = consumer.poll().await.expect("Poll failed");
        for record in &records {
            consumer.mark_processed(record);
        }
        consumer.commit_sync().await.expect("Commit failed");
        consumer.close().await.expect("Close failed");
    }

    // Second consumer with same group should continue from committed offset
    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(false)
            .poll_timeout(Duration::from_secs(5))
            .build()
            .expect("Consumer build failed");

        let _records = consumer.poll().await.expect("Poll failed");
        // Should get remaining messages (2 out of 5)
        // The exact count depends on server behavior
        consumer.close().await.expect("Close failed");
    }
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_high_load_multiple_messages() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("highload_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;

    // Insert many messages
    let message_count = 100;
    for i in 0..message_count {
        let sql = format!(
            "INSERT INTO {} (id, message) VALUES ({}, 'High load message {}')",
            table_name, i, i
        );
        client.execute_query(&sql, None, None, None).await.expect("Insert failed");
    }

    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .max_poll_records(20)
        .poll_timeout(Duration::from_secs(10))
        .build()
        .expect("Consumer build failed");

    let mut total_received = 0;
    let mut poll_count = 0;
    let max_polls = 10;

    while total_received < message_count && poll_count < max_polls {
        let records = consumer.poll().await.expect("Poll failed");
        if records.is_empty() {
            break;
        }
        total_received += records.len();
        for record in &records {
            consumer.mark_processed(record);
        }
        consumer.commit_sync().await.expect("Commit failed");
        poll_count += 1;
    }

    assert!(
        total_received >= message_count / 2,
        "Expected at least half the messages, got {}",
        total_received
    );

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_multiple_consumers_different_topics() {
    let client = create_client().await;
    let id = unique_id();

    let topic1 = format!("test.topic1_{}", id);
    let topic2 = format!("test.topic2_{}", id);
    let table1 = format!("test.table1_{}", id);
    let table2 = format!("test.table2_{}", id);

    setup_topic_and_table(&client, &topic1, &table1).await;
    setup_topic_and_table(&client, &topic2, &table2).await;

    insert_messages(&client, &table1, 3).await;
    insert_messages(&client, &table2, 5).await;

    // Consumer 1
    let mut consumer1 = client
        .consumer()
        .group_id(format!("multi_group1_{}", id))
        .topic(&topic1)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer 1 build failed");

    // Consumer 2
    let mut consumer2 = client
        .consumer()
        .group_id(format!("multi_group2_{}", id))
        .topic(&topic2)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer 2 build failed");

    // Poll both concurrently
    let (records1, records2) = tokio::join!(consumer1.poll(), consumer2.poll());

    let records1 = records1.expect("Poll 1 failed");
    let records2 = records2.expect("Poll 2 failed");

    // Verify no cross-contamination
    for record in &records1 {
        assert!(
            record.topic_name.contains("topic1"),
            "Topic 1 consumer got wrong topic: {}",
            record.topic_name
        );
    }
    for record in &records2 {
        assert!(
            record.topic_name.contains("topic2"),
            "Topic 2 consumer got wrong topic: {}",
            record.topic_name
        );
    }

    consumer1.close().await.expect("Close 1 failed");
    consumer2.close().await.expect("Close 2 failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_reconnect_after_disconnect() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("reconnect_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 5).await;

    // First consumer - consume but don't commit all
    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(false)
            .max_poll_records(2)
            .poll_timeout(Duration::from_secs(5))
            .build()
            .expect("Consumer build failed");

        let records = consumer.poll().await.expect("Poll failed");
        // Process and commit only first batch
        for record in &records {
            consumer.mark_processed(record);
        }
        consumer.commit_sync().await.expect("Commit failed");
        // Close without consuming remaining
        consumer.close().await.expect("Close failed");
    }

    // Simulate disconnect by creating new consumer with same group
    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(false)
            .poll_timeout(Duration::from_secs(5))
            .build()
            .expect("Consumer build failed");

        // Should receive remaining messages (at-least-once)
        let records = consumer.poll().await.expect("Poll failed");
        // Verify we got messages starting from committed offset
        assert!(
            !records.is_empty() || consumer.position() > 0,
            "Should have remaining messages or position > 0"
        );

        consumer.close().await.expect("Close failed");
    }
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_consumer_from_client_convenience() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;

    // Use the client.consumer() convenience method
    let consumer = client
        .consumer()
        .group_id(format!("convenience_group_{}", id))
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .build();

    assert!(consumer.is_ok(), "Consumer should build from client");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_max_poll_records_respected() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("maxpoll_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 20).await;

    let max_records = 5;
    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .max_poll_records(max_records)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    let records = consumer.poll().await.expect("Poll failed");
    assert!(
        records.len() <= max_records as usize,
        "Got {} records, expected at most {}",
        records.len(),
        max_records
    );

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_commit_async() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("async_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 3).await;

    let mut consumer = client
        .consumer()
        .group_id(&group_id)
        .topic(&topic_name)
        .auto_offset_reset(AutoOffsetReset::Earliest)
        .enable_auto_commit(false)
        .poll_timeout(Duration::from_secs(5))
        .build()
        .expect("Consumer build failed");

    let records = consumer.poll().await.expect("Poll failed");
    for record in &records {
        consumer.mark_processed(record);
    }

    // Async commit should return immediately
    consumer.commit_async().await.expect("Commit async failed");

    // Give it time to complete
    tokio::time::sleep(Duration::from_millis(500)).await;

    consumer.close().await.expect("Close failed");
}

#[tokio::test]
#[ignore = "Requires running KalamDB server"]
async fn test_close_flushes_auto_commit() {
    let client = create_client().await;
    let id = unique_id();
    let topic_name = format!("test.topic_{}", id);
    let table_name = format!("test.table_{}", id);
    let group_id = format!("closeflush_group_{}", id);

    setup_topic_and_table(&client, &topic_name, &table_name).await;
    insert_messages(&client, &table_name, 3).await;

    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(true)
            .auto_commit_interval(Duration::from_secs(300)) // Long interval
            .poll_timeout(Duration::from_secs(5))
            .build()
            .expect("Consumer build failed");

        let records = consumer.poll().await.expect("Poll failed");
        for record in &records {
            consumer.mark_processed(record);
        }

        // Close should flush commits even though interval hasn't elapsed
        consumer.close().await.expect("Close failed");
    }

    // New consumer should not see the same messages
    {
        let mut consumer = client
            .consumer()
            .group_id(&group_id)
            .topic(&topic_name)
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .enable_auto_commit(false)
            .poll_timeout(Duration::from_secs(2))
            .build()
            .expect("Consumer build failed");

        // Position should be after the committed offset
        // (this depends on server behavior)
        consumer.close().await.expect("Close failed");
    }
}

#[test]
fn test_offset_tracking() {
    use kalam_client::consumer::ConsumerRecord;

    // Create a mock record
    let record = ConsumerRecord {
        topic_id: "topic-123".to_string(),
        topic_name: "test.topic".to_string(),
        partition_id: 0,
        offset: 42,
        message_id: Some("msg-1".to_string()),
        source_table: "test.table".to_string(),
        op: kalam_client::TopicOp::Insert,
        timestamp_ms: 1700000000000,
        payload_mode: kalam_client::PayloadMode::Full,
        payload: vec![1, 2, 3],
    };

    assert_eq!(record.offset, 42);
    assert_eq!(record.partition_id, 0);
    assert_eq!(record.topic_name, "test.topic");
}