anytype 0.3.2

An ergonomic Anytype API client in rust
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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
//! Cache Behavior Integration Tests for anytype
//!
//! This test suite validates the caching behavior of the anytype client,
//! including cache warming, cache clearing, cache isolation, and cache disabled mode.
//!
//! ## Cache Design
//!
//! The client maintains in-memory caches for:
//! - Spaces: Global cache (not space-specific)
//! - Properties: Per-space cache (keyed by space_id)
//! - Types: Per-space cache (keyed by space_id)
//!
//! There are no apis for updating or deleting a single item, or marking items dirty.
//! Updating and Clearing cache based on application usage patterns
//! is the responsibility of the application.
//!
//! ### Cache Warming
//!
//! When calling `list()` for properties/types/spaces, the cache is populated.
//! Subsequent `get()` calls use the cache instead of making API calls.
//!
//! If `get()` is called first and cache is empty, it automatically warms the cache
//! by doing a full list operation, then returns the requested item from cache.
//!
//! ### Cache Clearing
//!
//! - `clear()` - clear everything
//! - `clear_space()` - clear properties and types for a space (but not the space itself)
//! - `clear_spaces()` - clears all spaces
//! - `clear_properties(Some(space_id))` - clears properties for one space
//! - `clear_properties(None)` - clears properties for all spaces
//! - `clear_types(Some(space_id))` - clears types for one space
//! - `clear_types(None)` - clears types for all spaces
//!
//! ## Environment Requirements
//!
//! Required environment variables (see .test-env):
//! - `ANYTYPE_TEST_URL` - API endpoint (default: http://127.0.0.1:31012)
//! - `ANYTYPE_TEST_KEY_FILE` - Path to file containing API key
//! - `ANYTYPE_TEST_SPACE_ID` - Existing space ID for testing
//!
//! ## Running
//!
//! Since there is one "global" cache (per client), cache tests only run serialized.
//! The cache is thread-safe, locked by per-entity mutexes, but tests regularly clear
//! the cache to reset state, and count number of items in the cache,
//! so they must be serialized to make them deterministic.
//!
//! The current design is fairly simplistic, in that there is no fine-grained cache
//! invalidation or updates, so multi-threaded tests and complex invalidation tests are
//! out of scope.
//!
//! ```bash
//! source .test-env
//! cargo test -p anytype --test test_cache
//! ```

mod common;

// =============================================================================
// Cache Warmth Tests
// =============================================================================

#[cfg(test)]
mod cache_warmth {
    use anytype::test_util::*;
    use serial_test::serial;
    use test_log::test;

