skybase 0.1.11

The Open-Source Backend & Developer Platform for the AT Protocol (Firebase for ATProto, powered by skyauth)
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! # `skybase`
//!
//! **The Turn-Key Micro-AppView Engine & Developer Platform for the AT Protocol.**
//!
//! `skybase` brings Firebase-like developer ergonomics to the decentralized AT Protocol ecosystem.
//! Rather than forcing developers to hand-roll custom firehose consumers, backfill pipelines, and
//! database schemas, `skybase` provides a turn-key, single-binary Micro-AppView engine that ingests
//! Bluesky Jetstream commits, indexes them into embedded SQLite WAL with JSON1 virtual columns and
//! FTS5 full-text search, and serves real-time REST and WebSocket live queries down to clients.
//!
//! Powered by [`skyauth`], `skybase` supports three distinct operating topologies:
//! - **Topology A (Client-Sovereign / Zero Custody)**: Client applications write directly to user PDSs;
//!   `skybase` acts as a zero-custody read and indexing engine holding zero private credentials.
//! - **Topology B (Daemon-Managed / Bots)**: Server-side daemons hold DPoP sessions with AES-256-GCM encryption at rest.
//! - **Topology C (Token-Mediated Session Proxy)**: Mediates confidential OAuth sessions for static frontends
//!   (GitHub Pages, Wisp, Tangled) to prevent 2-week session dropouts.

#![forbid(unsafe_code)]
#![deny(
    clippy::all,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::todo,
    clippy::unimplemented,
    missing_docs,
    rust_2018_idioms
)]

pub mod appview;
pub mod chat;
pub mod error;
#[cfg(feature = "storage")]
pub mod index;
pub mod ingest;
pub mod lexicon;
pub mod repo;
pub use appview::{
    ActorProfile, ActorSuggestion, AppViewClient, PostView, DEFAULT_APPVIEW_ENDPOINT,
    DEFAULT_APPVIEW_TIMEOUT_MS, DEFAULT_CDN_ENDPOINT,
};
pub use chat::{
    ChatClient, ChatMessageView, ConvoMember, ConvoView, DeletedMessageView, MessageSender,
    MessageView, SendMessagePayload, DEFAULT_CHAT_ENDPOINT,
};
pub use error::{Result, SkybaseError};
#[cfg(feature = "storage")]
pub use index::{
    parse_at_uri, BatchStats, BroadcastBus, ChangeNotification, QueryBuilder, QueryOp, RecordInput,
    RecordRow, RecordStore, RecordStoreConfig, SortDirection, StoreOperation,
    DEFAULT_BROADCAST_CAPACITY,
};
pub use ingest::{
    build_subscription_url, build_subscription_url_full, normalize_indexed_at,
    parse_frame_timestamp, parse_jetstream_commit, parse_jetstream_frame, resolve_resume_cursor,
    run_jetstream_streamer, BackoffManager, CommitOperation, CursorTracker, JetstreamCommit,
    JetstreamEvent, MockJetstreamServer, MockServerCommand, RawJetstreamCommit,
    RawJetstreamMessage, StreamConfig, CURSOR_REWIND_OVERLAP_US, DEFAULT_JETSTREAM_ENDPOINT,
    DEFAULT_PING_INTERVAL, DEFAULT_READ_TIMEOUT, INITIAL_BACKOFF, MAX_BACKOFF,
    MAX_JETSTREAM_REPLAY_WINDOW_US,
};
#[cfg(feature = "storage")]
pub use ingest::{
    sync_commit_to_store, ConsumerStats, IngesterConfig, JetstreamConsumer, JetstreamConsumerHandle,
};
pub use lexicon::{
    extract_link_facets, format_system_time_iso8601, format_unix_secs_iso8601, has_nsfw_self_label,
    now_iso8601, ByteSlice, Embed, Facet, FacetFeature, FacetIndex, FollowRecord, ListBlockRecord,
    ListItemRecord, ListRecordsResponse, ModListRecord, PostRecord, RecordEmbed,
    RecordWithMediaEmbed, ReplyRef, RepoRecordItem, SelfLabel, SelfLabels, StrongRef,
    NSFW_SELF_LABEL_VALUES,
};
pub use repo::{
    decode_tid_timestamp_secs, decode_tid_timestamp_us, extract_did_for_collection,
    extract_did_from_at_uri, format_at_uri, generate_tid, parse_at_post_uri, split_at_uri,
    validate_rkey, CreateRecordRequest, CreateRecordResult, DeleteRecordRequest, PdsRepoClient,
    PutRecordRequest, PutRecordResult, RecordView, TidGenerator, XrpcErrorResponse,
    MAX_PLAUSIBLE_TID_EPOCH_SECS, MIN_PLAUSIBLE_TID_EPOCH_SECS,
};
pub use skyauth;
pub use tokio_util::sync::CancellationToken;

