fraiseql-server 2.5.0

HTTP server for FraiseQL v2 GraphQL engine
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
// ── audit_tests ───────────────────────────────────────────────────────────────

#![allow(clippy::panic)] // Reason: test code, panics acceptable
mod audit_tests {
    #![allow(clippy::unwrap_used)] // Reason: test code, panics acceptable
    #![allow(clippy::missing_panics_doc)] // Reason: test helpers
    #![allow(clippy::missing_errors_doc)] // Reason: test helpers
    #![allow(missing_docs)] // Reason: test code

    use super::super::audit::*;

    #[tokio::test]
    async fn record_and_retrieve_event() {
        let log = InMemoryAuditLog::new();
        log.record("tenant-abc", TenantEventKind::Created, Some("admin"), None)
            .await
            .unwrap();

        let events = log.events_for("tenant-abc", 10, 0).await.unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].tenant_key, "tenant-abc");
        assert_eq!(events[0].event, TenantEventKind::Created);
        assert_eq!(events[0].actor.as_deref(), Some("admin"));
    }

    #[tokio::test]
    async fn events_filtered_by_tenant_key() {
        let log = InMemoryAuditLog::new();
        log.record("tenant-a", TenantEventKind::Created, None, None).await.unwrap();
        log.record("tenant-b", TenantEventKind::Created, None, None).await.unwrap();
        log.record("tenant-a", TenantEventKind::Suspended, None, None).await.unwrap();

        let events_a = log.events_for("tenant-a", 10, 0).await.unwrap();
        assert_eq!(events_a.len(), 2);

        let events_b = log.events_for("tenant-b", 10, 0).await.unwrap();
        assert_eq!(events_b.len(), 1);
    }

    #[tokio::test]
    async fn events_returned_newest_first() {
        let log = InMemoryAuditLog::new();
        log.record("t", TenantEventKind::Created, None, None).await.unwrap();
        log.record("t", TenantEventKind::Suspended, None, None).await.unwrap();
        log.record("t", TenantEventKind::Resumed, None, None).await.unwrap();

        let events = log.events_for("t", 10, 0).await.unwrap();
        assert_eq!(events[0].event, TenantEventKind::Resumed);
        assert_eq!(events[1].event, TenantEventKind::Suspended);
        assert_eq!(events[2].event, TenantEventKind::Created);
    }

    #[tokio::test]
    async fn pagination_with_limit_and_offset() {
        let log = InMemoryAuditLog::new();
        for _ in 0..5 {
            log.record("t", TenantEventKind::Created, None, None).await.unwrap();
        }

        let page1 = log.events_for("t", 2, 0).await.unwrap();
        assert_eq!(page1.len(), 2);

        let page2 = log.events_for("t", 2, 2).await.unwrap();
        assert_eq!(page2.len(), 2);

        let page3 = log.events_for("t", 2, 4).await.unwrap();
        assert_eq!(page3.len(), 1);
    }

    #[tokio::test]
    async fn config_changed_event_with_payload() {
        let log = InMemoryAuditLog::new();
        let payload = serde_json::json!({
            "max_concurrent": {"old": 5, "new": 10}
        });
        log.record(
            "tenant-abc",
            TenantEventKind::ConfigChanged,
            Some("user-42"),
            Some(payload.clone()),
        )
        .await
        .unwrap();

        let events = log.events_for("tenant-abc", 10, 0).await.unwrap();
        assert_eq!(events[0].payload.as_ref(), Some(&payload));
    }

    #[tokio::test]
    async fn append_only_no_update_or_delete() {
        // Verify by API: there are no update/delete methods on TenantAuditLog.
        // This test records multiple events and confirms all are preserved.
        let log = InMemoryAuditLog::new();
        log.record("t", TenantEventKind::Created, None, None).await.unwrap();
        log.record("t", TenantEventKind::Suspended, None, None).await.unwrap();
        log.record("t", TenantEventKind::Deleted, None, None).await.unwrap();

        let events = log.events_for("t", 100, 0).await.unwrap();
        assert_eq!(events.len(), 3, "all events must be preserved (append-only)");
    }

    #[test]
    fn event_kind_as_str() {
        assert_eq!(TenantEventKind::Created.as_str(), "created");
        assert_eq!(TenantEventKind::ConfigChanged.as_str(), "config_changed");
        assert_eq!(TenantEventKind::Suspended.as_str(), "suspended");
        assert_eq!(TenantEventKind::Resumed.as_str(), "resumed");
        assert_eq!(TenantEventKind::Deleted.as_str(), "deleted");
    }

    #[test]
    fn event_kind_serializes_to_snake_case() {
        let json = serde_json::to_string(&TenantEventKind::ConfigChanged).unwrap();
        assert_eq!(json, "\"config_changed\"");
    }
}

