omni-dev 0.33.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, and Datadog.
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
//! CLI commands for JIRA issue attachments.

use std::fs;
use std::io::{self, BufRead, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

use crate::atlassian::client::AtlassianClient;
use crate::atlassian::jira_types::JiraAttachment;
use crate::cli::atlassian::confirm::{guard_destructive_with_io, GuardOptions, GuardOutcome};
use crate::cli::atlassian::helpers::create_client;
use crate::utils::path::attachment_filename;

/// Image MIME types for filtering.
const IMAGE_MIME_TYPES: &[&str] = &[
    "image/png",
    "image/jpeg",
    "image/gif",
    "image/svg+xml",
    "image/webp",
];

/// Manages JIRA issue attachments.
#[derive(Parser)]
pub struct AttachmentCommand {
    /// The attachment subcommand to execute.
    #[command(subcommand)]
    pub command: AttachmentSubcommands,
}

/// Attachment subcommands.
#[derive(Subcommand)]
pub enum AttachmentSubcommands {
    /// Uploads one or more files as attachments to an issue (mirrors the `jira_attachment_upload` MCP tool).
    Upload(UploadCommand),
    /// Downloads all attachments (or filtered by pattern) (mirrors the `jira_attachment_download` MCP tool).
    Download(DownloadCommand),
    /// Downloads only image attachments (mirrors the `jira_attachment_images` MCP tool).
    Images(ImagesCommand),
    /// Deletes an attachment by ID (mirrors the `jira_attachment_delete` MCP tool).
    Delete(DeleteCommand),
}

impl AttachmentCommand {
    /// Executes the attachment command.
    pub async fn execute(self) -> Result<()> {
        match self.command {
            AttachmentSubcommands::Upload(cmd) => cmd.execute().await,
            AttachmentSubcommands::Download(cmd) => cmd.execute().await,
            AttachmentSubcommands::Images(cmd) => cmd.execute().await,
            AttachmentSubcommands::Delete(cmd) => cmd.execute().await,
        }
    }
}

/// Uploads one or more files as attachments to an issue.
#[derive(Parser)]
pub struct UploadCommand {
    /// JIRA issue key (e.g., PROJ-123).
    pub key: String,

    /// Path(s) to the local file(s) to upload.
    #[arg(required = true, num_args = 1..)]
    pub files: Vec<PathBuf>,
}

impl UploadCommand {
    /// Executes the upload command.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        run_upload(&client, &self.key, &self.files).await
    }
}

/// Uploads the given files to an issue and prints the created attachments.
async fn run_upload(client: &AtlassianClient, key: &str, files: &[PathBuf]) -> Result<()> {
    let attachments = client.upload_attachments(key, files).await?;
    println!("Uploaded {} file(s) to {key}:", attachments.len());
    for attachment in &attachments {
        println!(
            "  {} (id={}, {})",
            attachment.filename,
            attachment.id,
            format_size(attachment.size)
        );
    }
    Ok(())
}

/// Deletes an attachment by ID.
#[derive(Parser)]
pub struct DeleteCommand {
    /// Attachment ID.
    pub attachment_id: String,

    /// Skips the confirmation prompt.
    #[arg(long)]
    pub force: bool,

    /// Prints what would be deleted without making any API calls.
    #[arg(long)]
    pub dry_run: bool,
}

impl DeleteCommand {
    /// Executes the delete command.
    pub async fn execute(self) -> Result<()> {
        let (client, instance_url) = create_client()?;
        let mut reader = io::BufReader::new(io::stdin());
        let mut writer = io::stdout();
        self.execute_with_io(&client, &instance_url, &mut reader, &mut writer)
            .await
    }

