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
//! Integration tests for the Types API
//!
//! Tests the complete lifecycle of Anytype object types including:
//! - Listing and pagination
//! - Type retrieval
//! - Type creation with properties
//! - System type identification
//! - Template association
//! - Error handling
//!
//! ## 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 test_types
//! ```

mod common;

use anytype::{
    prelude::*,
    test_util::{TestError, TestResult, unique_suffix, with_test_context},
};
use tracing::debug;

use crate::common::{create_object_with_retry, lookup_property_tag_with_retry};

// =============================================================================
// Type Listing Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_list() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let result = ctx.client.types(&ctx.space_id).list().await?;
        let types = &result.items;

        ctx.increment_calls(1);

        assert!(!types.is_empty(), "expected at least 1 type");

        // Verify each type has required fields
        for typ in types {
            assert!(!typ.id.is_empty(), "Type ID should not be empty");
            assert!(!typ.key.is_empty(), "Type key should not be empty");
        }

        println!("Listed {} types", types.len());
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_list_with_limit() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let limit = 3;
        let result = ctx.client.types(&ctx.space_id).limit(limit).list().await?;
        let types = &result.items;

        ctx.increment_calls(1);

        // Verify we got at most the requested limit
        assert!(
            types.len() <= limit as usize,
            "Expected at most {} types, got {}",
            limit,
            types.len()
        );

        println!("Listed {} types with limit {}", types.len(), limit);
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_list_with_offset() -> TestResult<()> {
    with_test_context(|ctx| async move {
        // Get all types first
        let all_types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        // Skip some types and get the rest
        let offset = 2;
        let result = ctx
            .client
            .types(&ctx.space_id)
            .offset(offset)
            .list()
            .await?;
        let offset_types = &result.items;

        ctx.increment_calls(2);

        // Verify offset worked if we have enough types
        if all_types.len() > offset as usize {
            assert!(
                !offset_types.is_empty(),
                "Expected types after offset {}",
                offset
            );

            if offset_types.is_empty() {
                return Err(TestError::Assertion {
                    message: format!("Expected types after offset {}", offset),
                });
            }
        }

        println!(
            "Listed {} types with offset {} (total: {})",
            offset_types.len(),
            offset,
            all_types.len()
        );
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_list_field_presence() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let result = ctx.client.types(&ctx.space_id).list().await?;
        let types = &result.items;

        ctx.increment_calls(1);

        assert!(!types.is_empty(), "Expected types to be present");

        // Verify all required fields are present on each type
        for typ in types {
            assert!(!typ.id.is_empty(), "Type.id is required");
            assert!(!typ.key.is_empty(), "Type.key is required");
            assert!(
                !typ.properties.is_empty(),
                "Type.properties should have at least one property"
            );

            // Layout should be set (has default)
            // Note: We can't directly test the value but verify it deserializes
        }

        println!("Verified field presence on {} types", types.len());
        Ok(())
    })
    .await
}

