fix(search): harden authorized results and ordering
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/driver/postgres"
|
||||
@@ -45,6 +48,65 @@ func TestPostgresSearchKeepsAssignmentAndExplicitShareBoundaries(t *testing.T) {
|
||||
require.ElementsMatch(t, []string{assigned.Identity, sharedNote.Identity}, []string{results[0].ID, results[1].ID})
|
||||
}
|
||||
|
||||
func TestPostgresSearchUsesStableFairLimitAndRuneBoundedSnippets(t *testing.T) {
|
||||
database := newPostgresSearchTestDB(t)
|
||||
owner := createSearchUser(t, database, "postgres-search-order@example.com")
|
||||
baseTime := time.Date(2026, time.July, 21, 12, 0, 0, 0, time.UTC)
|
||||
for index := 0; index < 60; index++ {
|
||||
project := models.SenlinAgentProject{
|
||||
Identity: fmt.Sprintf("00000000-0000-7001-8000-%012d", index),
|
||||
OwnerID: owner.ID,
|
||||
Name: fmt.Sprintf("稳定排序项目 %02d", index),
|
||||
Identifier: fmt.Sprintf("PG-SORT-%02d", index),
|
||||
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||
}
|
||||
require.NoError(t, database.Create(&project).Error)
|
||||
require.NoError(t, database.Create(&models.SenlinAgentTask{
|
||||
Identity: fmt.Sprintf("00000000-0000-7002-8000-%012d", index),
|
||||
ProjectID: project.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Title: fmt.Sprintf("稳定排序任务 %02d", index),
|
||||
Status: "open",
|
||||
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||
}).Error)
|
||||
require.NoError(t, database.Create(&models.SenlinAgentNote{
|
||||
Identity: fmt.Sprintf("00000000-0000-7003-8000-%012d", index),
|
||||
ProjectID: project.ID,
|
||||
CreatedBy: owner.ID,
|
||||
Title: fmt.Sprintf("稳定排序笔记 %02d", index),
|
||||
Markdown: strings.Repeat("森", 300) + "尾",
|
||||
UpdatedAt: baseTime.Add(time.Duration(index) * time.Minute),
|
||||
}).Error)
|
||||
}
|
||||
|
||||
service := NewService(database)
|
||||
results, err := service.Search(owner.ID, "稳定排序")
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, maxSearchResults)
|
||||
require.Equal(t,
|
||||
[]string{"project", "task", "note", "project", "task", "note", "project", "task", "note"},
|
||||
resultTypes(results[:9]),
|
||||
)
|
||||
require.Equal(t, "00000000-0000-7001-8000-000000000059", results[0].ID)
|
||||
require.Equal(t, "00000000-0000-7002-8000-000000000059", results[1].ID)
|
||||
require.Equal(t, "00000000-0000-7003-8000-000000000059", results[2].ID)
|
||||
counts := map[string]int{}
|
||||
for _, result := range results {
|
||||
counts[result.Type]++
|
||||
}
|
||||
require.Equal(t, map[string]int{"project": 17, "task": 17, "note": 16}, counts)
|
||||
|
||||
again, err := service.Search(owner.ID, "稳定排序")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, results, again)
|
||||
|
||||
noteResults, err := service.Search(owner.ID, "森")
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, noteResults)
|
||||
require.Len(t, []rune(noteResults[0].Snippet), 240)
|
||||
require.NotContains(t, noteResults[0].Snippet, "尾")
|
||||
}
|
||||
|
||||
func newPostgresSearchTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
|
||||
Reference in New Issue
Block a user