package service
import "testing"
func TestSanitizeOCRText(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "empty string",
input: "",
want: "",
},
{
name: "whitespace only",
input: " \n\t ",
want: "",
},
{
name: "pure HTML skeleton with no text",
input: `
`,
want: "",
},
{
name: "HTML with only whitespace text",
input: " \n ",
want: "",
},
{
name: "valid markdown passes through",
input: "# 标题\n\n这是一段正文,包含一些内容。\n\n| 列1 | 列2 |\n| --- | --- |\n| 数据1 | 数据2 |",
want: "# 标题\n\n这是一段正文,包含一些内容。\n\n| 列1 | 列2 |\n| --- | --- |\n| 数据1 | 数据2 |",
},
{
name: "code block wrapper stripped",
input: "```markdown\n# 文档标题\n\n正文内容在这里。\n```",
want: "# 文档标题\n\n正文内容在这里。",
},
{
name: "html code block wrapper stripped",
input: "```html\n这是一段内容
\n```",
want: "这是一段内容",
},
{
name: "HTML document converted to markdown",
input: "标题
这是一段很长的正文内容,用来测试 HTML 到 Markdown 的转换。
",
want: "# 标题\n\n这是一段很长的正文内容,用来测试 HTML 到 Markdown 的转换。",
},
{
name: "known empty reply - Chinese",
input: "无文字内容",
want: "",
},
{
name: "known empty reply - no text",
input: "No text",
want: "",
},
{
name: "known empty reply - 图片中没有文字",
input: "图片中没有文字",
want: "",
},
{
name: "plain text with minimal HTML not converted",
input: "这是一段正常文本,价格 <100 元。",
want: "这是一段正常文本,价格 <100 元。",
},
{
name: "multiple blank lines collapsed",
input: "段落一\n\n\n\n\n段落二",
want: "段落一\n\n段落二",
},
{
name: "HTML with substantial text content is converted",
input: "报告摘要
本季度营收同比增长 15%,净利润达到 2.3 亿元。
",
want: "", // placeholder; will be checked for non-empty
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitizeOCRText(tt.input)
if tt.name == "HTML with substantial text content is converted" {
if got == "" {
t.Errorf("sanitizeOCRText() returned empty for substantial HTML content")
}
if got == tt.input {
t.Errorf("sanitizeOCRText() did not convert HTML, got original")
}
return
}
if got != tt.want {
t.Errorf("sanitizeOCRText() = %q, want %q", got, tt.want)
}
})
}
}
func TestStripMarkdownCodeBlock(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "no code block",
input: "just normal text",
want: "just normal text",
},
{
name: "markdown code block",
input: "```markdown\n# Title\nContent here\n```",
want: "# Title\nContent here",
},
{
name: "html code block",
input: "```html\nhello
\n```",
want: "hello
",
},
{
name: "plain code block",
input: "```\nsome text\n```",
want: "some text",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := stripMarkdownCodeBlock(tt.input)
if got != tt.want {
t.Errorf("stripMarkdownCodeBlock() = %q, want %q", got, tt.want)
}
})
}
}
func TestLooksLikeHTML(t *testing.T) {
tests := []struct {
name string
input string
want bool
}{
{
name: "HTML document",
input: "text
",
want: true,
},
{
name: "DOCTYPE",
input: "",
want: true,
},
{
name: "body tag",
input: "content
",
want: true,
},
{
name: "plain markdown",
input: "# Title\n\nSome paragraph text",
want: false,
},
{
name: "text with minor HTML",
input: "This is mostly text with a bold word.",
want: false,
},
{
name: "heavy HTML tags",
input: "",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := looksLikeHTML(tt.input)
if got != tt.want {
t.Errorf("looksLikeHTML() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsKnownEmptyReply(t *testing.T) {
tests := []struct {
input string
want bool
}{
{"无文字内容", true},
{"无法识别", true},
{"no text", true},
{"No Text", true},
{"NO CONTENT", true},
{"empty", true},
{"这是正常内容", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := isKnownEmptyReply(tt.input)
if got != tt.want {
t.Errorf("isKnownEmptyReply(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}