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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
use matrix_sdk::Client;
use matrix_sdk_base::ruma::{OwnedRoomId, OwnedUserId, UserId};
use scc::hash_map::{Entry, HashMap};
use std::collections::HashSet as StdHashSet;
use std::sync::{Arc, Mutex as StdMutex};
use tracing::{debug, error, info, instrument, trace, warn};

use crate::{
    models::{ActerModel, AnyActerModel},
    Error, Result,
};

#[derive(Clone, Debug)]
pub struct Store {
    pub(crate) client: Client,
    user_id: OwnedUserId,
    models: Arc<HashMap<String, AnyActerModel>>,
    indizes: Arc<HashMap<String, Vec<String>>>,
    dirty: Arc<StdMutex<StdHashSet<String>>>, // our key mutex;
}

static ALL_MODELS_KEY: &str = "ACTER::ALL";
static DB_VERSION_KEY: &str = "ACTER::DB_VERSION";
static CURRENT_DB_VERSION: u32 = 1;

type ModelKeysAndIndizes = (Vec<String>, Vec<String>);

async fn get_from_store<T: serde::de::DeserializeOwned>(client: Client, key: &str) -> Result<T> {
    let v = client
        .store()
        .get_custom_value(format!("acter:{key}").as_bytes())
        .await?
        .ok_or_else(|| Error::ModelNotFound(key.to_owned()))?;
    Ok(serde_json::from_slice(v.as_slice())?)
}

impl Store {
    pub async fn get_raw<T: serde::de::DeserializeOwned>(&self, key: &str) -> Result<T> {
        get_from_store(self.client.clone(), key).await
    }

    pub fn user_id(&self) -> &UserId {
        &self.user_id
    }

    pub async fn set_raw<T: serde::Serialize>(&self, key: &str, value: &T) -> Result<()> {
        trace!(key, "set_raw");
        self.client
            .store()
            .set_custom_value_no_read(
                format!("acter:{key}").as_bytes(),
                serde_json::to_vec(value)?,
            )
            .await?;
        Ok(())
    }

    pub async fn new(client: Client) -> Result<Self> {
        let user_id = client.user_id().ok_or(Error::ClientNotLoggedIn)?.to_owned();
        Self::new_inner(client, user_id).await
    }

    #[cfg(test)]
    pub(crate) async fn new_with_auth(client: Client, user_id: OwnedUserId) -> Result<Self> {
        Self::new_inner(client, user_id).await
    }

    async fn new_inner(client: Client, user_id: OwnedUserId) -> Result<Self> {
        let ver = client
            .store()
            .get_custom_value(DB_VERSION_KEY.as_bytes())
            .await
            .map_err(|e| Error::Custom(format!("failed to find DB version key: {e}")))?
            .map(|u| u32::from_le_bytes(u.as_chunks().0[0]))
            .unwrap_or_default();
        if ver < CURRENT_DB_VERSION {
            // "upgrading" by resetting
            client
                .store()
                .set_custom_value_no_read(ALL_MODELS_KEY.as_bytes(), vec![])
                .await
                .map_err(|e| Error::Custom(format!("setting all models to [] failed: {e}")))?;

            client
                .store()
                .set_custom_value_no_read(
                    DB_VERSION_KEY.as_bytes(),
                    CURRENT_DB_VERSION.to_le_bytes().to_vec(),
                )
                .await
                .map_err(|e| Error::Custom(format!("setting db version failed: {e}")))?;

            return Ok(Store {
                client,
                user_id,
                indizes: Default::default(),
                models: Default::default(),
                dirty: Default::default(),
            });
        }

        // current DB version, attempt to load models

        let data = client
            .store()
            .get_custom_value(ALL_MODELS_KEY.as_bytes())
            .await?
            .map(|v| {
                if v.is_empty() {
                    Ok(vec![])
                } else {
                    serde_json::from_slice::<Vec<String>>(&v)
                }
            })
            .transpose()
            .map_err(|e| Error::Custom(format!("deserializing all models index failed: {e}")))?;
        let models_vec = if let Some(v) = data {
            let items = v.iter().map(|k| {
                let client = client.clone();
                async move {
                    match get_from_store::<AnyActerModel>(client, k).await {
                        Ok(m) => Some(m),
                        Err(e) => {
                            tracing::error!("Couldn’t read model at startup. Skipping. {e}");
                            None
                        }
                    }
                }
            });
            futures::future::join_all(items).await
        } else {
            vec![]
        };

        let indizes: HashMap<String, Vec<String>> = HashMap::new();
        let models: HashMap<String, AnyActerModel> = HashMap::new();
        for m in models_vec {
            let Some(m) = m else {
                // skip None’s
                continue;
            };
            let key = m.event_id().to_string();
            for idx in m.indizes(&user_id) {
                match indizes.entry(idx) {
                    Entry::Occupied(mut o) => {
                        o.get_mut().push(key.clone());
                    }
                    Entry::Vacant(v) => {
                        v.insert_entry(vec![key.clone()]);
                    }
                };
            }
            // ignore duplicates
            let _ = models.insert(key, m);
        }

        Ok(Store {
            client,
            user_id,
            indizes: Arc::new(indizes),
            models: Arc::new(models),
            dirty: Default::default(),
        })
    }