#[cfg(feature = "storage")]
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

/// Configuration options for initializing a [`Skybase`] engine instance.
#[derive(Debug, Clone)]
pub struct SkybaseConfig {
    /// OAuth Client ID URI (e.g., `<https://app.example.com/oauth/client-metadata.json>`).
    pub client_id: String,
    /// OAuth callback redirect URI (e.g., `<https://app.example.com/oauth/callback>`).
    pub redirect_uri: String,
    /// Application display name presented during consent dialogs.
    pub app_name: String,
    /// Optional Jetstream WebSocket endpoint for real-time firehose subscription.
    pub jetstream_endpoint: Option<String>,
    /// Optional filesystem path to SQLite database. If set, opens a persistent WAL store.
    pub storage_path: Option<PathBuf>,
    /// Whether to open an isolated in-memory SQLite database upon initialization.
    pub in_memory_store: bool,
    /// Optional explicit configuration for the embedded record store.
    #[cfg(feature = "storage")]
    pub store_config: Option<RecordStoreConfig>,
    /// Target collection NSIDs to filter for at the edge during Jetstream ingestion.
    pub wanted_collections: Vec<String>,
    /// Target repository DIDs to filter for at the edge during Jetstream ingestion.
    pub wanted_dids: Vec<String>,
}

impl SkybaseConfig {
    /// Creates a new [`SkybaseConfig`] with required OAuth parameters.
    pub fn new(
        client_id: impl Into<String>,
        redirect_uri: impl Into<String>,
        app_name: impl Into<String>,
    ) -> Self {
        Self {
            client_id: client_id.into(),
            redirect_uri: redirect_uri.into(),
            app_name: app_name.into(),
            jetstream_endpoint: None,
            storage_path: None,
            in_memory_store: false,
            #[cfg(feature = "storage")]
            store_config: None,
            wanted_collections: Vec::new(),
            wanted_dids: Vec::new(),
        }
    }

    /// Sets a custom Jetstream WebSocket endpoint for event subscription.
    pub fn with_jetstream_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.jetstream_endpoint = Some(endpoint.into());
        self
    }

    /// Configures a persistent SQLite database file path.
    pub fn with_storage_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.storage_path = Some(path.into());
        self
    }

    /// Configures an isolated in-memory SQLite database.
    pub fn with_in_memory_store(mut self) -> Self {
        self.in_memory_store = true;
        self
    }

    /// Sets an explicit [`RecordStoreConfig`].
    #[cfg(feature = "storage")]
    pub fn with_store_config(mut self, config: RecordStoreConfig) -> Self {
        self.store_config = Some(config);
        self
    }

    /// Appends a target collection NSID to filter for at the edge.
    pub fn with_wanted_collection(mut self, collection: impl Into<String>) -> Self {
        self.wanted_collections.push(collection.into());
        self
    }

    /// Sets target collection NSIDs to filter for at the edge.
    pub fn with_wanted_collections<I, S>(mut self, collections: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.wanted_collections = collections.into_iter().map(Into::into).collect();
        self
    }

    /// Appends a target DID to filter for at the edge.
    pub fn with_wanted_did(mut self, did: impl Into<String>) -> Self {
        self.wanted_dids.push(did.into());
        self
    }

    /// Sets target DIDs to filter for at the edge.
    pub fn with_wanted_dids<I, S>(mut self, dids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.wanted_dids = dids.into_iter().map(Into::into).collect();
        self
    }
}