    /// Inner form taking explicit client, instance URL, and IO handles, for unit tests.
    async fn execute_with_io(
        self,
        client: &AtlassianClient,
        instance_url: &str,
        reader: &mut (dyn BufRead + Send),
        writer: &mut (dyn Write + Send),
    ) -> Result<()> {
        if !self.force || self.dry_run {
            let prompt = format_delete_prompt(&self.attachment_id);
            let dry_run_message = format!("Would delete attachment {}.", self.attachment_id);

            let outcome = guard_destructive_with_io(
                &GuardOptions {
                    prompt: &prompt,
                    dry_run_message: &dry_run_message,
                    force: self.force,
                    dry_run: self.dry_run,
                },
                reader,
                writer,
            )?;

            match outcome {
                GuardOutcome::Cancelled | GuardOutcome::DryRun => return Ok(()),
                GuardOutcome::Proceed => {}
            }
        }

        client.delete_attachment(&self.attachment_id).await?;
        writeln!(
            writer,
            "Deleted attachment {} from {}.",
            self.attachment_id, instance_url
        )?;

        Ok(())
    }
}

/// Formats the deletion confirmation prompt.
fn format_delete_prompt(attachment_id: &str) -> String {
    format!("Delete attachment {attachment_id}? [y/N] ")
}

/// Downloads all attachments for an issue.
#[derive(Parser)]
pub struct DownloadCommand {
    /// JIRA issue key (e.g., PROJ-123).
    pub key: String,

    /// Output directory (defaults to current directory).
    #[arg(long, default_value = ".")]
    pub output_dir: String,

    /// Filter filenames by substring (case-insensitive).
    #[arg(long)]
    pub filter: Option<String>,
}

impl DownloadCommand {
    /// Fetches attachment metadata and downloads matching files.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        run_download(&client, &self.key, &self.output_dir, self.filter.as_deref()).await
    }
}

/// Fetches, filters, and downloads attachments for an issue.
async fn run_download(
    client: &AtlassianClient,
    key: &str,
    output_dir: &str,
    filter: Option<&str>,
) -> Result<()> {
    let attachments = client.get_attachments(key).await?;
    let filtered = filter_attachments(&attachments, filter);

    if filtered.is_empty() {
        println!("No attachments found.");
        return Ok(());
    }

    ensure_dir(output_dir)?;

    for attachment in &filtered {
        download_file(client, attachment, output_dir).await?;
    }

    println!("Downloaded {} file(s) to {output_dir}.", filtered.len());
    Ok(())
}

/// Downloads only image attachments for an issue.
#[derive(Parser)]
pub struct ImagesCommand {
    /// JIRA issue key (e.g., PROJ-123).
    pub key: String,

    /// Output directory (defaults to current directory).
    #[arg(long, default_value = ".")]
    pub output_dir: String,
}

impl ImagesCommand {
    /// Fetches attachment metadata and downloads image files.
    pub async fn execute(self) -> Result<()> {
        let (client, _instance_url) = create_client()?;
        run_images(&client, &self.key, &self.output_dir).await
    }
}

/// Fetches and downloads image attachments for an issue.
async fn run_images(client: &AtlassianClient, key: &str, output_dir: &str) -> Result<()> {
    let attachments = client.get_attachments(key).await?;
    let images = filter_images(&attachments);

    if images.is_empty() {
        println!("No image attachments found.");
        return Ok(());
    }

    ensure_dir(output_dir)?;

    for attachment in &images {
        download_file(client, attachment, output_dir).await?;
    }

    println!("Downloaded {} image(s) to {output_dir}.", images.len());
    Ok(())
}

/// Filters attachments by a case-insensitive substring match on filename.
fn filter_attachments<'a>(
    attachments: &'a [JiraAttachment],
    filter: Option<&str>,
) -> Vec<&'a JiraAttachment> {
    match filter {
        Some(pattern) => {
            let pattern_lower = pattern.to_lowercase();
            attachments
                .iter()
                .filter(|a| a.filename.to_lowercase().contains(&pattern_lower))
                .collect()
        }
        None => attachments.iter().collect(),
    }
}

/// Filters attachments to only images by MIME type.
fn filter_images(attachments: &[JiraAttachment]) -> Vec<&JiraAttachment> {
    attachments
        .iter()
        .filter(|a| IMAGE_MIME_TYPES.contains(&a.mime_type.as_str()))
        .collect()
}

