rekuiper-server 1.0.0-rc4

REST API, OpenAPI routes, Prometheus metrics, and streaming hub for rekuiper
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
use anyhow::Result;
use async_trait::async_trait;
use rekuiper_conf::KuiperConfig;
use rekuiper_core::{KvStore, RuleManager, SqliteKvStore, StreamBus, StreamManager, TableManager};
use rekuiper_server::routes::{create_router, load_config_maps, restore_running_rules, AppState};
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;

struct TestServer {
    base_url: String,
    _state: AppState,
    handle: tokio::task::JoinHandle<()>,
}

impl TestServer {
    async fn start(kv: Arc<dyn KvStore>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();

        let stream_bus = StreamBus::new();
        let stream_manager = StreamManager::new_with_kv(kv.clone());
        let table_manager = TableManager::new_with_kv(kv.clone());
        let rule_manager = RuleManager::new_with_kv(stream_bus.clone(), kv.clone());

        let _ = stream_manager.load_from_kv(&kv).await;
        let _ = table_manager.load_from_kv(&kv).await;
        let _ = rule_manager.load_from_kv(&kv).await;

        let state = AppState {
            kv: Some(kv),
            ..AppState::new(
                "test".to_string(),
                KuiperConfig::default(),
                stream_manager,
                table_manager,
                rule_manager,
                stream_bus,
            )
        };

        load_config_maps(&state).await.unwrap();
        restore_running_rules(&state).await;

        let app = create_router(state.clone());
        let handle = tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        Self {
            base_url: format!("http://{}", addr),
            _state: state,
            handle,
        }
    }

    async fn shutdown(self) {
        self.handle.abort();
    }
}

struct TempFile(PathBuf);
impl TempFile {
    fn new(name: &str) -> Self {
        let mut p = std::env::temp_dir();
        p.push(format!(
            "rekuiper_test_{}_{}.json",
            name,
            uuid::Uuid::new_v4()
        ));
        let _ = std::fs::remove_file(&p);
        Self(p)
    }

    fn path_str(&self) -> String {
        self.0.to_string_lossy().to_string().replace('\\', "/")
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.0);
    }
}

struct TempDb(PathBuf);
impl TempDb {
    fn new(name: &str) -> Self {
        let mut p = std::env::temp_dir();
        p.push(format!("rekuiper_db_{}_{}.db", name, uuid::Uuid::new_v4()));
        let _ = std::fs::remove_file(&p);
        Self(p)
    }
}

impl Drop for TempDb {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.0);
        let mut wal = self.0.clone();
        wal.set_extension("db-wal");
        let _ = std::fs::remove_file(&wal);
        let mut shm = self.0.clone();
        shm.set_extension("db-shm");
        let _ = std::fs::remove_file(&shm);
    }
}

struct FaultyKvStore {
    inner: Arc<dyn KvStore>,
    fail_writes: Arc<AtomicBool>,
}

#[async_trait]
impl KvStore for FaultyKvStore {
    async fn get(&self, namespace: &str, key: &str) -> Result<Option<String>> {
        self.inner.get(namespace, key).await
    }
    async fn set(&self, namespace: &str, key: &str, val: &str) -> Result<()> {
        if self.fail_writes.load(Ordering::SeqCst) {
            anyhow::bail!("Injected KV storage failure on write");
        }
        self.inner.set(namespace, key, val).await
    }
    async fn delete(&self, namespace: &str, key: &str) -> Result<()> {
        if self.fail_writes.load(Ordering::SeqCst) {
            anyhow::bail!("Injected KV storage failure on delete");
        }
        self.inner.delete(namespace, key).await
    }
    async fn list_all(&self, namespace: &str) -> Result<Vec<(String, String)>> {
        self.inner.list_all(namespace).await
    }
}