/// Combined OAuth provider discovery and public profile preview for a user handle or DID.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct LoginIdentityPreview {
    /// Resolved OAuth identity and provider endpoints (`did`, `handle`, `pds_host`, `auth_server_host`, etc.).
    pub oauth: skyauth::client::OAuthLoginPreview,
    /// Optional public profile metadata fetched from the AppView (`display_name`, `avatar`, etc.).
    pub profile: Option<ActorProfile>,
}

/// The unified Skybase backend engine and client facade.
#[derive(Clone)]
pub struct Skybase {
    config: Arc<SkybaseConfig>,
    auth_client: Arc<skyauth::client::AtprotoOAuthClient>,
    #[cfg(feature = "storage")]
    store: Option<RecordStore>,
}

impl Skybase {
    /// Initializes a new [`Skybase`] engine instance with the given configuration.
    ///
    /// If `storage_path`, `in_memory_store`, or `store_config` is configured, initializes
    /// the embedded SQLite [`RecordStore`] with canonical WAL schemas and indexes.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if metadata is invalid, [`SkybaseError::Auth`] on OAuth
    /// client failure, or [`SkybaseError::Storage`] if SQLite initialization fails.
    pub fn new(config: SkybaseConfig) -> Result<Self> {
        if config.client_id.trim().is_empty() {
            return Err(SkybaseError::Config("client_id cannot be empty".into()));
        }
        if config.redirect_uri.trim().is_empty() {
            return Err(SkybaseError::Config("redirect_uri cannot be empty".into()));
        }

        let metadata =
            skyauth::client::OAuthClientMetadata::new(&config.client_id, &config.redirect_uri)
                .with_client_name(&config.app_name);

        let auth_client = skyauth::client::AtprotoOAuthClient::builder()
            .client_metadata(metadata)
            .build()
            .map_err(SkybaseError::Auth)?;

        #[cfg(feature = "storage")]
        let store = if let Some(ref store_cfg) = config.store_config {
            Some(RecordStore::with_config(store_cfg.clone())?)
        } else if let Some(ref path) = config.storage_path {
            Some(RecordStore::open(path)?)
        } else if config.in_memory_store {
            Some(RecordStore::open_in_memory()?)
        } else {
            None
        };

        Ok(Self {
            config: Arc::new(config),
            auth_client: Arc::new(auth_client),
            #[cfg(feature = "storage")]
            store,
        })
    }

    /// Convenience constructor initializing [`Skybase`] with an in-memory SQLite store.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] or [`SkybaseError::Auth`] if client metadata is invalid,
    /// or [`SkybaseError::Storage`] if in-memory database initialization fails.
    #[cfg(feature = "storage")]
    pub fn in_memory(config: SkybaseConfig) -> Result<Self> {
        Self::new(config.with_in_memory_store())
    }

    /// Attaches an existing [`RecordStore`] to this [`Skybase`] instance.
    #[cfg(feature = "storage")]
    #[must_use]
    pub fn with_store(mut self, store: RecordStore) -> Self {
        self.store = Some(store);
        self
    }

    /// Opens or replaces the active persistent [`RecordStore`] at the specified path.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] if opening the database fails.
    #[cfg(feature = "storage")]
    pub fn open_store(&mut self, path: impl AsRef<Path>) -> Result<RecordStore> {
        let store = RecordStore::open(path)?;
        self.store = Some(store.clone());
        Ok(store)
    }

    /// Opens or replaces the active [`RecordStore`] with an isolated in-memory database.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Storage`] if opening the in-memory database fails.
    #[cfg(feature = "storage")]
    pub fn open_in_memory_store(&mut self) -> Result<RecordStore> {
        let store = RecordStore::open_in_memory()?;
        self.store = Some(store.clone());
        Ok(store)
    }

    /// Returns a reference to the active [`SkybaseConfig`].
    #[must_use]
    pub fn config(&self) -> &SkybaseConfig {
        &self.config
    }

    /// Accesses the underlying [`skyauth::client::AtprotoOAuthClient`] identity and authentication engine.
    #[must_use]
    pub fn auth(&self) -> &skyauth::client::AtprotoOAuthClient {
        &self.auth_client
    }

