package knowledge

import "testing"

func TestResolveScanPatrolKeywordsIncludesExplicitHint(t *testing.T) {
	got := ResolveScanPatrolKeywords(
		[]string{"手動 關鍵字"},
		[]string{"已儲存"},
		PatrolTagInput{Questions: []string{"敏感肌 怎麼辦"}},
		nil,
	)
	if len(got) == 0 {
		t.Fatal("expected ranked keywords")
	}
	found := false
	for _, kw := range got {
		if kw == "手動 關鍵字" {
			found = true
			break
		}
	}
	if !found {
		t.Fatalf("ResolveScanPatrolKeywords() = %#v, want explicit hint included", got)
	}
}

func TestResolveScanPatrolKeywordsFromResearchMapWithoutGraph(t *testing.T) {
	got := ResolveScanPatrolKeywords(nil, nil, PatrolTagInput{
		Questions: []string{"化療後 皮膚乾 怎麼辦"},
	}, nil)
	if len(got) == 0 {
		t.Fatal("expected research map questions to produce patrol keywords without graph nodes")
	}
}

func TestResolveScanPatrolKeywordsPrefersSelectedNodes(t *testing.T) {
	nodes := []Node{
		{ID: "pain", Label: "化療後 皮膚乾", SelectedForScan: true, ProductFitScore: 88, NodeKind: "pain", DerivedTags: DerivedTags{Relevance: []string{"化療後 皮膚乾 沐浴乳"}}},
		{ID: "skip", Label: "無關節點", SelectedForScan: false, ProductFitScore: 90, DerivedTags: DerivedTags{Relevance: []string{"不該出現"}}},
	}
	got := ResolveScanPatrolKeywords(nil, nil, PatrolTagInput{}, nodes)
	if len(got) == 0 {
		t.Fatal("expected selected node to produce patrol keywords")
	}
	found := false
	for _, kw := range got {
		if kw == "化療後 皮膚乾 沐浴乳" || kw == "化療後 皮膚乾" {
			found = true
			break
		}
	}
	if !found {
		t.Fatalf("expected selected node keyword, got %#v", got)
	}
	for _, kw := range got {
		if kw == "不該出現" {
			t.Fatalf("unselected node leaked into keywords: %#v", got)
		}
	}
}
