business: fix contact projection and phone sharing

(cherry picked from commit c0a0e5b52240ed415d3b43ba77659821887bf50b)
This commit is contained in:
A 2026-06-07 23:36:06 +08:00
parent d84fa6e126
commit 860e581d06
18 changed files with 703 additions and 36 deletions

View file

@ -2,6 +2,7 @@ package contacts
import (
"context"
"errors"
"testing"
"telesrv/internal/domain"
@ -42,3 +43,90 @@ func TestImportContactsBatchesPhonesAndDedupesUpserts(t *testing.T) {
t.Fatalf("contact first name = %q, want final input", res.Contacts[0].FirstName)
}
}
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 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)
}
}