mockforge-registry-core 0.3.137

Shared domain models, storage abstractions, and OSS-safe handlers for MockForge's registry backends (SaaS Postgres + OSS SQLite admin UI).
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
//! Scenario marketplace models
//!
//! Handles data scenarios for mock systems

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use uuid::Uuid;

/// Scenario model
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
pub struct Scenario {
    pub id: Uuid,
    pub org_id: Option<Uuid>,
    pub name: String,
    pub slug: String,
    pub description: String,
    pub author_id: Uuid,
    pub current_version: String,
    pub category: String,
    pub tags: Vec<String>,
    pub license: String,
    pub repository: Option<String>,
    pub homepage: Option<String>,
    pub manifest_json: serde_json::Value,
    pub downloads_total: i64,
    pub rating_avg: rust_decimal::Decimal,
    pub rating_count: i32,
    pub verified_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[cfg(feature = "postgres")]
impl Scenario {
    /// Create a new scenario
    #[allow(clippy::too_many_arguments)]
    pub async fn create(
        pool: &sqlx::PgPool,
        org_id: Option<Uuid>,
        name: &str,
        slug: &str,
        description: &str,
        author_id: Uuid,
        current_version: &str,
        category: &str,
        license: &str,
        manifest_json: serde_json::Value,
    ) -> sqlx::Result<Self> {
        sqlx::query_as::<_, Self>(
            r#"
            INSERT INTO scenarios (
                org_id, name, slug, description, author_id, current_version,
                category, license, manifest_json
            )
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
            RETURNING *
            "#,
        )
        .bind(org_id)
        .bind(name)
        .bind(slug)
        .bind(description)
        .bind(author_id)
        .bind(current_version)
        .bind(category)
        .bind(license)
        .bind(manifest_json)
        .fetch_one(pool)
        .await
    }

    /// Find by ID
    pub async fn find_by_id(pool: &sqlx::PgPool, id: Uuid) -> sqlx::Result<Option<Self>> {
        sqlx::query_as::<_, Self>("SELECT * FROM scenarios WHERE id = $1")
            .bind(id)
            .fetch_optional(pool)
            .await
    }

    /// Find by name
    pub async fn find_by_name(pool: &sqlx::PgPool, name: &str) -> sqlx::Result<Option<Self>> {
        sqlx::query_as::<_, Self>("SELECT * FROM scenarios WHERE name = $1")
            .bind(name)
            .fetch_optional(pool)
            .await
    }

    /// Build WHERE clause for search queries (using parameterized queries for security)
    fn build_search_where_clause(
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
    ) -> (String, Vec<String>) {
        let mut where_parts = Vec::new();
        let mut param_placeholders = Vec::new();
        let mut param_index = 1;

        // Org filtering
        if let Some(_org) = org_id {
            param_placeholders.push(format!("${}", param_index));
            where_parts.push(format!("(org_id = ${} OR org_id IS NULL)", param_index));
            param_index += 1;
        } else {
            // Public scenarios only if no org context
            where_parts.push("org_id IS NULL".to_string());
        }

        // Category filter
        if let Some(_cat) = category {
            param_placeholders.push(format!("${}", param_index));
            where_parts.push(format!("category = ${}", param_index));
            param_index += 1;
        }

        // Tags filter
        if !tags.is_empty() {
            param_placeholders.push(format!("${}", param_index));
            where_parts.push(format!("tags && ${}::text[]", param_index));
            param_index += 1;
        }

        // Full-text search
        if let Some(_q) = query {
            param_placeholders.push(format!("${}", param_index));
            where_parts.push(format!(
                "to_tsvector('english', name || ' ' || COALESCE(description, '')) @@ plainto_tsquery('english', ${})",
                param_index
            ));
        }

        let where_clause = if where_parts.is_empty() {
            "WHERE 1=1".to_string()
        } else {
            format!("WHERE {}", where_parts.join(" AND "))
        };

        (where_clause, param_placeholders)
    }

    /// Count scenarios matching search criteria
    pub async fn count_search(
        pool: &sqlx::PgPool,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
    ) -> sqlx::Result<i64> {
        let (where_clause, _) = Self::build_search_where_clause(query, category, tags, org_id);
        let sql = format!("SELECT COUNT(*) FROM scenarios {}", where_clause);

        let mut query_builder = sqlx::query_as::<_, (i64,)>(&sql);

        // Bind parameters in order
        if let Some(org) = org_id {
            query_builder = query_builder.bind(org);
        }
        if let Some(cat) = category {
            query_builder = query_builder.bind(cat);
        }
        if !tags.is_empty() {
            query_builder = query_builder.bind(tags);
        }
        if let Some(q) = query {
            query_builder = query_builder.bind(q);
        }

        let result = query_builder.fetch_one(pool).await?;
        Ok(result.0)
    }

