atlassian-cli 0.4.3

Unified CLI for Atlassian Cloud products
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
use atlassian_cli_api::ApiClient;
use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

// ============================================================================
// Space Operations Tests
// ============================================================================

#[tokio::test]
async fn test_list_spaces() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/spaces"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "id": "123456",
                    "key": "DOCS",
                    "name": "Documentation",
                    "type": "global",
                    "status": "current"
                },
                {
                    "id": "789012",
                    "key": "TEAM",
                    "name": "Team Space",
                    "type": "global",
                    "status": "current"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/spaces").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["results"].as_array().unwrap().len(), 2);
}

#[tokio::test]
async fn test_get_space() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/spaces"))
        .and(query_param("keys", "DOCS"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [{
                "id": "123456",
                "key": "DOCS",
                "name": "Documentation",
                "type": "global",
                "status": "current",
                "description": {
                    "plain": {
                        "value": "Documentation space"
                    }
                }
            }]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/spaces?keys=DOCS").await;

    assert!(response.is_ok());
}

#[tokio::test]
async fn test_create_space() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/wiki/api/v2/spaces"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "999888",
            "key": "NEW",
            "name": "New Space",
            "type": "global",
            "status": "current"
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let payload = serde_json::json!({
        "key": "NEW",
        "name": "New Space",
        "description": {"plain": {"value": "A new space"}}
    });

    let response: Result<serde_json::Value, _> = client.post("/wiki/api/v2/spaces", &payload).await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["key"], "NEW");
}

// ============================================================================
// Page Operations Tests
// ============================================================================

#[tokio::test]
async fn test_list_pages() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages"))
        .and(query_param("space-key", "DOCS"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "id": "100001",
                    "title": "Getting Started",
                    "type": "page",
                    "status": "current"
                },
                {
                    "id": "100002",
                    "title": "API Reference",
                    "type": "page",
                    "status": "current"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> =
        client.get("/wiki/api/v2/pages?space-key=DOCS").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["results"].as_array().unwrap().len(), 2);
}

#[tokio::test]
async fn test_get_page() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Getting Started",
            "type": "page",
            "status": "current",
            "version": {
                "number": 3,
                "message": "Updated content"
            },
            "body": {
                "storage": {
                    "value": "<p>Welcome to the documentation</p>",
                    "representation": "storage"
                }
            }
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/pages/100001").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["title"], "Getting Started");
    assert_eq!(data["version"]["number"], 3);
}

#[tokio::test]
async fn test_create_page() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/wiki/api/v2/pages"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "200001",
            "title": "New Page",
            "type": "page",
            "status": "current"
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let payload = serde_json::json!({
        "spaceId": "123456",
        "title": "New Page",
        "status": "current",
        "body": {
            "representation": "storage",
            "value": "<p>Page content</p>"
        }
    });

    let response: Result<serde_json::Value, _> = client.post("/wiki/api/v2/pages", &payload).await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["id"], "200001");
}

#[tokio::test]
async fn test_update_page() {
    let mock_server = MockServer::start().await;

    // First mock: get current page version
    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Original Title",
            "version": {"number": 2}
        })))
        .mount(&mock_server)
        .await;

    // Second mock: update page
    Mock::given(method("PUT"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Updated Title",
            "version": {"number": 3}
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let payload = serde_json::json!({
        "id": "100001",
        "title": "Updated Title",
        "status": "current",
        "version": {"number": 3}
    });

    let response: Result<serde_json::Value, _> =
        client.put("/wiki/api/v2/pages/100001", &payload).await;

    assert!(response.is_ok());
}

// ============================================================================
// Blog Operations Tests
// ============================================================================

#[tokio::test]
async fn test_list_blogposts() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/blogposts"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "id": "300001",
                    "title": "Weekly Update",
                    "status": "current"
                },
                {
                    "id": "300002",
                    "title": "Release Notes",
                    "status": "current"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/blogposts").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["results"].as_array().unwrap().len(), 2);
}

#[tokio::test]
async fn test_create_blogpost() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/wiki/api/v2/blogposts"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "300003",
            "title": "New Blog Post",
            "status": "current"
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let payload = serde_json::json!({
        "spaceId": "123456",
        "title": "New Blog Post",
        "status": "current",
        "type": "blogpost",
        "body": {
            "representation": "storage",
            "value": "<p>Blog content</p>"
        }
    });

    let response: Result<serde_json::Value, _> =
        client.post("/wiki/api/v2/blogposts", &payload).await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["id"], "300003");
}

// ============================================================================
// Attachment Operations Tests
// ============================================================================

