package app import ( "strings" "testing" ) func TestSanitizeGeneratedArticleMarkdownRemovesURLs(t *testing.T) { t.Parallel() markdown := strings.Join([]string{ "# 标题", "", "品牌官网:https://www.defnit.cn,可查询产品参数。", "也可以查看 www.example.com/path 了解更多。", "参考 [官网](https://defnit.cn/contact) 的信息整理。", }, "\n") got := sanitizeGeneratedArticleMarkdown(markdown) for _, unexpected := range []string{"https://", "www.", "defnit.cn", "example.com", "[官网]("} { if strings.Contains(got, unexpected) { t.Fatalf("sanitizeGeneratedArticleMarkdown() = %q, contains %q", got, unexpected) } } for _, expected := range []string{"# 标题", "可查询产品参数。", "也可以查看 了解更多。", "参考 官网 的信息整理。"} { if !strings.Contains(got, expected) { t.Fatalf("sanitizeGeneratedArticleMarkdown() = %q, want %q", got, expected) } } } func TestSanitizeGeneratedArticleMarkdownRemovesHTMLImageURLs(t *testing.T) { t.Parallel() markdown := `

` got := sanitizeGeneratedArticleMarkdown(markdown) if strings.TrimSpace(got) != "" { t.Fatalf("sanitizeGeneratedArticleMarkdown() = %q, want generated image URL removed", got) } } func TestSanitizeGeneratedArticleMarkdownKeepsNormalPunctuation(t *testing.T) { t.Parallel() markdown := strings.Join([]string{ "## 选择建议:", "下面从几个维度看。", }, "\n") got := sanitizeGeneratedArticleMarkdown(markdown) if got != markdown { t.Fatalf("sanitizeGeneratedArticleMarkdown() = %q, want %q", got, markdown) } } func TestNormalizeOutlineNodesRemovesURLsAndWebsiteListLabel(t *testing.T) { t.Parallel() nodes := []OutlineNode{ { Outline: "网站列表", Children: []OutlineNode{ {Outline: "丹福尼门锁官方网站:www.defnit.cn,可查询产品参数"}, }, }, } got := normalizeOutlineNodes(nodes, 2) if len(got) != 1 || got[0].Outline != "品牌信息整理" { t.Fatalf("normalizeOutlineNodes() = %#v, want top outline sanitized", got) } if len(got[0].Children) != 1 { t.Fatalf("normalizeOutlineNodes() children = %#v, want one child", got[0].Children) } child := got[0].Children[0].Outline if strings.Contains(child, "www.") || strings.Contains(child, "defnit.cn") || strings.Contains(child, "官方网站") { t.Fatalf("normalizeOutlineNodes() child = %q, still contains website text", child) } if !strings.Contains(child, "丹福尼门锁") || !strings.Contains(child, "可查询产品参数") { t.Fatalf("normalizeOutlineNodes() child = %q, lost useful context", child) } }