package fetch_test import ( "testing" "atp.pics/internal/fetch" ) func TestBuildParamStr(t *testing.T) { tests := []struct { w, h, q int format string wantParam string wantExt string }{ // No params → default.webp {0, 0, 0, "webp", "", "webp"}, // Explicit ?f=webp&q=85 is identical output → same key as no params {0, 0, 85, "webp", "", "webp"}, // Width only — implicit quality becomes explicit 85 in key {200, 0, 0, "webp", "w200-q85", "webp"}, // Width only — explicit quality {200, 0, 80, "webp", "w200-q80", "webp"}, // Height only — implicit quality {0, 300, 0, "webp", "h300-q85", "webp"}, // Both dimensions, WebP, explicit quality {200, 200, 85, "webp", "w200-h200-q85", "webp"}, // Both dimensions, JPEG, explicit quality {200, 200, 80, "jpg", "w200-h200-q80", "jpg"}, // Both dimensions, JPEG, implicit quality {200, 200, 0, "jpg", "w200-h200-q85", "jpg"}, // Format only, JPEG — uses f{fmt}-q{q} pattern {0, 0, 0, "jpg", "fjpg-q85", "jpg"}, // Format only, JPEG, explicit quality {0, 0, 75, "jpg", "fjpg-q75", "jpg"}, // PNG with resize — quality omitted from key {100, 100, 90, "png", "w100-h100", "png"}, // PNG format only — uses f{fmt} pattern, no quality {0, 0, 0, "png", "fpng", "png"}, } for _, tt := range tests { param, ext := fetch.BuildParamStr(tt.w, tt.h, tt.q, tt.format) if param != tt.wantParam { t.Errorf("BuildParamStr(%d,%d,%d,%q) param = %q, want %q", tt.w, tt.h, tt.q, tt.format, param, tt.wantParam) } if ext != tt.wantExt { t.Errorf("BuildParamStr(%d,%d,%d,%q) ext = %q, want %q", tt.w, tt.h, tt.q, tt.format, ext, tt.wantExt) } } } func TestPublicURL(t *testing.T) { const bucket = "my-bucket" const key = "avatars/did:plc:abc/bafkrei123/default.webp" t.Run("custom host", func(t *testing.T) { store := fetch.New(nil, bucket, "cdn.example.com") got := store.PublicURL(key) want := "https://cdn.example.com/" + key if got != want { t.Errorf("PublicURL = %q, want %q", got, want) } }) t.Run("tigris fallback", func(t *testing.T) { store := fetch.New(nil, bucket, "") got := store.PublicURL(key) want := "https://my-bucket.fly.storage.tigris.dev/" + key if got != want { t.Errorf("PublicURL = %q, want %q", got, want) } }) } func TestTransformKey(t *testing.T) { did := "did:plc:abc" cid := "bafkrei123" tests := []struct { paramStr string ext string want string }{ {"", "webp", "avatars/did:plc:abc/bafkrei123/default.webp"}, {"w200-h200-q85", "webp", "avatars/did:plc:abc/bafkrei123/w200-h200-q85.webp"}, {"w200-h200-q80", "jpg", "avatars/did:plc:abc/bafkrei123/w200-h200-q80.jpg"}, {"w100-h100", "png", "avatars/did:plc:abc/bafkrei123/w100-h100.png"}, } for _, tt := range tests { got := fetch.TransformKey(did, cid, tt.paramStr, tt.ext) if got != tt.want { t.Errorf("TransformKey(%q, %q) = %q, want %q", tt.paramStr, tt.ext, got, tt.want) } } }