// =============================================================================
// Type Retrieval Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_get_by_id() -> TestResult<()> {
    with_test_context(|ctx| async move {
        // Get a type ID from the list
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        assert!(!types.is_empty(), "Need at least one type for this test");

        let first_type = &types[0];

        // Retrieve the specific type
        let retrieved = ctx
            .client
            .get_type(&ctx.space_id, &first_type.id)
            .get()
            .await?;

        ctx.increment_calls(2);

        // Verify it matches
        assert_eq!(retrieved.id, first_type.id);
        assert_eq!(retrieved.key, first_type.key);
        assert_eq!(retrieved.name, first_type.name);

        println!(
            "Successfully retrieved type: {} ({})",
            retrieved.key, retrieved.id
        );
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_get_consistency() -> TestResult<()> {
    with_test_context(|ctx| async move {
        // Get types via list
        let types_from_list = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        assert!(
            !types_from_list.is_empty(),
            "Need types for consistency test"
        );

        let sample_type = &types_from_list[0];

        // Get the same type via get
        let type_from_get = ctx
            .client
            .get_type(&ctx.space_id, &sample_type.id)
            .get()
            .await?;

        ctx.increment_calls(2);

        // Verify consistency
        assert_eq!(type_from_get.id, sample_type.id);
        assert_eq!(type_from_get.key, sample_type.key);
        assert_eq!(type_from_get.name, sample_type.name);
        assert_eq!(type_from_get.archived, sample_type.archived);
        assert_eq!(type_from_get.properties.len(), sample_type.properties.len());

        println!("Verified consistency for type: {}", sample_type.key);
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_get_nonexistent() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let fake_id = "nonexistent-type-12345";
        let result = ctx.client.get_type(&ctx.space_id, fake_id).get().await;

        ctx.increment_calls(1);

        // Should fail with NotFound error
        assert!(result.is_err(), "Expected error for nonexistent type");

        if let Err(e) = result {
            match e {
                AnytypeError::NotFound { obj_type, .. } if &obj_type == "Type" => {
                    println!("Correctly received NotFound error");
                }
                AnytypeError::Validation { .. } => {
                    println!("Correctly received Validation error for invalid type id");
                }
                _ => {
                    return Err(TestError::Assertion {
                        message: format!("Expected NotFound or Validation error, got: {:?}", e),
                    });
                }
            }
        }

        Ok(())
    })
    .await
}

// =============================================================================
// System Types Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_system_present() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        ctx.increment_calls(1);

        // Collect all type keys
        let type_keys: Vec<&str> = types.iter().map(|t| t.key.as_str()).collect();

        // Verify core system types are present
        let expected_system_types = ["page", "note", "task", "bookmark"];

        for expected_key in expected_system_types {
            assert!(
                type_keys.contains(&expected_key),
                "Expected system type '{}' to be present",
                expected_key
            );
        }

        println!(
            "Verified {} system types present",
            expected_system_types.len()
        );
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_system_properties() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        ctx.increment_calls(1);

        // Find a system type (e.g., "page")
        let page_type = types
            .iter()
            .find(|t| t.key == "page")
            .expect("Page type should exist");

        // System types should have properties
        assert!(
            !page_type.properties.is_empty(),
            "System type 'page' should have properties"
        );

        // Verify properties have required fields
        for prop in &page_type.properties {
            assert!(!prop.id.is_empty(), "Property id should not be empty");
            assert!(!prop.key.is_empty(), "Property key should not be empty");
            assert!(!prop.name.is_empty(), "Property name should not be empty");
        }

        println!(
            "System type 'page' has {} properties",
            page_type.properties.len()
        );
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_is_system_type() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        ctx.increment_calls(1);

        // Count system vs custom types
        let mut system_count = 0;
        let mut custom_count = 0;

        for typ in &types {
            if typ.is_system_type() {
                system_count += 1;
                // Verify it's actually a known system type
                assert!(
                    matches!(typ.key.as_str(), "page" | "note" | "task" | "bookmark"),
                    "is_system_type() returned true for non-system type: {}",
                    typ.key
                );
            } else {
                custom_count += 1;
            }
        }

        // We should have at least the 4 core system types
        assert!(
            system_count >= 4,
            "Expected at least 4 system types, found {}",
            system_count
        );

        println!(
            "Found {} system types and {} custom types",
            system_count, custom_count
        );
        Ok(())
    })
    .await
}

// =============================================================================
// Type Creation Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_create_simple() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_type_{}", unique_suffix());

        // Create a new type
        let created = ctx
            .client
            .new_type(&ctx.space_id, "Simple Type")
            .key(&unique_key)
            .create()
            .await?;
        ctx.register_type(&created.id);

        ctx.increment_calls(1);

        // Verify creation
        assert!(!created.id.is_empty(), "Created type should have an ID");
        assert_eq!(created.key, unique_key);
        assert_eq!(created.name.as_deref(), Some("Simple Type"));
        assert!(!created.is_system_type());

        debug!("Created custom type: {} ({})", created.key, created.id);
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_create_with_properties() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_type_props_{}", unique_suffix());

        // Create type with properties
        let created = ctx
            .client
            .new_type(&ctx.space_id, "Product Type")
            .key(&unique_key)
            .property("Status", "status", PropertyFormat::Select)
            .property("Priority", "priority", PropertyFormat::Number)
            .create()
            .await?;

        ctx.increment_calls(1);
        ctx.register_type(&created.id);

        // Verify properties were created
        // Note: The API may add default properties, so we check for at least our 2
        let custom_props: Vec<_> = created
            .properties
            .iter()
            .filter(|p| p.key == "status" || p.key == "priority")
            .collect();

        assert!(
            custom_props.len() >= 2,
            "Expected at least 2 custom properties, found {}",
            custom_props.len()
        );

        // Verify property details
        let status_prop = created
            .properties
            .iter()
            .find(|p| p.key == "status")
            .expect("Status property should exist");
        assert_eq!(status_prop.name, "Status");
        assert_eq!(status_prop.format(), PropertyFormat::Select);

        println!(
            "Created type with {} total properties",
            created.properties.len()
        );

        Ok(())
    })
    .await
}