#[tokio::test]
async fn test_list_attachments() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001/attachments"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "id": "att-001",
                    "title": "diagram.png",
                    "fileSize": 102400,
                    "mediaType": "image/png"
                },
                {
                    "id": "att-002",
                    "title": "report.pdf",
                    "fileSize": 524288,
                    "mediaType": "application/pdf"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> =
        client.get("/wiki/api/v2/pages/100001/attachments").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["results"].as_array().unwrap().len(), 2);
}

#[tokio::test]
async fn test_get_attachment() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/attachments/att-001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "att-001",
            "title": "diagram.png",
            "fileSize": 102400,
            "mediaType": "image/png",
            // The real v2 API returns downloadLink relative to the /wiki context,
            // i.e. WITHOUT a /wiki prefix. The old fixture invented one, which is
            // why no test caught the dropped-/wiki download bug.
            "downloadLink": "/download/attachments/100001/diagram.png?version=1&api=v2"
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> =
        client.get("/wiki/api/v2/attachments/att-001").await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["id"], "att-001");
    assert_eq!(data["fileSize"], 102400);
}

// ============================================================================
// Search Operations Tests
// ============================================================================

#[tokio::test]
async fn test_search_cql() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/rest/api/content/search"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "content": {
                        "id": "100001",
                        "type": "page",
                        "title": "Search Result 1"
                    }
                },
                {
                    "content": {
                        "id": "100002",
                        "type": "page",
                        "title": "Search Result 2"
                    }
                }
            ],
            "size": 2
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client
        .get("/wiki/rest/api/content/search?cql=type%3Dpage")
        .await;

    assert!(response.is_ok());
    let data = response.unwrap();
    assert_eq!(data["size"], 2);
}

#[tokio::test]
async fn test_search_text() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/rest/api/search"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "content": {
                        "id": "100003",
                        "title": "Documentation Page"
                    }
                }
            ],
            "size": 1
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client
        .get("/wiki/rest/api/search?cql=text~%22documentation%22")
        .await;

    assert!(response.is_ok());
}

// ============================================================================
// Bulk Operations Tests
// ============================================================================

#[tokio::test]
async fn test_bulk_export_json() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/rest/api/content/search"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "content": {
                        "id": "100001"
                    }
                },
                {
                    "content": {
                        "id": "100002"
                    }
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Page 1",
            "body": {"storage": {"value": "<p>Content 1</p>"}}
        })))
        .mount(&mock_server)
        .await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100002"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100002",
            "title": "Page 2",
            "body": {"storage": {"value": "<p>Content 2</p>"}}
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    // Test search to get page IDs
    let response: Result<serde_json::Value, _> = client
        .get("/wiki/rest/api/content/search?cql=type%3Dpage")
        .await;

    assert!(response.is_ok());
}

#[tokio::test]
async fn test_bulk_add_labels() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/wiki/rest/api/content/100001/label"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {"prefix": "global", "name": "archived"}
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let payload = serde_json::json!([{
        "prefix": "global",
        "name": "archived"
    }]);

    let response: Result<serde_json::Value, _> = client
        .post("/wiki/rest/api/content/100001/label", &payload)
        .await;

    assert!(response.is_ok());
}

// ============================================================================
// Draft Publishing Tests
// ============================================================================