// ── pool_factory_tests ────────────────────────────────────────────────────────

mod pool_factory_tests {
    #![allow(clippy::unwrap_used)] // Reason: test code, panics acceptable
    #![allow(clippy::missing_panics_doc)] // Reason: test helpers
    #![allow(clippy::missing_errors_doc)] // Reason: test helpers
    #![allow(missing_docs)] // Reason: test code

    use async_trait::async_trait;
    use fraiseql_core::{
        db::{
            WhereClause,
            traits::DatabaseAdapter,
            types::{DatabaseType, JsonbValue, PoolMetrics},
        },
        error::Result as FraiseQLResult,
        schema::CompiledSchema,
    };
    use fraiseql_error::FraiseQLError;

    use super::super::pool_factory::*;

    /// Stub adapter that implements `FromPoolConfig` for testing.
    #[derive(Debug, Clone)]
    struct StubPoolAdapter;

    #[async_trait]
    impl DatabaseAdapter for StubPoolAdapter {
        async fn execute_where_query(
            &self,
            _view: &str,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        async fn execute_with_projection(
            &self,
            _view: &str,
            _projection: Option<&fraiseql_core::schema::SqlProjectionHint>,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        fn database_type(&self) -> DatabaseType {
            DatabaseType::SQLite
        }

        async fn health_check(&self) -> FraiseQLResult<()> {
            Ok(())
        }

        fn pool_metrics(&self) -> PoolMetrics {
            PoolMetrics::default()
        }

        async fn execute_raw_query(
            &self,
            _sql: &str,
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            Ok(vec![])
        }

        async fn execute_parameterized_aggregate(
            &self,
            _sql: &str,
            _params: &[serde_json::Value],
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            Ok(vec![])
        }
    }

    #[async_trait]
    impl FromPoolConfig for StubPoolAdapter {
        async fn from_pool_config(_config: &TenantPoolConfig) -> FraiseQLResult<Self> {
            Ok(Self)
        }
    }

    fn test_pool_config() -> TenantPoolConfig {
        TenantPoolConfig {
            connection_string:    "stub://localhost/test".to_string(),
            max_connections:      5,
            connect_timeout_secs: 5,
            idle_timeout_secs:    300,
        }
    }

    #[tokio::test]
    async fn test_create_tenant_executor_success() {
        let schema = CompiledSchema::default();
        let schema_json = serde_json::to_string(&schema).unwrap();
        let config = test_pool_config();

        let executor = create_tenant_executor::<StubPoolAdapter>("acme", &schema_json, &config)
            .await
            .unwrap();
        assert_eq!(executor.schema().types.len(), 0);
    }

    #[tokio::test]
    async fn test_create_tenant_executor_invalid_json() {
        let config = test_pool_config();
        let Err(err) =
            create_tenant_executor::<StubPoolAdapter>("acme", "not valid json", &config).await
        else {
            panic!("expected Err for invalid JSON");
        };
        assert!(matches!(err, FraiseQLError::Parse { .. }), "Expected Parse error, got: {err:?}");
    }

    #[tokio::test]
    async fn test_create_tenant_executor_bad_format_version() {
        let schema = CompiledSchema {
            schema_format_version: Some(999),
            ..CompiledSchema::default()
        };
        let schema_json = serde_json::to_string(&schema).unwrap();
        let config = test_pool_config();

        let Err(err) =
            create_tenant_executor::<StubPoolAdapter>("acme", &schema_json, &config).await
        else {
            panic!("expected Err for bad format version");
        };
        assert!(
            matches!(err, FraiseQLError::Validation { .. }),
            "Expected Validation error, got: {err:?}"
        );
    }

    /// Adapter that always fails to connect — simulates unreachable DB.
    #[derive(Debug, Clone)]
    struct FailingAdapter;

    #[async_trait]
    impl DatabaseAdapter for FailingAdapter {
        async fn execute_where_query(
            &self,
            _view: &str,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        async fn execute_with_projection(
            &self,
            _view: &str,
            _projection: Option<&fraiseql_core::schema::SqlProjectionHint>,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        fn database_type(&self) -> DatabaseType {
            DatabaseType::PostgreSQL
        }

        async fn health_check(&self) -> FraiseQLResult<()> {
            Err(FraiseQLError::database("connection refused"))
        }

        fn pool_metrics(&self) -> PoolMetrics {
            PoolMetrics::default()
        }

        async fn execute_raw_query(
            &self,
            _sql: &str,
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            Ok(vec![])
        }

        async fn execute_parameterized_aggregate(
            &self,
            _sql: &str,
            _params: &[serde_json::Value],
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            Ok(vec![])
        }
    }

    #[async_trait]
    impl FromPoolConfig for FailingAdapter {
        async fn from_pool_config(_config: &TenantPoolConfig) -> FraiseQLResult<Self> {
            Err(FraiseQLError::ConnectionPool {
                message: "connection refused".to_string(),
            })
        }
    }

    #[tokio::test]
    async fn test_create_tenant_executor_unreachable_db() {
        let schema = CompiledSchema::default();
        let schema_json = serde_json::to_string(&schema).unwrap();
        let config = test_pool_config();

        let Err(err) =
            create_tenant_executor::<FailingAdapter>("acme", &schema_json, &config).await
        else {
            panic!("expected Err for unreachable DB");
        };
        assert!(
            matches!(err, FraiseQLError::ConnectionPool { .. }),
            "Expected ConnectionPool error, got: {err:?}"
        );
    }

    #[test]
    fn test_pool_config_defaults() {
        let json = r#"{"connection_string": "postgres://localhost/test"}"#;
        let config: TenantPoolConfig = serde_json::from_str(json).unwrap();
        assert_eq!(config.max_connections, 10);
        assert_eq!(config.connect_timeout_secs, 5);
        assert_eq!(config.idle_timeout_secs, 300);
    }
}

// ── schema_isolation_tests ────────────────────────────────────────────────────

mod schema_isolation_tests {
    #![allow(clippy::unwrap_used)] // Reason: test code, panics acceptable

    use std::sync::Mutex;

    use async_trait::async_trait;
    use fraiseql_core::{
        db::{
            WhereClause,
            traits::DatabaseAdapter,
            types::{DatabaseType, JsonbValue, PoolMetrics},
        },
        error::Result as FraiseQLResult,
    };
    use fraiseql_error::FraiseQLError;

    use super::super::schema_isolation::*;

    // ── tenant_schema_name ──────────────────────────────────────────────

    #[test]
    fn valid_key_produces_prefixed_name() {
        assert_eq!(tenant_schema_name("acme").unwrap(), "tenant_acme");
    }

    #[test]
    fn alphanumeric_key_accepted() {
        assert_eq!(tenant_schema_name("org123").unwrap(), "tenant_org123");
    }

    #[test]
    fn underscore_in_key_accepted() {
        assert_eq!(tenant_schema_name("my_org").unwrap(), "tenant_my_org");
    }

    #[test]
    fn empty_key_rejected() {
        let err = tenant_schema_name("").unwrap_err();
        assert!(
            matches!(err, FraiseQLError::Validation { .. }),
            "expected Validation, got: {err:?}"
        );
    }

    #[test]
    fn key_with_hyphen_rejected() {
        let err = tenant_schema_name("my-org").unwrap_err();
        assert!(err.to_string().contains("invalid characters"));
    }

    #[test]
    fn key_with_dot_rejected() {
        let err = tenant_schema_name("my.org").unwrap_err();
        assert!(err.to_string().contains("invalid characters"));
    }

    #[test]
    fn key_with_space_rejected() {
        assert!(tenant_schema_name("my org").is_err());
    }

    #[test]
    fn key_with_semicolon_rejected() {
        assert!(tenant_schema_name("org; DROP TABLE").is_err());
    }

    #[test]
    fn key_exceeding_max_length_rejected() {
        // MAX_PG_IDENTIFIER_LEN = 63, prefix = "tenant_" (7 chars)
        // So key can be at most 56 chars
        let long_key = "a".repeat(57);
        let err = tenant_schema_name(&long_key).unwrap_err();
        assert!(err.to_string().contains("63-character"));
    }

    #[test]
    fn key_at_max_length_accepted() {
        let key = "a".repeat(56); // tenant_ (7) + 56 = 63 exactly
        let name = tenant_schema_name(&key).unwrap();
        assert_eq!(name.len(), 63);
    }

    // ── DDL generation ──────────────────────────────────────────────────

    #[test]
    fn create_schema_ddl_generates_correct_sql() {
        assert_eq!(create_schema_ddl("acme").unwrap(), "CREATE SCHEMA IF NOT EXISTS tenant_acme");
    }

    #[test]
    fn drop_schema_ddl_generates_correct_sql() {
        assert_eq!(drop_schema_ddl("acme").unwrap(), "DROP SCHEMA IF EXISTS tenant_acme CASCADE");
    }

    #[test]
    fn create_schema_idempotent() {
        // IF NOT EXISTS means calling twice produces the same SQL
        let ddl1 = create_schema_ddl("acme").unwrap();
        let ddl2 = create_schema_ddl("acme").unwrap();
        assert_eq!(ddl1, ddl2);
        assert!(ddl1.contains("IF NOT EXISTS"));
    }

    #[test]
    fn create_schema_ddl_rejects_invalid_key() {
        assert!(create_schema_ddl("").is_err());
        assert!(create_schema_ddl("org; DROP").is_err());
    }

    #[test]
    fn drop_schema_ddl_rejects_invalid_key() {
        assert!(drop_schema_ddl("").is_err());
    }

    // ── search_path ─────────────────────────────────────────────────────

    #[test]
    fn search_path_sql_generates_correct_statement() {
        assert_eq!(search_path_sql("acme").unwrap(), "SET search_path TO tenant_acme, public");
    }

    #[test]
    fn search_path_sql_rejects_invalid_key() {
        assert!(search_path_sql("").is_err());
    }

    // ── Row mode skips DDL ──────────────────────────────────────────────
    // (Row mode never calls these functions — verified at the caller level)

    // ── Async adapter functions ────────────────────────────────────────

    /// Spy adapter that records all SQL passed to `execute_raw_query`.
    #[derive(Debug)]
    struct SpyAdapter {
        queries: Mutex<Vec<String>>,
    }

    impl SpyAdapter {
        fn new() -> Self {
            Self {
                queries: Mutex::new(Vec::new()),
            }
        }

        fn recorded_queries(&self) -> Vec<String> {
            self.queries.lock().unwrap().clone()
        }
    }

    #[async_trait]
    impl DatabaseAdapter for SpyAdapter {
        async fn execute_where_query(
            &self,
            _view: &str,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        async fn execute_with_projection(
            &self,
            _view: &str,
            _projection: Option<&fraiseql_core::schema::SqlProjectionHint>,
            _where_clause: Option<&WhereClause>,
            _limit: Option<u32>,
            _offset: Option<u32>,
            _order_by: Option<&[fraiseql_core::db::types::OrderByClause]>,
        ) -> FraiseQLResult<Vec<JsonbValue>> {
            Ok(vec![])
        }

        fn database_type(&self) -> DatabaseType {
            DatabaseType::PostgreSQL
        }

        async fn health_check(&self) -> FraiseQLResult<()> {
            Ok(())
        }

        fn pool_metrics(&self) -> PoolMetrics {
            PoolMetrics::default()
        }

        async fn execute_raw_query(
            &self,
            sql: &str,
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            self.queries.lock().unwrap().push(sql.to_string());
            Ok(vec![])
        }

        async fn execute_parameterized_aggregate(
            &self,
            _sql: &str,
            _params: &[serde_json::Value],
        ) -> FraiseQLResult<Vec<std::collections::HashMap<String, serde_json::Value>>> {
            Ok(vec![])
        }
    }

    #[tokio::test]
    async fn provision_executes_create_schema_ddl() {
        let adapter = SpyAdapter::new();
        provision_tenant_schema("acme", &adapter).await.unwrap();
        let queries = adapter.recorded_queries();
        assert_eq!(queries.len(), 1);
        assert_eq!(queries[0], "CREATE SCHEMA IF NOT EXISTS tenant_acme");
    }

    #[tokio::test]
    async fn drop_executes_drop_schema_ddl() {
        let adapter = SpyAdapter::new();
        drop_tenant_schema("acme", &adapter).await.unwrap();
        let queries = adapter.recorded_queries();
        assert_eq!(queries.len(), 1);
        assert_eq!(queries[0], "DROP SCHEMA IF EXISTS tenant_acme CASCADE");
    }

    #[tokio::test]
    async fn configure_search_path_executes_set_statement() {
        let adapter = SpyAdapter::new();
        configure_search_path("acme", &adapter).await.unwrap();
        let queries = adapter.recorded_queries();
        assert_eq!(queries.len(), 1);
        assert_eq!(queries[0], "SET search_path TO tenant_acme, public");
    }

    #[tokio::test]
    async fn provision_is_idempotent() {
        let adapter = SpyAdapter::new();
        provision_tenant_schema("acme", &adapter).await.unwrap();
        provision_tenant_schema("acme", &adapter).await.unwrap();
        let queries = adapter.recorded_queries();
        assert_eq!(queries.len(), 2);
        // Both should be IF NOT EXISTS — idempotent
        assert!(queries[0].contains("IF NOT EXISTS"));
        assert!(queries[1].contains("IF NOT EXISTS"));
    }

    #[tokio::test]
    async fn provision_rejects_invalid_key() {
        let adapter = SpyAdapter::new();
        let err = provision_tenant_schema("my-org", &adapter).await.unwrap_err();
        assert!(matches!(err, FraiseQLError::Validation { .. }));
        // No SQL should have been executed
        assert!(adapter.recorded_queries().is_empty());
    }

    #[tokio::test]
    async fn drop_rejects_invalid_key() {
        let adapter = SpyAdapter::new();
        let err = drop_tenant_schema("", &adapter).await.unwrap_err();
        assert!(matches!(err, FraiseQLError::Validation { .. }));
        assert!(adapter.recorded_queries().is_empty());
    }
}