    /// Test that listing properties warms the cache and get uses it
    #[tokio::test]
    #[test_log::test]
    #[serial]
    async fn test_properties_cache_warmth() {
        with_test_context_unit(|ctx| async move {
            // Clear cache to ensure clean state
            ctx.client.cache().clear_properties(None);

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_properties(),
                0,
                "Cache should be empty initially"
            );

            // List properties - this should warm the cache
            let properties = ctx
                .client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");

            assert!(!properties.is_empty(), "Should have at least one property");

            // Verify cache is now populated
            let cache_count = ctx.client.cache().num_properties();
            assert!(
                cache_count > 0,
                "Cache should be populated after list (got {})",
                cache_count
            );

            // Get metrics after list to establish baseline
            let metrics_after_list = ctx.client.http_metrics();

            // Get a specific property - should use cache (no API call)
            let first_property = properties.iter().next().unwrap();
            let property = ctx
                .client
                .property(&ctx.space_id, &first_property.id)
                .get()
                .await
                .expect("Failed to get property");

            assert_eq!(property.id, first_property.id);
            assert_eq!(property.key, first_property.key);

            // Verify no additional HTTP requests were made (proving it used cache)
            let metrics_after_get = ctx.client.http_metrics();
            assert_eq!(
                metrics_after_get.total_requests, metrics_after_list.total_requests,
                "No additional HTTP requests should be made when using cached data"
            );

            // Cleanup
            ctx.client.cache().clear_properties(None);
        })
        .await
    }

    /// Test that listing types warms the cache and get uses it
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_types_cache_warmth() {
        with_test_context_unit(|ctx| async move {
            // Clear cache to ensure clean state
            ctx.client.cache().clear_types(None);

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_types(),
                0,
                "Cache should be empty initially"
            );

            // List types - this should warm the cache
            let types = ctx
                .client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list types");

            assert!(!types.is_empty(), "Should have at least one type");

            // Verify cache is now populated
            let cache_count = ctx.client.cache().num_types();
            assert!(
                cache_count > 0,
                "Cache should be populated after list (got {})",
                cache_count
            );

            // Get metrics after list to establish baseline
            let metrics_after_list = ctx.client.http_metrics();

            // Get a specific type - should use cache (no API call)
            let first_type = types.iter().next().unwrap();
            let typ = ctx
                .client
                .get_type(&ctx.space_id, &first_type.id)
                .get()
                .await
                .expect("Failed to get type");

            assert_eq!(typ.id, first_type.id);
            assert_eq!(typ.key, first_type.key);

            // Verify no additional HTTP requests were made (proving it used cache)
            let metrics_after_get = ctx.client.http_metrics();
            assert_eq!(
                metrics_after_get.total_requests, metrics_after_list.total_requests,
                "No additional HTTP requests should be made when using cached data"
            );

            // Cleanup
            ctx.client.cache().clear_types(None);
        })
        .await
    }

    /// Test that listing spaces warms the cache and get uses it
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_spaces_cache_warmth() {
        with_test_context_unit(|ctx| async move {
            let spaces = ctx.client.spaces().list().await.expect("get spaces");
            eprintln!("TMP200 Warmth found {} spaces", spaces.len());

            // Clear cache to ensure clean state
            ctx.client.cache().clear();

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_spaces(),
                0,
                "Cache should be empty initially"
            );

            // List spaces - this should warm the cache
            let spaces = ctx
                .client
                .spaces()
                .list()
                .await
                .expect("Failed to list spaces");

            eprintln!("TMP216 list spaces found {} spaces", spaces.len());
            assert!(!spaces.is_empty(), "Should have at least one space");

            // Verify cache is now populated
            let cache_count = ctx.client.cache().num_spaces();
            assert!(
                cache_count > 0,
                "Cache should be populated after list (got {})",
                cache_count
            );
            eprintln!("TMP226, num_spaces in cache: {cache_count}");

            // Get metrics after list to establish baseline
            let metrics_after_list = ctx.client.http_metrics();

            // Get a specific space - should use cache (no API call)
            let first_space = spaces.iter().next().unwrap();
            let space = ctx
                .client
                .space(&first_space.id)
                .get()
                .await
                .expect("Failed to get space");

            assert_eq!(space.id, first_space.id);

            // Verify no additional HTTP requests were made (proving it used cache)
            let metrics_after_get = ctx.client.http_metrics();
            assert_eq!(
                metrics_after_get.total_requests, metrics_after_list.total_requests,
                "No additional HTTP requests should be made when using cached data"
            );

            // Cleanup
            ctx.client.cache().clear();
        })
        .await
    }

    /// Test that get() auto-warms cache when empty
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_get_auto_warms_cache() {
        with_test_context_unit(|ctx| async move {
            // Clear cache to ensure clean state
            ctx.client.cache().clear_properties(None);

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_properties(),
                0,
                "Cache should be empty initially"
            );

            // First, get a property ID by listing (then clear cache)
            let properties = ctx
                .client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");
            let property_id = properties.iter().next().unwrap().id.clone();

            // Clear cache again
            ctx.client.cache().clear_properties(None);
            assert_eq!(ctx.client.cache().num_properties(), 0);

            // Get property directly - should auto-warm cache via list
            let property = ctx
                .client
                .property(&ctx.space_id, &property_id)
                .get()
                .await
                .expect("Failed to get property");

            assert_eq!(property.id, property_id);

            // Verify cache was warmed
            assert!(
                ctx.client.cache().num_properties() > 0,
                "Cache should be warmed after get() with empty cache"
            );

            // Cleanup
            ctx.client.cache().clear_properties(None);
        })
        .await
    }
}

// =============================================================================
// Cache Clearing Tests
// =============================================================================

mod cache_clearing {
    use anytype::test_util::*;
    use serial_test::serial;
    use test_log::test;

    /// Test clearing properties cache for a specific space
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_clear_properties_cache_targeted() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches first
            ctx.client.cache().clear_properties(None);

            // Warm the cache for our test space
            let properties = ctx
                .client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties")
                .collect_all()
                .await
                .expect("collect-all properties");

