Files
gochat/backend/internal/search/coverage_test.go
T

86 lines
2.2 KiB
Go

package search
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDocumentUID(t *testing.T) {
uid := documentUID(ResultTypeConversation, 1, 42)
assert.Equal(t, "1_conversation_42", uid)
}
func TestSearchDocument_EnsureUID(t *testing.T) {
doc := SearchDocument{Type: ResultTypeMessage, AccountID: 1, ID: 5}
doc.ensureUID()
assert.Equal(t, "1_message_5", doc.UID)
}
func TestSearchDocument_EnsureUID_AlreadySet(t *testing.T) {
doc := SearchDocument{UID: "custom_uid"}
doc.ensureUID()
assert.Equal(t, "custom_uid", doc.UID)
}
func TestNormalizeEngineConfig_Defaults(t *testing.T) {
cfg := normalizeEngineConfig(EngineConfig{})
assert.Equal(t, EngineMeilisearch, cfg.Engine)
assert.Equal(t, "http://localhost:7700", cfg.Host)
assert.Equal(t, "gochat_", cfg.IndexPrefix)
}
func TestNormalizeEngineConfig_Custom(t *testing.T) {
cfg := normalizeEngineConfig(EngineConfig{
Engine: "MeiliSearch",
Host: "http://search:7700",
IndexPrefix: "custom_",
})
assert.Equal(t, "meilisearch", cfg.Engine)
assert.Equal(t, "http://search:7700", cfg.Host)
assert.Equal(t, "custom_", cfg.IndexPrefix)
}
func TestSearchableTypes_Default(t *testing.T) {
types := searchableTypes(nil)
assert.NotEmpty(t, types)
assert.Contains(t, types, ResultTypeConversation)
assert.Contains(t, types, ResultTypeMessage)
assert.Contains(t, types, ResultTypeContact)
}
func TestSearchableTypes_WithFilter(t *testing.T) {
filter := &SearchFilter{Types: []SearchResultType{ResultTypeConversation}}
types := searchableTypes(filter)
assert.Len(t, types, 1)
assert.Equal(t, ResultTypeConversation, types[0])
}
func TestTimestamp(t *testing.T) {
now := time.Now()
ts := timestamp(now)
assert.NotZero(t, ts)
}
func TestTimestampPtr_Nil(t *testing.T) {
assert.Equal(t, int64(0), timestampPtr(nil))
}
func TestTimestampPtr_Value(t *testing.T) {
v := int64(12345)
assert.Equal(t, int64(12345), timestampPtr(&v))
}
func TestNewSearchEngine_InvalidEngine(t *testing.T) {
_, err := NewSearchEngine(EngineConfig{Engine: "nonexistent"}, nil)
require.Error(t, err)
}
func TestNewSearchEngineDB(t *testing.T) {
// Create with nil repo - should still construct
eng := NewSearchEngineDB(nil)
assert.NotNil(t, eng)
}