Skip to main content

stackless_integrations/providers/privy/
app.rs

1//! `privy/app` 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};
12use crate::registry;
13
14pub const RESOURCE_KIND: &str = "integration-privy";
15
16#[derive(Debug, Serialize)]
17pub struct PrivyAppConfig {
18    pub app_name: String,
19}
20
21impl CatalogService for PrivyAppConfig {
22    const REFERENCE: &'static str = "privy/app";
23}
24
25#[derive(Debug)]
26pub struct PrivyApp;
27
28impl Hostable for PrivyApp {
29    const PROVIDER: &'static str = "privy";
30    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
31    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
32    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
33    const OUTPUTS: &'static [&'static str] = &["app_id", "app_secret"];
34}
35
36impl FamilyResource for PrivyApp {
37    type Config = PrivyAppConfig;
38    const PROVIDER_PREFIX: &'static str = "PRIVY";
39    // Provisional until pinned by `mise run discover privy/app`.
40    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
41        ("APP_ID", "app_id", true),
42        ("APP_SECRET", "app_secret", true),
43    ];
44
45    fn build_config(ctx: &ProvisionContext<'_>) -> Result<PrivyAppConfig, IntegrationError> {
46        let config = super::integration_config(ctx)?;
47        Ok(PrivyAppConfig {
48            app_name: super::interp_required(ctx, &config, "app_name")?,
49        })
50    }
51}
52
53pub fn validate_config(
54    name: &str,
55    config: &BTreeMap<String, toml::Value>,
56) -> Result<(), IntegrationError> {
57    registry::config_string(config, "app_name").map_err(|err| IntegrationError::ConfigInvalid {
58        location: format!("integrations.{name}.app_name"),
59        detail: err.to_string(),
60    })?;
61    let _ = (name, config);
62    Ok(())
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::ProviderOps;
69    use crate::resource::ResourcePayload;
70    use stackless_core::def::StackDef;
71    use stackless_stripe_projects::stripe::StripeProjects;
72    use stackless_stripe_projects::test_support;
73
74    #[test]
75    fn config_matches_catalog() {
76        const FIXTURE: &str = include_str!(concat!(
77            env!("CARGO_MANIFEST_DIR"),
78            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
79        ));
80        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
81        let failures = stackless_stripe_projects::verify_service(
82            &catalog,
83            &PrivyAppConfig {
84                app_name: "test-app_name".into(),
85            },
86        );
87        assert!(
88            failures.is_empty(),
89            "privy/app catalog gaps:\n{}",
90            failures.join("\n")
91        );
92    }
93
94    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_app","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_privy","provider_name":"Privy","service_id":"app","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"properties":{"app_name":{"description":"Display name for the app","type":"string"}},"required":["app_name"],"type":"object"}}]}}"##;
95
96    fn test_def() -> StackDef {
97        StackDef::parse(
98            r#"
99[stack]
100name = "atto"
101[stack.projects.stripe]
102project = "project_1"
103[integrations.res]
104provider = "privy"
105app_name = "test-app_name"
106[services.api]
107source = { repo = "r", ref = "main" }
108env = { OUT = "${integrations.res.app_id}" }
109health = { path = "/health" }
110[services.api.local]
111run = "true"
112"#,
113        )
114        .unwrap()
115    }
116
117    #[tokio::test]
118    async fn provision_records_outputs() {
119        let runner = test_support::provision_script(
120            CATALOG_ENVELOPE,
121            serde_json::json!({"PRIVY_APP_ID": "val_app_id", "PRIVY_APP_SECRET": "val_app_secret"}),
122            0,
123        );
124        let dir = tempfile::tempdir().unwrap();
125        std::fs::write(
126            dir.path().join("stackless.toml"),
127            "[stack]\nname=\"atto\"\n",
128        )
129        .unwrap();
130        let stripe = StripeProjects::new(&runner, dir.path());
131
132        let resource = PrivyApp
133            .provision(
134                &stripe.as_dyn(),
135                &test_def(),
136                dir.path(),
137                "demo",
138                "res",
139                "local",
140                false,
141            )
142            .await
143            .unwrap();
144        assert_eq!(resource.resource_kind, "integration-privy");
145        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
146        assert_eq!(payload.outputs["app_id"], "val_app_id");
147    }
148}