            let initial_count = ctx.client.cache().num_properties();
            assert!(initial_count > 0, "Cache should be populated after list");
            assert_eq!(properties.len(), initial_count);

            // Clear cache for this specific space
            ctx.client.cache().clear_properties(Some(&ctx.space_id));

            // Verify cache is now empty
            assert_eq!(
                ctx.client.cache().num_properties(),
                0,
                "Cache should be empty after targeted clear"
            );

            // Re-list to warm cache again
            let properties_next = ctx
                .client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to re-list properties")
                .collect_all()
                .await
                .expect("collect properties_next");

            let temp_dir = ctx.temp_dir("properties").expect("temp dir");
            let file1 = temp_dir.join("properties1.json");
            let file2 = temp_dir.join("properties2.json");

            std::fs::write(&file1, serde_json::to_string_pretty(&properties).unwrap())
                .expect("dump json1");

            std::fs::write(
                &file2,
                serde_json::to_string_pretty(&properties_next).unwrap(),
            )
            .expect("dump json2");

            assert_eq!(ctx.client.cache().num_properties(), properties_next.len());
            assert_eq!(
                properties_next.len(),
                initial_count,
                "Cache should be re-populated with same count"
            );

            // Cleanup
            ctx.client.cache().clear_properties(None);
        })
        .await
    }

    /// Test clearing properties cache globally (all spaces)
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_clear_properties_cache_global() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches first
            ctx.client.cache().clear_properties(None);

            // Warm the cache
            ctx.client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");

            assert!(
                ctx.client.cache().num_properties() > 0,
                "Cache should be populated"
            );

            // Clear all properties caches
            ctx.client.cache().clear_properties(None);

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_properties(),
                0,
                "Cache should be empty after global clear"
            );

            // Cleanup (redundant but consistent)
            ctx.client.cache().clear_properties(None);
        })
        .await
    }

    /// Test clearing types cache for a specific space
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_clear_types_cache_targeted() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches first
            ctx.client.cache().clear_types(None);

            // Warm the cache for our test space
            ctx.client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list types");

            let initial_count = ctx.client.cache().num_types();
            assert!(initial_count > 0, "Cache should be populated after list");

            // Clear cache for this specific space
            ctx.client.cache().clear_types(Some(&ctx.space_id));

            // Verify cache is now empty
            assert_eq!(
                ctx.client.cache().num_types(),
                0,
                "Cache should be empty after targeted clear"
            );

            // Re-list to warm cache again
            ctx.client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to re-list types");

            assert_eq!(
                ctx.client.cache().num_types(),
                initial_count,
                "Cache should be re-populated with same count"
            );

            // Cleanup
            ctx.client.cache().clear_types(None);
        })
        .await
    }

    /// Test clearing types cache globally (all spaces)
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_clear_types_global() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches first
            ctx.client.cache().clear_types(None);

            // Warm the cache
            ctx.client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list types");

            assert!(
                ctx.client.cache().num_types() > 0,
                "Cache should be populated"
            );

            // Clear all types caches
            ctx.client.cache().clear_types(None);

            // Verify cache is empty
            assert_eq!(
                ctx.client.cache().num_types(),
                0,
                "Cache should be empty after global clear"
            );

            // Cleanup (redundant but consistent)
            ctx.client.cache().clear_types(None);
        })
        .await
    }

    /// Test that clearing cache multiple times is idempotent
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_clear_idempotent() {
        with_test_context_unit(|ctx| async move {
            // Clear cache multiple times - should not fail
            ctx.client.cache().clear_properties(None);
            ctx.client.cache().clear_properties(None);

            ctx.client.cache().clear_types(None);
            ctx.client.cache().clear_types(None);

            ctx.client.cache().clear_spaces();
            ctx.client.cache().clear_spaces();

            ctx.client.cache().clear();
            ctx.client.cache().clear();

            // All caches should be empty
            assert_eq!(ctx.client.cache().num_properties(), 0);
            assert_eq!(ctx.client.cache().num_types(), 0);
            assert_eq!(ctx.client.cache().num_spaces(), 0);

            // Clear specific space cache when cache is empty - should not fail
            ctx.client.cache().clear_properties(Some(&ctx.space_id));
            ctx.client.cache().clear_types(Some(&ctx.space_id));

            // Cleanup (redundant but consistent)
            ctx.client.cache().clear();
        })
        .await
    }
}

// =============================================================================
// Cache Disabled Tests
// =============================================================================

