Spaces:
Paused
Paused
File size: 6,415 Bytes
83a59ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | package services
import (
"context"
"errors"
"testing"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/repositories"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/NdoleStudio/stacktrace"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
)
type messageThreadContactRepositoryStub struct {
repositories.MessageThreadRepository
threads []entities.MessageThread
err error
calls int
}
func (stub *messageThreadContactRepositoryStub) Index(_ context.Context, _ entities.UserID, _ string, _ bool, _ bool, _ repositories.IndexParams) (*[]entities.MessageThread, error) {
stub.calls++
if stub.err != nil {
return nil, stub.err
}
threads := make([]entities.MessageThread, len(stub.threads))
copy(threads, stub.threads)
return &threads, nil
}
type messageThreadContactProviderStub struct {
contacts map[string]*entities.Contact
err error
calls int
userID entities.UserID
numbers []string
}
func (stub *messageThreadContactProviderStub) GetContactMap(_ context.Context, userID entities.UserID, phoneNumbers []string) (map[string]*entities.Contact, error) {
stub.calls++
stub.userID = userID
stub.numbers = append([]string{}, phoneNumbers...)
return stub.contacts, stub.err
}
type messageThreadContactLogger struct {
errors []error
}
var _ telemetry.Logger = (*messageThreadContactLogger)(nil)
func (logger *messageThreadContactLogger) Error(err error) {
logger.errors = append(logger.errors, err)
}
func (logger *messageThreadContactLogger) WithService(string) telemetry.Logger { return logger }
func (logger *messageThreadContactLogger) WithString(string, string) telemetry.Logger { return logger }
func (logger *messageThreadContactLogger) WithSpan(trace.Span) telemetry.Logger { return logger }
func (logger *messageThreadContactLogger) Trace(string) {}
func (logger *messageThreadContactLogger) Info(string) {}
func (logger *messageThreadContactLogger) Warn(error) {}
func (logger *messageThreadContactLogger) Debug(string) {}
func (logger *messageThreadContactLogger) Fatal(error) {}
func (logger *messageThreadContactLogger) Printf(string, ...interface{}) {}
func newMessageThreadContactServiceForTest(repository repositories.MessageThreadRepository, provider contactMapProvider, logger telemetry.Logger) *MessageThreadService {
if logger == nil {
logger = &noopLogger{}
}
tracer := telemetry.NewOtelLogger("test", logger)
return NewMessageThreadService(logger, tracer, repository, nil, nil, provider)
}
func TestGetThreads_SkipsContactLookupWhenFlagOff(t *testing.T) {
repository := &messageThreadContactRepositoryStub{threads: []entities.MessageThread{{Contact: "+18005550199"}}}
provider := &messageThreadContactProviderStub{contacts: map[string]*entities.Contact{
"+18005550199": {Name: "Alice"},
}}
service := newMessageThreadContactServiceForTest(repository, provider, nil)
threads, err := service.GetThreads(context.Background(), MessageThreadGetParams{UserID: entities.UserID("user-id"), WithContacts: false})
require.NoError(t, err)
require.Len(t, *threads, 1)
assert.Nil(t, (*threads)[0].ContactDetails)
assert.Equal(t, 0, provider.calls)
}
func TestGetThreads_AttachesContactDetailsWhenFlagOn(t *testing.T) {
alice := &entities.Contact{ID: uuid.New(), Name: "Alice", PhoneNumbers: []string{"+18005550199"}}
repository := &messageThreadContactRepositoryStub{threads: []entities.MessageThread{
{Contact: "+18005550199"},
{Contact: "+18005550100"},
}}
provider := &messageThreadContactProviderStub{contacts: map[string]*entities.Contact{
"+18005550199": alice,
}}
service := newMessageThreadContactServiceForTest(repository, provider, nil)
threads, err := service.GetThreads(context.Background(), MessageThreadGetParams{UserID: entities.UserID("user-id"), WithContacts: true})
require.NoError(t, err)
require.Len(t, *threads, 2)
assert.Equal(t, 1, provider.calls)
assert.Equal(t, entities.UserID("user-id"), provider.userID)
assert.Equal(t, []string{"+18005550199", "+18005550100"}, provider.numbers)
require.NotNil(t, (*threads)[0].ContactDetails)
assert.Same(t, alice, (*threads)[0].ContactDetails)
assert.Equal(t, "Alice", (*threads)[0].ContactDetails.Name)
assert.Nil(t, (*threads)[1].ContactDetails)
}
func TestGetThreads_SkipsContactLookupWhenNoThreads(t *testing.T) {
repository := &messageThreadContactRepositoryStub{threads: []entities.MessageThread{}}
provider := &messageThreadContactProviderStub{contacts: map[string]*entities.Contact{}}
service := newMessageThreadContactServiceForTest(repository, provider, nil)
threads, err := service.GetThreads(context.Background(), MessageThreadGetParams{UserID: entities.UserID("user-id"), WithContacts: true})
require.NoError(t, err)
require.Empty(t, *threads)
assert.Equal(t, 0, provider.calls)
}
func TestGetThreads_LogsContactMapErrorAndReturnsThreads(t *testing.T) {
repository := &messageThreadContactRepositoryStub{threads: []entities.MessageThread{{Contact: "+18005550199"}}}
provider := &messageThreadContactProviderStub{err: errors.New("contacts unavailable")}
logger := &messageThreadContactLogger{}
service := newMessageThreadContactServiceForTest(repository, provider, logger)
threads, err := service.GetThreads(context.Background(), MessageThreadGetParams{UserID: entities.UserID("user-id"), WithContacts: true})
require.NoError(t, err)
require.Len(t, *threads, 1)
assert.Nil(t, (*threads)[0].ContactDetails)
assert.Equal(t, 1, provider.calls)
require.Len(t, logger.errors, 1)
assert.ErrorContains(t, logger.errors[0], "cannot build contact map")
}
func TestGetThreads_DoesNotLookupContactsWhenRepositoryFails(t *testing.T) {
repository := &messageThreadContactRepositoryStub{err: stacktrace.NewError("repository failed")}
provider := &messageThreadContactProviderStub{contacts: map[string]*entities.Contact{
"+18005550199": {Name: "Alice"},
}}
service := newMessageThreadContactServiceForTest(repository, provider, nil)
threads, err := service.GetThreads(context.Background(), MessageThreadGetParams{UserID: entities.UserID("user-id"), WithContacts: true})
require.Error(t, err)
assert.Nil(t, threads)
assert.Equal(t, 0, provider.calls)
}
|