feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -0,0 +1,201 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/inbound"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func newInboxHandlerFixture(t *testing.T) (*InboxHandler, *inboxFixture, outbound.ConsumedEvent) {
|
||||
t.Helper()
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
payload, err := SyntheticNoticeEventPayload(fixture.user.ID, fixture.organizationID)
|
||||
if err != nil {
|
||||
t.Fatalf("SyntheticNoticeEventPayload() error = %v", err)
|
||||
}
|
||||
event := outbound.ConsumedEvent{
|
||||
EventID: uuid.New(), EventType: string(domain.NotificationInboxSyntheticDelivered), SchemaVersion: 1,
|
||||
AggregateType: SyntheticNoticeAggregateType, AggregateID: uuid.New(), AggregateVersion: SyntheticNoticeAggregateVersion,
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC), Payload: payload,
|
||||
}
|
||||
return NewInboxHandler(slog.New(slog.DiscardHandler), true), fixture, event
|
||||
}
|
||||
|
||||
func TestInboxHandlerStoresOneRowForARepeatedEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler, fixture, event := newInboxHandlerFixture(t)
|
||||
if !handler.Handles(event.EventType) || handler.Handles("notification.mail_requested") {
|
||||
t.Fatal("Handles() must accept inbox events only")
|
||||
}
|
||||
for range 2 {
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle() error = %v", err)
|
||||
}
|
||||
}
|
||||
if len(fixture.repository.entries) != 1 {
|
||||
t.Fatalf("stored rows = %d, want one for a repeated event ID", len(fixture.repository.entries))
|
||||
}
|
||||
entry := fixture.repository.entries[0]
|
||||
if entry.EventID() != event.EventID || entry.RecipientUserID() != fixture.user.ID ||
|
||||
entry.OrganizationID() != fixture.organizationID || entry.SourceID() != fixture.organizationID ||
|
||||
entry.SourceType() != domain.NotificationInboxSourceOrganization {
|
||||
t.Fatalf("stored entry = %+v, want the references of the event", entry)
|
||||
}
|
||||
if _, isRead := entry.ReadAt(); isRead {
|
||||
t.Fatal("a delivered row starts as read")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerRejectsEventsThatBreakTheContract(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
mutate func(*outbound.ConsumedEvent, *inboxFixture)
|
||||
}{
|
||||
{name: "schema version", mutate: func(event *outbound.ConsumedEvent, _ *inboxFixture) { event.SchemaVersion = 2 }},
|
||||
{name: "aggregate type", mutate: func(event *outbound.ConsumedEvent, _ *inboxFixture) { event.AggregateType = "mail_request" }},
|
||||
{name: "unknown field", mutate: func(event *outbound.ConsumedEvent, fixture *inboxFixture) {
|
||||
event.Payload = json.RawMessage(`{"recipient_user_id":"` + fixture.user.ID.String() + `","organization_id":"` + fixture.organizationID.String() + `","email":"a@example.test"}`)
|
||||
}},
|
||||
{name: "missing organization", mutate: func(event *outbound.ConsumedEvent, fixture *inboxFixture) {
|
||||
event.Payload = json.RawMessage(`{"recipient_user_id":"` + fixture.user.ID.String() + `"}`)
|
||||
}},
|
||||
{name: "not an object", mutate: func(event *outbound.ConsumedEvent, _ *inboxFixture) { event.Payload = json.RawMessage(`[]`) }},
|
||||
{name: "trailing data", mutate: func(event *outbound.ConsumedEvent, _ *inboxFixture) {
|
||||
event.Payload = append(append(json.RawMessage{}, event.Payload...), []byte(` {}`)...)
|
||||
}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
handler, fixture, event := newInboxHandlerFixture(t)
|
||||
test.mutate(&event, fixture)
|
||||
err := handler.Handle(context.Background(), fixture.unit(), event)
|
||||
if !errors.Is(err, inbound.ErrEventRejected) {
|
||||
t.Fatalf("Handle() error = %v, want ErrEventRejected", err)
|
||||
}
|
||||
if len(fixture.repository.entries) != 0 {
|
||||
t.Fatal("a rejected event stored a row")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerRejectsAnEventIDReusedWithOtherMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler, fixture, event := newInboxHandlerFixture(t)
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle(first) error = %v", err)
|
||||
}
|
||||
event.OccurredAt = event.OccurredAt.Add(time.Hour)
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); !errors.Is(err, inbound.ErrEventRejected) {
|
||||
t.Fatalf("Handle(changed replay) error = %v, want ErrEventRejected", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerSkipsADeletedRecipientAndKeepsStoreErrorsRetryable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler, fixture, event := newInboxHandlerFixture(t)
|
||||
delete(fixture.users, fixture.user.ID)
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle(deleted recipient) error = %v, want a skip", err)
|
||||
}
|
||||
if len(fixture.repository.entries) != 0 {
|
||||
t.Fatal("a deleted recipient got a row")
|
||||
}
|
||||
|
||||
handler, fixture, event = newInboxHandlerFixture(t)
|
||||
fixture.repository.storeErr = errors.New("connection reset")
|
||||
err := handler.Handle(context.Background(), fixture.unit(), event)
|
||||
if err == nil || errors.Is(err, inbound.ErrEventRejected) {
|
||||
t.Fatalf("Handle(store failure) error = %v, want a retryable error", err)
|
||||
}
|
||||
fixture.repository.storeErr = outbound.ErrNotificationInboxReferenceMissing
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); !errors.Is(err, inbound.ErrEventRejected) {
|
||||
t.Fatalf("Handle(missing organization) error = %v, want ErrEventRejected", err)
|
||||
}
|
||||
// A recipient deleted between the check and the insert is not the event's
|
||||
// fault: the attempt is retried, and the retry skips the deleted account.
|
||||
fixture.repository.storeErr = outbound.ErrNotificationInboxRecipientMissing
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err == nil || errors.Is(err, inbound.ErrEventRejected) {
|
||||
t.Fatalf("Handle(vanished recipient) error = %v, want a retryable error", err)
|
||||
}
|
||||
delete(fixture.users, fixture.user.ID)
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle(retry after the recipient vanished) error = %v, want a skip", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerPlugsInInvitationEventsWithTheSameContract(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler, fixture, _ := newInboxHandlerFixture(t)
|
||||
invitationID := uuid.New()
|
||||
payload, err := json.Marshal(inboxEventPayload{RecipientUserID: fixture.user.ID, OrganizationID: fixture.organizationID})
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
event := outbound.ConsumedEvent{
|
||||
EventID: uuid.New(), EventType: string(domain.NotificationInboxInvitationDelivered), SchemaVersion: 1,
|
||||
AggregateType: "invitation", AggregateID: invitationID, AggregateVersion: 3,
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC), Payload: payload,
|
||||
}
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle(invitation.delivered) error = %v", err)
|
||||
}
|
||||
entry := fixture.repository.entries[0]
|
||||
if entry.SourceType() != domain.NotificationInboxSourceInvitation || entry.SourceID() != invitationID {
|
||||
t.Fatalf("invitation entry source = %s %s, want the invitation aggregate", entry.SourceType(), entry.SourceID())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerStoresARowWhateverTheStateOfTheAddress(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// A mail delivery skips an address that is not verified. The inbox is the
|
||||
// delivery state inside the platform and reads no mail state.
|
||||
handler, fixture, event := newInboxHandlerFixture(t)
|
||||
unverified := fixture.user
|
||||
unverified.EmailVerifiedAt = nil
|
||||
fixture.users[unverified.ID] = unverified
|
||||
if err := handler.Handle(context.Background(), fixture.unit(), event); err != nil {
|
||||
t.Fatalf("Handle(unverified address) error = %v", err)
|
||||
}
|
||||
if len(fixture.repository.entries) != 1 {
|
||||
t.Fatalf("stored rows = %d, want 1: an address state must not block a notification", len(fixture.repository.entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxHandlerIgnoresTheSyntheticEventWhileItIsSwitchedOff(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, fixture, event := newInboxHandlerFixture(t)
|
||||
off := NewInboxHandler(slog.New(slog.DiscardHandler), false)
|
||||
if off.Handles(event.EventType) {
|
||||
t.Fatal("the synthetic event is handled while the switch is off")
|
||||
}
|
||||
if !off.Handles(string(domain.NotificationInboxInvitationDelivered)) {
|
||||
t.Fatal("the invitation event must not depend on the synthetic switch")
|
||||
}
|
||||
if err := off.Handle(context.Background(), fixture.unit(), event); !errors.Is(err, inbound.ErrEventRejected) || len(fixture.repository.entries) != 0 {
|
||||
t.Fatalf("Handle(synthetic, off) error = %v, rows = %d, want a rejection and no row", err, len(fixture.repository.entries))
|
||||
}
|
||||
if _, ok := DefaultInboxSourceResolvers(false)[domain.NotificationInboxSourceOrganization]; ok {
|
||||
t.Fatal("the synthetic resolver is registered while the switch is off")
|
||||
}
|
||||
if _, ok := DefaultInboxSourceResolvers(true)[domain.NotificationInboxSourceOrganization]; !ok {
|
||||
t.Fatal("the synthetic resolver is missing while the switch is on")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user