mod cache_disabled {
    use anytype::prelude::*;
    use serial_test::serial;
    use test_log::test;

    /// Test that when cache is disabled, list operations don't populate cache
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_disabled_via_config() {
        // Create a client with cache disabled
        let base_url = std::env::var("ANYTYPE_TEST_URL")
            .unwrap_or_else(|_| "http://127.0.0.1:31012".to_string());
        let space_id =
            std::env::var("ANYTYPE_TEST_SPACE_ID").expect("ANYTYPE_TEST_SPACE_ID required");
        let keystore = if let Ok(key_file) = std::env::var("ANYTYPE_TEST_KEY_FILE") {
            format!("file:path={key_file}")
        } else if let Ok(store) = std::env::var("ANYTYPE_KEYSTORE") {
            store
        } else {
            panic!("set ANYTYPE_TEST_KEY_FILE or ANYTYPE_KEYSTORE");
        };

        let config = ClientConfig {
            base_url: Some(base_url),
            app_name: "anytype-cache-test".to_string(),
            rate_limit_max_retries: 0,
            disable_cache: true, // Disable cache
            keystore: Some(keystore),
            keystore_service: Some("anyr".into()),
            ..Default::default()
        };
        let client = AnytypeClient::with_config(config).expect("Failed to create client");

        // Verify cache is initially empty
        assert_eq!(client.cache().num_properties(), 0);
        assert_eq!(client.cache().num_types(), 0);
        assert_eq!(client.cache().num_spaces(), 0);

        // List properties - should NOT populate cache
        let properties = client
            .properties(&space_id)
            .list()
            .await
            .expect("Failed to list properties");

        assert!(!properties.is_empty(), "Should have properties");

        // Cache should still be empty
        assert_eq!(
            client.cache().num_properties(),
            0,
            "Cache should remain empty when disabled"
        );

        // List types - should NOT populate cache
        let types = client
            .types(&space_id)
            .list()
            .await
            .expect("Failed to list types");

        assert!(!types.is_empty(), "Should have types");

        // Cache should still be empty
        assert_eq!(
            client.cache().num_types(),
            0,
            "Cache should remain empty when disabled"
        );

        // List spaces - should NOT populate cache
        let spaces = client.spaces().list().await.expect("Failed to list spaces");

        assert!(!spaces.is_empty(), "Should have spaces");

        // Cache should still be empty
        assert_eq!(
            client.cache().num_spaces(),
            0,
            "Cache should remain empty when disabled"
        );

        // Get operations should also not use or populate cache
        let first_property = properties.iter().next().unwrap();
        let _property = client
            .property(&space_id, &first_property.id)
            .get()
            .await
            .expect("Failed to get property");

        assert_eq!(
            client.cache().num_properties(),
            0,
            "Cache should remain empty after get with cache disabled"
        );
    }
}

// =============================================================================
// Cache Isolation Tests
// =============================================================================

mod cache_isolation {
    use anytype::test_util::*;
    use serial_test::serial;
    use test_log::test;

    /// Test that cache for space A doesn't affect space B
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_space_isolation() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches
            ctx.client.cache().clear();

            // Get all spaces to find a second space (if available)
            let spaces = ctx
                .client
                .spaces()
                .list()
                .await
                .expect("Failed to list spaces");

            if spaces.len() < 2 {
                println!(
                    "Skipping cache isolation test - need at least 2 spaces, found {}",
                    spaces.len()
                );
                return;
            }

            let space_a = &ctx.space_id;
            let space_b = spaces
                .iter()
                .find(|s| s.id != *space_a)
                .map(|s| &s.id)
                .expect("Failed to find second space");

            // Warm cache for space A
            ctx.client
                .properties(space_a)
                .list()
                .await
                .expect("Failed to list properties for space A");

            let cache_count_after_a = ctx.client.cache().num_properties();
            assert!(cache_count_after_a > 0, "Space A cache should be populated");

            // Warm cache for space B
            ctx.client
                .properties(space_b)
                .list()
                .await
                .expect("Failed to list properties for space B");

            let cache_count_after_b = ctx.client.cache().num_properties();
            assert!(
                cache_count_after_b >= cache_count_after_a,
                "Cache should include both spaces"
            );

            // Clear cache for space A only
            ctx.client.cache().clear_properties(Some(space_a));

