package entity import ( "strings" "testing" ) func TestBuildObjectKeyByPurpose(t *testing.T) { avatar := BuildObjectKey(PurposeAvatar, 11, "99", "20260917", "jpg") if avatar != "perm/avatar/20260917/99.jpg" { t.Fatalf("avatar %s", avatar) } chat := BuildObjectKey(PurposeChatImage, 11, "99", "20260917", "jpg") if chat != "chat/image/20260917/99.jpg" { t.Fatalf("chat %s", chat) } voice := BuildObjectKey(PurposeChatVoice, 11, "aa", "20260917", "m4a") if voice != "chat/voice/20260917/aa.m4a" { t.Fatalf("voice %s", voice) } group := BuildObjectKey(PurposeGroupAvatar, 7, "99", "20260917", "png") if group != "perm/group/7/99.png" { t.Fatalf("group %s", group) } fav := BuildFavKey("99", "20260917", "m4a") if fav != "perm/fav/20260917/99.m4a" { t.Fatalf("fav %s", fav) } } func TestValidObjectKey(t *testing.T) { if !ValidObjectKey("chat/20260917/1/2.jpg") || !ValidObjectKey("perm/avatar/1/2.jpg") || !ValidObjectKey("perm/group/3/4.jpg") { t.Fatal("want valid") } for _, key := range []string{"", "../x", "/chat/a", "other/a", "chat/a b.jpg", "chat/.. /x"} { if ValidObjectKey(key) { t.Fatalf("accepted %q", key) } } } func TestNormalizeMIMEAndExt(t *testing.T) { if got := NormalizeMIME("Image/JPEG; charset=utf-8"); got != "image/jpeg" { t.Fatalf("mime %s", got) } if ExtForMIME("image/png") != "png" || ExtOfKey("chat/a/b.M4A") != "m4a" { t.Fatal("ext") } spec, ok := LookupSpec(PurposeChatVoice) if !ok || spec.MaxSize <= 0 { t.Fatal("spec") } if _, ok := spec.MIMEs["audio/mpeg"]; !ok { t.Fatal("voice mime") } img, ok := LookupSpec(PurposeChatImage) if !ok { t.Fatal("image spec") } if _, ok := img.MIMEs["image/jpeg"]; !ok { t.Fatal("jpeg") } for _, mime := range []string{"image/heic", "image/heif", "image/webp"} { if _, ok := img.MIMEs[mime]; ok { t.Fatalf("interchange must not allow %s", mime) } } if strings.Contains(BuildObjectKey(PurposeAvatar, 1, "1", "d", "jpg"), PrefixChat) { t.Fatal("avatar must be perm") } }