87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package domain_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"mtusi-messenger/internal/domain"
|
|
)
|
|
|
|
// Длины, regex и uniqueItems проверяет сгенерированный слой по contracts/openapi.json.
|
|
// Домен закрывает только то, что схема выразить не может.
|
|
|
|
func TestUserDraftRejectsBlankDisplayName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, blank := range []string{" ", " ", "\t", "\n", "\u00a0"} {
|
|
draft := domain.UserDraft{Username: "alice", DisplayName: blank}
|
|
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
|
}
|
|
}
|
|
|
|
func TestUserDraftAcceptsVisibleName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
draft := domain.UserDraft{Username: "alice", DisplayName: "Алиса"}
|
|
assert.NoError(t, draft.Validate())
|
|
}
|
|
|
|
func TestChatDraftRequiresCallerAmongMembers(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
caller := uuid.New()
|
|
|
|
outside := domain.ChatDraft{
|
|
Title: "Проект",
|
|
MemberIDs: []uuid.UUID{uuid.New(), uuid.New()},
|
|
CallerID: caller,
|
|
}
|
|
require.ErrorIs(t, outside.Validate(), domain.ErrValidation)
|
|
|
|
inside := domain.ChatDraft{
|
|
Title: "Проект",
|
|
MemberIDs: []uuid.UUID{uuid.New(), caller},
|
|
CallerID: caller,
|
|
}
|
|
assert.NoError(t, inside.Validate())
|
|
}
|
|
|
|
func TestChatDraftRejectsBlankTitle(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
caller := uuid.New()
|
|
|
|
draft := domain.ChatDraft{
|
|
Title: " ",
|
|
MemberIDs: []uuid.UUID{caller},
|
|
CallerID: caller,
|
|
}
|
|
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
|
}
|
|
|
|
func TestMessageDraftRejectsBlankText(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
draft := domain.MessageDraft{
|
|
ChatID: uuid.New(),
|
|
SenderID: uuid.New(),
|
|
Text: strings.Repeat(" ", 10),
|
|
}
|
|
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
|
|
}
|
|
|
|
func TestMessageDraftAcceptsTextWithSurroundingSpaces(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
draft := domain.MessageDraft{
|
|
ChatID: uuid.New(),
|
|
SenderID: uuid.New(),
|
|
Text: " привет ",
|
|
}
|
|
assert.NoError(t, draft.Validate(), "контракт запрещает только полностью пробельный текст")
|
|
}
|