thread-master/apps/backend/internal/module/scout/usecase/relevance_test.go

64 lines
2.2 KiB
Go
Raw Normal View History

2026-08-09 07:57:35 +00:00
package usecase
import (
"context"
"testing"
"apps/backend/internal/module/scout/domain"
"apps/backend/internal/module/scout/repository"
)
func TestTextMatchesSearchTerm(t *testing.T) {
if !textMatchesSearchTerm("最近在找外包做 app", "外包 求推薦") {
t.Fatal("should match 外包")
}
if textMatchesSearchTerm("今天天氣真好去爬山", "外包 求推薦") {
t.Fatal("unrelated text must not match")
}
if !textMatchesSearchTerm("求推薦好用的保母", "保母") {
t.Fatal("should match 保母")
}
// 只有「求推薦」命中、沒主題核 → 最長核是求推薦(3字) 會要求含求推薦
if !textMatchesSearchTerm("有人求推薦嗎", "求推薦") {
t.Fatal("exact anchor term")
}
}
func TestPersistSearchHitsRejectsIrrelevantAPIResult(t *testing.T) {
svc := New(repository.NewMemory())
posts, err := svc.persistSearchHits(context.Background(), 1, &domain.RunBrief{
Mode: domain.ModeActivity, ScanTerms: []string{"後端工程師"},
}, domain.PathAPI, []ThreadSearchResult{
{URL: "https://www.threads.net/@dev/post/1", Snippet: "正在找後端工程師一起做產品"},
{URL: "https://www.threads.net/@film/post/2", Snippet: "劇組徵拍電影的夥伴"},
})
if err != nil {
t.Fatalf("persistSearchHits: %v", err)
}
if len(posts) != 1 {
t.Fatalf("posts=%d want 1", len(posts))
}
if posts[0].Permalink != "https://www.threads.net/@dev/post/1" {
t.Fatalf("unexpected post %q", posts[0].Permalink)
}
}
func TestPersistActivityHitsPrioritizesDiscussionMomentum(t *testing.T) {
svc := New(repository.NewMemory())
posts, err := svc.persistSearchHits(context.Background(), 1, &domain.RunBrief{
Mode: domain.ModeActivity, ScanTerms: []string{"外包"},
}, domain.PathAPI, []ThreadSearchResult{
{URL: "https://www.threads.net/@chat/post/1", Snippet: "分享外包合作的心得", PublishedAt: 200},
{URL: "https://www.threads.net/@ask/post/2", Snippet: "請問外包工程師怎麼找?", PublishedAt: 100},
})
if err != nil {
t.Fatalf("persistSearchHits: %v", err)
}
if len(posts) != 2 {
t.Fatalf("posts=%d want 2", len(posts))
}
if posts[0].Permalink != "https://www.threads.net/@ask/post/2" {
t.Fatalf("first post=%q want asking post", posts[0].Permalink)
}
}