Skip to main content

stackless_integrations/providers/runloop/
sandbox.rs

1//! `runloop/sandbox` integration.
2
3use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12
13pub const RESOURCE_KIND: &str = "integration-runloop";
14
15#[derive(Debug, Serialize)]
16pub struct RunloopSandboxConfig {}
17
18impl CatalogService for RunloopSandboxConfig {
19    const REFERENCE: &'static str = "runloop/sandbox";
20}
21
22#[derive(Debug)]
23pub struct RunloopSandbox;
24
25impl Hostable for RunloopSandbox {
26    const PROVIDER: &'static str = "runloop";
27    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
28    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
29    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
30    const OUTPUTS: &'static [&'static str] = &["account_id", "api_key", "base_url"];
31}
32
33impl FamilyResource for RunloopSandbox {
34    type Config = RunloopSandboxConfig;
35    const PROVIDER_PREFIX: &'static str = "RUNLOOP";
36    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
37        ("ACCOUNT_ID", "account_id", false),
38        ("API_KEY", "api_key", true),
39        ("BASE_URL", "base_url", true),
40    ];
41
42    fn build_config(ctx: &ProvisionContext<'_>) -> Result<RunloopSandboxConfig, IntegrationError> {
43        let _ = super::integration_config(ctx)?;
44        Ok(RunloopSandboxConfig {})
45    }
46}
47
48pub fn validate_config(
49    name: &str,
50    config: &BTreeMap<String, toml::Value>,
51) -> Result<(), IntegrationError> {
52    let _ = (name, config);
53    Ok(())
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::ProviderOps;
60    use crate::resource::ResourcePayload;
61    use stackless_core::def::StackDef;
62    use stackless_stripe_projects::stripe::StripeProjects;
63    use stackless_stripe_projects::test_support;
64
65    #[test]
66    fn config_matches_catalog() {
67        const FIXTURE: &str = include_str!(concat!(
68            env!("CARGO_MANIFEST_DIR"),
69            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
70        ));
71        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
72        let failures =
73            stackless_stripe_projects::verify_service(&catalog, &RunloopSandboxConfig {});
74        assert!(
75            failures.is_empty(),
76            "runloop/sandbox catalog gaps:\n{}",
77            failures.join("\n")
78        );
79    }
80
81    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_sandbox","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_runloop","provider_name":"Runloop","service_id":"sandbox","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"type":"object","required":[],"additionalProperties":false,"properties":{}}}]}}"##;
82
83    fn test_def() -> StackDef {
84        StackDef::parse(
85            r#"
86[stack]
87name = "atto"
88[stack.projects.stripe]
89project = "project_1"
90[integrations.res]
91provider = "runloop"
92[services.api]
93source = { repo = "r", ref = "main" }
94env = { OUT = "${integrations.res.api_key}" }
95health = { path = "/health" }
96[services.api.local]
97run = "true"
98"#,
99        )
100        .unwrap()
101    }
102
103    #[tokio::test]
104    async fn provision_records_outputs() {
105        let runner = test_support::provision_script(
106            CATALOG_ENVELOPE,
107            serde_json::json!({"RUNLOOP_ACCOUNT_ID": "val_account_id", "RUNLOOP_API_KEY": "val_api_key", "RUNLOOP_BASE_URL": "val_base_url"}),
108            0,
109        );
110        let dir = tempfile::tempdir().unwrap();
111        std::fs::write(
112            dir.path().join("stackless.toml"),
113            "[stack]\nname=\"atto\"\n",
114        )
115        .unwrap();
116        let stripe = StripeProjects::new(&runner, dir.path());
117
118        let resource = RunloopSandbox
119            .provision(
120                &stripe.as_dyn(),
121                &test_def(),
122                dir.path(),
123                "demo",
124                "res",
125                "local",
126                false,
127            )
128            .await
129            .unwrap();
130        assert_eq!(resource.resource_kind, "integration-runloop");
131        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
132        assert_eq!(payload.outputs["api_key"], "val_api_key");
133        assert_eq!(payload.outputs["base_url"], "val_base_url");
134    }
135}