/// create custom type, and an object of that type
/// custom type has properties: priority (number), status (select).
/// This also tests set_property and get_property with number and select fields.
#[tokio::test]
#[test_log::test]
async fn test_types_create_type_and_object() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique = unique_suffix();

        let type_name = format!("Type {unique}");
        let type_key = format!("type_{unique}");

        // Create type with properties
        let created = ctx
            .client
            .new_type(&ctx.space_id, &type_name)
            .key(&type_key)
            .property("Status", "status", PropertyFormat::Select)
            .property("Priority", "priority", PropertyFormat::Number)
            .create()
            .await?;

        ctx.increment_calls(1);
        ctx.register_type(&created.id);

        let object_name = format!("Type Object {unique}");
        let object_key = type_key.clone();

        // find tag id for "Done"
        let tag_done = lookup_property_tag_with_retry(ctx.as_ref(), "status", "Done").await?;

        // create object of custom type
        let obj = create_object_with_retry("Type Object", || async {
            ctx.client
                .new_object(&ctx.space_id, &object_key)
                .name(&object_name)
                .set_select("status", &tag_done.id)
                .set_number("priority", 2)
                .create()
                .await
        })
        .await?;
        ctx.increment_calls(1);
        ctx.register_object(&obj.id);

        assert_eq!(
            obj.get_property_number("priority"),
            Some(&serde_json::Number::from(2u64)),
            "get priority as Number"
        );
        assert_eq!(
            obj.get_property_u64("priority"),
            Some(2u64),
            "get priority as u64"
        );
        assert_eq!(
            obj.get_property_i64("priority"),
            Some(2i64),
            "get priority as i64"
        );
        assert_eq!(
            obj.get_property_f64("priority"),
            Some(2.0f64),
            "get priority as f64"
        );

        let status_value = obj
            .get_property_select("status")
            .expect("expected to find 'status' property");
        assert_eq!(status_value.name, "Done", "Done name match");
        assert_eq!(status_value.id, tag_done.id, "Done id match");

        println!("Created object with custom type");

        Ok(())
    })
    .await
}

// =============================================================================
// Type Template Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_list_templates() -> TestResult<()> {
    with_test_context(|ctx| async move {
        // Get a system type (they often have templates)
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        let page_type = types
            .iter()
            .find(|t| t.key == "page")
            .expect("Page type should exist");

        // List templates for this type
        let result = ctx
            .client
            .templates(&ctx.space_id, &page_type.id)
            .list()
            .await?;

        let templates = &result.items;

        ctx.increment_calls(2);

        // Templates may or may not exist, but the call should succeed
        println!("Type '{}' has {} templates", page_type.key, templates.len());

        // If templates exist, verify they're objects
        for template in templates {
            assert!(!template.id.is_empty(), "Template should have an ID");
        }

        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_get_template() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let types = ctx.client.types(&ctx.space_id).list().await?;

        for typ in types.iter().take(10) {
            let templates = ctx.client.templates(&ctx.space_id, &typ.id).list().await?;
            if let Some(template) = templates.iter().next() {
                let fetched = ctx
                    .client
                    .template(&ctx.space_id, &typ.id, &template.id)
                    .get()
                    .await?;
                assert_eq!(
                    fetched.id, template.id,
                    "template get should match listed template id"
                );
                return Ok(());
            }
        }

        println!("No templates found to test template get (this is OK)");
        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_template_type_linkage() -> TestResult<()> {
    with_test_context(|ctx| async move {
        // Get all types
        let types = ctx.client.types(&ctx.space_id).list().await?.items.clone();

        ctx.increment_calls(1);

        // Try to find a type with templates
        for typ in types.iter().take(3) {
            // Limit to first 3 to avoid too many API calls
            let result = ctx.client.templates(&ctx.space_id, &typ.id).list().await?;

            ctx.increment_calls(1);

            let templates = &result.items;

            if !templates.is_empty() {
                println!(
                    "Type '{}' has {} template(s), verifying linkage",
                    typ.key,
                    templates.len()
                );

                let mut matched = false;

                // Verify template has type reference
                for template in templates {
                    // Templates are objects and should have a type field
                    let template_type = template
                        .r#type
                        .as_ref()
                        .expect("Template should have a type");
                    assert!(
                        !template_type.id.is_empty(),
                        "Template type ID should be present"
                    );
                    if template_type.id == typ.id {
                        matched = true;
                    }
                }

                if matched {
                    return Ok(());
                }

                println!("âš  Templates returned, but none matched type '{}'", typ.key);
            }
        }

        println!("No types with templates found in first 3 types (this is OK)");
        Ok(())
    })
    .await
}

// =============================================================================
// Type Update and Delete Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_update_custom() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_update_{}", chrono::Utc::now().timestamp_millis());

        // Create a type
        let created = ctx
            .client
            .new_type(&ctx.space_id, "Original Name")
            .key(&unique_key)
            .create()
            .await?;
        ctx.register_type(&created.id);

        // Update it
        let updated = ctx
            .client
            .update_type(&ctx.space_id, &created.id)
            .name("Updated Name")
            .update()
            .await?;

        ctx.increment_calls(2);

        // Verify update
        assert_eq!(updated.id, created.id);
        assert_eq!(updated.name.as_deref(), Some("Updated Name"));

        println!("Successfully updated type name");

        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_delete_custom() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_delete_{}", chrono::Utc::now().timestamp_millis());

        // Create a type
        let created = ctx
            .client
            .new_type(&ctx.space_id, "To Be Deleted")
            .key(&unique_key)
            .create()
            .await?;

        // Delete it
        let deleted = ctx
            .client
            .get_type(&ctx.space_id, &created.id)
            .delete()
            .await?;

        ctx.increment_calls(2);

        // Verify deletion (delete actually archives)
        assert_eq!(deleted.id, created.id);
        if !deleted.archived {
            println!("Delete returned archived=false");
        }

        println!("Successfully deleted (archived) type");

        Ok(())
    })
    .await
}