            // Space B cache should still exist
            assert!(
                ctx.client.cache().has_properties(space_b),
                "space b property cache should not be empty"
            );

            // Space A cache should be gone
            //
            assert!(
                !ctx.client.cache().has_properties(space_a),
                "space a property cache should be empty"
            );

            // Cleanup
            ctx.client.cache().clear();
        })
        .await
    }

    /// Test cache isolation between different resource types
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_isolation_between_types() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches
            ctx.client.cache().clear();

            // Warm properties cache
            ctx.client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");

            assert!(ctx.client.cache().num_properties() > 0);
            assert_eq!(ctx.client.cache().num_types(), 0);
            assert_eq!(ctx.client.cache().num_spaces(), 0);

            // Warm types cache
            ctx.client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list types");

            assert!(ctx.client.cache().num_properties() > 0);
            assert!(ctx.client.cache().num_types() > 0);
            assert_eq!(ctx.client.cache().num_spaces(), 0);

            // Warm spaces cache
            let spaces = ctx
                .client
                .spaces()
                .list()
                .await
                .expect("Failed to list spaces");
            eprintln!("(2) Found {} spaces", spaces.len());

            assert!(ctx.client.cache().num_properties() > 0);
            assert!(ctx.client.cache().num_types() > 0);
            assert!(ctx.client.cache().num_spaces() > 0);

            // Clear only properties
            ctx.client.cache().clear_properties(None);

            assert_eq!(ctx.client.cache().num_properties(), 0);
            assert!(
                ctx.client.cache().num_types() > 0,
                "Types cache should be unaffected"
            );
            assert!(
                ctx.client.cache().num_spaces() > 0,
                "Spaces cache should be unaffected"
            );

            // Clear only types
            ctx.client.cache().clear_types(None);

            assert_eq!(ctx.client.cache().num_properties(), 0);
            assert_eq!(ctx.client.cache().num_types(), 0);
            assert!(
                ctx.client.cache().num_spaces() > 0,
                "Spaces cache should be unaffected"
            );

            // Cleanup
            ctx.client.cache().clear();
        })
        .await
    }
}

// =============================================================================
// Cache Query Behavior Tests
// =============================================================================

mod cache_query_behavior {
    use anytype::test_util::*;
    use serial_test::serial;
    use test_log::test;

    /// Test that cache introspection methods work correctly
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_introspection() {
        with_test_context_unit(|ctx| async move {
            // Clear all caches
            ctx.client.cache().clear();

            // Verify all counts are zero
            assert_eq!(ctx.client.cache().num_properties(), 0);
            assert_eq!(ctx.client.cache().num_types(), 0);
            assert_eq!(ctx.client.cache().num_spaces(), 0);

            // Warm properties cache
            ctx.client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");

            let prop_count = ctx.client.cache().num_properties();
            assert!(prop_count > 0);

            // Get properties for specific space

            assert!(
                ctx.client.cache().has_properties(&ctx.space_id),
                "Should have properties for test space"
            );

            // Get properties for non-existent space
            assert!(
                !ctx.client.cache().has_properties("non-existent space"),
                "Should not have properties for non-existent space"
            );

            // Warm types cache
            ctx.client
                .types(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list types");

            let type_count = ctx.client.cache().num_types();
            assert!(type_count > 0);

            // Cleanup
            ctx.client.cache().clear();
        })
        .await
    }

    /// Test that cache returns complete data structures
    #[test(tokio::test)]
    #[test_log::test]
    #[serial]
    async fn test_cache_returns_complete_data() {
        with_test_context_unit(|ctx| async move {
            // Clear cache
            ctx.client.cache().clear_properties(None);

            // Warm cache
            let properties_from_list = ctx
                .client
                .properties(&ctx.space_id)
                .list()
                .await
                .expect("Failed to list properties");

            let first_prop = properties_from_list.iter().next().unwrap();

            // Get from cache
            let property_from_cache = ctx
                .client
                .property(&ctx.space_id, &first_prop.id)
                .get()
                .await
                .expect("Failed to get property from cache");

            // Verify all fields match
            assert_eq!(property_from_cache.id, first_prop.id);
            assert_eq!(property_from_cache.key, first_prop.key);
            assert_eq!(property_from_cache.name, first_prop.name);
            assert_eq!(property_from_cache.format(), first_prop.format());

            // Cleanup
            ctx.client.cache().clear_properties(None);
        })
        .await
    }
}