    /// Search scenarios
    #[allow(clippy::too_many_arguments)]
    pub async fn search(
        pool: &sqlx::PgPool,
        query: Option<&str>,
        category: Option<&str>,
        tags: &[String],
        org_id: Option<Uuid>,
        sort: &str,
        limit: i64,
        offset: i64,
    ) -> sqlx::Result<Vec<Self>> {
        let (where_clause, _) = Self::build_search_where_clause(query, category, tags, org_id);

        // Sort
        let order_by = match sort {
            "downloads" => "ORDER BY downloads_total DESC",
            "rating" => "ORDER BY rating_avg DESC",
            "recent" => "ORDER BY created_at DESC",
            "name" => "ORDER BY name ASC",
            _ => "ORDER BY downloads_total DESC",
        };

        // Calculate parameter offset for LIMIT/OFFSET
        let mut param_count = 1;
        if org_id.is_some() {
            param_count += 1;
        }
        if category.is_some() {
            param_count += 1;
        }
        if !tags.is_empty() {
            param_count += 1;
        }
        if query.is_some() {
            param_count += 1;
        }

        let sql = format!(
            "SELECT * FROM scenarios {} {} LIMIT ${} OFFSET ${}",
            where_clause,
            order_by,
            param_count,
            param_count + 1
        );

        let mut query_builder = sqlx::query_as::<_, Self>(&sql);

        // Bind parameters in order
        if let Some(org) = org_id {
            query_builder = query_builder.bind(org);
        }
        if let Some(cat) = category {
            query_builder = query_builder.bind(cat);
        }
        if !tags.is_empty() {
            query_builder = query_builder.bind(tags);
        }
        if let Some(q) = query {
            query_builder = query_builder.bind(q);
        }
        query_builder = query_builder.bind(limit).bind(offset);

        query_builder.fetch_all(pool).await
    }

    /// Find scenarios by organization
    pub async fn find_by_org(pool: &sqlx::PgPool, org_id: Uuid) -> sqlx::Result<Vec<Self>> {
        sqlx::query_as::<_, Self>(
            "SELECT * FROM scenarios WHERE org_id = $1 ORDER BY created_at DESC",
        )
        .bind(org_id)
        .fetch_all(pool)
        .await
    }
}

/// Scenario version
#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
pub struct ScenarioVersion {
    pub id: Uuid,
    pub scenario_id: Uuid,
    pub version: String,
    pub manifest_json: serde_json::Value,
    pub download_url: String,
    pub checksum: String,
    pub file_size: i64,
    pub min_mockforge_version: Option<String>,
    pub yanked: bool,
    pub downloads: i32,
    pub published_at: DateTime<Utc>,
}

#[cfg(feature = "postgres")]
impl ScenarioVersion {
    /// Create a new version
    #[allow(clippy::too_many_arguments)]
    pub async fn create(
        pool: &sqlx::PgPool,
        scenario_id: Uuid,
        version: &str,
        manifest_json: serde_json::Value,
        download_url: &str,
        checksum: &str,
        file_size: i64,
        min_mockforge_version: Option<&str>,
    ) -> sqlx::Result<Self> {
        sqlx::query_as::<_, Self>(
            r#"
            INSERT INTO scenario_versions (
                scenario_id, version, manifest_json, download_url,
                checksum, file_size, min_mockforge_version
            )
            VALUES ($1, $2, $3, $4, $5, $6, $7)
            RETURNING *
            "#,
        )
        .bind(scenario_id)
        .bind(version)
        .bind(manifest_json)
        .bind(download_url)
        .bind(checksum)
        .bind(file_size)
        .bind(min_mockforge_version)
        .fetch_one(pool)
        .await
    }

    /// Find by scenario and version
    pub async fn find(
        pool: &sqlx::PgPool,
        scenario_id: Uuid,
        version: &str,
    ) -> sqlx::Result<Option<Self>> {
        sqlx::query_as::<_, Self>(
            "SELECT * FROM scenario_versions WHERE scenario_id = $1 AND version = $2",
        )
        .bind(scenario_id)
        .bind(version)
        .fetch_optional(pool)
        .await
    }

