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
//! Smoke Test for anytype
//!
//! This smoke test validates basic API functionality against a live Anytype server.
//! It covers approximately 50-100 API calls testing spaces, types, properties,
//! objects, search, and property formats.
//!
//! ## 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
//!
//! ```bash
//! source .test-env
//! cargo test -p anytype --test smoke_test
//! ```

use std::{collections::HashSet, time::Duration};

use anytype::{prelude::*, test_util::*};

// =============================================================================
// Test Configuration
// =============================================================================

const TEST_TIMEOUT_SECS: u64 = 120;
const ESTIMATED_RUNTIME_SECS: u64 = 60;

// =============================================================================
// Main Smoke Test
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn smoke_test() {
    println!("\n========================================");
    println!("  Anytype API Smoke Test");
    println!("========================================");
    println!("Estimated runtime: ~{} seconds", ESTIMATED_RUNTIME_SECS);
    println!();

    // Setup
    let ctx = match TestContext::new().await {
        Ok(ctx) => ctx,
        Err(e) => {
            eprintln!("SMOKE TEST SETUP FAILED: {}", e);
            eprintln!("\nPlease ensure:");
            eprintln!("  1. Anytype server is running");
            eprintln!("  2. Environment variables are set (source .test-env)");
            eprintln!("  3. API key file exists and is valid");
            panic!("Setup failed: {}", e);
        }
    };

    println!("Configuration:");
    println!("  URL: {}", ctx.client.get_http_endpoint());
    println!("  Space ID: {}", ctx.space_id);
    println!("  Keystore: {:?}", ctx.client.get_key_store());
    println!();

    // Create timeout guard
    let timeout = Duration::from_secs(TEST_TIMEOUT_SECS);
    let result = tokio::time::timeout(timeout, run_smoke_tests(&ctx)).await;

    match result {
        Ok(test_results) => {
            let metrics = ctx.client.http_metrics();
            println!("\n========================================");
            println!("  Smoke Test Results");
            println!("========================================");
            println!("API Calls: ~{}", ctx.call_count());
            println!("Duration: {} seconds", ctx.elapsed_secs());
            println!("{}", test_results.summary());
            println!();
            println!("HTTP Metrics:");
            println!("  {}", metrics);

            if !test_results.is_success() {
                println!("\nFailed tests:");
                for (name, error) in test_results.failures() {
                    println!("  - {}: {}", name, error);
                }
            }

            assert!(
                test_results.is_success(),
                "Smoke test failed: {}",
                test_results.summary()
            );
        }
        Err(_) => {
            panic!(
                "Smoke test timed out after {} seconds. This may indicate a hang or deadlock.",
                TEST_TIMEOUT_SECS
            );
        }
    }
}

async fn run_smoke_tests(ctx: &TestContext) -> TestResults {
    let mut results = TestResults::default();

    // Public API contracts
    println!("Testing: Spaces API");
    test_spaces_api(ctx, &mut results).await;

    println!("\nTesting: Types API");
    test_types_api(ctx, &mut results).await;

    println!("\nTesting: Properties API");
    test_properties_api(ctx, &mut results).await;

    println!("\nTesting: Objects API (list)");
    test_objects_list_api(ctx, &mut results).await;

    println!("\nTesting: Members API");
    test_members_api(ctx, &mut results).await;

    // Property format coverage
    println!("\nTesting: Property Formats");
    test_property_formats(ctx, &mut results).await;

    // Object CRUD
    println!("\nTesting: Object CRUD Operations");
    test_object_crud(ctx, &mut results).await;

    // Search tests
    println!("\nTesting: Search API");
    test_search_api(ctx, &mut results).await;

    // Filter tests
    println!("\nTesting: Filter Operations");
    test_filters(ctx, &mut results).await;

    results
}

// =============================================================================
// Public API Contracts
// =============================================================================

