owpengram-server/internal/app/contacts/service_test.go

434 lines
15 KiB
Go

package contacts
import (
"context"
"errors"
"reflect"
"testing"
"telesrv/internal/domain"
"telesrv/internal/store"
"telesrv/internal/store/memory"
)
type serviceCountingContactStore struct {
store.ContactStore
listCalls int
}
func (s *serviceCountingContactStore) ListByUser(ctx context.Context, userID int64) (domain.ContactList, error) {
s.listCalls++
return s.ContactStore.ListByUser(ctx, userID)
}
type serviceCountingUserStore struct {
store.UserStore
byIDsCalls int
}
func (s *serviceCountingUserStore) ByIDs(ctx context.Context, ids []int64) ([]domain.User, error) {
s.byIDsCalls++
return s.UserStore.ByIDs(ctx, ids)
}
type fakeReadModelVersions struct {
hash int64
found bool
}
func (f *fakeReadModelVersions) ReadModelHash(_ context.Context, _ string, _ int64, _ domain.PeerType, _ int64) (int64, bool, error) {
return f.hash, f.found, nil
}
func (f *fakeReadModelVersions) ReadModelHashes(ctx context.Context, keys []store.ReadModelKey) (map[store.ReadModelKey]int64, error) {
out := make(map[store.ReadModelKey]int64, len(keys))
for _, key := range keys {
hash, found, err := f.ReadModelHash(ctx, key.Model, key.OwnerUserID, key.PeerType, key.PeerID)
if err != nil {
return nil, err
}
if found {
out[key] = hash
}
}
return out, nil
}
func TestGetContactsReturnsNotModifiedFromReadModelHashWithoutLoadingList(t *testing.T) {
ctx := context.Background()
base := memory.NewContactStore()
counting := &serviceCountingContactStore{ContactStore: base}
versions := &fakeReadModelVersions{hash: 99101, found: true}
svc := NewService(counting).Configure(WithReadModelVersions(versions))
list, notModified, err := svc.GetContacts(ctx, 1, versions.hash)
if err != nil {
t.Fatalf("GetContacts: %v", err)
}
if !notModified {
t.Fatalf("notModified = false, want true")
}
if list.Hash != versions.hash {
t.Fatalf("notModified list hash = %d, want %d", list.Hash, versions.hash)
}
if counting.listCalls != 0 {
t.Fatalf("ListByUser calls = %d, want 0 on hash hit", counting.listCalls)
}
}
func TestGetContactsCachesProjectedReadModelAndRejectsStaleHash(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
owner, err := users.Create(ctx, domain.User{Phone: "100", FirstName: "Owner"})
if err != nil {
t.Fatalf("create owner: %v", err)
}
target, err := users.Create(ctx, domain.User{Phone: "101", FirstName: "Target"})
if err != nil {
t.Fatalf("create target: %v", err)
}
base := memory.NewContactStore()
if _, err := base.Upsert(ctx, owner.ID, domain.ContactInput{ContactUserID: target.ID, FirstName: "Saved"}); err != nil {
t.Fatalf("upsert contact: %v", err)
}
counting := &serviceCountingContactStore{ContactStore: base}
countingUsers := &serviceCountingUserStore{UserStore: users}
versions := &fakeReadModelVersions{hash: 12345, found: true}
svc := NewService(counting, countingUsers).Configure(WithReadModelVersions(versions))
first, notModified, err := svc.GetContacts(ctx, owner.ID, 0)
if err != nil || notModified {
t.Fatalf("first GetContacts notModified=%v err=%v", notModified, err)
}
if first.Hash != versions.hash || len(first.Contacts) != 1 {
t.Fatalf("first result hash=%d contacts=%d, want hash %d and one contact", first.Hash, len(first.Contacts), versions.hash)
}
second, notModified, err := svc.GetContacts(ctx, owner.ID, 0)
if err != nil || notModified {
t.Fatalf("second GetContacts notModified=%v err=%v", notModified, err)
}
if second.Hash != versions.hash || counting.listCalls != 1 {
t.Fatalf("second result hash=%d listCalls=%d, want cached hash %d and one load", second.Hash, counting.listCalls, versions.hash)
}
versions.hash = 67890
third, notModified, err := svc.GetContacts(ctx, owner.ID, 12345)
if err != nil || notModified {
t.Fatalf("third GetContacts notModified=%v err=%v", notModified, err)
}
if third.Hash != versions.hash {
t.Fatalf("third hash = %d, want new read-model hash %d", third.Hash, versions.hash)
}
if counting.listCalls != 2 {
t.Fatalf("ListByUser calls after hash change = %d, want 2", counting.listCalls)
}
if countingUsers.byIDsCalls != 0 {
t.Fatalf("Users.ByIDs calls = %d, want 0; presence must stay out of contact read model", countingUsers.byIDsCalls)
}
}
func TestImportContactsBatchesPhonesAndDedupesUpserts(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
owner, err := users.Create(ctx, domain.User{Phone: "100", FirstName: "Owner"})
if err != nil {
t.Fatalf("create owner: %v", err)
}
target, err := users.Create(ctx, domain.User{Phone: "15551234567", FirstName: "Alice"})
if err != nil {
t.Fatalf("create target: %v", err)
}
svc := NewService(contactsStore, users).Configure(WithPhotoProvider(contactProfilePhotos{
target.ID: {PhotoID: 9400, DCID: 2},
}))
res, err := svc.ImportContacts(ctx, owner.ID, []domain.ContactInput{
{ClientID: 11, Phone: "+1 (555) 123-4567", FirstName: "A"},
{ClientID: 12, Phone: "15551234567", FirstName: "Alice Final"},
})
if err != nil {
t.Fatalf("ImportContacts: %v", err)
}
if len(res.Imported) != 2 {
t.Fatalf("imported = %d, want 2", len(res.Imported))
}
if res.Imported[0].UserID != target.ID || res.Imported[1].UserID != target.ID {
t.Fatalf("imported user ids = %+v, want target %d", res.Imported, target.ID)
}
if len(res.Contacts) != 1 {
t.Fatalf("contacts = %d, want 1 deduped upsert", len(res.Contacts))
}
if res.Contacts[0].FirstName != "Alice Final" {
t.Fatalf("contact first name = %q, want final input", res.Contacts[0].FirstName)
}
if res.Contacts[0].User.PhotoID != 9400 || res.Contacts[0].User.PhotoDCID != 2 {
t.Fatalf("imported contact photo = id %d dc %d, want 9400/2", res.Contacts[0].User.PhotoID, res.Contacts[0].User.PhotoDCID)
}
}
func TestGetContactsProjectsCurrentProfilePhoto(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
owner, err := users.Create(ctx, domain.User{Phone: "100", FirstName: "Owner"})
if err != nil {
t.Fatalf("create owner: %v", err)
}
target, err := users.Create(ctx, domain.User{Phone: "15551234567", FirstName: "Alice"})
if err != nil {
t.Fatalf("create target: %v", err)
}
if _, err := contactsStore.Upsert(ctx, owner.ID, domain.ContactInput{
ContactUserID: target.ID,
Phone: "1111",
FirstName: "Alice",
LastName: "Saved",
}); err != nil {
t.Fatalf("upsert contact: %v", err)
}
svc := NewService(contactsStore, users).Configure(WithPhotoProvider(contactProfilePhotos{
target.ID: {PhotoID: 9401, DCID: 2, Stripped: []byte{11, 12}},
}))
list, notModified, err := svc.GetContacts(ctx, owner.ID, 0)
if err != nil {
t.Fatalf("GetContacts: %v", err)
}
if notModified || len(list.Contacts) != 1 {
t.Fatalf("contacts notModified=%v len=%d, want one full contact", notModified, len(list.Contacts))
}
contact := list.Contacts[0]
if contact.User.PhotoID != 9401 || contact.User.PhotoDCID != 2 || string(contact.User.PhotoStripped) != string([]byte{11, 12}) {
t.Fatalf("contact user photo = id %d dc %d stripped %v, want 9401/2/[11 12]", contact.User.PhotoID, contact.User.PhotoDCID, contact.User.PhotoStripped)
}
if contact.User.FirstName != "Alice" || contact.User.LastName != "Saved" || contact.User.Phone != "1111" {
t.Fatalf("contact user projection = %+v, want contact name/phone", contact.User)
}
}
func TestEditCloseFriendsReplacesOwnerContactFlags(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
owner, err := users.Create(ctx, domain.User{Phone: "100", FirstName: "Owner"})
if err != nil {
t.Fatalf("create owner: %v", err)
}
bob, err := users.Create(ctx, domain.User{Phone: "101", FirstName: "Bob"})
if err != nil {
t.Fatalf("create bob: %v", err)
}
carol, err := users.Create(ctx, domain.User{Phone: "102", FirstName: "Carol"})
if err != nil {
t.Fatalf("create carol: %v", err)
}
bot, err := users.Create(ctx, domain.User{Phone: "103", FirstName: "Bot", Bot: true, BotInfoVersion: 1})
if err != nil {
t.Fatalf("create bot: %v", err)
}
for _, user := range []domain.User{bob, carol, bot} {
if _, err := contactsStore.Upsert(ctx, owner.ID, domain.ContactInput{ContactUserID: user.ID, FirstName: user.FirstName}); err != nil {
t.Fatalf("upsert contact %d: %v", user.ID, err)
}
}
svc := NewService(contactsStore, users)
result, err := svc.EditCloseFriends(ctx, owner.ID, []int64{bob.ID, bob.ID, 0, owner.ID, bot.ID, 999999})
if err != nil {
t.Fatalf("EditCloseFriends first: %v", err)
}
if got, want := result.AddedUserIDs, []int64{bob.ID}; !reflect.DeepEqual(got, want) {
t.Fatalf("first added = %v, want %v", got, want)
}
if len(result.RemovedUserIDs) != 0 {
t.Fatalf("first removed = %v, want empty", result.RemovedUserIDs)
}
list, notModified, err := svc.GetContacts(ctx, owner.ID, 0)
if err != nil || notModified {
t.Fatalf("GetContacts after first edit notModified=%v err=%v", notModified, err)
}
if !contactByID(t, list, bob.ID).CloseFriend || !contactByID(t, list, bob.ID).User.CloseFriend {
t.Fatalf("bob close friend projection = %+v, want true", contactByID(t, list, bob.ID))
}
if contactByID(t, list, carol.ID).CloseFriend || contactByID(t, list, bot.ID).CloseFriend {
t.Fatalf("carol/bot close friend flags = %+v / %+v, want false", contactByID(t, list, carol.ID), contactByID(t, list, bot.ID))
}
result, err = svc.EditCloseFriends(ctx, owner.ID, []int64{carol.ID})
if err != nil {
t.Fatalf("EditCloseFriends replace: %v", err)
}
if got, want := result.AddedUserIDs, []int64{carol.ID}; !reflect.DeepEqual(got, want) {
t.Fatalf("replace added = %v, want %v", got, want)
}
if got, want := result.RemovedUserIDs, []int64{bob.ID}; !reflect.DeepEqual(got, want) {
t.Fatalf("replace removed = %v, want %v", got, want)
}
replaced, _, err := svc.GetContacts(ctx, owner.ID, 0)
if err != nil {
t.Fatalf("GetContacts after replace: %v", err)
}
if contactByID(t, replaced, bob.ID).CloseFriend || !contactByID(t, replaced, carol.ID).CloseFriend {
t.Fatalf("replace flags bob=%+v carol=%+v, want bob false carol true", contactByID(t, replaced, bob.ID), contactByID(t, replaced, carol.ID))
}
}
func TestAcceptContactSharesPhoneAndClearsShareContact(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
alice, err := users.Create(ctx, domain.User{Phone: "15550000001", FirstName: "Alice", LastName: "A"})
if err != nil {
t.Fatalf("create alice: %v", err)
}
bob, err := users.Create(ctx, domain.User{Phone: "15550000002", FirstName: "Bob", LastName: "B"})
if err != nil {
t.Fatalf("create bob: %v", err)
}
svc := NewService(contactsStore, users)
if _, err := svc.AddContact(ctx, alice.ID, domain.ContactInput{
ContactUserID: bob.ID,
Phone: bob.Phone,
FirstName: "Bobby",
LastName: "Remark",
}); err != nil {
t.Fatalf("alice add bob: %v", err)
}
settings, err := svc.GetPeerSettings(ctx, alice.ID, domain.Peer{Type: domain.PeerTypeUser, ID: bob.ID})
if err != nil {
t.Fatalf("alice peer settings before accept: %v", err)
}
if !settings.ShareContact {
t.Fatalf("alice settings before accept = %+v, want share contact", settings)
}
contact, err := svc.AcceptContact(ctx, alice.ID, bob.ID)
if err != nil {
t.Fatalf("AcceptContact: %v", err)
}
if !contact.Mutual || !contact.User.Mutual {
t.Fatalf("accepted contact = %+v, want mutual", contact)
}
aliceSettings, err := svc.GetPeerSettings(ctx, alice.ID, domain.Peer{Type: domain.PeerTypeUser, ID: bob.ID})
if err != nil {
t.Fatalf("alice peer settings after accept: %v", err)
}
if aliceSettings.ShareContact || aliceSettings.AddContact {
t.Fatalf("alice settings after accept = %+v, want no share/add", aliceSettings)
}
bobSettings, err := svc.GetPeerSettings(ctx, bob.ID, domain.Peer{Type: domain.PeerTypeUser, ID: alice.ID})
if err != nil {
t.Fatalf("bob peer settings after accept: %v", err)
}
if bobSettings.ShareContact || bobSettings.AddContact {
t.Fatalf("bob settings after accept = %+v, want no share/add", bobSettings)
}
reverse, found, err := contactsStore.Get(ctx, bob.ID, alice.ID)
if err != nil || !found {
t.Fatalf("bob contact alice found=%v err=%v", found, err)
}
if reverse.Phone != alice.Phone || reverse.FirstName != alice.FirstName || reverse.LastName != alice.LastName || !reverse.Mutual {
t.Fatalf("bob contact alice = %+v, want alice phone/name and mutual", reverse)
}
repeated, err := svc.AcceptContact(ctx, alice.ID, bob.ID)
if err != nil {
t.Fatalf("AcceptContact repeat: %v", err)
}
if !repeated.Mutual {
t.Fatalf("repeated accept = %+v, want mutual", repeated)
}
}
func TestAddContactNormalizesPhoneToDigits(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
alice, err := users.Create(ctx, domain.User{Phone: "15550060301", FirstName: "Alice", LastName: "A"})
if err != nil {
t.Fatalf("create alice: %v", err)
}
bob, err := users.Create(ctx, domain.User{Phone: "15550060302", FirstName: "Bob", LastName: "B"})
if err != nil {
t.Fatalf("create bob: %v", err)
}
svc := NewService(contactsStore, users)
contact, err := svc.AddContact(ctx, alice.ID, domain.ContactInput{
ContactUserID: bob.ID,
Phone: "+1 555-006-0302",
FirstName: "Bob B",
})
if err != nil {
t.Fatalf("AddContact: %v", err)
}
if contact.Phone != "15550060302" {
t.Fatalf("contact phone = %q, want digits-only 15550060302", contact.Phone)
}
if contact.User.Phone != "15550060302" {
t.Fatalf("projected user phone = %q, want digits-only 15550060302", contact.User.Phone)
}
stored, found, err := contactsStore.Get(ctx, alice.ID, bob.ID)
if err != nil || !found {
t.Fatalf("stored contact found=%v err=%v", found, err)
}
if stored.Phone != "15550060302" {
t.Fatalf("stored contact phone = %q, want digits-only 15550060302", stored.Phone)
}
emptied, err := svc.AddContact(ctx, alice.ID, domain.ContactInput{
ContactUserID: bob.ID,
Phone: "+",
FirstName: "Bob B",
})
if err != nil {
t.Fatalf("AddContact digitless phone: %v", err)
}
if emptied.Phone != bob.Phone {
t.Fatalf("digitless phone contact = %q, want fallback to target phone %q", emptied.Phone, bob.Phone)
}
}
func TestAcceptContactRequiresExistingContactRequest(t *testing.T) {
ctx := context.Background()
users := memory.NewUserStore()
contactsStore := memory.NewContactStore()
alice, err := users.Create(ctx, domain.User{Phone: "15550000001", FirstName: "Alice"})
if err != nil {
t.Fatalf("create alice: %v", err)
}
bob, err := users.Create(ctx, domain.User{Phone: "15550000002", FirstName: "Bob"})
if err != nil {
t.Fatalf("create bob: %v", err)
}
svc := NewService(contactsStore, users)
if _, err := svc.AcceptContact(ctx, alice.ID, bob.ID); !errors.Is(err, ErrContactReqMissing) {
t.Fatalf("AcceptContact without contact err = %v, want ErrContactReqMissing", err)
}
}
func contactByID(t *testing.T, list domain.ContactList, id int64) domain.Contact {
t.Helper()
for _, contact := range list.Contacts {
if contact.User.ID == id {
return contact
}
}
t.Fatalf("contact %d not found in %+v", id, list.Contacts)
return domain.Contact{}
}
type contactProfilePhotos map[int64]domain.ProfilePhotoRef
func (p contactProfilePhotos) CurrentProfilePhotos(_ context.Context, _ domain.PeerType, ids []int64) (map[int64]domain.ProfilePhotoRef, error) {
out := make(map[int64]domain.ProfilePhotoRef, len(ids))
for _, id := range ids {
if ref, ok := p[id]; ok {
out[id] = ref
}
}
return out, nil
}