package attach import ( "strings" "testing" ) // TestVariantKeyTable pins VariantKey beside ThumbKey's names: the // extension is replaced (case-insensitively, the format lower-cased), a key // with the format's extension gets it appended, a key without an extension // gets one, only the last extension counts, and a format outside the token // rule or an empty key answers "". A variant never equals its thumbnail. func TestVariantKeyTable(t *testing.T) { for _, tc := range []struct{ thumb, format, want string }{ {"a/b/c/thumb_1_480_0_0_0_auto.jpg", "webp", "a/b/c/thumb_1_480_0_0_0_auto.webp"}, {"a/b/c/thumb_1_480_0_0_0_auto.JPG", "webp", "a/b/c/thumb_1_480_0_0_0_auto.webp"}, {"a/b/c/thumb_1_480_0_0_0_auto.jpeg", "AVIF", "a/b/c/thumb_1_480_0_0_0_auto.avif"}, {"a/b/c/thumb_1_480_0_0_0_auto.WEBP", "webp", "a/b/c/thumb_1_480_0_0_0_auto.WEBP.webp"}, {"a/b/c/thumb_1_480_0_0_0_auto.tar.gz", "webp", "a/b/c/thumb_1_480_0_0_0_auto.tar.webp"}, {"a/b/c/thumb_1_480_0_0_0_auto", "webp", "a/b/c/thumb_1_480_0_0_0_auto.webp"}, {"a/b.c/thumb", "webp", "a/b.c/thumb.webp"}, {"a/b/c/thumb_1.jpg", "web p", ""}, {"a/b/c/thumb_1.jpg", "webp/", ""}, {"a/b/c/thumb_1.jpg", "-", ""}, {"", "jpg", ""}, } { got := VariantKey(tc.thumb, tc.format) if got != tc.want { t.Errorf("VariantKey(%q, %q) = %q, want %q", tc.thumb, tc.format, got, tc.want) } if got != "" && got == tc.thumb { t.Errorf("VariantKey(%q, %q) overwrites the thumbnail", tc.thumb, tc.format) } } } // TestVariantKeyOfThumbFilename pins the variant of a real thumbnail name // (ThumbFilename in the original's partition directory): it keeps the // directory and the stem and differs only in the extension, for an // original of each common format. func TestVariantKeyOfThumbFilename(t *testing.T) { for _, ext := range []string{"jpg", "png", "gif", "webp"} { disk := "abc123def456ghi789jkl0." + ext thumb := PartitionDirectory(disk) + ThumbFilename(42, 480, 320, 0, 0, "exact", ext) got := VariantKey(thumb, "webp") stem := strings.TrimSuffix(thumb, "."+ext) if got == thumb || !strings.HasPrefix(got, stem+".") || !strings.HasSuffix(got, ".webp") || strings.Contains(strings.TrimPrefix(got, stem), "/") { t.Errorf("%s: VariantKey(%q) = %q", disk, thumb, got) } } }