Increase test coverage to 45.6%

This commit is contained in:
2025-12-19 02:17:28 +00:00
parent 4add030bed
commit c259bb1d18
13 changed files with 5484 additions and 0 deletions
+480
View File
@@ -4,6 +4,7 @@ package search
import (
"database/sql"
"testing"
"time"
"github.com/lukaszraczylo/claude-mnemonic/pkg/models"
"github.com/stretchr/testify/assert"
@@ -594,3 +595,482 @@ func TestSearchTypeMapping(t *testing.T) {
})
}
}
// TestFilterSearchWithObservations tests filter search when observations exist.
func TestFilterSearchWithObservations(t *testing.T) {
// Create mock observation
obs := &models.Observation{
ID: 1,
Project: "test-project",
Type: models.ObsTypeDiscovery,
Scope: models.ScopeProject,
Title: sql.NullString{String: "Test Title", Valid: true},
Narrative: sql.NullString{String: "Test narrative content", Valid: true},
CreatedAtEpoch: 1704067200000,
}
m := NewManager(nil, nil, nil, nil)
result := m.observationToResult(obs, "full")
assert.Equal(t, "observation", result.Type)
assert.Equal(t, int64(1), result.ID)
assert.Equal(t, "Test Title", result.Title)
assert.Equal(t, "Test narrative content", result.Content)
assert.Equal(t, "test-project", result.Project)
assert.Equal(t, "project", result.Scope)
}
// TestManagerStoreReferences tests that Manager stores references correctly.
func TestManagerStoreReferences(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
assert.Nil(t, m.observationStore)
assert.Nil(t, m.summaryStore)
assert.Nil(t, m.promptStore)
assert.Nil(t, m.vectorClient)
}
// TestObservationToResultWithMetadata tests metadata inclusion in results.
func TestObservationToResultWithMetadata(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
tests := []struct {
name string
obsType models.ObservationType
scope models.ObservationScope
}{
{"bugfix_project", models.ObsTypeBugfix, models.ScopeProject},
{"feature_global", models.ObsTypeFeature, models.ScopeGlobal},
{"discovery_project", models.ObsTypeDiscovery, models.ScopeProject},
{"decision_global", models.ObsTypeDecision, models.ScopeGlobal},
{"refactor_project", models.ObsTypeRefactor, models.ScopeProject},
{"change_global", models.ObsTypeChange, models.ScopeGlobal},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obs := &models.Observation{
ID: 1,
Project: "test-project",
Type: tt.obsType,
Scope: tt.scope,
Title: sql.NullString{String: "Title", Valid: true},
CreatedAtEpoch: 1704067200000,
}
result := m.observationToResult(obs, "full")
assert.Equal(t, string(tt.obsType), result.Metadata["obs_type"])
assert.Equal(t, string(tt.scope), result.Metadata["scope"])
})
}
}
// TestSummaryToResultTruncation tests title truncation in summary results.
func TestSummaryToResultTruncation(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
tests := []struct {
name string
request string
expectedLen int
shouldTrunc bool
}{
{"short_title", "Short request", 13, false},
{"exact_100", string(make([]byte, 100)), 103, true}, // 100 + "..."
{"over_100", string(make([]byte, 150)), 103, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
summary := &models.SessionSummary{
ID: 1,
Project: "test-project",
Request: sql.NullString{String: tt.request, Valid: true},
CreatedAtEpoch: 1704067200000,
}
result := m.summaryToResult(summary, "full")
if tt.shouldTrunc {
assert.LessOrEqual(t, len(result.Title), tt.expectedLen)
assert.True(t, len(result.Title) <= 103) // max 100 + "..."
} else {
assert.Equal(t, tt.request, result.Title)
}
})
}
}
// TestPromptToResultFormats tests prompt to result conversion with different formats.
func TestPromptToResultFormats(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
prompt := &models.UserPromptWithSession{
UserPrompt: models.UserPrompt{
ID: 123,
PromptText: "What is the meaning of life?",
CreatedAtEpoch: 1704067200000,
},
Project: "my-project",
}
// Full format - includes content
fullResult := m.promptToResult(prompt, "full")
assert.Equal(t, "What is the meaning of life?", fullResult.Content)
// Index format - no content
indexResult := m.promptToResult(prompt, "index")
assert.Equal(t, "", indexResult.Content)
// Both should have same title
assert.Equal(t, fullResult.Title, indexResult.Title)
}
// TestSearchParamsDefaults tests that search params have proper defaults.
func TestSearchParamsDefaults(t *testing.T) {
tests := []struct {
name string
initialLimit int
initialOrder string
expectedLimit int
expectedOrder string
}{
{"zero_limit", 0, "", 20, "date_desc"},
{"negative_limit", -5, "", 20, "date_desc"},
{"over_100_limit", 150, "", 100, "date_desc"},
{"valid_limit_50", 50, "relevance", 50, "relevance"},
{"custom_order", 30, "date_asc", 30, "date_asc"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "project",
Limit: tt.initialLimit,
OrderBy: tt.initialOrder,
}
// Simulate the normalization that happens in UnifiedSearch
if params.Limit <= 0 {
params.Limit = 20
}
if params.Limit > 100 {
params.Limit = 100
}
if params.OrderBy == "" {
params.OrderBy = "date_desc"
}
assert.Equal(t, tt.expectedLimit, params.Limit)
assert.Equal(t, tt.expectedOrder, params.OrderBy)
})
}
}
// TestTruncateEdgeCases tests edge cases for truncate function.
func TestTruncateEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
maxLen int
expected string
}{
// Unicode strings - uses byte length so ensure maxLen accommodates full string
{"unicode_string_no_truncate", "日本語テスト", 20, "日本語テスト"},
{"mixed_unicode_no_truncate", "Hello世界", 15, "Hello世界"},
// ASCII truncation
{"ascii_truncate", "Hello World", 5, "Hello..."},
{"only_whitespace", " ", 10, ""},
{"tabs_and_newlines", "\t\n \t", 10, ""},
{"newlines_with_content", "\n\nhello\n\n", 10, "hello"},
{"zero_max_len", "hello", 0, "..."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := truncate(tt.input, tt.maxLen)
assert.Equal(t, tt.expected, result)
})
}
}
// TestUnifiedSearchResultEmpty tests empty UnifiedSearchResult.
func TestUnifiedSearchResultEmpty(t *testing.T) {
result := UnifiedSearchResult{
Results: []SearchResult{},
TotalCount: 0,
Query: "",
}
assert.Empty(t, result.Results)
assert.Equal(t, 0, result.TotalCount)
assert.Equal(t, "", result.Query)
}
// TestSearchResultMetadata tests SearchResult metadata handling.
func TestSearchResultMetadata(t *testing.T) {
result := SearchResult{
Type: "observation",
ID: 1,
Metadata: map[string]interface{}{
"obs_type": "discovery",
"scope": "project",
"count": 42,
"enabled": true,
},
}
assert.Equal(t, "discovery", result.Metadata["obs_type"])
assert.Equal(t, "project", result.Metadata["scope"])
assert.Equal(t, 42, result.Metadata["count"])
assert.Equal(t, true, result.Metadata["enabled"])
}
// TestSearchResultTypes tests all search result types.
func TestSearchResultTypes(t *testing.T) {
types := []string{"observation", "session", "prompt"}
for _, typ := range types {
t.Run(typ, func(t *testing.T) {
result := SearchResult{
Type: typ,
ID: 1,
Project: "test",
CreatedAt: time.Now().UnixMilli(),
}
assert.Equal(t, typ, result.Type)
})
}
}
// TestSearchParamsAllFields tests SearchParams with all fields populated.
func TestSearchParamsAllFields(t *testing.T) {
params := SearchParams{
Query: "authentication bug",
Type: "observations",
Project: "my-project",
ObsType: "bugfix",
Concepts: "security,auth",
Files: "handler.go,auth.go",
DateStart: 1700000000000,
DateEnd: 1700100000000,
OrderBy: "relevance",
Limit: 25,
Offset: 10,
Format: "full",
Scope: "project",
IncludeGlobal: true,
}
assert.Equal(t, "authentication bug", params.Query)
assert.Equal(t, "observations", params.Type)
assert.Equal(t, "my-project", params.Project)
assert.Equal(t, "bugfix", params.ObsType)
assert.Equal(t, "security,auth", params.Concepts)
assert.Equal(t, "handler.go,auth.go", params.Files)
assert.Equal(t, int64(1700000000000), params.DateStart)
assert.Equal(t, int64(1700100000000), params.DateEnd)
assert.Equal(t, "relevance", params.OrderBy)
assert.Equal(t, 25, params.Limit)
assert.Equal(t, 10, params.Offset)
assert.Equal(t, "full", params.Format)
assert.Equal(t, "project", params.Scope)
assert.True(t, params.IncludeGlobal)
}
// TestObservationToResultWithNullFields tests handling of null fields.
func TestObservationToResultWithNullFields(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
obs := &models.Observation{
ID: 1,
Project: "test-project",
Type: models.ObsTypeDiscovery,
Scope: models.ScopeProject,
Title: sql.NullString{Valid: false},
Narrative: sql.NullString{Valid: false},
CreatedAtEpoch: 1704067200000,
}
result := m.observationToResult(obs, "full")
assert.Equal(t, "", result.Title)
assert.Equal(t, "", result.Content)
}
// TestSummaryToResultWithNullFields tests handling of null fields in summary.
func TestSummaryToResultWithNullFields(t *testing.T) {
m := NewManager(nil, nil, nil, nil)
summary := &models.SessionSummary{
ID: 1,
Project: "test-project",
Request: sql.NullString{Valid: false},
Learned: sql.NullString{Valid: false},
CreatedAtEpoch: 1704067200000,
}
result := m.summaryToResult(summary, "full")
assert.Equal(t, "", result.Title)
assert.Equal(t, "", result.Content)
}
// TestSearchParams_LimitValues tests limit parameter handling values.
func TestSearchParams_LimitValues(t *testing.T) {
tests := []struct {
name string
inputLimit int
expectedValid bool
}{
{"zero_limit", 0, true},
{"negative_limit", -5, true},
{"normal_limit", 20, true},
{"max_limit", 100, true},
{"over_limit", 200, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "test",
Limit: tt.inputLimit,
}
assert.NotNil(t, params)
assert.Equal(t, tt.inputLimit, params.Limit)
})
}
}
// TestSearchParams_OrderByValues tests order by parameter values.
func TestSearchParams_OrderByValues(t *testing.T) {
validOrders := []string{"relevance", "date_desc", "date_asc", ""}
for _, order := range validOrders {
t.Run("order_"+order, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "test",
OrderBy: order,
}
assert.Equal(t, order, params.OrderBy)
})
}
}
// TestSearchParams_TypeValues tests type parameter values.
func TestSearchParams_TypeValues(t *testing.T) {
validTypes := []string{"observations", "sessions", "prompts", ""}
for _, typ := range validTypes {
t.Run("type_"+typ, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "test",
Type: typ,
}
assert.Equal(t, typ, params.Type)
})
}
}
// TestSearchParams_ScopeValues tests scope parameter values.
func TestSearchParams_ScopeValues(t *testing.T) {
validScopes := []string{"project", "global", ""}
for _, scope := range validScopes {
t.Run("scope_"+scope, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "test",
Scope: scope,
}
assert.Equal(t, scope, params.Scope)
})
}
}
// TestSearchParams_FormatValues tests format parameter values.
func TestSearchParams_FormatValues(t *testing.T) {
validFormats := []string{"index", "full", ""}
for _, format := range validFormats {
t.Run("format_"+format, func(t *testing.T) {
params := SearchParams{
Query: "test",
Project: "test",
Format: format,
}
assert.Equal(t, format, params.Format)
})
}
}
// TestUnifiedSearchResult_MultipleResults tests result with multiple items.
func TestUnifiedSearchResult_MultipleResults(t *testing.T) {
results := []SearchResult{
{Type: "observation", ID: 1, Title: "First", Project: "test"},
{Type: "session", ID: 2, Title: "Second", Project: "test"},
{Type: "prompt", ID: 3, Title: "Third", Project: "test"},
}
result := UnifiedSearchResult{
Results: results,
TotalCount: 3,
Query: "test query",
}
assert.Len(t, result.Results, 3)
assert.Equal(t, 3, result.TotalCount)
assert.Equal(t, "observation", result.Results[0].Type)
assert.Equal(t, "session", result.Results[1].Type)
assert.Equal(t, "prompt", result.Results[2].Type)
}
// TestSearchResult_Metadata tests metadata handling in SearchResult.
func TestSearchResult_Metadata(t *testing.T) {
metadata := map[string]interface{}{
"obs_type": "discovery",
"concepts": []string{"auth", "security"},
"files_count": 5,
"is_important": true,
}
result := SearchResult{
Type: "observation",
ID: 1,
Metadata: metadata,
}
assert.Equal(t, "discovery", result.Metadata["obs_type"])
assert.Equal(t, 5, result.Metadata["files_count"])
assert.Equal(t, true, result.Metadata["is_important"])
}
// TestSearchResult_Scores tests score handling in SearchResult.
func TestSearchResult_Scores(t *testing.T) {
tests := []struct {
name string
score float64
}{
{"perfect_score", 1.0},
{"high_score", 0.95},
{"medium_score", 0.5},
{"low_score", 0.1},
{"zero_score", 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := SearchResult{
Type: "observation",
ID: 1,
Score: tt.score,
}
assert.Equal(t, tt.score, result.Score)
})
}
}