/// Creates the output directory if it doesn't exist.
fn ensure_dir(dir: &str) -> Result<()> {
    if !Path::new(dir).exists() {
        fs::create_dir_all(dir)
            .with_context(|| format!("Failed to create output directory: {dir}"))?;
    }
    Ok(())
}

/// Downloads a single attachment to the output directory.
async fn download_file(
    client: &crate::atlassian::client::AtlassianClient,
    attachment: &JiraAttachment,
    output_dir: &str,
) -> Result<()> {
    eprintln!(
        "Downloading {} ({})...",
        attachment.filename,
        format_size(attachment.size)
    );
    let data = client.get_bytes(&attachment.content_url).await?;
    let path =
        Path::new(output_dir).join(attachment_filename(&attachment.filename, &attachment.id));
    fs::write(&path, &data).with_context(|| format!("Failed to write {}", path.display()))?;
    Ok(())
}

/// Formats a file size for display.
fn format_size(size: u64) -> String {
    if size < 1024 {
        format!("{size} B")
    } else if size < 1024 * 1024 {
        format!("{:.1} KB", size as f64 / 1024.0)
    } else {
        format!("{:.1} MB", size as f64 / (1024.0 * 1024.0))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    fn sample_attachment(id: &str, filename: &str, mime_type: &str, size: u64) -> JiraAttachment {
        JiraAttachment {
            id: id.to_string(),
            filename: filename.to_string(),
            mime_type: mime_type.to_string(),
            size,
            content_url: format!("https://org.atlassian.net/attachment/{id}"),
        }
    }

    // ── filter_attachments ─────────────────────────────────────────

    #[test]
    fn filter_no_pattern_returns_all() {
        let attachments = vec![
            sample_attachment("1", "file.txt", "text/plain", 100),
            sample_attachment("2", "image.png", "image/png", 200),
        ];
        let result = filter_attachments(&attachments, None);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn filter_by_pattern() {
        let attachments = vec![
            sample_attachment("1", "screenshot.png", "image/png", 100),
            sample_attachment("2", "report.pdf", "application/pdf", 200),
            sample_attachment("3", "Screenshot_2.png", "image/png", 300),
        ];
        let result = filter_attachments(&attachments, Some("screenshot"));
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn filter_no_match() {
        let attachments = vec![sample_attachment("1", "file.txt", "text/plain", 100)];
        let result = filter_attachments(&attachments, Some("nonexistent"));
        assert!(result.is_empty());
    }

    // ── filter_images ──────────────────────────────────────────────

    #[test]
    fn filter_images_mixed() {
        let attachments = vec![
            sample_attachment("1", "photo.png", "image/png", 100),
            sample_attachment("2", "doc.pdf", "application/pdf", 200),
            sample_attachment("3", "icon.gif", "image/gif", 50),
            sample_attachment("4", "page.svg", "image/svg+xml", 75),
            sample_attachment("5", "hero.webp", "image/webp", 150),
            sample_attachment("6", "photo.jpg", "image/jpeg", 300),
        ];
        let result = filter_images(&attachments);
        assert_eq!(result.len(), 5);
    }

    #[test]
    fn filter_images_none() {
        let attachments = vec![
            sample_attachment("1", "doc.pdf", "application/pdf", 200),
            sample_attachment("2", "data.json", "application/json", 100),
        ];
        let result = filter_images(&attachments);
        assert!(result.is_empty());
    }

    // ── format_size ────────────────────────────────────────────────

    #[test]
    fn format_size_bytes() {
        assert_eq!(format_size(500), "500 B");
    }

    #[test]
    fn format_size_kilobytes() {
        assert_eq!(format_size(2048), "2.0 KB");
    }

    #[test]
    fn format_size_megabytes() {
        assert_eq!(format_size(5_242_880), "5.0 MB");
    }

    #[test]
    fn format_size_zero() {
        assert_eq!(format_size(0), "0 B");
    }

    // ── ensure_dir ─────────────────────────────────────────────────

    #[test]
    fn ensure_dir_creates_directory() {
        let temp = tempfile::tempdir().unwrap();
        let new_dir = temp.path().join("subdir");
        ensure_dir(new_dir.to_str().unwrap()).unwrap();
        assert!(new_dir.exists());
    }

    #[test]
    fn ensure_dir_existing_is_ok() {
        let temp = tempfile::tempdir().unwrap();
        ensure_dir(temp.path().to_str().unwrap()).unwrap();
    }

    // ── run_download (wiremock) ──────────────────────────────────────

    #[tokio::test]
    async fn run_download_success() {
        let server = wiremock::MockServer::start().await;
        let content_url = format!("{}/attachment/1", server.uri());

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/PROJ-1"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "fields": {
                        "attachment": [{
                            "id": "1",
                            "filename": "test.txt",
                            "mimeType": "text/plain",
                            "size": 5,
                            "content": content_url
                        }]
                    }
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/attachment/1"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_bytes(b"hello".as_slice()))
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let temp = tempfile::tempdir().unwrap();
        let result = run_download(&client, "PROJ-1", temp.path().to_str().unwrap(), None).await;
        assert!(result.is_ok());
        assert!(temp.path().join("test.txt").exists());
    }

    #[tokio::test]
    async fn run_download_sanitizes_traversal_filename() {
        let server = wiremock::MockServer::start().await;
        let content_url = format!("{}/attachment/1", server.uri());

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/PROJ-1"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "fields": {
                        "attachment": [{
                            "id": "1",
                            "filename": "../../evil.txt",
                            "mimeType": "text/plain",
                            "size": 4,
                            "content": content_url
                        }]
                    }
                })),
            )
            .expect(1)
            .mount(&server)
            .await;

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/attachment/1"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_bytes(b"EVIL".as_slice()))
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let temp = tempfile::tempdir().unwrap();
        run_download(&client, "PROJ-1", temp.path().to_str().unwrap(), None)
            .await
            .unwrap();
        assert!(temp.path().join("evil.txt").exists());
        let escaped = temp.path().parent().unwrap().parent().unwrap();
        assert!(!escaped.join("evil.txt").exists());
    }

    #[tokio::test]
    async fn run_download_empty() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/PROJ-1"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"fields": {"attachment": []}})),
            )
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let result = run_download(&client, "PROJ-1", ".", None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn run_download_api_error() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/NOPE-1"))
            .respond_with(wiremock::ResponseTemplate::new(404).set_body_string("Not Found"))
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let err = run_download(&client, "NOPE-1", ".", None)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("404"));
    }

    // ── run_images (wiremock) ─────────────────────────────────────────

    #[tokio::test]
    async fn run_images_success() {
        let server = wiremock::MockServer::start().await;
        let content_url = format!("{}/attachment/1", server.uri());

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/PROJ-1"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(
                serde_json::json!({
                    "fields": {
                        "attachment": [
                            {"id": "1", "filename": "photo.png", "mimeType": "image/png", "size": 100, "content": content_url},
                            {"id": "2", "filename": "doc.pdf", "mimeType": "application/pdf", "size": 200, "content": "https://example.com/2"}
                        ]
                    }
                }),
            ))
            .expect(1)
            .mount(&server)
            .await;

        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/attachment/1"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_bytes(b"\x89PNG".as_slice()),
            )
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let temp = tempfile::tempdir().unwrap();
        let result = run_images(&client, "PROJ-1", temp.path().to_str().unwrap()).await;
        assert!(result.is_ok());
        assert!(temp.path().join("photo.png").exists());
    }

    #[tokio::test]
    async fn run_images_no_images() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/rest/api/3/issue/PROJ-1"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(
                serde_json::json!({
                    "fields": {
                        "attachment": [
                            {"id": "1", "filename": "doc.pdf", "mimeType": "application/pdf", "size": 200, "content": "https://example.com/1"}
                        ]
                    }
                }),
            ))
            .expect(1)
            .mount(&server)
            .await;

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let result = run_images(&client, "PROJ-1", ".").await;
        assert!(result.is_ok());
    }

    // ── dispatch ───────────────────────────────────────────────────

    #[test]
    fn attachment_command_download_variant() {
        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Download(DownloadCommand {
                key: "PROJ-1".to_string(),
                output_dir: ".".to_string(),
                filter: None,
            }),
        };
        assert!(matches!(cmd.command, AttachmentSubcommands::Download(_)));
    }

    #[test]
    fn attachment_command_images_variant() {
        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Images(ImagesCommand {
                key: "PROJ-1".to_string(),
                output_dir: ".".to_string(),
            }),
        };
        assert!(matches!(cmd.command, AttachmentSubcommands::Images(_)));
    }

    #[test]
    fn download_command_with_filter() {
        let cmd = DownloadCommand {
            key: "PROJ-1".to_string(),
            output_dir: "/tmp/out".to_string(),
            filter: Some("screenshot".to_string()),
        };
        assert_eq!(cmd.filter.as_deref(), Some("screenshot"));
    }

    #[test]
    fn attachment_command_upload_variant() {
        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Upload(UploadCommand {
                key: "PROJ-1".to_string(),
                files: vec![PathBuf::from("a.txt")],
            }),
        };
        assert!(matches!(cmd.command, AttachmentSubcommands::Upload(_)));
    }

    #[test]
    fn attachment_command_delete_variant() {
        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Delete(DeleteCommand {
                attachment_id: "10042".to_string(),
                force: true,
                dry_run: false,
            }),
        };
        assert!(matches!(cmd.command, AttachmentSubcommands::Delete(_)));
    }

    // ── run_upload (wiremock) ────────────────────────────────────────

    #[tokio::test]
    async fn run_upload_success() {
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path(
                "/rest/api/3/issue/PROJ-1/attachments",
            ))
            .and(wiremock::matchers::header("X-Atlassian-Token", "no-check"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!([
                {"id": "10001", "filename": "log.txt", "mimeType": "text/plain", "size": 5, "content": "https://example.com/10001"}
            ])))
            .expect(1)
            .mount(&server)
            .await;

        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("log.txt");
        fs::write(&file, b"hello").unwrap();

        let client =
            crate::atlassian::client::AtlassianClient::new(&server.uri(), "u@t.com", "tok")
                .unwrap();
        let result = run_upload(&client, "PROJ-1", &[file]).await;
        assert!(result.is_ok(), "{result:?}");
    }

    // ── DeleteCommand::execute_with_io (wiremock, injected IO) ────────

    fn delete_test_client(server: &wiremock::MockServer) -> AtlassianClient {
        AtlassianClient::new(&server.uri(), "u@t.com", "tok").unwrap()
    }

    async fn mount_delete_mock(server: &wiremock::MockServer, attachment_id: &str) {
        wiremock::Mock::given(wiremock::matchers::method("DELETE"))
            .and(wiremock::matchers::path(format!(
                "/rest/api/3/attachment/{attachment_id}"
            )))
            .respond_with(wiremock::ResponseTemplate::new(204))
            .expect(1)
            .mount(server)
            .await;
    }

    async fn run_delete_with_io(
        cmd: DeleteCommand,
        client: &AtlassianClient,
        input: &[u8],
    ) -> (Result<()>, String) {
        let mut reader = std::io::Cursor::new(input.to_vec());
        let mut output = Vec::<u8>::new();
        let result = cmd
            .execute_with_io(
                client,
                "https://example.atlassian.net",
                &mut reader,
                &mut output,
            )
            .await;
        (result, String::from_utf8(output).unwrap())
    }

    #[tokio::test]
    async fn delete_execute_with_force_calls_delete() {
        let server = wiremock::MockServer::start().await;
        mount_delete_mock(&server, "10042").await;

        let cmd = DeleteCommand {
            attachment_id: "10042".to_string(),
            force: true,
            dry_run: false,
        };
        let (result, out) = run_delete_with_io(cmd, &delete_test_client(&server), b"").await;
        assert!(result.is_ok(), "{result:?}");
        assert!(out.contains("Deleted attachment 10042 from https://example.atlassian.net."));
    }

    #[tokio::test]
    async fn delete_execute_with_dry_run_does_not_call_delete() {
        // No DELETE mock — confirms the API is *not* called on dry-run.
        let server = wiremock::MockServer::start().await;

        let cmd = DeleteCommand {
            attachment_id: "10042".to_string(),
            force: false,
            dry_run: true,
        };
        let (result, out) = run_delete_with_io(cmd, &delete_test_client(&server), b"").await;
        assert!(result.is_ok(), "{result:?}");
        assert!(out.contains("Would delete attachment 10042."));
        assert!(!out.contains("Deleted attachment"));
    }

    #[tokio::test]
    async fn delete_execute_with_prompt_yes_calls_delete() {
        let server = wiremock::MockServer::start().await;
        mount_delete_mock(&server, "10042").await;

        let cmd = DeleteCommand {
            attachment_id: "10042".to_string(),
            force: false,
            dry_run: false,
        };
        let (result, out) = run_delete_with_io(cmd, &delete_test_client(&server), b"y\n").await;
        assert!(result.is_ok(), "{result:?}");
        assert!(out.contains("Delete attachment 10042? [y/N]"));
        assert!(out.contains("Deleted attachment 10042"));
    }

    #[tokio::test]
    async fn delete_execute_with_prompt_no_skips_api() {
        // No DELETE mock — confirms the API is *not* called when declined.
        let server = wiremock::MockServer::start().await;

        let cmd = DeleteCommand {
            attachment_id: "10042".to_string(),
            force: false,
            dry_run: false,
        };
        let (result, out) = run_delete_with_io(cmd, &delete_test_client(&server), b"n\n").await;
        assert!(result.is_ok(), "{result:?}");
        assert!(!out.contains("Deleted attachment"));
    }

    // ── execute() wrappers via dispatch (create_client boundary) ────
    //
    // These drive AttachmentCommand::execute → the subcommand execute() →
    // create_client(), pointing the credential env at a wiremock server so the
    // real credential-loading path runs end to end.

    #[tokio::test(flavor = "current_thread")]
    async fn attachment_command_upload_dispatch_via_mock() {
        let guard = crate::atlassian::auth::test_util::EnvGuard::take();
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("POST"))
            .and(wiremock::matchers::path(
                "/rest/api/3/issue/PROJ-1/attachments",
            ))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_json(serde_json::json!([
                {"id": "10001", "filename": "log.txt", "mimeType": "text/plain", "size": 2, "content": "https://example.com/10001"}
            ])))
            .mount(&server)
            .await;
        let _home = guard.set_credentials(&server.uri());

        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("log.txt");
        fs::write(&file, b"hi").unwrap();

        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Upload(UploadCommand {
                key: "PROJ-1".to_string(),
                files: vec![file],
            }),
        };
        cmd.execute().await.unwrap();
    }

    #[tokio::test(flavor = "current_thread")]
    async fn attachment_command_delete_dispatch_via_mock() {
        let guard = crate::atlassian::auth::test_util::EnvGuard::take();
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::method("DELETE"))
            .and(wiremock::matchers::path("/rest/api/3/attachment/10042"))
            .respond_with(wiremock::ResponseTemplate::new(204))
            .mount(&server)
            .await;
        let _home = guard.set_credentials(&server.uri());

        // force: true skips the prompt, so stdin is never read.
        let cmd = AttachmentCommand {
            command: AttachmentSubcommands::Delete(DeleteCommand {
                attachment_id: "10042".to_string(),
                force: true,
                dry_run: false,
            }),
        };
        cmd.execute().await.unwrap();
    }
}