    /// Get all versions for a scenario
    pub async fn get_by_scenario(
        pool: &sqlx::PgPool,
        scenario_id: Uuid,
    ) -> sqlx::Result<Vec<Self>> {
        sqlx::query_as::<_, Self>(
            "SELECT * FROM scenario_versions WHERE scenario_id = $1 ORDER BY published_at DESC",
        )
        .bind(scenario_id)
        .fetch_all(pool)
        .await
    }
}

#[cfg(all(test, feature = "postgres"))]
mod tests {
    use super::*;
    use rust_decimal::prelude::*;

    #[test]
    fn test_scenario_serialization() {
        let scenario = Scenario {
            id: Uuid::new_v4(),
            org_id: Some(Uuid::new_v4()),
            name: "Test Scenario".to_string(),
            slug: "test-scenario".to_string(),
            description: "A test scenario".to_string(),
            author_id: Uuid::new_v4(),
            current_version: "1.0.0".to_string(),
            category: "testing".to_string(),
            tags: vec!["test".to_string(), "demo".to_string()],
            license: "MIT".to_string(),
            repository: Some("https://github.com/test/repo".to_string()),
            homepage: Some("https://test.com".to_string()),
            manifest_json: serde_json::json!({"version": "1.0.0"}),
            downloads_total: 100,
            rating_avg: Decimal::from_str("4.5").unwrap(),
            rating_count: 10,
            verified_at: Some(Utc::now()),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let json = serde_json::to_string(&scenario).unwrap();
        assert!(json.contains("Test Scenario"));
        assert!(json.contains("test-scenario"));
        assert!(json.contains("testing"));
    }

    #[test]
    fn test_scenario_deserialization() {
        let json = r#"{
            "id": "00000000-0000-0000-0000-000000000001",
            "org_id": "00000000-0000-0000-0000-000000000002",
            "name": "Test Scenario",
            "slug": "test-scenario",
            "description": "A test scenario",
            "author_id": "00000000-0000-0000-0000-000000000003",
            "current_version": "1.0.0",
            "category": "testing",
            "tags": ["test", "demo"],
            "license": "MIT",
            "repository": "https://github.com/test/repo",
            "homepage": "https://test.com",
            "manifest_json": {"version": "1.0.0"},
            "downloads_total": 100,
            "rating_avg": 4.5,
            "rating_count": 10,
            "verified_at": "2024-01-01T00:00:00Z",
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z"
        }"#;

        let scenario: Scenario = serde_json::from_str(json).unwrap();
        assert_eq!(scenario.name, "Test Scenario");
        assert_eq!(scenario.slug, "test-scenario");
        assert_eq!(scenario.category, "testing");
        assert_eq!(scenario.tags.len(), 2);
    }

    #[test]
    fn test_scenario_clone() {
        let scenario = Scenario {
            id: Uuid::new_v4(),
            org_id: None,
            name: "Test Scenario".to_string(),
            slug: "test-scenario".to_string(),
            description: "A test scenario".to_string(),
            author_id: Uuid::new_v4(),
            current_version: "1.0.0".to_string(),
            category: "testing".to_string(),
            tags: vec!["test".to_string()],
            license: "MIT".to_string(),
            repository: None,
            homepage: None,
            manifest_json: serde_json::json!({}),
            downloads_total: 0,
            rating_avg: Decimal::from_str("0.0").unwrap(),
            rating_count: 0,
            verified_at: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let cloned = scenario.clone();
        assert_eq!(scenario.id, cloned.id);
        assert_eq!(scenario.name, cloned.name);
        assert_eq!(scenario.slug, cloned.slug);
    }

    #[test]
    fn test_scenario_version_serialization() {
        let version = ScenarioVersion {
            id: Uuid::new_v4(),
            scenario_id: Uuid::new_v4(),
            version: "1.0.0".to_string(),
            manifest_json: serde_json::json!({"version": "1.0.0"}),
            download_url: "https://example.com/scenario.tar.gz".to_string(),
            checksum: "abc123".to_string(),
            file_size: 1024,
            min_mockforge_version: Some("0.1.0".to_string()),
            yanked: false,
            downloads: 50,
            published_at: Utc::now(),
        };

        let json = serde_json::to_string(&version).unwrap();
        assert!(json.contains("1.0.0"));
        assert!(json.contains("abc123"));
        assert!(json.contains("1024"));
    }

    #[test]
    fn test_scenario_version_clone() {
        let version = ScenarioVersion {
            id: Uuid::new_v4(),
            scenario_id: Uuid::new_v4(),
            version: "1.0.0".to_string(),
            manifest_json: serde_json::json!({}),
            download_url: "https://example.com/scenario.tar.gz".to_string(),
            checksum: "abc123".to_string(),
            file_size: 1024,
            min_mockforge_version: None,
            yanked: false,
            downloads: 0,
            published_at: Utc::now(),
        };

        let cloned = version.clone();
        assert_eq!(version.id, cloned.id);
        assert_eq!(version.scenario_id, cloned.scenario_id);
        assert_eq!(version.version, cloned.version);
        assert_eq!(version.checksum, cloned.checksum);
    }

    #[test]
    fn test_build_search_where_clause_empty() {
        let (where_clause, params) = Scenario::build_search_where_clause(None, None, &[], None);

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("org_id IS NULL"));
        assert!(params.is_empty() || !params.is_empty()); // params may vary
    }

