package domain import ( "errors" "testing" "time" "github.com/google/uuid" ) func TestNewNotificationInboxEntry(t *testing.T) { t.Parallel() occurredAt := time.Date(2026, time.October, 8, 12, 30, 0, 123456789, time.FixedZone("test", 3*60*60)) input := NotificationInboxEntryInput{ EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(), EventType: NotificationInboxInvitationDelivered, SchemaVersion: 1, SourceType: NotificationInboxSourceInvitation, SourceID: uuid.New(), OccurredAt: occurredAt, } entry, err := NewNotificationInboxEntry(input) if err != nil { t.Fatalf("NewNotificationInboxEntry() error = %v", err) } if entry.EventID() != input.EventID || entry.RecipientUserID() != input.RecipientUserID || entry.OrganizationID() != input.OrganizationID { t.Fatal("entry identities do not match input") } if entry.SourceID() != input.SourceID || entry.SourceType() != input.SourceType || entry.EventType() != input.EventType || entry.SchemaVersion() != input.SchemaVersion { t.Fatal("entry source metadata does not match input") } wantOccurredAt := occurredAt.UTC().Truncate(time.Microsecond) if !entry.OccurredAt().Equal(wantOccurredAt) { t.Fatalf("OccurredAt() = %s, want %s", entry.OccurredAt(), wantOccurredAt) } if _, isRead := entry.ReadAt(); isRead { t.Fatal("new entry is already read") } } func TestRestoreNotificationInboxEntryRejectsInvalidMetadata(t *testing.T) { t.Parallel() base := NotificationInboxEntryInput{ EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(), EventType: NotificationInboxInvitationDelivered, SchemaVersion: 1, SourceType: NotificationInboxSourceInvitation, SourceID: uuid.New(), OccurredAt: time.Date(2026, time.October, 8, 12, 30, 0, 0, time.UTC), } zeroTime := time.Time{} tests := []struct { name string mutate func(*NotificationInboxEntryInput) }{ {name: "missing event ID", mutate: func(input *NotificationInboxEntryInput) { input.EventID = uuid.Nil }}, {name: "missing recipient ID", mutate: func(input *NotificationInboxEntryInput) { input.RecipientUserID = uuid.Nil }}, {name: "missing organization ID", mutate: func(input *NotificationInboxEntryInput) { input.OrganizationID = uuid.Nil }}, {name: "missing source ID", mutate: func(input *NotificationInboxEntryInput) { input.SourceID = uuid.Nil }}, {name: "unknown event type", mutate: func(input *NotificationInboxEntryInput) { input.EventType = "notification.mail_requested" }}, {name: "unsupported schema version", mutate: func(input *NotificationInboxEntryInput) { input.SchemaVersion = 2 }}, {name: "unknown source type", mutate: func(input *NotificationInboxEntryInput) { input.SourceType = "mail_request" }}, {name: "source type of another event type", mutate: func(input *NotificationInboxEntryInput) { input.SourceType = NotificationInboxSourceOrganization }}, {name: "missing occurrence time", mutate: func(input *NotificationInboxEntryInput) { input.OccurredAt = time.Time{} }}, {name: "zero read time", mutate: func(input *NotificationInboxEntryInput) { input.ReadAt = &zeroTime }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { t.Parallel() input := base test.mutate(&input) if _, err := RestoreNotificationInboxEntry(input); !errors.Is(err, ErrInvalidNotificationInboxEntry) { t.Fatalf("RestoreNotificationInboxEntry() error = %v, want ErrInvalidNotificationInboxEntry", err) } }) } } func TestNewNotificationInboxEntryRejectsReadState(t *testing.T) { t.Parallel() readAt := time.Date(2026, time.October, 8, 13, 0, 0, 0, time.UTC) input := NotificationInboxEntryInput{ EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(), EventType: NotificationInboxInvitationDelivered, SchemaVersion: 1, SourceType: NotificationInboxSourceInvitation, SourceID: uuid.New(), OccurredAt: time.Date(2026, time.October, 8, 12, 30, 0, 0, time.UTC), ReadAt: &readAt, } if _, err := NewNotificationInboxEntry(input); !errors.Is(err, ErrInvalidNotificationInboxEntry) { t.Fatalf("NewNotificationInboxEntry() error = %v, want ErrInvalidNotificationInboxEntry", err) } } func TestNotificationInboxEntryMarkReadPreservesFirstTimestamp(t *testing.T) { t.Parallel() entry, err := NewNotificationInboxEntry(NotificationInboxEntryInput{ EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(), EventType: NotificationInboxInvitationDelivered, SchemaVersion: 1, SourceType: NotificationInboxSourceInvitation, SourceID: uuid.New(), OccurredAt: time.Date(2026, time.October, 8, 12, 30, 0, 0, time.UTC), }) if err != nil { t.Fatalf("NewNotificationInboxEntry() error = %v", err) } first := time.Date(2026, time.October, 8, 13, 0, 0, 987654321, time.FixedZone("test", -4*60*60)) marked, err := entry.MarkRead(first) if err != nil { t.Fatalf("MarkRead(first) error = %v", err) } second := first.Add(time.Hour) retried, err := marked.MarkRead(second) if err != nil { t.Fatalf("MarkRead(retry) error = %v", err) } want := first.UTC().Truncate(time.Microsecond) got, isRead := retried.ReadAt() if !isRead || !got.Equal(want) { t.Fatalf("ReadAt() = %s, %t, want %s, true", got, isRead, want) } if retried.EventID() != entry.EventID() || retried.SourceID() != entry.SourceID() { t.Fatal("MarkRead changed immutable source identity") } } func TestNotificationInboxEntryAcceptsTheSyntheticEventOnlyWithItsSource(t *testing.T) { t.Parallel() input := NotificationInboxEntryInput{ EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(), EventType: NotificationInboxSyntheticDelivered, SchemaVersion: 1, SourceType: NotificationInboxSourceOrganization, SourceID: uuid.New(), OccurredAt: time.Date(2026, time.October, 8, 12, 30, 0, 0, time.UTC), } if _, err := NewNotificationInboxEntry(input); err != nil { t.Fatalf("NewNotificationInboxEntry(synthetic) error = %v", err) } input.SourceType = NotificationInboxSourceInvitation if _, err := NewNotificationInboxEntry(input); !errors.Is(err, ErrInvalidNotificationInboxEntry) { t.Fatalf("NewNotificationInboxEntry(synthetic with invitation source) error = %v, want ErrInvalidNotificationInboxEntry", err) } }