    #[instrument(skip(self))]
    pub async fn get_list(&self, key: &str) -> Result<impl Iterator<Item = AnyActerModel>> {
        self.get_list_inner(key)
    }

    pub fn get_list_inner(&self, key: &str) -> Result<impl Iterator<Item = AnyActerModel>> {
        let listing = if let Some(r) = self.indizes.get(key) {
            r.get().clone()
        } else {
            debug!(user=?self.user_id, key, "No list found");
            vec![]
        };
        let models = self.models.clone();
        let res = listing
            .into_iter()
            .filter_map(move |name| models.get(&name).map(|v| v.get().clone()));
        Ok(res)
    }

    pub async fn get(&self, model_key: &str) -> Result<AnyActerModel> {
        let Some(o) = self.models.get_async(model_key).await else {
            return Err(Error::ModelNotFound(model_key.to_owned()));
        };

        Ok(o.get().clone())
    }

    pub async fn get_many(&self, model_keys: Vec<String>) -> Vec<Option<AnyActerModel>> {
        let models = model_keys.iter().map(|k| async { self.get(k).await.ok() });
        futures::future::join_all(models).await
    }

    #[instrument(skip(self))]
    async fn save_model_inner(&self, mdl: AnyActerModel) -> Result<Vec<String>> {
        let mut dirty = self.dirty.lock()?; // hold the lock
        let (mut keys, indizes) = self.model_inner_under_lock(mdl)?;
        dirty.extend(keys.clone());
        keys.extend(indizes);
        Ok(keys)
    }

    fn model_inner_under_lock(&self, mdl: AnyActerModel) -> Result<ModelKeysAndIndizes> {
        let key = mdl.event_id().to_string();
        let user_id = self.user_id();
        let room_id_idx = format!("{}::models", mdl.room_id());
        let mut keys_changed = vec![key.clone()];
        trace!(user = ?user_id, key, "saving");
        let mut indizes = mdl.indizes(user_id);
        match self.models.entry(key.clone()) {
            Entry::Vacant(v) => {
                v.insert_entry(mdl);
            }
            Entry::Occupied(mut o) => {
                trace!(user=?self.user_id, key, "previous model found");
                let prev = o.insert(mdl);

                let mut remove_idzs = Vec::new();
                for idz in prev.indizes(user_id) {
                    if let Some(idx) = indizes.iter().position(|i| i == &idz) {
                        indizes.remove(idx);
                    } else {
                        remove_idzs.push(idz)
                    }
                }

                for idz in remove_idzs {
                    if let Some(mut v) = self.indizes.get(&idz) {
                        v.get_mut().retain(|k| k != &key);
                    }
                    keys_changed.push(idz);
                }
            }
        }

        for idx in indizes.iter().chain([&room_id_idx]) {
            trace!(user = ?self.user_id, idx, key, exists=self.indizes.contains(idx), "adding to index");
            match self.indizes.entry(idx.clone()) {
                Entry::Vacant(v) => {
                    v.insert_entry(vec![key.clone()]);
                }
                Entry::Occupied(mut o) => {
                    o.get_mut().push(key.clone());
                }
            }
            trace!(user = ?self.user_id, idx, key, "added to index");
        }
        trace!(user=?self.user_id, key, ?keys_changed, "saved");
        Ok((keys_changed, indizes))
    }

