|
| 1 | +package clitests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCardAttachFlag(t *testing.T) { |
| 13 | + h := newHarness(t) |
| 14 | + boardID := createBoard(t, h) |
| 15 | + imagePath := fixtureFile(t, "test_image.png") |
| 16 | + docPath := fixtureFile(t, "test_document.txt") |
| 17 | + |
| 18 | + t.Run("creates card with single attach and downloads it", func(t *testing.T) { |
| 19 | + title := fmt.Sprintf("Attach Flag Card %d", time.Now().UnixNano()) |
| 20 | + result := h.Run("card", "create", "--board", boardID, "--title", title, "--attach", imagePath) |
| 21 | + assertOK(t, result) |
| 22 | + |
| 23 | + cardNumber := result.GetNumberFromLocation() |
| 24 | + if cardNumber == 0 { |
| 25 | + cardNumber = result.GetDataInt("number") |
| 26 | + } |
| 27 | + if cardNumber == 0 { |
| 28 | + t.Fatalf("failed to get card number from create (location: %s)", result.GetLocation()) |
| 29 | + } |
| 30 | + |
| 31 | + showResult := h.Run("card", "attachments", "show", strconv.Itoa(cardNumber)) |
| 32 | + assertOK(t, showResult) |
| 33 | + arr := showResult.GetDataArray() |
| 34 | + if len(arr) != 1 { |
| 35 | + t.Fatalf("expected 1 attachment, got %d", len(arr)) |
| 36 | + } |
| 37 | + attachment := asMap(arr[0]) |
| 38 | + if got := mapValueString(attachment, "filename"); got != "test_image.png" { |
| 39 | + t.Fatalf("expected filename test_image.png, got %v", got) |
| 40 | + } |
| 41 | + |
| 42 | + outputPath := filepath.Join(t.TempDir(), "test_image.png") |
| 43 | + downloadResult := h.Run("card", "attachments", "download", strconv.Itoa(cardNumber), "1", "-o", outputPath) |
| 44 | + assertOK(t, downloadResult) |
| 45 | + assertFileExists(t, outputPath) |
| 46 | + }) |
| 47 | + |
| 48 | + t.Run("creates card with multiple attaches in order", func(t *testing.T) { |
| 49 | + title := fmt.Sprintf("Attach Flag Multi Card %d", time.Now().UnixNano()) |
| 50 | + result := h.Run( |
| 51 | + "card", "create", |
| 52 | + "--board", boardID, |
| 53 | + "--title", title, |
| 54 | + "--description", "See attached files", |
| 55 | + "--attach", imagePath, |
| 56 | + "--attach", docPath, |
| 57 | + ) |
| 58 | + assertOK(t, result) |
| 59 | + |
| 60 | + cardNumber := result.GetNumberFromLocation() |
| 61 | + if cardNumber == 0 { |
| 62 | + cardNumber = result.GetDataInt("number") |
| 63 | + } |
| 64 | + |
| 65 | + showResult := h.Run("card", "attachments", "show", strconv.Itoa(cardNumber)) |
| 66 | + assertOK(t, showResult) |
| 67 | + arr := showResult.GetDataArray() |
| 68 | + if len(arr) != 2 { |
| 69 | + t.Fatalf("expected 2 attachments, got %d", len(arr)) |
| 70 | + } |
| 71 | + if got := mapValueString(asMap(arr[0]), "filename"); got != "test_image.png" { |
| 72 | + t.Fatalf("expected first attachment test_image.png, got %v", got) |
| 73 | + } |
| 74 | + if got := mapValueString(asMap(arr[1]), "filename"); got != "test_document.txt" { |
| 75 | + t.Fatalf("expected second attachment test_document.txt, got %v", got) |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("works with description_file", func(t *testing.T) { |
| 80 | + descriptionFile := filepath.Join(t.TempDir(), "description.md") |
| 81 | + mustWriteFile(t, descriptionFile, []byte("See file-based content")) |
| 82 | + |
| 83 | + title := fmt.Sprintf("Attach Flag File Card %d", time.Now().UnixNano()) |
| 84 | + result := h.Run( |
| 85 | + "card", "create", |
| 86 | + "--board", boardID, |
| 87 | + "--title", title, |
| 88 | + "--description_file", descriptionFile, |
| 89 | + "--attach", imagePath, |
| 90 | + ) |
| 91 | + assertOK(t, result) |
| 92 | + |
| 93 | + cardNumber := result.GetNumberFromLocation() |
| 94 | + if cardNumber == 0 { |
| 95 | + cardNumber = result.GetDataInt("number") |
| 96 | + } |
| 97 | + |
| 98 | + showResult := h.Run("card", "attachments", "show", strconv.Itoa(cardNumber)) |
| 99 | + assertOK(t, showResult) |
| 100 | + if got := len(showResult.GetDataArray()); got != 1 { |
| 101 | + t.Fatalf("expected 1 attachment from description_file flow, got %d", got) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func assertFileExists(t *testing.T, path string) { |
| 107 | + t.Helper() |
| 108 | + if _, err := os.Stat(path); err != nil { |
| 109 | + t.Fatalf("expected file at %s: %v", path, err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func mustWriteFile(t *testing.T, path string, content []byte) { |
| 114 | + t.Helper() |
| 115 | + if err := os.WriteFile(path, content, 0o644); err != nil { |
| 116 | + t.Fatalf("failed to write %s: %v", path, err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestCommentAttachFlag(t *testing.T) { |
| 121 | + h := newHarness(t) |
| 122 | + cardNumber := createCard(t, h, fixture.BoardID) |
| 123 | + cardStr := strconv.Itoa(cardNumber) |
| 124 | + imagePath := fixtureFile(t, "test_image.png") |
| 125 | + docPath := fixtureFile(t, "test_document.txt") |
| 126 | + |
| 127 | + t.Run("creates attachment-only comment with single attach", func(t *testing.T) { |
| 128 | + before := h.Run("comment", "attachments", "show", "--card", cardStr) |
| 129 | + assertOK(t, before) |
| 130 | + beforeCount := len(before.GetDataArray()) |
| 131 | + |
| 132 | + result := h.Run("comment", "create", "--card", cardStr, "--attach", imagePath) |
| 133 | + assertOK(t, result) |
| 134 | + |
| 135 | + showResult := h.Run("comment", "attachments", "show", "--card", cardStr) |
| 136 | + assertOK(t, showResult) |
| 137 | + arr := showResult.GetDataArray() |
| 138 | + if len(arr) != beforeCount+1 { |
| 139 | + t.Fatalf("expected %d comment attachments, got %d", beforeCount+1, len(arr)) |
| 140 | + } |
| 141 | + added := asMap(arr[len(arr)-1]) |
| 142 | + if got := mapValueString(added, "filename"); got != "test_image.png" { |
| 143 | + t.Fatalf("expected filename test_image.png, got %v", got) |
| 144 | + } |
| 145 | + |
| 146 | + outputPath := filepath.Join(t.TempDir(), "test_image.png") |
| 147 | + downloadResult := h.Run("comment", "attachments", "download", "--card", cardStr, strconv.Itoa(len(arr)), "-o", outputPath) |
| 148 | + assertOK(t, downloadResult) |
| 149 | + assertFileExists(t, outputPath) |
| 150 | + }) |
| 151 | + |
| 152 | + t.Run("creates comment with multiple attaches in order", func(t *testing.T) { |
| 153 | + before := h.Run("comment", "attachments", "show", "--card", cardStr) |
| 154 | + assertOK(t, before) |
| 155 | + beforeCount := len(before.GetDataArray()) |
| 156 | + |
| 157 | + result := h.Run( |
| 158 | + "comment", "create", |
| 159 | + "--card", cardStr, |
| 160 | + "--body", "See attached files", |
| 161 | + "--attach", imagePath, |
| 162 | + "--attach", docPath, |
| 163 | + ) |
| 164 | + assertOK(t, result) |
| 165 | + |
| 166 | + showResult := h.Run("comment", "attachments", "show", "--card", cardStr) |
| 167 | + assertOK(t, showResult) |
| 168 | + arr := showResult.GetDataArray() |
| 169 | + if len(arr) != beforeCount+2 { |
| 170 | + t.Fatalf("expected %d comment attachments, got %d", beforeCount+2, len(arr)) |
| 171 | + } |
| 172 | + |
| 173 | + lastTwo := arr[len(arr)-2:] |
| 174 | + if got := mapValueString(asMap(lastTwo[0]), "filename"); got != "test_image.png" { |
| 175 | + t.Fatalf("expected first new attachment test_image.png, got %v", got) |
| 176 | + } |
| 177 | + if got := mapValueString(asMap(lastTwo[1]), "filename"); got != "test_document.txt" { |
| 178 | + t.Fatalf("expected second new attachment test_document.txt, got %v", got) |
| 179 | + } |
| 180 | + }) |
| 181 | +} |
0 commit comments