Files

92 lines
3.7 KiB
Go

package postgres
import (
"encoding/json"
"testing"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
func TestConsentRevocationEventsStayBoundedForLargeContactHistory(t *testing.T) {
const historySize = 500
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
subject := uuid.New()
organization := uuid.New()
revocation, err := domain.NewConsentRevocation(domain.ConsentRevocationInput{
SubjectID: subject, Purpose: domain.ConsentPurposeContactDisclosure, OrganizationID: organization,
Source: domain.ConsentRevocationSourceConsentSettings, RevokedAt: now,
})
if err != nil {
t.Fatal(err)
}
fields, err := domain.NewContactFieldSet(domain.ContactFieldPhone)
if err != nil {
t.Fatal(err)
}
fieldCodes := make([]string, 0, len(fields.Fields()))
for _, field := range fields.Fields() {
fieldCodes = append(fieldCodes, string(field))
}
hash := domain.HashConsentText("c4 text with the stored organization snapshot")
records := make([]domain.ConsentRecord, 0, historySize)
legacyFacts := make([]uuid.UUID, 0, historySize)
for index := 0; index < historySize; index++ {
id := uuid.New()
legacyFacts = append(legacyFacts, id)
record, err := domain.RestoreConsentRecord(domain.StoredConsentRecord{
ID: id, SubjectID: subject, Purpose: string(domain.ConsentPurposeContactDisclosure),
TextVersion: "c4-2026-10-01", TextHash: hash[:], Source: string(domain.ConsentSourceInvitationAcceptance),
State: string(domain.ConsentStateActive), GrantedAt: now.Add(-time.Duration(index+1) * time.Minute),
OrganizationID: organization, OrganizationName: "Acme Synthetic LLC", FieldSet: fieldCodes,
InvitationID: uuid.New(), InvitationTermsVersion: 1,
})
if err != nil {
t.Fatalf("RestoreConsentRecord() error = %v", err)
}
records = append(records, record)
}
legacyPayload, err := json.Marshal(map[string]any{"records": legacyFacts})
if err != nil {
t.Fatal(err)
}
if len(legacyPayload) <= domain.MaxDomainEventPayloadBytes {
t.Fatalf("legacy aggregate fixture has %d bytes; expected it to exceed %d", len(legacyPayload), domain.MaxDomainEventPayloadBytes)
}
events, err := newConsentRevocationEvents(revocation, records)
if err != nil {
t.Fatalf("newConsentRevocationEvents() error = %v", err)
}
if len(events) != historySize {
t.Fatalf("event count = %d, want one for every revoked record (%d)", len(events), historySize)
}
seen := make(map[uuid.UUID]bool, historySize)
for _, event := range events {
if event.AggregateType() != "consent_record" || event.AggregateVersion() != 2 || event.EventType() != "consent.revoked" {
t.Fatalf("event identity = %s/%s/%d, want one revoked record version", event.EventType(), event.AggregateType(), event.AggregateVersion())
}
if len(event.Payload()) > domain.MaxDomainEventPayloadBytes {
t.Fatalf("event payload has %d bytes, above the limit", len(event.Payload()))
}
var payload struct {
RecordID uuid.UUID `json:"record_id"`
SubjectID uuid.UUID `json:"subject_id"`
Purpose domain.ConsentPurpose `json:"purpose"`
OrganizationID uuid.UUID `json:"organization_id"`
Fields []domain.ContactField `json:"fields"`
}
if err := json.Unmarshal(event.Payload(), &payload); err != nil {
t.Fatalf("decode event payload: %v", err)
}
if event.AggregateID() != payload.RecordID || payload.RecordID == uuid.Nil || seen[payload.RecordID] ||
payload.SubjectID != subject || payload.Purpose != domain.ConsentPurposeContactDisclosure ||
payload.OrganizationID != organization || len(payload.Fields) != 1 || payload.Fields[0] != domain.ContactFieldPhone {
t.Fatalf("event payload lost or changed a stored revocation fact: %+v", payload)
}
seen[payload.RecordID] = true
}
}