Files
geo/server/internal/tenant/app/article_url_sanitizer_test.go
T
root 52997e36fe fix(content-gen): strip URLs and site-list sections from generated content
Forbid URLs/domains/markdown links across prompt rules (runtime, platform
templates, outline/imitation/title flows) and rename "官网" placeholder to
"官网信息" with explicit instructions that links are background only.
Add sanitizeGeneratedArticleMarkdown to strip URLs, HTML images, and
"网站列表/官网列表" labels from LLM output, wired into article, prompt-rule,
template, and outline generation. Drop the site_list outline section seed
and filter blocked outline keys in the wizard so stored drafts cannot
resurrect it.
2026-05-24 22:19:43 +08:00

87 lines
2.7 KiB
Go

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 := `<p class="article-editor-image"><img src="https://example.com/cover.webp" data-asset-id="88" /></p>`
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)
}
}