// =============================================================================
// Advanced Type Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_with_icon() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_icon_{}", chrono::Utc::now().timestamp_millis());

        // Create type with emoji icon
        let created = ctx
            .client
            .new_type(&ctx.space_id, "Type With Icon")
            .key(&unique_key)
            .icon(Icon::Emoji {
                emoji: "📋".to_string(),
            })
            .create()
            .await?;

        ctx.increment_calls(1);
        ctx.register_type(&created.id);

        // Verify icon
        assert!(created.icon.is_some(), "Type should have an icon");
        if let Some(Icon::Emoji { emoji }) = &created.icon {
            assert_eq!(emoji, "📋");
        } else {
            return Err(TestError::Assertion {
                message: "Expected Emoji icon variant".to_string(),
            });
        }

        println!("Created type with icon: {}", created.display_name());

        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_display_name() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_display_{}", chrono::Utc::now().timestamp_millis());

        // Create type with name
        let with_name = ctx
            .client
            .new_type(&ctx.space_id, "Display Name Test")
            .key(&unique_key)
            .create()
            .await?;

        ctx.increment_calls(1);
        ctx.register_type(&with_name.id);

        // display_name() should return the name
        assert_eq!(with_name.display_name(), "Display Name Test");

        println!("Type display_name() works correctly");

        Ok(())
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_types_layouts() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let layouts = &[
            TypeLayout::Basic,
            TypeLayout::Note,
            TypeLayout::Action,
            TypeLayout::Profile,
        ];

        for (idx, layout) in layouts.iter().enumerate() {
            let unique_key = format!(
                "test_layout_{}_{}",
                idx,
                chrono::Utc::now().timestamp_millis()
            );

            let created = ctx
                .client
                .new_type(&ctx.space_id, format!("Layout Test {:?}", layout))
                .key(&unique_key)
                .layout(layout.clone())
                .create()
                .await?;

            ctx.increment_calls(1);
            ctx.register_type(&created.id);
            println!("Created type with layout: {:?}", layout);
        }

        println!("Successfully created types with all layout variants");

        Ok(())
    })
    .await
}

// =============================================================================
// Error Handling Tests
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_types_duplicate_key() -> TestResult<()> {
    with_test_context(|ctx| async move {
        let unique_key = format!("test_dup_{}", chrono::Utc::now().timestamp_millis());

        // Create first type
        let first = ctx
            .client
            .new_type(&ctx.space_id, "First Type")
            .key(&unique_key)
            .create()
            .await?;
        ctx.register_type(&first.id);

        // Try to create second type with same key
        let result = ctx
            .client
            .new_type(&ctx.space_id, "Second Type")
            .key(&unique_key)
            .create()
            .await;

        ctx.increment_calls(2);

        // May succeed or fail depending on API validation
        // If it succeeds, clean up the second type too
        if let Ok(second) = result {
            ctx.register_type(&second.id);
            println!("API allows duplicate type keys (types have different IDs)");
        } else {
            println!("API correctly rejected duplicate type key");
        }

        Ok(())
    })
    .await
}