Skip to main content

qualia_core_db/q42/app_registry/
mod.rs

1//! Installed-app registry and read-only inspection (WD-02).
2//!
3//! Cold, bounded in-memory registry of portable application manifests.
4//! Registration decodes/validates, checks optional package digests, and
5//! evaluates simple engine compatibility. Malformed packages are quarantined
6//! when identity is recoverable; otherwise rejected. **Inspection never
7//! launches or executes an app.**
8//!
9//! POET is the first bundled fixture via `AppRegistry::with_bundled_poet_fixture`.
10
11mod compat;
12mod error;
13mod fixture;
14mod probe;
15mod record;
16mod registry;
17
18pub use error::AppRegistryError;
19pub use fixture::{poet_fixture_manifest, POET_APP_ID};
20pub use record::{AppInspectView, AppRecordState, InstalledAppRecord, PermissionIntentSummary};
21pub use registry::{AppRegistry, DEFAULT_ENGINE_VERSION, MAX_INSTALLED_APPS};
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use crate::q42::app_manifest::{
27        sha256_of, AppAuthor, AppIdentity, Compatibility, EntryProjection, Integrity,
28        PermissionIntent, PermissionKind, PortableAppManifest, ProjectionKind, RequiredCapability,
29        StateSchema, UpdateChannel, APP_MANIFEST_MAGIC,
30    };
31
32    fn sample_digest() -> [u8; 32] {
33        sha256_of(b"registry-test-package-v1")
34    }
35
36    fn sample_manifest(app_id: &str) -> PortableAppManifest {
37        PortableAppManifest {
38            identity: AppIdentity {
39                app_id: app_id.into(),
40                version: "1.0.0".into(),
41                author: AppAuthor {
42                    name: "Test Author".into(),
43                    did: "did:q42:author:test".into(),
44                },
45            },
46            entries: vec![EntryProjection {
47                projection: ProjectionKind::Manifold,
48                entry_id: "main".into(),
49                relative_path: "entries/main.json".into(),
50            }],
51            required_capabilities: vec![RequiredCapability {
52                id: "qualia.graph.query".into(),
53                min_version: "0.0.40".into(),
54            }],
55            required_assets: vec![],
56            state_schema: StateSchema {
57                schema_id: "q42:TestState".into(),
58                schema_version: "1".into(),
59            },
60            permission_intents: vec![PermissionIntent {
61                kind: PermissionKind::ReadLocalState,
62                scope: "*".into(),
63                optional: false,
64            }],
65            presentation_hints: vec![],
66            compatibility: Compatibility {
67                min_engine_version: "0.0.40".into(),
68                max_engine_version: String::new(),
69                required_features: vec![],
70            },
71            integrity: Integrity {
72                package_sha256: sample_digest(),
73            },
74            update_channel: UpdateChannel {
75                channel_id: "none".into(),
76                relative_feed: String::new(),
77            },
78        }
79    }
80
81    #[test]
82    fn register_ok() {
83        let mut reg = AppRegistry::new();
84        let m = sample_manifest("did:q42:app:health.proof");
85        let rec = reg.register_manifest(&m).unwrap();
86        assert_eq!(rec.app_id, "did:q42:app:health.proof");
87        assert!(rec.integrity_ok);
88        assert_eq!(rec.state, AppRecordState::Active);
89        assert_eq!(reg.len(), 1);
90        assert!(reg.get("did:q42:app:health.proof").is_some());
91    }
92
93    #[test]
94    fn malformed_quarantined_when_identity_recoverable() {
95        let mut reg = AppRegistry::new();
96        let mut bytes = sample_manifest("did:q42:app:broken").encode().unwrap();
97        // Trailing garbage keeps identity probeable but fails closed on decode.
98        bytes.push(0xFF);
99        let rec = reg
100            .register_manifest_bytes(&bytes, None)
101            .expect("quarantine slot");
102        assert_eq!(rec.app_id, "did:q42:app:broken");
103        assert!(rec.state.is_quarantined());
104        assert!(!rec.integrity_ok);
105    }
106
107    #[test]
108    fn malformed_rejected_when_identity_unrecoverable() {
109        let mut reg = AppRegistry::new();
110        let garbage = b"not-a-manifest";
111        assert_eq!(
112            reg.register_manifest_bytes(garbage, None),
113            Err(AppRegistryError::RejectedMalformed)
114        );
115        assert!(reg.is_empty());
116    }
117
118    #[test]
119    fn digest_mismatch_quarantines() {
120        let mut reg = AppRegistry::new();
121        let m = sample_manifest("did:q42:app:digest");
122        let bytes = m.encode().unwrap();
123        let wrong = sha256_of(b"wrong-package-bytes");
124        let rec = reg.register_manifest_bytes(&bytes, Some(&wrong)).unwrap();
125        assert!(rec.state.is_quarantined());
126        assert!(!rec.integrity_ok);
127        assert_eq!(rec.digest, sample_digest());
128    }
129
130    #[test]
131    fn digest_match_active() {
132        let mut reg = AppRegistry::new();
133        let m = sample_manifest("did:q42:app:ok");
134        let bytes = m.encode().unwrap();
135        let digest = sample_digest();
136        let rec = reg.register_manifest_bytes(&bytes, Some(&digest)).unwrap();
137        assert_eq!(rec.state, AppRecordState::Active);
138        assert!(rec.integrity_ok);
139    }
140
141    #[test]
142    fn incompatible_when_min_engine_too_new() {
143        let mut reg = AppRegistry::with_engine_version("0.0.40");
144        let mut m = sample_manifest("did:q42:app:future");
145        m.compatibility.min_engine_version = "0.0.99".into();
146        let rec = reg.register_manifest(&m).unwrap();
147        assert_eq!(rec.state, AppRecordState::Incompatible);
148        assert!(rec.integrity_ok);
149    }
150
151    #[test]
152    fn list_bounded_at_capacity() {
153        let mut reg = AppRegistry::new();
154        for i in 0..MAX_INSTALLED_APPS {
155            let id = format!("did:q42:app:slot{i}");
156            reg.register_manifest(&sample_manifest(&id)).unwrap();
157        }
158        assert_eq!(reg.len(), MAX_INSTALLED_APPS);
159        assert_eq!(reg.list().len(), MAX_INSTALLED_APPS);
160        let overflow = sample_manifest("did:q42:app:overflow");
161        assert_eq!(
162            reg.register_manifest(&overflow),
163            Err(AppRegistryError::RegistryFull)
164        );
165        // Upsert same id still works without growing.
166        reg.register_manifest(&sample_manifest("did:q42:app:slot0"))
167            .unwrap();
168        assert_eq!(reg.len(), MAX_INSTALLED_APPS);
169    }
170
171    #[test]
172    fn inspect_does_not_mutate() {
173        let mut reg = AppRegistry::new();
174        let m = sample_manifest("did:q42:app:inspect");
175        reg.register_manifest(&m).unwrap();
176        let before = reg.len();
177        let view = reg.inspect("did:q42:app:inspect").unwrap();
178        assert_eq!(view.app_id, "did:q42:app:inspect");
179        assert_eq!(view.permission_intents.len(), 1);
180        assert_eq!(view.entry_ids, vec!["main".to_string()]);
181        assert_eq!(view.state, AppRecordState::Active);
182        // Re-inspect and list — length unchanged, state unchanged.
183        let _ = reg.inspect("did:q42:app:inspect");
184        assert_eq!(reg.len(), before);
185        assert_eq!(
186            reg.get("did:q42:app:inspect").unwrap().state,
187            AppRecordState::Active
188        );
189        // inspect is &self — no launch side effects; magic constant still unused here
190        // as a reminder that registry never opens APP_MANIFEST_MAGIC as code.
191        let _ = APP_MANIFEST_MAGIC;
192    }
193
194    #[test]
195    fn poet_fixture_present() {
196        let reg = AppRegistry::with_bundled_poet_fixture();
197        let poet = reg.get(POET_APP_ID).expect("poet bundled");
198        assert_eq!(poet.app_id, POET_APP_ID);
199        assert_eq!(poet.state, AppRecordState::Active);
200        assert!(poet.integrity_ok);
201        let view = reg.inspect(POET_APP_ID).unwrap();
202        assert!(view.permission_summary.kinds.contains(&"read_local_state"));
203        // First occupied slot is POET.
204        assert_eq!(reg.list()[0].app_id, POET_APP_ID);
205    }
206}