    /// Resolves a user handle or DID into a [`LoginIdentityPreview`] combining OAuth provider
    /// discovery (`did`, `handle`, `pds_host`, `auth_server_host`) with public AppView profile
    /// enrichment (`display_name`, `avatar`).
    ///
    /// # Errors
    /// Returns [`SkybaseError::Auth`] if OAuth identity or endpoint discovery fails.
    pub async fn preview_login(
        &self,
        appview: &AppViewClient,
        handle_or_did: &str,
    ) -> Result<LoginIdentityPreview> {
        let oauth = self
            .auth_client
            .preview_login(handle_or_did)
            .await
            .map_err(SkybaseError::Auth)?;
        let profile = appview.fetch_profile(&oauth.did).await;
        Ok(LoginIdentityPreview { oauth, profile })
    }

    /// Returns a reference to the active [`RecordStore`], if initialized.
    #[cfg(feature = "storage")]
    #[must_use]
    pub fn store(&self) -> Option<&RecordStore> {
        self.store.as_ref()
    }

    /// Returns a reference to the active [`RecordStore`] or returns [`SkybaseError::Config`].
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn require_store(&self) -> Result<&RecordStore> {
        self.store.as_ref().ok_or_else(|| {
            SkybaseError::Config("RecordStore is not initialized on this Skybase instance".into())
        })
    }

    /// Initiates a structured [`QueryBuilder`] scoped to the given collection NSID.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn query(&self, collection: impl Into<String>) -> Result<QueryBuilder<'_>> {
        Ok(self.require_store()?.query(collection))
    }

    /// Initiates a structured [`QueryBuilder`] scoped to the given collection NSID (alias for [`query`](Self::query)).
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn collection(&self, collection: impl Into<String>) -> Result<QueryBuilder<'_>> {
        self.query(collection)
    }

    /// Subscribes to the live [`ChangeNotification`] broadcast stream.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn subscribe(&self) -> Result<tokio::sync::broadcast::Receiver<ChangeNotification>> {
        Ok(self.require_store()?.subscribe())
    }

    /// Builds a [`JetstreamConsumer`] using the configured `jetstream_endpoint` and `wanted_collections`.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if `jetstream_endpoint` or `RecordStore` is not configured.
    #[cfg(feature = "storage")]
    pub fn consumer(&self) -> Result<JetstreamConsumer> {
        let endpoint = self.config.jetstream_endpoint.as_ref().ok_or_else(|| {
            SkybaseError::Config(
                "Cannot build JetstreamConsumer: 'jetstream_endpoint' is not configured".into(),
            )
        })?;

        let mut config = IngesterConfig::new(endpoint);
        if !self.config.wanted_collections.is_empty() {
            config = config.with_collections(self.config.wanted_collections.clone());
        }
        if !self.config.wanted_dids.is_empty() {
            config = config.with_dids(self.config.wanted_dids.clone());
        }

        self.consumer_with_config(config)
    }

    /// Builds a [`JetstreamConsumer`] with custom [`IngesterConfig`] bound to the internal [`RecordStore`].
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn consumer_with_config(&self, config: IngesterConfig) -> Result<JetstreamConsumer> {
        let store = self.require_store()?.clone();
        Ok(JetstreamConsumer::new(config, store))
    }

    /// Spawns the default [`JetstreamConsumer`] as a managed background task.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if `jetstream_endpoint` or `RecordStore` is not configured.
    #[cfg(feature = "storage")]
    pub fn start_consumer(
        &self,
        cancel: tokio_util::sync::CancellationToken,
    ) -> Result<JetstreamConsumerHandle> {
        let consumer = self.consumer()?;
        Ok(consumer.start(cancel))
    }

    /// Spawns a [`JetstreamConsumer`] with custom configuration as a managed background task.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Config`] if no record store is attached.
    #[cfg(feature = "storage")]
    pub fn start_consumer_with_config(
        &self,
        config: IngesterConfig,
        cancel: tokio_util::sync::CancellationToken,
    ) -> Result<JetstreamConsumerHandle> {
        let consumer = self.consumer_with_config(config)?;
        Ok(consumer.start(cancel))
    }

    /// Creates a sovereign PDS repository client bound to the provided OAuth session.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Network`] if HTTP client initialization fails.
    pub fn repo_client(
        &self,
        session: Arc<skyauth::session::OAuthSession>,
    ) -> Result<PdsRepoClient> {
        PdsRepoClient::new(session, Arc::clone(&self.auth_client))
    }

    /// Creates a sovereign PDS repository client from explicit credentials or mock endpoint tokens.
    ///
    /// Leverages the facade's internal OAuth client for DPoP proof generation and nonce caching.
    ///
    /// # Errors
    /// Returns [`SkybaseError::Auth`] if session creation fails, or [`SkybaseError::Network`]
    /// if HTTP client creation fails.
    pub fn repo_client_from_credentials(
        &self,
        pds_endpoint: impl Into<String>,
        repo_did: impl Into<String>,
        access_token: impl Into<String>,
    ) -> Result<PdsRepoClient> {
        let endpoint = pds_endpoint.into();
        let did = repo_did.into();
        let token = access_token.into();

        let session = skyauth::session::OAuthSession::new(
            did,
            token,
            None,
            "DPoP",
            None,
            None,
            skyauth::dpop::DPoPKey::generate(),
            Some(endpoint),
            None,
            None,
        )
        .map_err(SkybaseError::Auth)?;

        PdsRepoClient::new(Arc::new(session), Arc::clone(&self.auth_client))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, missing_docs)]
