Files
iqudo-top1/internal/knowledge/content_test.go
2026-08-18 16:27:02 +08:00

45 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package knowledge
import "testing"
func TestParseContentAcceptsLegacyStandardCopy(t *testing.T) {
content, err := ParseContent([]byte(`{"standard_copy":"请说明情况","risk_note":"必要时转诊"}`))
if err != nil {
t.Fatal(err)
}
if len(content.CopyTemplates) != 1 || content.CopyTemplates[0].ID != "default" || content.StandardCopy != "请说明情况" {
t.Fatalf("unexpected legacy content: %#v", content)
}
}
func TestValidateReferencesRejectsUnknownScenarioField(t *testing.T) {
content, err := ParseContent([]byte(`{"fields":[{"key":"symptom","name":"症状","source_field":"pet_symptom"}],"copy_templates":[{"id":"copy","title":"说明","content":"{{card.symptom}}"}]}`))
if err != nil {
t.Fatal(err)
}
if err := ValidateReferences(content, map[string]bool{"pet_name": true}); err == nil {
t.Fatal("ValidateReferences() error = nil, want unknown source field error")
}
}
func TestValidateReferencesRejectsUnknownTemplateField(t *testing.T) {
content, err := ParseContent([]byte(`{"fields":[{"key":"symptom","name":"症状"}],"copy_templates":[{"id":"copy","title":"说明","content":"{{input.pet_name}} {{card.disease}}"}]}`))
if err != nil {
t.Fatal(err)
}
if err := ValidateReferences(content, map[string]bool{"pet_name": true}); err == nil {
t.Fatal("ValidateReferences() error = nil, want unknown knowledge field error")
}
}
func TestRenderResolvesInputAndCardFields(t *testing.T) {
content, err := ParseContent([]byte(`{"fields":[{"key":"symptom","name":"症状","value":"未明确","source_field":"symptom"},{"key":"disease","name":"可能疾病","value":"需要医生评估"}],"copy_templates":[{"id":"copy","title":"说明","content":"{{input.pet_name}}出现{{card.symptom}}{{card.disease}}{{input.pet_age}}岁。"}]}`))
if err != nil {
t.Fatal(err)
}
copies := Render(content, map[string]interface{}{"pet_name": "团子", "symptom": "呕吐", "pet_age": 3})
if len(copies) != 1 || copies[0].Content != "团子出现呕吐需要医生评估。3岁。" {
t.Fatalf("rendered copies = %#v", copies)
}
}