    pub async fn save_many(&self, models: Vec<AnyActerModel>) -> Result<Vec<String>> {
        let mut total_keys = Vec::new();
        let mut total_indizes = Vec::new();
        {
            let mut dirty = self.dirty.lock()?; // hold the lock
            for mdl in models.into_iter() {
                let (keys, indizes) = self.model_inner_under_lock(mdl)?;
                dirty.extend(keys.clone());
                total_keys.extend(keys);
                total_indizes.extend(indizes);
            }
        }
        self.sync().await?; // FIXME: should we really run this every time?

        // clean out the duplicates
        total_keys.sort();
        total_keys.dedup();
        total_indizes.sort();
        total_indizes.dedup();

        Ok(total_keys
            .into_iter()
            .chain(total_indizes.into_iter())
            .collect())
    }

    pub async fn save(&self, mdl: AnyActerModel) -> Result<Vec<String>> {
        let keys = self.save_model_inner(mdl).await?;
        self.sync().await?; // FIXME: should we really run this every time?
        Ok(keys)
    }

    pub async fn clear_room(&self, room_id: &OwnedRoomId) -> Result<Vec<String>> {
        info!(?room_id, "clearing room");
        let idx = format!("{room_id}::models");
        let mut total_changed = {
            let mut dirty = self.dirty.lock()?; // hold the lock
            let mut total_changed = Vec::new();
            for model in self.get_list_inner(&idx)? {
                let model_id = model.event_id().to_string();
                let indizes = model.indizes(&self.user_id);
                // remove it from all indizes
                for index in indizes {
                    let _ = self
                        .indizes
                        .entry(index.clone())
                        .and_modify(|l| l.retain(|o| *o != model_id));
                    total_changed.push(index);
                }
                // remove the model itself
                self.models.remove(&model_id);
                dirty.insert(model_id.clone());
                total_changed.push(model_id);
            }

            // remove the room-id based index
            self.indizes.remove(&idx);
            total_changed
        };
        self.sync().await?;

        total_changed.sort();
        total_changed.dedup();

        Ok(total_changed)
    }

    async fn sync(&self) -> Result<()> {
        trace!("sync start");
        let (models_to_write, to_remove, all_models) = {
            trace!("preparing models");
            // preparing for sync
            let mut dirty = self.dirty.lock()?;
            let mut to_remove = Vec::new();
            let mut models_to_write = Vec::new();
            for key in dirty.iter() {
                let Some(r) = self.models.get(key) else {
                    info!(?key, "Model missing, removing custom value");
                    to_remove.push(format!("acter:{key}"));
                    continue;
                };
                let raw = match serde_json::to_vec(r.get()) {
                    Ok(r) => r,
                    Err(error) => {
                        error!(?key, ?error, "failed to serialize. remove");
                        continue;
                    }
                };
                models_to_write.push((format!("acter:{key}"), raw))
            }

            let model_keys: Vec<String> = {
                let mut model_keys: StdHashSet<String> = StdHashSet::new();
                // deduplicate the model_keys;
                self.models.scan(|k, _v| {
                    model_keys.insert(k.clone());
                });
                model_keys.into_iter().collect()
            };

            dirty.clear(); // we clear the current set
            trace!("preparation done");
            (models_to_write, to_remove, serde_json::to_vec(&model_keys)?)
        };
        trace!("store sync");
        let client_store = self.client.store();
        for (key, value) in models_to_write.into_iter() {
            if let Err(error) = client_store
                .set_custom_value_no_read(key.as_bytes(), value)
                .await
            {
                error!(?key, ?error, "syncing model failed");
            }
        }
        trace!("done store syncing");

        trace!("syncing all models");
        client_store
            .set_custom_value_no_read(ALL_MODELS_KEY.as_bytes(), all_models)
            .await?;

        trace!("removing old models");
        for key in to_remove.into_iter() {
            if let Err(error) = client_store.remove_custom_value(key.as_bytes()).await {
                warn!(key, ?error, "Error removing model");
            }
        }

        trace!("sync done");

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{TestModel, TestModelBuilder};
    use anyhow::bail;
    use matrix_sdk_base::{
        ruma::{api::MatrixVersion, event_id, user_id, OwnedEventId, OwnedRoomId},
        store::{MemoryStore, StoreConfig},
    };

    async fn fresh_store_and_client() -> Result<(Store, Client)> {
        let config = StoreConfig::default().state_store(MemoryStore::new());
        let client = Client::builder()
            .homeserver_url("http://localhost")
            .server_versions([MatrixVersion::V1_5])
            .store_config(config)
            .build()
            .await
            .unwrap();

        Ok((
            Store::new_with_auth(client.clone(), user_id!("@test:example.org").to_owned()).await?,
            client,
        ))
    }
    async fn fresh_store() -> Result<Store> {
        Ok(fresh_store_and_client().await?.0)
    }

    #[tokio::test]
    async fn smoke_test() -> Result<()> {
        let _ = env_logger::try_init();
        let _ = fresh_store().await?;
        Ok(())
    }

    #[tokio::test]
    async fn save_and_get_one_simple() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let model = TestModelBuilder::default().simple().build().unwrap();
        let key = model.event_id().to_string();
        let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
        assert_eq!(vec![key.clone()], res_keys);
        let mdl = store.get(&key).await?;
        let AnyActerModel::TestModel(other) = mdl else {
            bail!("Returned model isn’t test model: {mdl:?}");
        };
        assert_eq!(model, other);
        Ok(())
    }