mod tests {
    use super::*;

    #[test]
    fn test_skybase_initialization() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Test App",
        );

        let skybase = Skybase::new(config).expect("Skybase should initialize successfully");
        assert_eq!(skybase.config().app_name, "Test App");
    }

    #[test]
    fn test_skybase_config_validation() {
        let invalid_config = SkybaseConfig::new("", "https://app.example.com/callback", "App");
        let result = Skybase::new(invalid_config);
        assert!(matches!(result, Err(SkybaseError::Config(_))));
    }

    #[test]
    fn test_skybase_repo_client_creation() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Test App",
        );
        let skybase = Skybase::new(config).expect("Skybase init failed");

        let session = skyauth::session::OAuthSession::new(
            "did:plc:alice",
            "access_token",
            None,
            "DPoP",
            None,
            None,
            skyauth::dpop::DPoPKey::generate(),
            Some("https://pds.example.com".into()),
            None,
            None,
        )
        .expect("session creation failed");

        let client = skybase.repo_client(Arc::new(session)).expect("repo_client");
        assert_eq!(client.did(), "did:plc:alice");
        assert_eq!(
            client.pds_endpoint().expect("endpoint"),
            "https://pds.example.com"
        );
    }

    #[cfg(feature = "storage")]
    #[test]
    fn test_skybase_config_builders() {
        let path = std::path::PathBuf::from("/tmp/skybase_test.db");
        let store_cfg = RecordStoreConfig::in_memory();
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Builder Test App",
        )
        .with_jetstream_endpoint("wss://jetstream.example.com/subscribe")
        .with_storage_path(&path)
        .with_in_memory_store()
        .with_store_config(store_cfg)
        .with_wanted_collections(vec!["app.bsky.actor.profile", "app.bsky.feed.like"])
        .with_wanted_collection("app.bsky.feed.post")
        .with_wanted_dids(vec!["did:plc:bob", "did:plc:charlie"])
        .with_wanted_did("did:plc:alice");

        assert_eq!(
            config.jetstream_endpoint.as_deref(),
            Some("wss://jetstream.example.com/subscribe")
        );
        assert_eq!(config.storage_path.as_deref(), Some(path.as_path()));
        assert!(config.in_memory_store);
        assert!(config.store_config.is_some());
        assert_eq!(config.wanted_collections.len(), 3);
        assert_eq!(config.wanted_dids.len(), 3);
    }

    #[cfg(feature = "storage")]
    #[test]
    fn test_skybase_in_memory_and_store_delegation() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "In Memory App",
        );

        let skybase = Skybase::in_memory(config).expect("in_memory init failed");
        assert!(skybase.store().is_some());
        assert!(skybase.require_store().is_ok());

        // Test query and collection builder
        let q1 = skybase.query("app.bsky.feed.post").expect("query failed");
        let (sql1, _) = q1.build_sql().expect("sql build failed");
        assert!(sql1.contains("WHERE collection = ?"));

        let q2 = skybase
            .collection("app.bsky.feed.post")
            .expect("collection failed");
        let (sql2, _) = q2.build_sql().expect("sql build failed");
        assert_eq!(sql1, sql2);

        // Test subscribe
        let mut rx = skybase.subscribe().expect("subscribe failed");
        let input = RecordInput::new(
            "did:plc:alice",
            "app.bsky.feed.post",
            "post_facade_1",
            "cid_1",
            serde_json::json!({"text": "Hello facade"}),
            1_000,
        );
        skybase
            .require_store()
            .unwrap()
            .upsert_record(&input)
            .expect("upsert failed");

        let event = rx.try_recv().expect("broadcast event expected");
        match event {
            ChangeNotification::Upsert(row) => {
                assert_eq!(row.uri, input.uri);
                assert_eq!(row.record_json["text"], "Hello facade");
            }
            _ => panic!("Expected Upsert event"),
        }
    }

    #[cfg(feature = "storage")]
    #[test]
    fn test_skybase_uninitialized_store_errors() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "No Store App",
        );
        let skybase = Skybase::new(config).expect("init failed");
        assert!(skybase.store().is_none());
        assert!(matches!(
            skybase.require_store(),
            Err(SkybaseError::Config(_))
        ));
        assert!(matches!(skybase.query("col"), Err(SkybaseError::Config(_))));
        assert!(matches!(
            skybase.collection("col"),
            Err(SkybaseError::Config(_))
        ));
        assert!(matches!(skybase.subscribe(), Err(SkybaseError::Config(_))));
        assert!(matches!(skybase.consumer(), Err(SkybaseError::Config(_))));
    }

    #[cfg(feature = "storage")]
    #[test]
    fn test_skybase_with_store_and_open_store() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Attach Store App",
        );
        let mut skybase = Skybase::new(config).expect("init failed");
        assert!(skybase.store().is_none());

        let store = RecordStore::open_in_memory().expect("store init failed");
        skybase = skybase.with_store(store);
        assert!(skybase.store().is_some());

        // Replace with open_in_memory_store
        let store2 = skybase
            .open_in_memory_store()
            .expect("open_in_memory_store failed");
        assert!(skybase.store().is_some());
        let _ = store2;

        // Replace with open_store in a tempdir
        let temp_dir = tempfile::tempdir().expect("tempdir failed");
        let db_path = temp_dir.path().join("skybase_facade.db");
        let store3 = skybase.open_store(&db_path).expect("open_store failed");
        assert!(skybase.store().is_some());
        let _ = store3;
    }

    #[cfg(feature = "storage")]
    #[tokio::test]
    async fn test_skybase_consumer_wiring() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Consumer App",
        )
        .with_jetstream_endpoint("wss://jetstream.example.com/subscribe")
        .with_in_memory_store()
        .with_wanted_collection("app.bsky.feed.post")
        .with_wanted_did("did:plc:alice");

        let skybase = Skybase::new(config).expect("init failed");
        let consumer = skybase.consumer().expect("consumer build failed");
        assert_eq!(
            consumer.config().wanted_collections,
            vec!["app.bsky.feed.post"]
        );
        assert_eq!(consumer.config().wanted_dids, vec!["did:plc:alice"]);

        let cancel = CancellationToken::new();
        let handle = skybase
            .start_consumer(cancel.clone())
            .expect("start_consumer failed");
        handle.stop();
        let _ = handle.join().await;
    }

    #[test]
    fn test_skybase_repo_client_from_credentials() {
        let config = SkybaseConfig::new(
            "https://app.example.com/oauth/client-metadata.json",
            "https://app.example.com/oauth/callback",
            "Repo App",
        );
        let skybase = Skybase::new(config).expect("init failed");
        let client = skybase
            .repo_client_from_credentials(
                "https://pds.example.com",
                "did:plc:bob",
                "dpop_access_token_123",
            )
            .expect("repo_client_from_credentials failed");

        assert_eq!(client.did(), "did:plc:bob");
        assert_eq!(
            client.pds_endpoint().expect("endpoint"),
            "https://pds.example.com"
        );
    }
}