async fn test_spaces_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: List spaces
    match ctx.client.spaces().list().await {
        Ok(spaces) => {
            ctx.increment_calls(1);
            if spaces.is_empty() {
                results.fail("spaces.list", "No spaces returned");
            } else {
                results.pass(&format!("spaces.list ({} spaces)", spaces.len()));

                // Verify space fields
                let first = spaces.iter().next().unwrap();
                if first.id.is_empty() {
                    results.fail("spaces.list.fields", "Missing required id field");
                } else if first.name.is_empty() {
                    println!("spaces.list.fields: empty name returned (allowed)");
                    results.pass("spaces.list.fields");
                } else {
                    results.pass("spaces.list.fields");
                }
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("spaces.list", &e.to_string());
        }
    }

    // Test: Get specific space
    match ctx.client.space(&ctx.space_id).get().await {
        Ok(space) => {
            ctx.increment_calls(1);
            if space.id == ctx.space_id {
                results.pass("space.get");
            } else {
                results.fail("space.get", "Space ID mismatch");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("space.get", &e.to_string());
        }
    }
}

async fn test_types_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: List types
    let types_result = ctx.client.types(&ctx.space_id).list().await;
    match types_result {
        Ok(types) => {
            ctx.increment_calls(1);
            if types.is_empty() {
                results.fail("types.list", "No types returned");
                return;
            }
            results.pass(&format!("types.list ({} types)", types.len()));

            // Verify type fields
            let first = types.iter().next().unwrap();
            if first.id.is_empty() || first.key.is_empty() {
                results.fail("types.list.fields", "Missing required fields (id, key)");
            } else {
                results.pass("types.list.fields");
            }

            // Test: Get specific type
            match ctx.client.get_type(&ctx.space_id, &first.id).get().await {
                Ok(typ) => {
                    ctx.increment_calls(1);
                    if typ.id == first.id && typ.key == first.key {
                        results.pass("type.get");
                    } else {
                        results.fail("type.get", "Type data mismatch");
                    }
                }
                Err(e) => {
                    ctx.increment_calls(1);
                    results.fail("type.get", &e.to_string());
                }
            }

            // Check for common system types
            let type_keys: HashSet<_> = types.iter().map(|t| t.key.as_str()).collect();
            if type_keys.contains("page") {
                results.pass("types.contains_page");
            } else {
                results.fail("types.contains_page", "Missing 'page' type");
            }
            if type_keys.contains("task") {
                results.pass("types.contains_task");
            } else {
                results.fail("types.contains_task", "Missing 'task' type");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("types.list", &e.to_string());
        }
    }

    // lookup type by key
    match ctx.client.lookup_type_by_key(&ctx.space_id, "page").await {
        Err(e) => results.fail("types.lookup_page", &e.to_string()),
        Ok(_) => results.pass("types.lookup_page"),
    }
}

async fn test_properties_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: List properties
    let props_result = ctx.client.properties(&ctx.space_id).list().await;
    match props_result {
        Ok(properties) => {
            ctx.increment_calls(1);
            if properties.is_empty() {
                results.fail("properties.list", "No properties returned");
                return;
            }
            results.pass(&format!(
                "properties.list ({} properties)",
                properties.len()
            ));

            // Verify property fields
            let first = properties.iter().next().unwrap();
            if first.id.is_empty() || first.key.is_empty() {
                results.fail(
                    "properties.list.fields",
                    "Missing required fields (id, key)",
                );
            } else {
                results.pass("properties.list.fields");
            }

            // Test: Get specific property
            match ctx.client.property(&ctx.space_id, &first.id).get().await {
                Ok(prop) => {
                    ctx.increment_calls(1);
                    if prop.id == first.id && prop.key == first.key {
                        results.pass("property.get");
                    } else {
                        results.fail("property.get", "Property data mismatch");
                    }
                }
                Err(e) => {
                    ctx.increment_calls(1);
                    results.fail("property.get", &e.to_string());
                }
            }

            // Check for common system properties
            let prop_keys: HashSet<_> = properties.iter().map(|p| p.key.as_str()).collect();
            let has_name = prop_keys.contains("name");
            let has_description = prop_keys.contains("description");
            if has_name || has_description {
                results.pass("properties.system_props");
            } else {
                results.fail(
                    "properties.system_props",
                    "Missing common system properties",
                );
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("properties.list", &e.to_string());
        }
    }

    // lookup property by key
    let property = ctx
        .client
        .lookup_property_by_key(&ctx.space_id, "done")
        .await
        .expect("lookup_property_by_key");

    match property.format() {
        PropertyFormat::Checkbox => {
            results.pass("properties.done_format");
        }
        fmt => {
            results.fail(
                "properties.done_format",
                &format!("done format is {fmt}, expected checkbox"),
            );
        }
    }
}

async fn test_objects_list_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: List objects with limit
    match ctx.client.objects(&ctx.space_id).limit(10).list().await {
        Ok(objects) => {
            ctx.increment_calls(1);
            results.pass(&format!("objects.list ({} objects)", objects.len()));

            if !objects.is_empty() {
                let first = objects.iter().next().unwrap();
                // Verify required fields
                if first.id.is_empty() || first.space_id.is_empty() {
                    results.fail("objects.list.fields", "Missing required fields");
                } else {
                    results.pass("objects.list.fields");
                }

                // Test: Get specific object
                match ctx.client.object(&ctx.space_id, &first.id).get().await {
                    Ok(obj) => {
                        ctx.increment_calls(1);
                        if obj.id == first.id {
                            results.pass("object.get");
                        } else {
                            results.fail("object.get", "Object ID mismatch");
                        }
                    }
                    Err(e) => {
                        ctx.increment_calls(1);
                        results.fail("object.get", &e.to_string());
                    }
                }
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("objects.list", &e.to_string());
        }
    }
}

async fn test_members_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: List members
    match ctx.client.members(&ctx.space_id).list().await {
        Ok(members) => {
            ctx.increment_calls(1);
            if members.is_empty() {
                results.fail(
                    "members.list",
                    "No members returned (expected at least owner)",
                );
            } else {
                results.pass(&format!("members.list ({} members)", members.len()));

                // Verify member fields
                let first = members.iter().next().unwrap();
                if first.id.is_empty() {
                    results.fail("members.list.fields", "Missing member ID");
                } else {
                    results.pass("members.list.fields");
                }
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("members.list", &e.to_string());
        }
    }
}

// =============================================================================
// Property Format Coverage
// =============================================================================

async fn test_property_formats(ctx: &TestContext, results: &mut TestResults) {
    // List all properties and check format coverage
    match ctx.client.properties(&ctx.space_id).list().await {
        Ok(properties) => {
            ctx.increment_calls(1);

            // Collect unique formats using Vec (PropertyFormat doesn't impl Hash)
            let mut found_formats: Vec<PropertyFormat> = Vec::new();
            for prop in properties.iter() {
                if !found_formats.iter().any(|f| *f == prop.format()) {
                    found_formats.push(prop.format());
                }
            }

            // Check which formats are present
            let all_formats = [
                PropertyFormat::Text,
                PropertyFormat::Number,
                PropertyFormat::Select,
                PropertyFormat::MultiSelect,
                PropertyFormat::Date,
                PropertyFormat::Checkbox,
                PropertyFormat::Url,
                PropertyFormat::Email,
                PropertyFormat::Phone,
                PropertyFormat::Objects,
                PropertyFormat::Files,
            ];

            let mut found_count = 0;
            for format in &all_formats {
                if found_formats.iter().any(|f| f == format) {
                    found_count += 1;
                }
            }

            // Report coverage
            results.pass(&format!(
                "property_formats ({}/{} formats found)",
                found_count,
                all_formats.len()
            ));

            // Text format should always be present
            if found_formats.contains(&PropertyFormat::Text) {
                results.pass("property_format.text");
            } else {
                results.fail("property_format.text", "Text format not found");
            }

            // Select format commonly present
            if found_formats.contains(&PropertyFormat::Select) {
                results.pass("property_format.select");
            }

            // Date format commonly present
            if found_formats.contains(&PropertyFormat::Date) {
                results.pass("property_format.date");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("property_formats", &e.to_string());
        }
    }
}

// =============================================================================
// Object CRUD
// =============================================================================

async fn test_object_crud(ctx: &TestContext, results: &mut TestResults) {
    let test_name = format!("Smoke Test Object {}", chrono::Utc::now().timestamp());

    // CREATE
    let create_result = ctx
        .client
        .new_object(&ctx.space_id, "page")
        .name(&test_name)
        .body("# Smoke Test\n\nThis is a test object created by the smoke test.")
        .description("Created by anytype smoke test")
        .create()
        .await;

    let created_obj = match create_result {
        Ok(obj) => {
            ctx.increment_calls(1);
            if obj.name.as_deref() == Some(&test_name) {
                results.pass("object.create");
                obj
            } else {
                results.fail("object.create", "Name mismatch after creation");
                return;
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("object.create", &e.to_string());
            return;
        }
    };

    let object_id = &created_obj.id;
    ctx.register_object(object_id);

    // READ (verify creation)
    match ctx.client.object(&ctx.space_id, object_id).get().await {
        Ok(obj) => {
            ctx.increment_calls(1);
            if &obj.id == object_id && obj.name.as_deref() == Some(&test_name) {
                results.pass("object.read_after_create");
            } else {
                results.fail("object.read_after_create", "Data mismatch");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("object.read_after_create", &e.to_string());
        }
    }

    // UPDATE
    let updated_name = format!("{} (Updated)", test_name);
    match ctx
        .client
        .update_object(&ctx.space_id, object_id)
        .name(&updated_name)
        .body("# Updated Smoke Test\n\nThis object has been updated.")
        .update()
        .await
    {
        Ok(obj) => {
            ctx.increment_calls(1);
            if obj.name.as_deref() == Some(&updated_name) {
                results.pass("object.update");
            } else {
                results.fail("object.update", "Name not updated");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("object.update", &e.to_string());
        }
    }

    // READ (verify update)
    match ctx.client.object(&ctx.space_id, object_id).get().await {
        Ok(obj) => {
            ctx.increment_calls(1);
            if obj.name.as_deref() == Some(&updated_name) {
                results.pass("object.read_after_update");
            } else {
                results.fail("object.read_after_update", "Update not persisted");
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("object.read_after_update", &e.to_string());
        }
    }

    // DELETE (archive)
    match ctx.client.object(&ctx.space_id, object_id).delete().await {
        Ok(obj) => {
            ctx.increment_calls(1);
            // The API may or may not immediately reflect archived status
            // Success means the delete call worked
            results.pass(&format!("object.delete (archived={})", obj.archived));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("object.delete", &e.to_string());
        }
    }

    // Verify deletion - object should be archived or not found
    match ctx.client.object(&ctx.space_id, object_id).get().await {
        Ok(obj) => {
            ctx.increment_calls(1);
            // Object exists - check if it's archived or accept it either way
            // since the primary test is that delete() succeeded
            results.pass(&format!(
                "object.read_after_delete (archived={})",
                obj.archived
            ));
        }
        Err(e) => {
            ctx.increment_calls(1);
            // NotFound is acceptable - object may be fully deleted
            if matches!(e, AnytypeError::NotFound { .. }) {
                results.pass("object.read_after_delete (not_found)");
            } else {
                results.fail("object.read_after_delete", &e.to_string());
            }
        }
    }
}

// =============================================================================
// Search API Tests
// =============================================================================

async fn test_search_api(ctx: &TestContext, results: &mut TestResults) {
    // Test: Global search
    match ctx.client.search_global().limit(5).execute().await {
        Ok(search_results) => {
            ctx.increment_calls(1);
            results.pass(&format!("search.global ({} results)", search_results.len()));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("search.global", &e.to_string());
        }
    }

    // Test: Search in space
    match ctx.client.search_in(&ctx.space_id).limit(5).execute().await {
        Ok(search_results) => {
            ctx.increment_calls(1);
            results.pass(&format!(
                "search.in_space ({} results)",
                search_results.len()
            ));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("search.in_space", &e.to_string());
        }
    }

    // Test: Search with text query
    match ctx
        .client
        .search_in(&ctx.space_id)
        .text("test")
        .limit(5)
        .execute()
        .await
    {
        Ok(search_results) => {
            ctx.increment_calls(1);
            results.pass(&format!(
                "search.with_text ({} results)",
                search_results.len()
            ));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("search.with_text", &e.to_string());
        }
    }

    // Test: Search with type filter
    match ctx
        .client
        .search_in(&ctx.space_id)
        .types(["page"])
        .limit(5)
        .execute()
        .await
    {
        Ok(search_results) => {
            ctx.increment_calls(1);
            results.pass(&format!(
                "search.with_types ({} results)",
                search_results.len()
            ));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("search.with_types", &e.to_string());
        }
    }
}

// =============================================================================
// Filter Tests
// =============================================================================

async fn test_filters(ctx: &TestContext, results: &mut TestResults) {
    // Test: Not empty filter (name exists)
    match ctx
        .client
        .objects(&ctx.space_id)
        .filter(Filter::not_empty("name"))
        .limit(5)
        .list()
        .await
    {
        Ok(objects) => {
            ctx.increment_calls(1);
            results.pass(&format!("filter.not_empty ({} results)", objects.len()));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("filter.not_empty", &e.to_string());
        }
    }

    // Test: Empty filter (find objects with empty description)
    match ctx
        .client
        .objects(&ctx.space_id)
        .filter(Filter::is_empty("description"))
        .limit(5)
        .list()
        .await
    {
        Ok(objects) => {
            ctx.increment_calls(1);
            results.pass(&format!("filter.is_empty ({} results)", objects.len()));
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("filter.is_empty", &e.to_string());
        }
    }

    // Test: Pagination with offset
    match ctx
        .client
        .objects(&ctx.space_id)
        .limit(3)
        .offset(0)
        .list()
        .await
    {
        Ok(first_page) => {
            ctx.increment_calls(1);
            if first_page.len() <= 3 {
                results.pass("pagination.limit");

                // Get second page
                match ctx
                    .client
                    .objects(&ctx.space_id)
                    .limit(3)
                    .offset(3)
                    .list()
                    .await
                {
                    Ok(second_page) => {
                        ctx.increment_calls(1);
                        // Just verify we can paginate
                        results.pass(&format!(
                            "pagination.offset (page2: {} items)",
                            second_page.len()
                        ));
                    }
                    Err(e) => {
                        ctx.increment_calls(1);
                        results.fail("pagination.offset", &e.to_string());
                    }
                }
            } else {
                results.fail(
                    "pagination.limit",
                    &format!("Expected <= 3 items, got {}", first_page.len()),
                );
            }
        }
        Err(e) => {
            ctx.increment_calls(1);
            results.fail("pagination.limit", &e.to_string());
        }
    }
}