#[tokio::test]
async fn test_publish_draft_page() {
    let mock_server = MockServer::start().await;

    // Mock: get current page (draft status)
    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Draft Page",
            "status": "draft",
            "version": {"number": 1}
        })))
        .mount(&mock_server)
        .await;

    // Mock: publish page (PUT with version 1)
    Mock::given(method("PUT"))
        .and(path("/wiki/api/v2/pages/100001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100001",
            "title": "Draft Page",
            "status": "current",
            "version": {"number": 1, "message": "Published via CLI"}
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    // Verify we can get the draft page
    let get_response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/pages/100001").await;
    assert!(get_response.is_ok());
    let data = get_response.unwrap();
    assert_eq!(data["status"], "draft");

    // Publish the page with version 1 (not incremented)
    let publish_payload = serde_json::json!({
        "id": "100001",
        "status": "current",
        "title": "Draft Page",
        "version": {"number": 1, "message": "Published via CLI"},
        "body": {
            "representation": "storage",
            "value": "<p>Published content</p>"
        }
    });

    let put_response: Result<serde_json::Value, _> = client
        .put("/wiki/api/v2/pages/100001", &publish_payload)
        .await;

    assert!(put_response.is_ok());
    let result = put_response.unwrap();
    assert_eq!(result["status"], "current");
    assert_eq!(result["version"]["number"], 1);
}

#[tokio::test]
async fn test_update_published_page_increments_version() {
    let mock_server = MockServer::start().await;

    // Mock: get current page (published, version 3)
    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100002"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100002",
            "title": "Published Page",
            "status": "current",
            "version": {"number": 3}
        })))
        .mount(&mock_server)
        .await;

    // Mock: update page (version should be 4)
    Mock::given(method("PUT"))
        .and(path("/wiki/api/v2/pages/100002"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "100002",
            "title": "Published Page Updated",
            "status": "current",
            "version": {"number": 4}
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    // Verify current version
    let get_response: Result<serde_json::Value, _> = client.get("/wiki/api/v2/pages/100002").await;
    assert!(get_response.is_ok());
    let data = get_response.unwrap();
    assert_eq!(data["version"]["number"], 3);

    // Update the page (version should increment to 4)
    let update_payload = serde_json::json!({
        "id": "100002",
        "status": "current",
        "title": "Published Page Updated",
        "version": {"number": 4}
    });

    let put_response: Result<serde_json::Value, _> = client
        .put("/wiki/api/v2/pages/100002", &update_payload)
        .await;

    assert!(put_response.is_ok());
    let result = put_response.unwrap();
    assert_eq!(result["version"]["number"], 4);
}

#[tokio::test]
async fn test_publish_draft_blogpost() {
    let mock_server = MockServer::start().await;

    // Mock: get current blog post (draft status)
    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/blogposts/300001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "300001",
            "title": "Draft Blog Post",
            "status": "draft",
            "version": {"number": 1}
        })))
        .mount(&mock_server)
        .await;

    // Mock: publish blog post (PUT with version 1)
    Mock::given(method("PUT"))
        .and(path("/wiki/api/v2/blogposts/300001"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "300001",
            "title": "Draft Blog Post",
            "status": "current",
            "version": {"number": 1, "message": "Published via CLI"}
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    // Verify we can get the draft blog post
    let get_response: Result<serde_json::Value, _> =
        client.get("/wiki/api/v2/blogposts/300001").await;
    assert!(get_response.is_ok());
    let data = get_response.unwrap();
    assert_eq!(data["status"], "draft");

    // Publish the blog post with version 1
    let publish_payload = serde_json::json!({
        "id": "300001",
        "status": "current",
        "title": "Draft Blog Post",
        "version": {"number": 1, "message": "Published via CLI"},
        "body": {
            "representation": "storage",
            "value": "<p>Published blog content</p>"
        }
    });

    let put_response: Result<serde_json::Value, _> = client
        .put("/wiki/api/v2/blogposts/300001", &publish_payload)
        .await;

    assert!(put_response.is_ok());
    let result = put_response.unwrap();
    assert_eq!(result["status"], "current");
    assert_eq!(result["version"]["number"], 1);
}

// ============================================================================
// Comments
// ============================================================================

// Regression: the v2 footer-comment object has no top-level `createdAt` (it lives
// at `version.createdAt`) and only returns `body` when body-format is requested.
// The old model required a top-level `createdAt: String`, so this payload aborted
// the whole `page comments` command with "missing field `createdAt`".
#[tokio::test]
async fn test_list_page_comments_real_shape() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/api/v2/pages/100001/footer-comments"))
        .and(query_param("body-format", "storage"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "results": [
                {
                    "id": "c-1",
                    "title": "Re: Design",
                    "version": { "number": 1, "createdAt": "2026-01-02T03:04:05Z" },
                    "body": { "storage": { "value": "<p>Looks good</p>", "representation": "storage" } }
                },
                { "id": "c-2" }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    let response: Result<serde_json::Value, _> = client
        .get("/wiki/api/v2/pages/100001/footer-comments?body-format=storage")
        .await;

    assert!(response.is_ok(), "comments request failed: {response:?}");
    let data = response.unwrap();
    let results = data["results"].as_array().unwrap();
    assert_eq!(results.len(), 2);
    assert_eq!(results[0]["version"]["createdAt"], "2026-01-02T03:04:05Z");
    assert!(results[0].get("createdAt").is_none());
}

// Regression: downloadLink is relative to the /wiki context, so the bytes must be
// fetched from /wiki + downloadLink. Fetching base_url + downloadLink dropped the
// /wiki segment and every download 404'd. Query string must survive too.
#[tokio::test]
async fn test_download_attachment_bytes_uses_wiki_prefix() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/wiki/download/attachments/100001/diagram.png"))
        .and(query_param("version", "1"))
        .respond_with(ResponseTemplate::new(200).set_body_bytes(b"PNGDATA".to_vec()))
        .mount(&mock_server)
        .await;

    let client = ApiClient::new(mock_server.uri())
        .unwrap()
        .with_basic_auth("test@example.com", "fake-token");

    // This is exactly the path download_attachment builds from the v2 downloadLink
    // "/download/attachments/100001/diagram.png?version=1&api=v2".
    let bytes = client
        .get_bytes("/wiki/download/attachments/100001/diagram.png?version=1&api=v2")
        .await;

    assert!(bytes.is_ok(), "download failed: {bytes:?}");
    assert_eq!(bytes.unwrap(), b"PNGDATA".to_vec());
}