|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Regression test for #195. The ECR image list rendered tag/digest/etc. |
| 10 | +// as space-padded but variable-width columns, so the digest column |
| 11 | +// started at a different position for each tag length and the list was |
| 12 | +// hard to scan. After the fix the digest substring lands at the same |
| 13 | +// position in every rendered row, regardless of tag length. |
| 14 | +func TestECRImageDisplayTitleColumnsAlign(t *testing.T) { |
| 15 | + now := time.Now() |
| 16 | + mk := func(tag string) ECRImage { |
| 17 | + return ECRImage{ |
| 18 | + Tags: []string{tag}, |
| 19 | + Digest: "sha256:abcdef0123456789", |
| 20 | + PushedAt: now, |
| 21 | + SizeBytes: 1234567, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + short := mk("v1").DisplayTitle() |
| 26 | + medium := mk("v1.2.3-rc.1").DisplayTitle() |
| 27 | + long := mk("very-long-feature-branch-tag-that-overflows-the-column").DisplayTitle() |
| 28 | + |
| 29 | + // shortDigest output for our digest. Reused as a column-position probe. |
| 30 | + const digest = "sha256:abcdef0123" |
| 31 | + |
| 32 | + // Compare rune positions, not bytes — the truncated row contains a |
| 33 | + // multi-byte ellipsis ('…' is 3 bytes / 1 rune) so byte indexes |
| 34 | + // disagree even when the rendered column lines up correctly in a |
| 35 | + // terminal. Rune count matches what the user actually sees. |
| 36 | + runePos := func(s, needle string) int { |
| 37 | + idx := strings.Index(s, needle) |
| 38 | + if idx < 0 { |
| 39 | + return -1 |
| 40 | + } |
| 41 | + return len([]rune(s[:idx])) |
| 42 | + } |
| 43 | + pShort := runePos(short, digest) |
| 44 | + pMedium := runePos(medium, digest) |
| 45 | + pLong := runePos(long, digest) |
| 46 | + |
| 47 | + if pShort < 0 || pMedium < 0 || pLong < 0 { |
| 48 | + t.Fatalf("digest substring missing from rendered row:\n short=%q\n medium=%q\n long=%q", |
| 49 | + short, medium, long) |
| 50 | + } |
| 51 | + if pShort != pMedium || pMedium != pLong { |
| 52 | + t.Fatalf("digest column not aligned (short=%d medium=%d long=%d):\n %s\n %s\n %s", |
| 53 | + pShort, pMedium, pLong, short, medium, long) |
| 54 | + } |
| 55 | + |
| 56 | + // Overflow must truncate via ellipsis, not push later columns right. |
| 57 | + if !strings.Contains(long, "…") { |
| 58 | + t.Fatalf("long tag should truncate with ellipsis, got %q", long) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// fitColumn is the helper that does the truncate-or-pad work; pin its |
| 63 | +// contract directly so any future width tweak surfaces here. |
| 64 | +func TestFitColumn(t *testing.T) { |
| 65 | + cases := []struct { |
| 66 | + name string |
| 67 | + in string |
| 68 | + width int |
| 69 | + want string |
| 70 | + }{ |
| 71 | + {"shorter pads with spaces", "abc", 6, "abc "}, |
| 72 | + {"exact length unchanged", "abcdef", 6, "abcdef"}, |
| 73 | + {"longer truncates with ellipsis", "abcdefghij", 6, "abcde…"}, |
| 74 | + {"unicode tag still respects rune width", "v1.2-α", 6, "v1.2-α"}, |
| 75 | + {"width 0 returns input", "abc", 0, "abc"}, |
| 76 | + {"width 1 with overflow keeps single rune", "abcde", 1, "a"}, |
| 77 | + } |
| 78 | + for _, tc := range cases { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + if got := fitColumn(tc.in, tc.width); got != tc.want { |
| 81 | + t.Fatalf("fitColumn(%q, %d) = %q, want %q", tc.in, tc.width, got, tc.want) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments