Files
mtusi2026lab4/internal/domain/message_test.go
T

71 lines
2.1 KiB
Go

package domain
import (
"strings"
"testing"
)
func TestValidateMessageInput(t *testing.T) {
validAttachment := "11111111-1111-4111-8111-111111111111"
testCases := []struct {
name string
text string
attachmentIDs []string
wantError bool
}{
{name: "text only", text: "hello"},
{name: "attachment only", attachmentIDs: []string{validAttachment}},
{name: "whitespace with attachment", text: " ", attachmentIDs: []string{validAttachment}},
{name: "empty", wantError: true},
{name: "whitespace only", text: " ", wantError: true},
{name: "text too long", text: strings.Repeat("界", 4001), wantError: true},
{name: "invalid attachment uuid", text: "hello", attachmentIDs: []string{"not-a-uuid"}, wantError: true},
{
name: "duplicate attachment uuid ignores case",
text: "hello",
attachmentIDs: []string{
"AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA",
"aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
},
wantError: true,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
err := ValidateMessageInput(testCase.text, testCase.attachmentIDs)
if (err != nil) != testCase.wantError {
t.Fatalf("ValidateMessageInput() error = %v, wantError %v", err, testCase.wantError)
}
})
}
}
func TestValidateMessageInputAttachmentLimit(t *testing.T) {
attachmentIDs := []string{
"00000000-0000-4000-8000-000000000001",
"00000000-0000-4000-8000-000000000002",
"00000000-0000-4000-8000-000000000003",
"00000000-0000-4000-8000-000000000004",
"00000000-0000-4000-8000-000000000005",
"00000000-0000-4000-8000-000000000006",
"00000000-0000-4000-8000-000000000007",
"00000000-0000-4000-8000-000000000008",
"00000000-0000-4000-8000-000000000009",
"00000000-0000-4000-8000-000000000010",
}
if err := ValidateMessageInput("", attachmentIDs); err != nil {
t.Fatalf("10 attachments must be accepted: %v", err)
}
attachmentIDs = append(
attachmentIDs,
"00000000-0000-4000-8000-000000000011",
)
if err := ValidateMessageInput("hello", attachmentIDs); err == nil {
t.Fatal("11 attachments must be rejected")
}
}