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
//! Validation and Error Handling Tests for anytype
//!
//! This test suite validates comprehensive error handling and validation scenarios
//! against a live Anytype API server. Each test focuses on a single error condition.
//!
//! ## 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_validation
//! ```

mod common;

use anytype::{prelude::*, test_util::with_test_context_unit};
use common::unique_test_name;

// =============================================================================
// Invalid ID Errors
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_invalid_space_id() {
    with_test_context_unit(|ctx| async move {
        let invalid_id = "nonexistent_space_id_12345678901234567890";

        let result = ctx.client.space(invalid_id).get().await;

        // Should be NotFound or Validation error
        assert!(
            result.is_err(),
            "Expected error for invalid space ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } | AnytypeError::Validation { .. } => {
                println!("✓ Correctly returned error for invalid space ID");
            }
            e => panic!("Expected NotFound or Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_object_id() {
    with_test_context_unit(|ctx| async move {
        let invalid_id = "nonexistent_object_id_12345678901234567890";

        let result = ctx.client.object(ctx.space_id(), invalid_id).get().await;

        assert!(
            result.is_err(),
            "Expected error for invalid object ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } | AnytypeError::Validation { .. } => {
                println!("✓ Correctly returned error for invalid object ID");
            }
            e => panic!("Expected NotFound or Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_property_id() {
    with_test_context_unit(|ctx| async move {
        let invalid_id = "nonexistent_property_id_1234567890123456789";

        let result = ctx.client.property(ctx.space_id(), invalid_id).get().await;

        assert!(
            result.is_err(),
            "Expected error for invalid property ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } | AnytypeError::Validation { .. } => {
                println!("✓ Correctly returned error for invalid property ID");
            }
            e => panic!("Expected NotFound or Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_type_id() {
    with_test_context_unit(|ctx| async move {
        let invalid_id = "nonexistent_type_id_123456789012345678901";

        let result = ctx.client.get_type(ctx.space_id(), invalid_id).get().await;

        assert!(
            result.is_err(),
            "Expected error for invalid type ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } | AnytypeError::Validation { .. } => {
                println!("✓ Correctly returned error for invalid type ID");
            }
            e => panic!("Expected NotFound or Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_member_id() {
    with_test_context_unit(|ctx| async move {
        let invalid_id = "nonexistent_member_id_12345678901234567890";

        let result = ctx.client.member(ctx.space_id(), invalid_id).get().await;

        assert!(
            result.is_err(),
            "Expected error for invalid member ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } | AnytypeError::Validation { .. } => {
                println!("✓ Correctly returned error for invalid member ID");
            }
            e => panic!("Expected NotFound or Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_malformed_id_format() {
    with_test_context_unit(|ctx| async move {
        // Test IDs with invalid characters
        let test_cases = vec![
            ("empty", ""),
            ("too_short", "abc"),
            ("with_newline", "id_with\n_newline_12345678901234567"),
            ("with_null", "id_with\0_null_12345678901234567890"),
            ("with_tab", "id_with\t_tab_12345678901234567890"),
        ];

        for (name, invalid_id) in test_cases {
            let result = ctx.client.object(ctx.space_id(), invalid_id).get().await;

            assert!(
                result.is_err(),
                "Expected error for malformed ID '{}', got success",
                name
            );
            match result.unwrap_err() {
                AnytypeError::Validation { message } => {
                    println!("✓ Correctly rejected malformed ID '{}': {}", name, message);
                }
                e => {
                    // Some IDs might return NotFound if they pass validation but don't exist
                    println!(
                        "⚠ Malformed ID '{}' returned {:?} instead of Validation",
                        name, e
                    );
                }
            }
        }
    })
    .await
}

// =============================================================================
// Empty/Missing Required Fields
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_create_object_without_type() {
    with_test_context_unit(|ctx| async move {
        // This test verifies that the client-side validation catches missing type
        // by testing with an empty type_key, which should fail validation

        // The API requires type_key - we test with empty string
        let result = ctx
            .client
            .new_object(ctx.space_id(), "")
            .name("Test Object")
            .create()
            .await;

        assert!(
            result.is_err(),
            "Expected error for object creation without type, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                println!(
                    "✓ Correctly rejected object creation without type: {}",
                    message
                );
            }
            AnytypeError::Http { .. } => {
                println!("✓ Correctly rejected object creation without type (HTTP error)");
            }
            e => {
                // Server may return different error
                println!(
                    "⚠ Object creation without type returned {:?} instead of Validation",
                    e
                );
            }
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_create_property_without_name() {
    with_test_context_unit(|ctx| async move {
        // Create property with empty name should fail
        let result = ctx
            .client
            .new_property(ctx.space_id(), "", PropertyFormat::Text)
            .key("test_key")
            .create()
            .await;

        assert!(
            result.is_err(),
            "Expected error for property creation without name, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    message.contains("name") || message.contains("empty"),
                    "Error message should mention name or empty: {}",
                    message
                );
                println!(
                    "✓ Correctly rejected property creation without name: {}",
                    message
                );
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_update_without_changes() {
    with_test_context_unit(|ctx| async move {
        // First create an object
        let obj = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(unique_test_name("Update Test"))
            .create()
            .await
            .expect("Failed to create test object");
        ctx.register_object(&obj.id);

        // Try to update without setting any fields
        let result = ctx
            .client
            .update_object(ctx.space_id(), &obj.id)
            .update()
            .await;

        assert!(
            result.is_err(),
            "Expected error for update without changes, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    message.contains("at least one field") || message.contains("must set"),
                    "Error message should mention setting at least one field: {}",
                    message
                );
                println!("✓ Correctly rejected update without changes: {}", message);
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_create_space_without_name() {
    with_test_context_unit(|ctx| async move {
        // Create space with empty name may be rejected or allowed (server dependent)
        let result = ctx.client.new_space("").create().await;

        match result {
            Ok(space) => {
                if space.name.is_empty() {
                    println!("✓ Space creation without name allowed (empty name returned)");
                } else {
                    println!(
                        "⚠ Space creation without name returned non-empty name: {}",
                        space.name
                    );
                }
            }
            Err(AnytypeError::Validation { message }) => {
                assert!(
                    message.contains("name") || message.contains("empty"),
                    "Error message should mention name or empty: {}",
                    message
                );
                println!(
                    "✓ Correctly rejected space creation without name: {}",
                    message
                );
            }
            Err(e) => panic!("Expected Validation error or success, got: {:?}", e),
        }
    })
    .await
}

// =============================================================================
// Field Length/Format Validation
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_name_too_long() {
    with_test_context_unit(|ctx| async move {
        // Create a name longer than the validation limit (4096 bytes default)
        let long_name = "x".repeat(ctx.client.get_config().get_limits().name_max_len as usize + 10);

        let result = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(long_name)
            .create()
            .await;
        if let Ok(obj) = &result {
            ctx.register_object(&obj.id);
        }

        assert!(
            result.is_err(),
            "Expected error for name exceeding max length, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    message.contains("too long") || message.contains("max"),
                    "Error message should mention length limit: {}",
                    message
                );
                println!(
                    "✓ Correctly rejected name exceeding max length: {}",
                    message
                );
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_email_format() {
    with_test_context_unit(|ctx| async move {
        // First, we need to find or create an email property
        // For this test, we'll create an object and try to set an invalid email
        // The API might not validate email format client-side, so we expect server validation

        let obj = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(unique_test_name("Email Test"))
            .create()
            .await
            .expect("Failed to create test object");
        ctx.register_object(&obj.id);

        // Try to set an invalid email using set_email
        // Note: The API may or may not validate email format strictly
        let result = ctx
            .client
            .update_object(ctx.space_id(), &obj.id)
            .set_email("email", "not-a-valid-email")
            .update()
            .await;

        // Email validation might be lenient, so we just check if it errors or succeeds
        match result {
            Ok(_) => {
                println!("⚠ API accepted invalid email format (validation may be lenient)");
            }
            Err(AnytypeError::Validation { message }) => {
                println!("✓ Correctly rejected invalid email format: {}", message);
            }
            Err(e) => {
                println!("⚠ Invalid email returned {:?} instead of Validation", e);
            }
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_url_format() {
    with_test_context_unit(|ctx| async move {
        // Create an object and set an invalid URL
        let obj = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(unique_test_name("URL Test"))
            .create()
            .await
            .expect("Failed to create test object");
        ctx.register_object(&obj.id);

        // Try to set an invalid URL
        let result = ctx
            .client
            .update_object(ctx.space_id(), &obj.id)
            .set_url("url", "not a valid url with spaces")
            .update()
            .await;

        // URL validation might be lenient, so we check behavior
        match result {
            Ok(_) => {
                println!("⚠ API accepted invalid URL format (validation may be lenient)");
            }
            Err(AnytypeError::Validation { message }) => {
                println!("✓ Correctly rejected invalid URL format: {}", message);
            }
            Err(e) => {
                println!("⚠ Invalid URL returned {:?} instead of Validation", e);
            }
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_date_format() {
    with_test_context_unit(|ctx| async move {
        // Create an object and try to set an invalid date
        let obj = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(unique_test_name("Date Test"))
            .create()
            .await
            .expect("Failed to create test object");
        ctx.register_object(&obj.id);

        // Try to set an invalid date using set_date with a malformed string
        let result = ctx
            .client
            .update_object(ctx.space_id(), &obj.id)
            .set_date("created_date", "not-a-valid-date")
            .update()
            .await;

        // Date validation should catch invalid format
        match result {
            Ok(_) => {
                println!("⚠ API accepted invalid date format (validation may be lenient)");
            }
            Err(AnytypeError::Validation { message }) => {
                println!("✓ Correctly rejected invalid date format: {}", message);
            }
            Err(AnytypeError::Serialization { .. }) => {
                println!("✓ Correctly rejected invalid date format (Serialization error)");
            }
            Err(e) => {
                println!("⚠ Invalid date returned {:?} instead of Validation", e);
            }
        }
    })
    .await
}

// =============================================================================
// Auth Errors
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_request_without_api_key() {
    with_test_context_unit(|ctx| async move {
        // Create a new client without setting an API key
        let config = ClientConfig {
            base_url: ctx.client.get_config().base_url.clone(),
            app_name: "test-no-auth".to_string(),
            rate_limit_max_retries: 0,
            ..Default::default()
        };

        let unauth_client = AnytypeClient::with_config(config).expect("Failed to create client");

        // Try to make a request without authentication
        let result = unauth_client.spaces().list().await;

        assert!(
            result.is_err(),
            "Expected error for request without API key, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Auth { message } => {
                assert!(
                    message.contains("API key") || message.contains("not set"),
                    "Error message should mention API key: {}",
                    message
                );
                println!("✓ Correctly rejected request without API key: {}", message);
            }
            AnytypeError::Unauthorized => {
                println!("✓ Correctly rejected request without API key (Unauthorized)");
            }
            e => panic!("Expected Auth or Unauthorized error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_invalid_api_key() {
    with_test_context_unit(|ctx| async move {
        // Create a new client with an invalid API key
        let config = ClientConfig {
            base_url: ctx.client.get_config().base_url.clone(),
            app_name: "test-bad-auth".to_string(),
            rate_limit_max_retries: 0,
            ..Default::default()
        };

        let bad_client = AnytypeClient::with_config(config).expect("Failed to create client");
        bad_client.set_api_key(HttpCredentials::new("invalid_api_key_12345".to_string()));

        // Try to make a request with invalid key
        let result = bad_client.spaces().list().await;

        assert!(
            result.is_err(),
            "Expected error for invalid API key, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Unauthorized | AnytypeError::Forbidden => {
                println!("✓ Correctly rejected request with invalid API key");
            }
            AnytypeError::Auth { message } => {
                println!(
                    "✓ Correctly rejected request with invalid API key: {}",
                    message
                );
            }
            e => panic!("Expected Unauthorized/Forbidden/Auth error, got: {:?}", e),
        }
    })
    .await
}

// =============================================================================
// Permission Errors
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_delete_system_type() {
    with_test_context_unit(|ctx| async move {
        // Try to find a system type (like "page")
        let types = ctx
            .client
            .types(ctx.space_id())
            .list()
            .await
            .expect("Failed to list types");

        let page_type = types
            .into_response()
            .items
            .into_iter()
            .find(|t| t.key == "page")
            .expect("Could not find 'page' type");

        // Try to delete the system type
        let result = ctx
            .client
            .get_type(ctx.space_id(), &page_type.id)
            .delete()
            .await;

        // Deleting system types should fail
        match result {
            Ok(_) => {
                println!("⚠ API allowed deletion of system type (unexpected)");
            }
            Err(AnytypeError::Forbidden) => {
                println!("✓ Correctly rejected deletion of system type (Forbidden)");
            }
            Err(AnytypeError::Validation { message }) => {
                println!("✓ Correctly rejected deletion of system type: {}", message);
            }
            Err(e) => {
                println!(
                    "⚠ Deletion of system type returned {:?} (expected Forbidden/Validation)",
                    e
                );
            }
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_modify_readonly_property() {
    with_test_context_unit(|ctx| async move {


        // Find a system/readonly property
        let properties = ctx
            .client
            .properties(ctx.space_id())
            .list()
            .await
            .expect("Failed to list properties");

        // System properties like "id" or "type" are typically read-only
        let system_prop = properties
            .into_response()
            .items
            .into_iter()
            .find(|p| {
                matches!(
                    p.key.as_str(),
                    "id"
                        | "type"
                        | "created_date"
                        | "last_modified_date"
                        | "space_id"
                        | "creator"
                        | "last_modified_by"
                )
            });
        let system_prop = match system_prop {
            Some(prop) => prop,
            None => {
                println!("⚠ Could not find system property to test");
                return;
            }
        };

        // Try to update the system property
        let result = ctx
            .client
            .update_property(ctx.space_id(), &system_prop.id)
            .name("Modified System Property")
            .update()
            .await;

        // Modifying system properties should fail
        match result {
            Ok(_) => {
                println!("⚠ API allowed modification of system property (unexpected)");
            }
            Err(AnytypeError::Forbidden) => {
                println!("✓ Correctly rejected modification of system property (Forbidden)");
            }
            Err(AnytypeError::Validation { message }) => {
                println!(
                    "✓ Correctly rejected modification of system property: {}",
                    message
                );
            }
            Err(e) => {
                println!(
                    "⚠ Modification of system property returned {:?} (expected Forbidden/Validation)",
                    e
                );
            }
        }
    }).await
}

// =============================================================================
// Error Type Verification
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_validation_error_has_message() {
    with_test_context_unit(|ctx| async move {
        // Trigger a validation error (empty name)
        let result = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name("")
            .create()
            .await;

        assert!(result.is_err(), "Expected validation error for empty name");
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    !message.is_empty(),
                    "Validation error should include a message"
                );
                assert!(
                    message.len() > 10,
                    "Validation error message should be descriptive"
                );
                println!("✓ Validation error includes message: {}", message);
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_not_found_error() {
    with_test_context_unit(|ctx| async move {
        let nonexistent_id = "bafyreie6n5l5nkbjal37su54cha4coy7qzuhrnajluzv5qd5jvtsrxkequ";

        let result = ctx
            .client
            .object(ctx.space_id(), nonexistent_id)
            .get()
            .await;

        assert!(result.is_err(), "Expected NotFound error");
        match result.unwrap_err() {
            AnytypeError::NotFound { .. } => {
                println!("✓ Correctly returned NotFound error for missing resource");
            }
            e => {
                // Some APIs might return Validation for malformed IDs
                println!(
                    "⚠ Missing resource returned {:?} instead of NotFound (may be acceptable)",
                    e
                );
            }
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_auth_error() {
    // This is tested in test_request_without_api_key and test_invalid_api_key
    // Here we verify the error type is Auth/Unauthorized
    let config = ClientConfig {
        base_url: Some(
            std::env::var("ANYTYPE_TEST_URL")
                .unwrap_or_else(|_| "http://127.0.0.1:31012".to_string()),
        ),
        app_name: "test-auth-error".to_string(),
        rate_limit_max_retries: 0,
        ..Default::default()
    };

    let client = AnytypeClient::with_config(config).expect("Failed to create client");

    let result = client.spaces().list().await;

    assert!(result.is_err(), "Expected auth error");
    match result.unwrap_err() {
        AnytypeError::Auth { message } => {
            assert!(!message.is_empty(), "Auth error should include a message");
            println!("✓ Auth error includes message: {}", message);
        }
        AnytypeError::Unauthorized => {
            println!("✓ Correctly returned Unauthorized error");
        }
        e => panic!("Expected Auth or Unauthorized error, got: {:?}", e),
    }
}

// =============================================================================
// Additional Edge Cases
// =============================================================================

#[tokio::test]
#[test_log::test]
async fn test_body_too_long() {
    with_test_context_unit(|ctx| async move {
        // Create markdown content exceeding the limit (10 MiB default)
        // Create 11 MB of content
        let huge_body =
            "x".repeat(ctx.client.get_config().get_limits().markdown_max_len as usize + 1000);

        let result = ctx
            .client
            .new_object(ctx.space_id(), "page")
            .name(unique_test_name("Huge Body Test"))
            .body(huge_body)
            .create()
            .await;
        if let Ok(obj) = &result {
            ctx.register_object(&obj.id);
        }

        assert!(
            result.is_err(),
            "Expected error for body exceeding max length, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    message.contains("too long") || message.contains("max"),
                    "Error message should mention length limit: {}",
                    message
                );
                println!(
                    "✓ Correctly rejected body exceeding max length: {}",
                    message
                );
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_empty_space_id() {
    with_test_context_unit(|ctx| async move {
        let result = ctx
            .client
            .object("", "some_object_id_1234567890123456789")
            .get()
            .await;

        assert!(
            result.is_err(),
            "Expected error for empty space ID, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                assert!(
                    message.contains("space")
                        || message.contains("empty")
                        || message.contains("id"),
                    "Error message should mention space/empty/id: {}",
                    message
                );
                println!("✓ Correctly rejected empty space ID: {}", message);
            }
            e => panic!("Expected Validation error, got: {:?}", e),
        }
    })
    .await
}

#[tokio::test]
#[test_log::test]
async fn test_multiple_validation_errors() {
    with_test_context_unit(|ctx| async move {
        // Create an object with multiple validation issues
        let result = ctx
            .client
            .new_object(ctx.space_id(), "")
            .name("")
            .create()
            .await;

        assert!(
            result.is_err(),
            "Expected error for multiple validation issues, got success"
        );
        match result.unwrap_err() {
            AnytypeError::Validation { message } => {
                // Should report at least one validation error
                assert!(
                    !message.is_empty(),
                    "Validation error should include a message"
                );
                println!(
                    "✓ Correctly rejected request with multiple validation issues: {}",
                    message
                );
            }
            e => {
                // Other error types are also acceptable
                println!(
                    "⚠ Multiple validation issues returned {:?} instead of Validation",
                    e
                );
            }
        }
    })
    .await
}