package service import ( "context" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" ) type mockServiceSearchIndexer struct { indexed []string deleted []string } func (m *mockServiceSearchIndexer) IndexConversation(ctx context.Context, conversation *model.Conversation) error { m.indexed = append(m.indexed, "conversation") return nil } func (m *mockServiceSearchIndexer) DeleteConversation(ctx context.Context, accountID uint, id uint) error { m.deleted = append(m.deleted, "conversation") return nil } func (m *mockServiceSearchIndexer) IndexMessage(ctx context.Context, message *model.Message) error { m.indexed = append(m.indexed, "message") return nil } func (m *mockServiceSearchIndexer) DeleteMessage(ctx context.Context, accountID uint, id uint) error { m.deleted = append(m.deleted, "message") return nil } func (m *mockServiceSearchIndexer) IndexContact(ctx context.Context, contact *model.Contact) error { m.indexed = append(m.indexed, "contact") return nil } func (m *mockServiceSearchIndexer) DeleteContact(ctx context.Context, accountID uint, id uint) error { m.deleted = append(m.deleted, "contact") return nil } func (m *mockServiceSearchIndexer) IndexCompany(ctx context.Context, company *model.Company) error { m.indexed = append(m.indexed, "company") return nil } func (m *mockServiceSearchIndexer) DeleteCompany(ctx context.Context, accountID uint, id uint) error { m.deleted = append(m.deleted, "company") return nil } func (m *mockServiceSearchIndexer) IndexArticle(ctx context.Context, article *model.Article) error { m.indexed = append(m.indexed, "article") return nil } func (m *mockServiceSearchIndexer) DeleteArticle(ctx context.Context, accountID uint, id uint) error { m.deleted = append(m.deleted, "article") return nil } func TestConversationService_SearchIndexHooks(t *testing.T) { svc, db := setupConversationService(t) indexer := &mockServiceSearchIndexer{} svc.SetSearchIndexer(indexer) account := createConversationServiceTestAccount(t, db) inbox := createConversationServiceTestInbox(t, db, account.ID) contact := createConversationServiceTestContact(t, db, account.ID) conversation, err := svc.Create(context.Background(), account.ID, CreateConversationRequest{InboxID: inbox.ID, ContactID: contact.ID}) require.NoError(t, err) _, err = svc.UpdatePriority(context.Background(), account.ID, conversation.ID, "high") require.NoError(t, err) require.NoError(t, svc.Delete(context.Background(), account.ID, conversation.ID)) assert.Equal(t, []string{"conversation", "conversation"}, indexer.indexed) assert.Equal(t, []string{"conversation"}, indexer.deleted) } func TestMessageService_SearchIndexHooks(t *testing.T) { db, _, _, svc := setupMessageServiceWithDefaultLLM(t) indexer := &mockServiceSearchIndexer{} svc.SetSearchIndexer(indexer) account := createTestAccount(t, db) user := createTestUser(t, db, account.ID) inbox := createTestInbox(t, db, account.ID, "web_widget") contact := createTestContact(t, db, account.ID) conversation := createTestConversation(t, db, account.ID, inbox.ID, contact.ID) message, err := svc.Create(context.Background(), account.ID, user.ID, CreateMessageRequest{ConversationID: conversation.ID, Content: "hello", MessageType: "outgoing"}) require.NoError(t, err) _, err = svc.Update(context.Background(), account.ID, message.ID, UpdateMessageRequest{Content: "updated"}) require.NoError(t, err) _, err = svc.Delete(context.Background(), account.ID, message.ID) require.NoError(t, err) assert.Equal(t, []string{"message", "message"}, indexer.indexed) assert.Equal(t, []string{"message"}, indexer.deleted) } func TestContactService_SearchIndexHooks(t *testing.T) { db := setupServiceTestDB(t) repo := NewContactService(repository.NewContactRepo(db), nil, nil) indexer := &mockServiceSearchIndexer{} repo.SetSearchIndexer(indexer) account := createTestAccount(t, db) contact, err := repo.Create(context.Background(), account.ID, CreateContactRequest{Name: "Ada", Email: "ada@example.com"}) require.NoError(t, err) _, err = repo.Update(context.Background(), account.ID, contact.ID, UpdateContactRequest{Name: "Ada Lovelace"}) require.NoError(t, err) require.NoError(t, repo.Delete(context.Background(), account.ID, contact.ID)) assert.Equal(t, []string{"contact", "contact"}, indexer.indexed) assert.Equal(t, []string{"contact"}, indexer.deleted) } func TestCompanyService_SearchIndexHooks(t *testing.T) { db, _, _, _, svc := setupCompanyServiceTest(t) indexer := &mockServiceSearchIndexer{} svc.SetSearchIndexer(indexer) account := createTestAccount(t, db) company, err := svc.Create(context.Background(), account.ID, &CreateCompanyRequest{Name: "Acme"}) require.NoError(t, err) _, err = svc.Update(context.Background(), company.ID, account.ID, &UpdateCompanyRequest{Name: "Acme Inc"}) require.NoError(t, err) require.NoError(t, svc.Delete(context.Background(), company.ID, account.ID)) assert.Equal(t, []string{"company", "company"}, indexer.indexed) assert.Equal(t, []string{"company"}, indexer.deleted) } func TestArticleService_SearchIndexHooks(t *testing.T) { _, _, svc := setupArticleService(t) indexer := &mockServiceSearchIndexer{} svc.SetSearchIndexer(indexer) article, err := svc.CreateWithAccount(context.Background(), 7, 3, 1, &CreateArticleRequest{Title: "Install", Slug: "install"}) require.NoError(t, err) _, err = svc.Update(context.Background(), article.ID, &UpdateArticleRequest{Title: "Install GoChat"}) require.NoError(t, err) require.NoError(t, svc.Delete(context.Background(), article.ID)) assert.Equal(t, []string{"article", "article"}, indexer.indexed) assert.Equal(t, []string{"article"}, indexer.deleted) }