    #[tokio::test]
    async fn save_and_get_many_simple() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let models: Vec<TestModel> = (0..5)
            .map(|idx| {
                TestModelBuilder::default()
                    .simple()
                    .event_id(OwnedEventId::try_from(format!("$ASDF{idx}")).unwrap())
                    .build()
                    .unwrap()
            })
            .collect();
        let res_keys = store
            .save_many(
                models
                    .iter()
                    .map(|m| AnyActerModel::TestModel(m.clone()))
                    .collect(),
            )
            .await?;
        assert_eq!(
            models
                .iter()
                .map(|m| m.event_id().to_string())
                .collect::<Vec<String>>(),
            res_keys
        );

        let loaded_models = store
            .get_many(
                models
                    .iter()
                    .map(|m| m.event_id().to_string())
                    .collect::<Vec<String>>(),
            )
            .await
            .into_iter()
            .filter_map(|m| match m {
                Some(AnyActerModel::TestModel(inner)) => Some(inner),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(models, loaded_models);

        for model in models.into_iter() {
            let key = model.event_id().to_string();
            let mdl = store.get(&key).await?;
            let AnyActerModel::TestModel(other) = mdl else {
                bail!("Returned model isn’t test model: {mdl:?}");
            };
            assert_eq!(model, other);
        }
        Ok(())
    }

    #[tokio::test]
    async fn save_and_get_one_from_index() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let model = TestModelBuilder::default()
            .simple()
            .indizes(vec!["indexA".to_owned(), "index::b".to_owned()])
            .build()
            .unwrap();
        let key = model.event_id().to_string();
        let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
        assert_eq!(
            vec![key.clone(), "indexA".to_owned(), "index::b".to_owned()],
            res_keys
        );
        let mdl = store.get(&key).await?;
        let AnyActerModel::TestModel(other) = mdl else {
            bail!("Returned model isn’t test model: {mdl:?}");
        };
        assert_eq!(model, other);

        let mut index = store.get_list("indexA").await?;
        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert!(index.next().is_none()); // and nothing else
        assert_eq!(model, other);

        let mut index = store.get_list("index::b").await?;
        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert!(index.next().is_none()); // and nothing else
        assert_eq!(model, other);

        let mut index = store.get_list("empty_index").await?;
        assert!(index.next().is_none()); // and nothing here
        assert_eq!(model, other);

        Ok(())
    }

    #[tokio::test]
    async fn add_get_one_to_index() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let model = TestModelBuilder::default()
            .simple()
            .indizes(vec!["indexA".to_owned()])
            .build()
            .unwrap();
        let key = model.event_id().to_string();
        let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
        assert_eq!(vec![key.clone(), "indexA".to_owned()], res_keys);

        let mut index = store.get_list("indexA").await?;
        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert!(index.next().is_none()); // and nothing else
        assert_eq!(model, other);

