package messaging import ( "strings" "testing" ) // TestValidateInbound pins that an attachment the Gateway will act on cannot // claim a type its content contradicts, and that ordinary platform media is not // refused: a rejected voice note never gets transcribed, a rejected image never // reaches a model. func TestValidateInbound(t *testing.T) { oversize := int64(MaxInboundAttachmentBytes) + 1 tests := []struct { name string att MediaAttachment authored bool wantErr bool }{ {"a photo", MediaAttachment{Type: MediaTypeImage, MIMEType: "image/jpeg"}, false, false}, {"a voice note", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg"}, true, false}, {"a browser clip with codec parameters", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm;codecs=opus"}, true, false}, {"ogg served as application/ogg", MediaAttachment{Type: MediaTypeAudio, MIMEType: "application/ogg"}, false, false}, {"a safari clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/mp4"}, true, false}, {"any document", MediaAttachment{Type: MediaTypeDocument, MIMEType: "application/zip"}, false, false}, {"a platform that reports no content type is not claiming one", MediaAttachment{Type: MediaTypeDocument}, false, false}, {"a caller that chose the type must state a content type", MediaAttachment{Type: MediaTypeVoice}, true, true}, {"a voice note that declares html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "text/html"}, false, true}, {"an image that declares a document", MediaAttachment{Type: MediaTypeImage, MIMEType: "application/pdf"}, false, true}, {"a content type that is not one", MediaAttachment{Type: MediaTypeImage, MIMEType: "jpeg"}, false, true}, {"an attachment type the Gateway does not use", MediaAttachment{Type: "sticker", MIMEType: "image/webp"}, false, true}, {"declared bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, FileSize: &oversize}, false, true}, {"downloaded bytes over the ceiling", MediaAttachment{Type: MediaTypeImage, Data: make([]byte, oversize)}, false, true}, {"an ogg clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg", Data: []byte("OggS\x00\x02\x00\x00")}, true, false}, {"a webm clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/webm;codecs=opus", Data: []byte("\x1a\x45\xdf\xa3\x01\x00")}, true, false}, {"a safari mp4 clip", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/mp4", Data: []byte("\x00\x00\x00\x20ftypM4A ")}, true, false}, {"a wav clip", MediaAttachment{Type: MediaTypeAudio, MIMEType: "audio/wav", Data: []byte("RIFF\x24\x00\x00\x00WAVEfmt ")}, false, false}, {"a voice note carrying html", MediaAttachment{Type: MediaTypeVoice, MIMEType: "audio/ogg", Data: []byte("