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
@@ -3,6 +3,7 @@ package notification
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -12,78 +13,168 @@ import (
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestInboxServiceStoreInvitationDeliveredDeduplicatesRecipientEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
repository := &memoryInboxRepository{}
|
||||
service := newInboxTestService(t, repository, &memoryInboxResolver{}, time.Now)
|
||||
input := inboxTestEntryInput()
|
||||
created, err := service.StoreInvitationDelivered(context.Background(), input)
|
||||
if err != nil || !created {
|
||||
t.Fatalf("StoreInvitationDelivered(first) = %t, %v, want true, nil", created, err)
|
||||
}
|
||||
created, err = service.StoreInvitationDelivered(context.Background(), input)
|
||||
if err != nil || created {
|
||||
t.Fatalf("StoreInvitationDelivered(retry) = %t, %v, want false, nil", created, err)
|
||||
}
|
||||
if len(repository.entries) != 1 {
|
||||
t.Fatalf("stored entries = %d, want one", len(repository.entries))
|
||||
}
|
||||
|
||||
changed := input
|
||||
changed.SourceID = uuid.New()
|
||||
if _, err := service.StoreInvitationDelivered(context.Background(), changed); !errors.Is(err, outbound.ErrNotificationInboxEventConflict) {
|
||||
t.Fatalf("StoreInvitationDelivered(changed replay) error = %v, want ErrNotificationInboxEventConflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxServiceListRechecksContentAndFallsBackSafely(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
first := mustInboxTestEntry(t, inboxTestEntryInput())
|
||||
secondInput := inboxTestEntryInput()
|
||||
secondInput.RecipientUserID = first.RecipientUserID()
|
||||
secondInput.OrganizationID = first.OrganizationID()
|
||||
secondInput.EventID = uuid.New()
|
||||
second := mustInboxTestEntry(t, secondInput)
|
||||
wrongOrganizationInput := inboxTestEntryInput()
|
||||
wrongOrganizationInput.RecipientUserID = first.RecipientUserID()
|
||||
wrongOrganizationInput.EventID = uuid.New()
|
||||
wrongOrganizationInput.OrganizationID = uuid.New()
|
||||
wrongOrganization := mustInboxTestEntry(t, wrongOrganizationInput)
|
||||
repository := &memoryInboxRepository{entries: []domain.NotificationInboxEntry{first, second, wrongOrganization}}
|
||||
resolver := &memoryInboxResolver{unavailable: map[uuid.UUID]bool{second.EventID(): true}}
|
||||
service := newInboxTestService(t, repository, resolver, time.Now)
|
||||
orgID := first.OrganizationID()
|
||||
page, err := service.List(context.Background(), InboxListRequest{
|
||||
RecipientUserID: first.RecipientUserID(), OrganizationID: &orgID, Limit: 10,
|
||||
})
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
first := fixture.add(t, fixture.entryInput())
|
||||
second := fixture.add(t, fixture.entryInput())
|
||||
fixture.resolver.unavailable[second.EventID()] = true
|
||||
otherRecipient := fixtureEntryInput(uuid.New(), fixture.organizationID)
|
||||
fixture.add(t, otherRecipient)
|
||||
|
||||
page, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("List() error = %v", err)
|
||||
}
|
||||
if len(page.Items) != 2 {
|
||||
t.Fatalf("List() returned %d items, want 2", len(page.Items))
|
||||
t.Fatalf("List() returned %d items, want only the recipient's 2", len(page.Items))
|
||||
}
|
||||
if page.Items[0].Content == nil || page.Items[0].ContentUnavailable {
|
||||
t.Fatalf("visible item = %+v, want current content", page.Items[0])
|
||||
byID := map[uuid.UUID]InboxItem{}
|
||||
for _, item := range page.Items {
|
||||
byID[item.Entry.EventID()] = item
|
||||
}
|
||||
if page.Items[1].Content != nil || !page.Items[1].ContentUnavailable {
|
||||
t.Fatalf("hidden item = %+v, want safe fallback without source content", page.Items[1])
|
||||
if item := byID[first.EventID()]; item.Content == nil || item.ContentUnavailable {
|
||||
t.Fatalf("visible item = %+v, want current content", item)
|
||||
}
|
||||
if resolver.calls != 2 {
|
||||
t.Fatalf("resolver calls = %d, want one current visibility check per returned item", resolver.calls)
|
||||
if item := byID[second.EventID()]; item.Content != nil || !item.ContentUnavailable {
|
||||
t.Fatalf("hidden item = %+v, want safe fallback without source content", item)
|
||||
}
|
||||
if fixture.resolver.calls != 2 {
|
||||
t.Fatalf("resolver calls = %d, want one current visibility check per returned item", fixture.resolver.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxServiceAuthorizesTheCabinetInTheReadTransaction(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("the candidate inbox refuses an employer", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
||||
_, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
||||
if !errors.Is(err, ErrInboxForbidden) {
|
||||
t.Fatalf("List(employer in the candidate cabinet) error = %v, want ErrInboxForbidden", err)
|
||||
}
|
||||
if fixture.repository.listCalls != 0 {
|
||||
t.Fatal("the repository was read before the account was authorized")
|
||||
}
|
||||
})
|
||||
t.Run("the organization inbox refuses a candidate", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
_, err := fixture.service.List(context.Background(), InboxListRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
||||
})
|
||||
if !errors.Is(err, ErrInboxForbidden) {
|
||||
t.Fatalf("List(candidate in the organization cabinet) error = %v, want ErrInboxForbidden", err)
|
||||
}
|
||||
})
|
||||
t.Run("an unknown or unverified account is refused", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
if _, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: uuid.New(), Limit: 10}); !errors.Is(err, ErrInboxForbidden) {
|
||||
t.Fatalf("List(unknown account) error = %v, want ErrInboxForbidden", err)
|
||||
}
|
||||
fixture.user.EmailVerifiedAt = nil
|
||||
fixture.users[fixture.user.ID] = fixture.user
|
||||
if _, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10}); !errors.Is(err, ErrInboxForbidden) {
|
||||
t.Fatalf("List(unverified account) error = %v, want ErrInboxForbidden", err)
|
||||
}
|
||||
})
|
||||
t.Run("a revoked membership answers like an unknown organization", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
||||
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateActive)
|
||||
entry := fixture.add(t, fixture.entryInput())
|
||||
request := InboxListRequest{RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10}
|
||||
page, err := fixture.service.List(context.Background(), request)
|
||||
if err != nil || len(page.Items) != 1 {
|
||||
t.Fatalf("List(active member) = %d items, %v, want 1 item", len(page.Items), err)
|
||||
}
|
||||
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateRemoved)
|
||||
if _, err := fixture.service.List(context.Background(), request); !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
||||
t.Fatalf("List(revoked member) error = %v, want ErrOrganizationNotFound", err)
|
||||
}
|
||||
_, err = fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: entry.EventID(),
|
||||
})
|
||||
if !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
||||
t.Fatalf("MarkRead(revoked member) error = %v, want ErrOrganizationNotFound", err)
|
||||
}
|
||||
if _, isRead := fixture.repository.entries[0].ReadAt(); isRead {
|
||||
t.Fatal("a revoked member marked an item read")
|
||||
}
|
||||
unknown := uuid.New()
|
||||
_, err = fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, OrganizationID: &unknown, Limit: 10})
|
||||
if !errors.Is(err, outbound.ErrOrganizationNotFound) {
|
||||
t.Fatalf("List(unknown organization) error = %v, want the same ErrOrganizationNotFound", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestInboxServiceOrganizationCabinetServesEveryRoleItsOwnItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, role := range []domain.OrganizationRole{
|
||||
domain.OrganizationRoleOwner, domain.OrganizationRoleRecruiter,
|
||||
domain.OrganizationRoleHiringManager, domain.OrganizationRoleTechnicalExpert,
|
||||
} {
|
||||
t.Run(string(role), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
||||
fixture.addMembership(t, role, domain.MembershipStateActive)
|
||||
own := fixture.add(t, fixture.entryInput())
|
||||
otherMember := fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
||||
otherOrganization := fixture.add(t, fixtureEntryInput(fixture.user.ID, uuid.New()))
|
||||
|
||||
page, err := fixture.service.List(context.Background(), InboxListRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
||||
})
|
||||
if err != nil || len(page.Items) != 1 || page.Items[0].Entry.EventID() != own.EventID() {
|
||||
t.Fatalf("List(%s) = %d items, %v, want only its own item", role, len(page.Items), err)
|
||||
}
|
||||
if fixture.locks == 0 {
|
||||
t.Fatal("the membership was not locked for the read")
|
||||
}
|
||||
for _, foreign := range []domain.NotificationInboxEntry{otherMember, otherOrganization} {
|
||||
_, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: foreign.EventID(),
|
||||
})
|
||||
if !errors.Is(err, outbound.ErrNotificationInboxEntryNotFound) {
|
||||
t.Fatalf("MarkRead(%s, foreign item) error = %v, want ErrNotificationInboxEntryNotFound", role, err)
|
||||
}
|
||||
}
|
||||
if _, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, EventID: own.EventID(),
|
||||
}); err != nil {
|
||||
t.Fatalf("MarkRead(%s, own item) error = %v", role, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxServiceOrganizationCabinetNeedsAConfirmedEmail(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fixture := newInboxFixture(t, domain.ScenarioEmployer)
|
||||
fixture.addMembership(t, domain.OrganizationRoleOwner, domain.MembershipStateActive)
|
||||
fixture.user.EmailVerifiedAt = nil
|
||||
fixture.users[fixture.user.ID] = fixture.user
|
||||
_, err := fixture.service.List(context.Background(), InboxListRequest{
|
||||
RecipientUserID: fixture.user.ID, OrganizationID: &fixture.organizationID, Limit: 10,
|
||||
})
|
||||
if !errors.Is(err, ErrInboxEmailNotVerified) {
|
||||
t.Fatalf("List(unverified employer) error = %v, want ErrInboxEmailNotVerified", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxServiceListRejectsRepositoryRowsOutsideRecipientScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
entry := mustInboxTestEntry(t, inboxTestEntryInput())
|
||||
repository := &memoryInboxRepository{entries: []domain.NotificationInboxEntry{entry}, ignoreListScope: true}
|
||||
service := newInboxTestService(t, repository, &memoryInboxResolver{}, time.Now)
|
||||
_, err := service.List(context.Background(), InboxListRequest{
|
||||
RecipientUserID: uuid.New(), OrganizationID: uuidPointer(entry.OrganizationID()), Limit: 10,
|
||||
})
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
fixture.repository.ignoreListScope = true
|
||||
fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
||||
_, err := fixture.service.List(context.Background(), InboxListRequest{RecipientUserID: fixture.user.ID, Limit: 10})
|
||||
if !errors.Is(err, ErrInboxUnavailable) {
|
||||
t.Fatalf("List(out-of-scope repository row) error = %v, want ErrInboxUnavailable", err)
|
||||
}
|
||||
@@ -92,21 +183,17 @@ func TestInboxServiceListRejectsRepositoryRowsOutsideRecipientScope(t *testing.T
|
||||
func TestInboxServiceMarkReadPersistsAndKeepsFirstTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
entry := mustInboxTestEntry(t, inboxTestEntryInput())
|
||||
firstRead := time.Date(2026, time.October, 8, 13, 0, 0, 1000, time.UTC)
|
||||
secondRead := firstRead.Add(time.Hour)
|
||||
clockCalls := 0
|
||||
clock := func() time.Time {
|
||||
fixture := newInboxFixtureWithClock(t, domain.ScenarioCandidate, func() time.Time {
|
||||
clockCalls++
|
||||
if clockCalls == 1 {
|
||||
return firstRead
|
||||
}
|
||||
return secondRead
|
||||
}
|
||||
repository := &memoryInboxRepository{entries: []domain.NotificationInboxEntry{entry}}
|
||||
service := newInboxTestService(t, repository, &memoryInboxResolver{}, clock)
|
||||
return firstRead.Add(time.Duration(clockCalls-1) * time.Hour)
|
||||
})
|
||||
entry := fixture.add(t, fixture.entryInput())
|
||||
for range 2 {
|
||||
read, err := service.MarkRead(context.Background(), entry.RecipientUserID(), entry.OrganizationID(), entry.EventID())
|
||||
read, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
||||
RecipientUserID: fixture.user.ID, EventID: entry.EventID(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("MarkRead() error = %v", err)
|
||||
}
|
||||
@@ -117,6 +204,22 @@ func TestInboxServiceMarkReadPersistsAndKeepsFirstTime(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboxServiceMarkReadRefusesAnotherRecipientsItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
foreign := fixture.add(t, fixtureEntryInput(uuid.New(), fixture.organizationID))
|
||||
_, err := fixture.service.MarkRead(context.Background(), InboxMarkReadRequest{
|
||||
RecipientUserID: fixture.user.ID, EventID: foreign.EventID(),
|
||||
})
|
||||
if !errors.Is(err, outbound.ErrNotificationInboxEntryNotFound) {
|
||||
t.Fatalf("MarkRead(foreign item) error = %v, want ErrNotificationInboxEntryNotFound", err)
|
||||
}
|
||||
if _, isRead := fixture.repository.entries[0].ReadAt(); isRead {
|
||||
t.Fatal("another recipient's item was marked read")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInboxContentRejectsExternalOrAmbiguousLinks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -129,6 +232,9 @@ func TestNewInboxContentRejectsExternalOrAmbiguousLinks(t *testing.T) {
|
||||
{name: "query string", path: "/candidate/invitations?token=secret"},
|
||||
{name: "fragment", path: "/candidate/invitations#current"},
|
||||
{name: "backslash", path: `/candidate\\invitations`},
|
||||
{name: "escape", path: "/candidate/%2e%2e/invitations"},
|
||||
{name: "dot segment", path: "/candidate/../invitations"},
|
||||
{name: "too long", path: "/" + strings.Repeat("a", 300)},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -138,32 +244,112 @@ func TestNewInboxContentRejectsExternalOrAmbiguousLinks(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := NewInboxContent("Приглашение", "Проверьте новое приглашение.", "/candidate/invitations/01928f7a-3c4d-7e5f-8a9b-0c1d2e3f4a5b"); err != nil {
|
||||
t.Fatalf("NewInboxContent(same-origin path) error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func newInboxTestService(
|
||||
t *testing.T,
|
||||
repository outbound.NotificationInboxRepository,
|
||||
resolver InboxContentResolver,
|
||||
clock func() time.Time,
|
||||
) *InboxService {
|
||||
func TestSyntheticNoticeResolverFollowsTheRecipientStanding(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fixture := newInboxFixture(t, domain.ScenarioCandidate)
|
||||
entry := fixture.add(t, fixture.entryInput())
|
||||
unit := fixture.unit()
|
||||
content, err := ResolveSyntheticNotice(context.Background(), unit, entry)
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveSyntheticNotice(visible) error = %v", err)
|
||||
}
|
||||
if content.Title() != syntheticNoticeTitle || content.LinkPath() != "" || content.Summary() == "" {
|
||||
t.Fatalf("synthetic content = %+v", content)
|
||||
}
|
||||
fixture.repository.organizationName = ""
|
||||
if _, err := ResolveSyntheticNotice(context.Background(), unit, entry); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("ResolveSyntheticNotice(not visible) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
resolvers := NewInboxContentResolver(DefaultInboxSourceResolvers(true))
|
||||
invitation := fixtureEntry(t, domain.NotificationInboxEntryInput{
|
||||
EventID: uuid.New(), RecipientUserID: fixture.user.ID, OrganizationID: fixture.organizationID,
|
||||
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceInvitation, SourceID: uuid.New(),
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
})
|
||||
if _, err := resolvers.Resolve(context.Background(), unit, invitation); !errors.Is(err, ErrInboxContentUnavailable) {
|
||||
t.Fatalf("Resolve(source without a resolver) error = %v, want ErrInboxContentUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
type inboxFixture struct {
|
||||
service *InboxService
|
||||
repository *memoryInboxRepository
|
||||
resolver *memoryInboxResolver
|
||||
users map[uuid.UUID]domain.User
|
||||
memberships map[uuid.UUID]domain.Membership
|
||||
user domain.User
|
||||
organizationID uuid.UUID
|
||||
locks int
|
||||
}
|
||||
|
||||
func newInboxFixture(t *testing.T, scenario domain.Scenario) *inboxFixture {
|
||||
t.Helper()
|
||||
service, err := NewInboxService(repository, resolver, clock)
|
||||
return newInboxFixtureWithClock(t, scenario, time.Now)
|
||||
}
|
||||
|
||||
func newInboxFixtureWithClock(t *testing.T, scenario domain.Scenario, clock func() time.Time) *inboxFixture {
|
||||
t.Helper()
|
||||
verifiedAt := time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC)
|
||||
fixture := &inboxFixture{
|
||||
repository: &memoryInboxRepository{organizationName: "Север"},
|
||||
resolver: &memoryInboxResolver{unavailable: map[uuid.UUID]bool{}},
|
||||
users: map[uuid.UUID]domain.User{},
|
||||
memberships: map[uuid.UUID]domain.Membership{},
|
||||
user: domain.User{ID: uuid.New(), Scenario: scenario, EmailVerifiedAt: &verifiedAt},
|
||||
organizationID: uuid.New(),
|
||||
}
|
||||
fixture.users[fixture.user.ID] = fixture.user
|
||||
service, err := NewInboxService(inboxTestTransactor{fixture: fixture}, fixture.resolver, clock)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInboxService() error = %v", err)
|
||||
}
|
||||
return service
|
||||
fixture.service = service
|
||||
return fixture
|
||||
}
|
||||
|
||||
func inboxTestEntryInput() domain.NotificationInboxEntryInput {
|
||||
func (fixture *inboxFixture) unit() outbound.UnitOfWork { return inboxTestUnit{fixture: fixture} }
|
||||
|
||||
func (fixture *inboxFixture) entryInput() domain.NotificationInboxEntryInput {
|
||||
return fixtureEntryInput(fixture.user.ID, fixture.organizationID)
|
||||
}
|
||||
|
||||
func (fixture *inboxFixture) add(t *testing.T, input domain.NotificationInboxEntryInput) domain.NotificationInboxEntry {
|
||||
t.Helper()
|
||||
entry := fixtureEntry(t, input)
|
||||
fixture.repository.entries = append(fixture.repository.entries, entry)
|
||||
return entry
|
||||
}
|
||||
|
||||
func (fixture *inboxFixture) addMembership(t *testing.T, role domain.OrganizationRole, state domain.MembershipState) {
|
||||
t.Helper()
|
||||
now := time.Date(2026, time.October, 1, 10, 0, 0, 0, time.UTC)
|
||||
membership, err := domain.RestoreMembership(domain.StoredMembership{
|
||||
ID: uuid.New(), OrganizationID: fixture.organizationID, UserID: fixture.user.ID,
|
||||
Role: string(role), State: string(state), CreatedAt: now, UpdatedAt: now,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreMembership() error = %v", err)
|
||||
}
|
||||
fixture.memberships[fixture.user.ID] = membership
|
||||
}
|
||||
|
||||
func fixtureEntryInput(recipientID, organizationID uuid.UUID) domain.NotificationInboxEntryInput {
|
||||
return domain.NotificationInboxEntryInput{
|
||||
EventID: uuid.New(), RecipientUserID: uuid.New(), OrganizationID: uuid.New(),
|
||||
EventType: domain.NotificationInboxInvitationDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceInvitation, SourceID: uuid.New(),
|
||||
EventID: uuid.New(), RecipientUserID: recipientID, OrganizationID: organizationID,
|
||||
EventType: domain.NotificationInboxSyntheticDelivered, SchemaVersion: 1,
|
||||
SourceType: domain.NotificationInboxSourceOrganization, SourceID: organizationID,
|
||||
OccurredAt: time.Date(2026, time.October, 8, 12, 0, 0, 0, time.UTC),
|
||||
}
|
||||
}
|
||||
|
||||
func mustInboxTestEntry(t *testing.T, input domain.NotificationInboxEntryInput) domain.NotificationInboxEntry {
|
||||
func fixtureEntry(t *testing.T, input domain.NotificationInboxEntryInput) domain.NotificationInboxEntry {
|
||||
t.Helper()
|
||||
entry, err := domain.NewNotificationInboxEntry(input)
|
||||
if err != nil {
|
||||
@@ -172,14 +358,71 @@ func mustInboxTestEntry(t *testing.T, input domain.NotificationInboxEntryInput)
|
||||
return entry
|
||||
}
|
||||
|
||||
func uuidPointer(value uuid.UUID) *uuid.UUID { return &value }
|
||||
type inboxTestTransactor struct{ fixture *inboxFixture }
|
||||
|
||||
func (transactor inboxTestTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
||||
return fn(ctx, transactor.fixture.unit())
|
||||
}
|
||||
|
||||
type inboxTestUnit struct {
|
||||
outbound.UnitOfWork
|
||||
|
||||
fixture *inboxFixture
|
||||
}
|
||||
|
||||
func (unit inboxTestUnit) Users() outbound.UserRepository {
|
||||
return inboxTestUsers{fixture: unit.fixture}
|
||||
}
|
||||
func (unit inboxTestUnit) Organizations() outbound.OrganizationRepository {
|
||||
return inboxTestOrganizations{fixture: unit.fixture}
|
||||
}
|
||||
func (unit inboxTestUnit) NotificationInbox() outbound.NotificationInboxRepository {
|
||||
return unit.fixture.repository
|
||||
}
|
||||
|
||||
type inboxTestUsers struct {
|
||||
outbound.UserRepository
|
||||
|
||||
fixture *inboxFixture
|
||||
}
|
||||
|
||||
func (repository inboxTestUsers) GetByID(_ context.Context, id uuid.UUID) (domain.User, error) {
|
||||
user, ok := repository.fixture.users[id]
|
||||
if !ok {
|
||||
return domain.User{}, outbound.ErrUserNotFound
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
type inboxTestOrganizations struct {
|
||||
outbound.OrganizationRepository
|
||||
|
||||
fixture *inboxFixture
|
||||
}
|
||||
|
||||
// LockActiveMembership answers like the real repository: only an active
|
||||
// membership of the account in the organization is returned.
|
||||
func (repository inboxTestOrganizations) LockActiveMembership(_ context.Context, organizationID, userID uuid.UUID) (domain.Membership, error) {
|
||||
repository.fixture.locks++
|
||||
membership, ok := repository.fixture.memberships[userID]
|
||||
if !ok || membership.OrganizationID() != organizationID || !membership.GrantsAccess() {
|
||||
return domain.Membership{}, outbound.ErrOrganizationNotFound
|
||||
}
|
||||
return membership, nil
|
||||
}
|
||||
|
||||
type memoryInboxRepository struct {
|
||||
entries []domain.NotificationInboxEntry
|
||||
ignoreListScope bool
|
||||
entries []domain.NotificationInboxEntry
|
||||
ignoreListScope bool
|
||||
listCalls int
|
||||
organizationName string
|
||||
storeErr error
|
||||
}
|
||||
|
||||
func (repository *memoryInboxRepository) Store(_ context.Context, entry domain.NotificationInboxEntry) (bool, error) {
|
||||
if repository.storeErr != nil {
|
||||
return false, repository.storeErr
|
||||
}
|
||||
for _, existing := range repository.entries {
|
||||
if existing.RecipientUserID() != entry.RecipientUserID() || existing.EventID() != entry.EventID() {
|
||||
continue
|
||||
@@ -194,6 +437,7 @@ func (repository *memoryInboxRepository) Store(_ context.Context, entry domain.N
|
||||
}
|
||||
|
||||
func (repository *memoryInboxRepository) List(_ context.Context, query outbound.NotificationInboxListQuery) (outbound.NotificationInboxPage, error) {
|
||||
repository.listCalls++
|
||||
page := outbound.NotificationInboxPage{}
|
||||
for _, entry := range repository.entries {
|
||||
if !repository.ignoreListScope && (entry.RecipientUserID() != query.RecipientUserID || (query.OrganizationID != nil && entry.OrganizationID() != *query.OrganizationID)) {
|
||||
@@ -207,12 +451,13 @@ func (repository *memoryInboxRepository) List(_ context.Context, query outbound.
|
||||
func (repository *memoryInboxRepository) MarkRead(
|
||||
_ context.Context,
|
||||
recipientUserID uuid.UUID,
|
||||
organizationID uuid.UUID,
|
||||
organizationID *uuid.UUID,
|
||||
eventID uuid.UUID,
|
||||
at time.Time,
|
||||
) (domain.NotificationInboxEntry, error) {
|
||||
for index, entry := range repository.entries {
|
||||
if entry.RecipientUserID() != recipientUserID || entry.OrganizationID() != organizationID || entry.EventID() != eventID {
|
||||
if entry.RecipientUserID() != recipientUserID || entry.EventID() != eventID ||
|
||||
(organizationID != nil && entry.OrganizationID() != *organizationID) {
|
||||
continue
|
||||
}
|
||||
read, err := entry.MarkRead(at)
|
||||
@@ -225,6 +470,13 @@ func (repository *memoryInboxRepository) MarkRead(
|
||||
return domain.NotificationInboxEntry{}, outbound.ErrNotificationInboxEntryNotFound
|
||||
}
|
||||
|
||||
func (repository *memoryInboxRepository) OrganizationNameForRecipient(context.Context, uuid.UUID, uuid.UUID) (string, error) {
|
||||
if repository.organizationName == "" {
|
||||
return "", outbound.ErrNotificationInboxSourceNotVisible
|
||||
}
|
||||
return repository.organizationName, nil
|
||||
}
|
||||
|
||||
func sameInboxEvent(left, right domain.NotificationInboxEntry) bool {
|
||||
return left.OrganizationID() == right.OrganizationID() && left.EventType() == right.EventType() &&
|
||||
left.SchemaVersion() == right.SchemaVersion() && left.SourceType() == right.SourceType() &&
|
||||
@@ -236,7 +488,7 @@ type memoryInboxResolver struct {
|
||||
calls int
|
||||
}
|
||||
|
||||
func (resolver *memoryInboxResolver) Resolve(_ context.Context, entry domain.NotificationInboxEntry) (InboxContent, error) {
|
||||
func (resolver *memoryInboxResolver) Resolve(_ context.Context, _ outbound.UnitOfWork, entry domain.NotificationInboxEntry) (InboxContent, error) {
|
||||
resolver.calls++
|
||||
if resolver.unavailable[entry.EventID()] {
|
||||
return InboxContent{}, ErrInboxContentUnavailable
|
||||
|
||||
Reference in new issue
Block a user