        let second_model = TestModelBuilder::default()
            .simple()
            .event_id(OwnedEventId::try_from("$secondModel").unwrap())
            .indizes(vec!["indexA".to_owned()])
            .build()
            .unwrap();
        let key = second_model.event_id().to_string();
        let res_keys = store
            .save(AnyActerModel::TestModel(second_model.clone()))
            .await?;
        assert_eq!(vec![key.clone(), "indexA".to_owned()], res_keys);

        let mut index = store.get_list("indexA").await?;
        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert_eq!(model, other);

        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert_eq!(second_model, other);

        assert!(index.next().is_none()); // and nothing else

        Ok(())
    }

    #[tokio::test]
    async fn model_changed_index() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let e_id = event_id!("$asdfAsdf");
        let store = fresh_store().await?;
        {
            let model = TestModelBuilder::default()
                .simple()
                .event_id(e_id.to_owned())
                .indizes(vec!["indexA".to_owned(), "index::b".to_owned()])
                .build()
                .unwrap();
            let key = model.event_id().to_string();
            let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
            assert_eq!(
                vec![key.clone(), "indexA".to_owned(), "index::b".to_owned()],
                res_keys
            );
            let mdl = store.get(&key).await?;
            let AnyActerModel::TestModel(other) = mdl else {
                bail!("Returned model isn’t test model: {mdl:?}");
            };
            assert_eq!(model, other);

            let mut index = store.get_list("indexA").await?;
            let Some(AnyActerModel::TestModel(other)) = index.next() else {
                bail!("Returned model isn’t test model.");
            };
            assert!(index.next().is_none()); // and nothing else
            assert_eq!(model, other);

            let mut index = store.get_list("index::b").await?;
            let Some(AnyActerModel::TestModel(other)) = index.next() else {
                bail!("Returned model isn’t test model.");
            };
            assert!(index.next().is_none()); // and nothing else
            assert_eq!(model, other);

            let mut index = store.get_list("new_index").await?;
            assert!(index.next().is_none()); // and nothing here
            assert_eq!(model, other);
        }
        {
            // overwriting the indexes with a new set
            let model = TestModelBuilder::default()
                .simple()
                .event_id(e_id.to_owned())
                .indizes(vec!["new_index".to_owned()])
                .build()
                .unwrap();
            let key = model.event_id().to_string();
            // we overwrite this
            let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
            assert_eq!(
                vec![
                    key.clone(),
                    "indexA".to_owned(),
                    "index::b".to_owned(),
                    "new_index".to_owned()
                ],
                res_keys
            );
            let mut index = store.get_list("indexA").await?;
            assert!(index.next().is_none()); // empty now

            let mut index = store.get_list("index::b").await?;
            assert!(index.next().is_none()); // empty now

            // only via our new index
            let mut index = store.get_list("new_index").await?;
            let Some(AnyActerModel::TestModel(other)) = index.next() else {
                bail!("Returned model isn’t test model.");
            };
            assert!(index.next().is_none()); // and nothing else
            assert_eq!(model, other);
        }

        Ok(())
    }

    #[tokio::test]
    async fn recover() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let (client, model) = {
            let (store, client) = fresh_store_and_client().await?;
            let model = TestModelBuilder::default()
                .simple()
                .indizes(vec!["test_index".to_owned()])
                .build()
                .unwrap();
            let key = model.event_id().to_string();
            let res_keys = store.save(AnyActerModel::TestModel(model.clone())).await?;
            assert_eq!(vec![key.clone(), "test_index".to_owned()], res_keys);
            let mdl = store.get(&key).await?;
            let AnyActerModel::TestModel(other) = mdl else {
                bail!("Returned model isn’t test model: {mdl:?}");
            };
            assert_eq!(model, other);
            (client, model)
        };

        // let’s attempt to recover
        let store =
            Store::new_with_auth(client.clone(), user_id!("@test:example.org").to_owned()).await?;

        // and we should be able to get it again.
        let mdl = store.get(model.event_id().as_ref()).await?;
        let AnyActerModel::TestModel(other) = mdl else {
            bail!("Returned model isn’t test model: {mdl:?}");
        };
        assert_eq!(model, other);

        // now recover from the the index!

        let mut index = store.get_list("test_index").await?;
        let Some(AnyActerModel::TestModel(other)) = index.next() else {
            bail!("Returned model isn’t test model.");
        };
        assert!(index.next().is_none()); // and nothing else
        assert_eq!(model, other);

        let mut index = store.get_list("empty_index").await?;
        assert!(index.next().is_none()); // and nothing here
        assert_eq!(model, other);

        Ok(())
    }

    #[tokio::test]
    async fn save_and_get_raw_simple() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let model = vec!["Just a simple string".to_owned()];
        let key = "random key we use";
        store.set_raw(key, &model).await?;
        let other: Vec<String> = store.get_raw(key).await?;
        assert_eq!(model, other);
        Ok(())
    }

    #[tokio::test]
    async fn remove_room() -> anyhow::Result<()> {
        let _ = env_logger::try_init();
        let store = fresh_store().await?;
        let first_room_id = OwnedRoomId::try_from("!firstRoom:example.org").unwrap();
        let second_room_id = OwnedRoomId::try_from("!secondRoom:example.org").unwrap();
        let index_a = "index_a".to_owned();
        let index_b = "index_b".to_owned();
        let index_c = "index_c".to_owned();

        let first_room_models = (0..5)
            .map(|idx| {
                TestModelBuilder::default()
                    .simple()
                    .event_id(OwnedEventId::try_from(format!("$ASDF{idx}")).unwrap())
                    .indizes(vec![index_a.clone(), index_c.clone()])
                    .room_id(first_room_id.clone())
                    .build()
                    .unwrap()
            })
            .collect::<Vec<_>>();

        let second_room_models = (0..5)
            .map(|idx| {
                TestModelBuilder::default()
                    .simple()
                    .event_id(OwnedEventId::try_from(format!("$IDX{idx}")).unwrap())
                    .indizes(vec![index_a.clone(), index_b.clone()])
                    .room_id(second_room_id.clone())
                    .build()
                    .unwrap()
            })
            .collect::<Vec<_>>();

        let first_model_keys = first_room_models
            .iter()
            .map(|m| m.event_id().to_string())
            .collect::<Vec<String>>();
        let second_model_keys = second_room_models
            .iter()
            .map(|m| m.event_id().to_string())
            .collect::<Vec<String>>();

        let all_model_keys = first_model_keys
            .iter()
            .chain(second_model_keys.iter())
            .map(Clone::clone)
            .collect::<Vec<String>>();

        // submit all
        let res_keys = store
            .save_many(
                first_room_models
                    .iter()
                    .chain(second_room_models.iter())
                    .map(|m| AnyActerModel::TestModel(m.clone()))
                    .collect(),
            )
            .await?;

        // confirm all is in order:
        assert_eq!(
            all_model_keys
                .into_iter()
                .chain(
                    // add the indizes that are also updated
                    ["index_a", "index_b", "index_c"]
                        .into_iter()
                        .map(ToString::to_string)
                )
                .collect::<Vec<String>>(),
            res_keys
        );

        let loaded_models_first = store
            .get_many(first_model_keys.clone())
            .await
            .into_iter()
            .filter_map(|m| match m {
                Some(AnyActerModel::TestModel(inner)) => Some(inner),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(first_room_models, loaded_models_first);

        let loaded_models_second = store
            .get_many(second_model_keys.clone())
            .await
            .into_iter()
            .filter_map(|m| match m {
                Some(AnyActerModel::TestModel(inner)) => Some(inner),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(second_room_models, loaded_models_second);

        //  --- now let’s remove the first room ---
        let notifiers = store.clear_room(&first_room_id).await?;
        assert_eq!(notifiers.len(), 7); // 5 models & 2 indizes = 7 changes

        // first room models are all gone:
        for new in store.get_many(first_model_keys.clone()).await {
            assert!(new.is_none(), "first model still found {new:?}");
        }

        // but second are all there

        let loaded_models_second = store
            .get_many(second_model_keys.clone())
            .await
            .into_iter()
            .filter_map(|m| match m {
                Some(AnyActerModel::TestModel(inner)) => Some(inner),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(second_room_models, loaded_models_second);

        Ok(())
    }
}