    #[test]
    fn test_build_search_where_clause_with_org() {
        let org_id = Uuid::new_v4();
        let (where_clause, _params) =
            Scenario::build_search_where_clause(None, None, &[], Some(org_id));

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("org_id"));
    }

    #[test]
    fn test_build_search_where_clause_with_category() {
        let (where_clause, _params) =
            Scenario::build_search_where_clause(None, Some("testing"), &[], None);

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("category"));
    }

    #[test]
    fn test_build_search_where_clause_with_tags() {
        let tags = vec!["test".to_string(), "demo".to_string()];
        let (where_clause, _params) = Scenario::build_search_where_clause(None, None, &tags, None);

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("tags"));
    }

    #[test]
    fn test_build_search_where_clause_with_query() {
        let (where_clause, _params) =
            Scenario::build_search_where_clause(Some("search term"), None, &[], None);

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("tsvector") || where_clause.contains("tsquery"));
    }

    #[test]
    fn test_build_search_where_clause_all_params() {
        let org_id = Uuid::new_v4();
        let tags = vec!["test".to_string()];
        let (where_clause, _params) = Scenario::build_search_where_clause(
            Some("search"),
            Some("testing"),
            &tags,
            Some(org_id),
        );

        assert!(where_clause.contains("WHERE"));
        assert!(where_clause.contains("AND"));
    }

    #[test]
    fn test_scenario_with_org() {
        let org_id = Uuid::new_v4();
        let scenario = Scenario {
            id: Uuid::new_v4(),
            org_id: Some(org_id),
            name: "Org Scenario".to_string(),
            slug: "org-scenario".to_string(),
            description: "An org scenario".to_string(),
            author_id: Uuid::new_v4(),
            current_version: "1.0.0".to_string(),
            category: "testing".to_string(),
            tags: vec![],
            license: "MIT".to_string(),
            repository: None,
            homepage: None,
            manifest_json: serde_json::json!({}),
            downloads_total: 0,
            rating_avg: Decimal::from_str("0.0").unwrap(),
            rating_count: 0,
            verified_at: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert_eq!(scenario.org_id, Some(org_id));
    }

    #[test]
    fn test_scenario_public() {
        let scenario = Scenario {
            id: Uuid::new_v4(),
            org_id: None,
            name: "Public Scenario".to_string(),
            slug: "public-scenario".to_string(),
            description: "A public scenario".to_string(),
            author_id: Uuid::new_v4(),
            current_version: "1.0.0".to_string(),
            category: "testing".to_string(),
            tags: vec![],
            license: "MIT".to_string(),
            repository: None,
            homepage: None,
            manifest_json: serde_json::json!({}),
            downloads_total: 0,
            rating_avg: Decimal::from_str("0.0").unwrap(),
            rating_count: 0,
            verified_at: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert_eq!(scenario.org_id, None);
    }

    #[test]
    fn test_scenario_version_yanked() {
        let version = ScenarioVersion {
            id: Uuid::new_v4(),
            scenario_id: Uuid::new_v4(),
            version: "1.0.0".to_string(),
            manifest_json: serde_json::json!({}),
            download_url: "https://example.com/scenario.tar.gz".to_string(),
            checksum: "abc123".to_string(),
            file_size: 1024,
            min_mockforge_version: None,
            yanked: true,
            downloads: 0,
            published_at: Utc::now(),
        };

        assert!(version.yanked);
    }

    #[test]
    fn test_scenario_version_not_yanked() {
        let version = ScenarioVersion {
            id: Uuid::new_v4(),
            scenario_id: Uuid::new_v4(),
            version: "1.0.0".to_string(),
            manifest_json: serde_json::json!({}),
            download_url: "https://example.com/scenario.tar.gz".to_string(),
            checksum: "abc123".to_string(),
            file_size: 1024,
            min_mockforge_version: None,
            yanked: false,
            downloads: 0,
            published_at: Utc::now(),
        };

        assert!(!version.yanked);
    }
}