This commit is contained in:
Vladislav Kunetskiy
2026-10-21 21:22:42 +03:00
parent f4bd361f12
commit 464db7fdd6
238 changed files with 13332 additions and 704 deletions
+1
View File
@@ -9,6 +9,7 @@ var (
ErrValidation = errors.New("validation failed")
ErrInvalidCursor = errors.New("invalid cursor")
ErrConflict = errors.New("conflict")
ErrIdempotency = errors.New("idempotency key already used with another payload")
ErrUnauthenticated = errors.New("unauthenticated")
ErrUnavailable = errors.New("temporarily unavailable")
)
+23 -4
View File
@@ -1,6 +1,8 @@
package domain
import (
"crypto/sha256"
"fmt"
"time"
"github.com/google/uuid"
@@ -14,12 +16,29 @@ type Message struct {
CreatedAt time.Time
}
// MessageIdempotency is the client Idempotency-Key together with the fingerprint
// of the payload it was first used with. The same key and fingerprint replay the
// stored message; the same key with another payload is a conflict.
type MessageIdempotency struct {
Key string
RequestHash [sha256.Size]byte
}
type MessageDraft struct {
ChatID uuid.UUID
SenderID uuid.UUID
Text string
ChatID uuid.UUID
SenderID uuid.UUID
Text string
Idempotency MessageIdempotency
}
func (d MessageDraft) Validate() error {
return validateNotBlank("text", d.Text)
if err := validateNotBlank("text", d.Text); err != nil {
return err
}
if d.Idempotency.Key == "" {
return fmt.Errorf("idempotency key is missing: %w", ErrValidation)
}
return nil
}
+20 -6
View File
@@ -88,20 +88,34 @@ func TestMessageDraftRejectsBlankText(t *testing.T) {
t.Parallel()
draft := domain.MessageDraft{
ChatID: uuid.New(),
SenderID: uuid.New(),
Text: strings.Repeat(" ", 10),
ChatID: uuid.New(),
SenderID: uuid.New(),
Text: strings.Repeat(" ", 10),
Idempotency: domain.MessageIdempotency{Key: "key-1"},
}
require.ErrorIs(t, draft.Validate(), domain.ErrValidation)
}
func TestMessageDraftRejectsMissingIdempotencyKey(t *testing.T) {
t.Parallel()
draft := domain.MessageDraft{
ChatID: uuid.New(),
SenderID: uuid.New(),
Text: "привет",
}
require.ErrorIs(t, draft.Validate(), domain.ErrValidation,
"с lab3 отправка сообщения невозможна без Idempotency-Key")
}
func TestMessageDraftAcceptsTextWithSurroundingSpaces(t *testing.T) {
t.Parallel()
draft := domain.MessageDraft{
ChatID: uuid.New(),
SenderID: uuid.New(),
Text: " привет ",
ChatID: uuid.New(),
SenderID: uuid.New(),
Text: " привет ",
Idempotency: domain.MessageIdempotency{Key: "key-1"},
}
assert.NoError(t, draft.Validate(), "контракт запрещает только полностью пробельный текст")
}