async fn wait_for_file_lines(path: &Path, count: usize) -> Vec<Value> {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    loop {
        if let Ok(content) = tokio::fs::read_to_string(path).await {
            let lines: Vec<Value> = content
                .lines()
                .filter(|l| !l.trim().is_empty())
                .filter_map(|l| serde_json::from_str(l).ok())
                .collect();
            if lines.len() >= count {
                return lines;
            }
        }
        if tokio::time::Instant::now() >= deadline {
            panic!("Timed out waiting for {} lines in {:?}", count, path);
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

#[tokio::test]
async fn test_update_running_rule_resumes_processing_and_persists_status() {
    let db = TempDb::new("update_running");
    let kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&db.0).await.unwrap());
    let server = TestServer::start(kv.clone()).await;
    let client = reqwest::Client::new();
    let out = TempFile::new("out_update_running");

    // 1. Create stream
    let resp = client
        .post(format!("{}/streams", server.base_url))
        .json(&json!({
            "sql": "CREATE STREAM s_run () WITH (TYPE=\"memory\", FORMAT=\"json\")"
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::CREATED);

    // 2. Create running rule selecting 'a'
    let resp = client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_run",
            "sql": "SELECT a FROM s_run",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::CREATED);

    // Check status is running
    let status_resp: Value = client
        .get(format!("{}/rules/r_run/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status_resp["status"], "running");

    // Send first record
    let resp = client
        .post(format!("{}/streams/s_run/data", server.base_url))
        .json(&json!({ "a": 100, "b": 200 }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    let lines = wait_for_file_lines(&out.0, 1).await;
    assert_eq!(lines[0].get("a").and_then(|v| v.as_i64()), Some(100));
    assert!(lines[0].get("b").is_none());

    // 3. Update the running rule to select 'b' instead
    let resp = client
        .put(format!("{}/rules/r_run", server.base_url))
        .json(&json!({
            "id": "r_run",
            "sql": "SELECT b FROM s_run",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    // Verify status remains "running"
    let status_resp: Value = client
        .get(format!("{}/rules/r_run/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status_resp["status"], "running");

    // Send second record
    let resp = client
        .post(format!("{}/streams/s_run/data", server.base_url))
        .json(&json!({ "a": 300, "b": 400 }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    let lines = wait_for_file_lines(&out.0, 2).await;
    // Second line must use new projection (b = 400, no a)
    assert_eq!(lines[1].get("b").and_then(|v| v.as_i64()), Some(400));
    assert!(lines[1].get("a").is_none());

    server.shutdown().await;
}

#[tokio::test]
async fn test_restart_restores_updated_running_rule() {
    let db = TempDb::new("restart_updated");
    let kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&db.0).await.unwrap());
    let server1 = TestServer::start(kv.clone()).await;
    let client = reqwest::Client::new();
    let out = TempFile::new("out_restart");

    // Create stream and rule
    client
        .post(format!("{}/streams", server1.base_url))
        .json(&json!({
            "sql": "CREATE STREAM s_rest () WITH (TYPE=\"memory\", FORMAT=\"json\")"
        }))
        .send()
        .await
        .unwrap();

    client
        .post(format!("{}/rules", server1.base_url))
        .json(&json!({
            "id": "r_rest",
            "sql": "SELECT val FROM s_rest",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();

    // Update rule to select val * 2 as doubled
    let resp = client
        .put(format!("{}/rules/r_rest", server1.base_url))
        .json(&json!({
            "id": "r_rest",
            "sql": "SELECT val * 2 AS doubled FROM s_rest",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    // Stop server 1 (simulate restart)
    server1.shutdown().await;

    // Start server 2 with same KV database
    let server2 = TestServer::start(kv.clone()).await;

    // Check status is restored as "running"
    let status_resp: Value = client
        .get(format!("{}/rules/r_rest/status", server2.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status_resp["status"], "running");

    // Send record to restored server
    let resp = client
        .post(format!("{}/streams/s_rest/data", server2.base_url))
        .json(&json!({ "val": 21 }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    let lines = wait_for_file_lines(&out.0, 1).await;
    assert_eq!(lines[0].get("doubled").and_then(|v| v.as_i64()), Some(42));

    server2.shutdown().await;
}

#[tokio::test]
async fn test_injected_persistence_failure_preserves_previous_usable_rule() {
    let db = TempDb::new("fail_inject");
    let base_kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&db.0).await.unwrap());
    let fail_flag = Arc::new(AtomicBool::new(false));
    let faulty_kv: Arc<dyn KvStore> = Arc::new(FaultyKvStore {
        inner: base_kv,
        fail_writes: fail_flag.clone(),
    });

    let server = TestServer::start(faulty_kv.clone()).await;
    let client = reqwest::Client::new();
    let out = TempFile::new("out_fail_inject");

    // 1. Create stream & rule normally
    client
        .post(format!("{}/streams", server.base_url))
        .json(&json!({
            "sql": "CREATE STREAM s_inj () WITH (TYPE=\"memory\", FORMAT=\"json\")"
        }))
        .send()
        .await
        .unwrap();

    client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_inj",
            "sql": "SELECT good FROM s_inj",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();

    // Verify initial data works
    client
        .post(format!("{}/streams/s_inj/data", server.base_url))
        .json(&json!({ "good": 1, "bad": 99 }))
        .send()
        .await
        .unwrap();
    let lines = wait_for_file_lines(&out.0, 1).await;
    assert_eq!(lines[0].get("good").and_then(|v| v.as_i64()), Some(1));

    // 2. Enable write failures
    fail_flag.store(true, Ordering::SeqCst);

    // 3. Attempt to update rule; must fail with 500
    let resp = client
        .put(format!("{}/rules/r_inj", server.base_url))
        .json(&json!({
            "id": "r_inj",
            "sql": "SELECT bad FROM s_inj",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::INTERNAL_SERVER_ERROR);

    // 4. Verify previous rule is still usable and running with previous definition
    let rule_def: Value = client
        .get(format!("{}/rules/r_inj", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(rule_def["sql"], "SELECT good FROM s_inj");

    let status_resp: Value = client
        .get(format!("{}/rules/r_inj/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status_resp["status"], "running");

    // Post data; rule must still process using original definition
    client
        .post(format!("{}/streams/s_inj/data", server.base_url))
        .json(&json!({ "good": 2, "bad": 999 }))
        .send()
        .await
        .unwrap();

    let lines = wait_for_file_lines(&out.0, 2).await;
    assert_eq!(lines[1].get("good").and_then(|v| v.as_i64()), Some(2));
    assert!(lines[1].get("bad").is_none());

    server.shutdown().await;
}

#[tokio::test]
async fn test_stopped_rule_update_and_concurrent_lifecycle() {
    let db = TempDb::new("stopped_and_concurrent");
    let kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&db.0).await.unwrap());
    let server = TestServer::start(kv.clone()).await;
    let client = reqwest::Client::new();
    let out = TempFile::new("out_stopped");

    // 1. Create stream & rule
    client
        .post(format!("{}/streams", server.base_url))
        .json(&json!({
            "sql": "CREATE STREAM s_stop () WITH (TYPE=\"memory\", FORMAT=\"json\")"
        }))
        .send()
        .await
        .unwrap();

    client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_stop",
            "sql": "SELECT a FROM s_stop",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();

    // 2. Stop the rule
    let resp = client
        .post(format!("{}/rules/r_stop/stop", server.base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    let status: Value = client
        .get(format!("{}/rules/r_stop/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status["status"], "stopped");

    // 3. Update the stopped rule
    let resp = client
        .put(format!("{}/rules/r_stop", server.base_url))
        .json(&json!({
            "id": "r_stop",
            "sql": "SELECT a * 10 AS a FROM s_stop",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    // Verify it is STILL stopped
    let status: Value = client
        .get(format!("{}/rules/r_stop/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status["status"], "stopped");

    // Posting data while stopped should NOT emit anything
    client
        .post(format!("{}/streams/s_stop/data", server.base_url))
        .json(&json!({ "a": 5 }))
        .send()
        .await
        .unwrap();
    tokio::time::sleep(Duration::from_millis(300)).await;
    assert!(!Path::new(&out.0).exists());

    // 4. Start the rule explicitly
    let resp = client
        .post(format!("{}/rules/r_stop/start", server.base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    let status: Value = client
        .get(format!("{}/rules/r_stop/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(status["status"], "running");

    // Post data now -> receives updated multiplied value
    client
        .post(format!("{}/streams/s_stop/data", server.base_url))
        .json(&json!({ "a": 7 }))
        .send()
        .await
        .unwrap();

    let lines = wait_for_file_lines(&out.0, 1).await;
    assert_eq!(lines[0].get("a").and_then(|v| v.as_i64()), Some(70));

    // 5. Concurrent lifecycle stress test
    let mut tasks = Vec::new();
    for i in 0..10 {
        let base = server.base_url.clone();
        let c = client.clone();
        let path = out.path_str();
        tasks.push(tokio::spawn(async move {
            match i % 4 {
                0 => {
                    let _ = c.post(format!("{}/rules/r_stop/start", base)).send().await;
                }
                1 => {
                    let _ = c.post(format!("{}/rules/r_stop/stop", base)).send().await;
                }
                2 => {
                    let _ = c
                        .post(format!("{}/rules/r_stop/restart", base))
                        .send()
                        .await;
                }
                _ => {
                    let _ = c
                        .put(format!("{}/rules/r_stop", base))
                        .json(&json!({
                            "id": "r_stop",
                            "sql": "SELECT a * 100 AS a FROM s_stop",
                            "actions": [{ "file": { "path": path } }]
                        }))
                        .send()
                        .await;
                }
            }
        }));
    }

    for t in tasks {
        t.await.unwrap();
    }

    // Verify engine is still responsive and status is clean (either running or stopped)
    let status: Value = client
        .get(format!("{}/rules/r_stop/status", server.base_url))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    let s = status["status"].as_str().unwrap();
    assert!(s == "running" || s == "stopped");

    server.shutdown().await;
}

#[tokio::test]
async fn test_restart_rule_persistence_failure_preserves_running_source() {
    let db = TempDb::new("restart_fail_preserves");
    let base_kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&db.0).await.unwrap());
    let fail_flag = Arc::new(AtomicBool::new(false));
    let faulty_kv: Arc<dyn KvStore> = Arc::new(FaultyKvStore {
        inner: base_kv,
        fail_writes: fail_flag.clone(),
    });

    let server = TestServer::start(faulty_kv.clone()).await;
    let client = reqwest::Client::new();
    let out = TempFile::new("out_restart_preserves");

    // 1. Create stream & rule
    client
        .post(format!("{}/streams", server.base_url))
        .json(&json!({
            "sql": "CREATE STREAM s_rf () WITH (TYPE=\"memory\", FORMAT=\"json\")"
        }))
        .send()
        .await
        .unwrap();

    client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_rf",
            "sql": "SELECT val FROM s_rf",
            "actions": [{ "file": { "path": out.path_str() } }]
        }))
        .send()
        .await
        .unwrap();

    // Verify initial message is delivered
    client
        .post(format!("{}/streams/s_rf/data", server.base_url))
        .json(&json!({ "val": 100 }))
        .send()
        .await
        .unwrap();

    let lines = wait_for_file_lines(&out.0, 1).await;
    assert_eq!(lines[0].get("val").and_then(|v| v.as_i64()), Some(100));

    // 2. Inject persistence failure
    fail_flag.store(true, Ordering::SeqCst);

    // 3. Attempt restart; handler MUST return 500
    let resp = client
        .post(format!("{}/rules/r_rf/restart", server.base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::INTERNAL_SERVER_ERROR);

    // 4. Verify that running rule source was NOT disconnected/cancelled by failed restart!
    client
        .post(format!("{}/streams/s_rf/data", server.base_url))
        .json(&json!({ "val": 200 }))
        .send()
        .await
        .unwrap();

    let lines = wait_for_file_lines(&out.0, 2).await;
    assert_eq!(lines[1].get("val").and_then(|v| v.as_i64()), Some(200));

    // 5. Restore persistence and restart cleanly
    fail_flag.store(false, Ordering::SeqCst);
    let resp = client
        .post(format!("{}/rules/r_rf/restart", server.base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::OK);

    // Post third record to confirm restarted rule continues processing
    client
        .post(format!("{}/streams/s_rf/data", server.base_url))
        .json(&json!({ "val": 300 }))
        .send()
        .await
        .unwrap();

    let lines = wait_for_file_lines(&out.0, 3).await;
    assert_eq!(lines[2].get("val").and_then(|v| v.as_i64()), Some(300));

    server.shutdown().await;
}

#[tokio::test]
async fn test_rule_validation_rejections() {
    let temp_db = TempDb::new("val_rej");
    let kv: Arc<dyn KvStore> = Arc::new(SqliteKvStore::new(&temp_db.0).await.unwrap());
    let server = TestServer::start(kv).await;
    let client = reqwest::Client::new();

    // Create stream first
    let resp = client
        .post(format!("{}/streams", server.base_url))
        .json(&json!({
            "sql": "create stream s () WITH (DATASOURCE=\"s\", TYPE=\"mqtt\")"
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::CREATED);

    // 1. Duplicate output fields (unaliased duplicate)
    let resp = client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_dup1",
            "sql": "SELECT id, id FROM s",
            "actions": [{"log": {}}]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::BAD_REQUEST);
    let body = resp.text().await.unwrap();
    assert!(
        body.contains("duplicate field definition id"),
        "expected duplicate field error, got: {}",
        body
    );

    // 2. Duplicate output fields (aliased duplicate)
    let resp = client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_dup2",
            "sql": "SELECT id, temp AS id FROM s",
            "actions": [{"log": {}}]
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::BAD_REQUEST);
    let body = resp.text().await.unwrap();
    assert!(
        body.contains("duplicate field definition id"),
        "expected duplicate field error, got: {}",
        body
    );

    // 3. Rule with missing actions
    let resp = client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_no_act",
            "sql": "SELECT id FROM s",
            "actions": []
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::BAD_REQUEST);
    let body = resp.text().await.unwrap();
    assert!(
        body.contains("Missing rule actions."),
        "expected missing rule actions error, got: {}",
        body
    );

    // 4. Rule option with wrong type (qos: "high" instead of integer)
    let resp = client
        .post(format!("{}/rules", server.base_url))
        .json(&json!({
            "id": "r_bad_opt",
            "sql": "SELECT id FROM s",
            "actions": [{"log": {}}],
            "options": {
                "qos": "high"
            }
        }))
        .send()
        .await
        .unwrap();
    assert_eq!(resp.status(), reqwest::StatusCode::BAD_REQUEST);
    let body = resp.text().await.unwrap();
    assert!(
        body.contains("invalid rule json"),
        "expected invalid rule json error, got: {}",
        body
    );

    server.shutdown().await;
}