Files
xinmi/internal/application/service/ocr_sanitizer_test.go
T
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

214 lines
5.0 KiB
Go

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: `<html><body><div class="image"><img/></div></body></html>`,
want: "",
},
{
name: "HTML with only whitespace text",
input: "<html><body> \n </body></html>",
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<p>这是一段内容</p>\n```",
want: "这是一段内容",
},
{
name: "HTML document converted to markdown",
input: "<html><body><h1>标题</h1><p>这是一段很长的正文内容,用来测试 HTML 到 Markdown 的转换。</p></body></html>",
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: "<div><h2>报告摘要</h2><p>本季度营收同比增长 15%,净利润达到 2.3 亿元。</p><table><tr><th>指标</th><th>数值</th></tr><tr><td>营收</td><td>10亿</td></tr></table></div>",
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\n<p>hello</p>\n```",
want: "<p>hello</p>",
},
{
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: "<html><body><p>text</p></body></html>",
want: true,
},
{
name: "DOCTYPE",
input: "<!DOCTYPE html><html><body></body></html>",
want: true,
},
{
name: "body tag",
input: "<body><p>content</p></body>",
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 <b>bold</b> word.",
want: false,
},
{
name: "heavy HTML tags",
input: "<div><p><span>x</span></p></div><div><p><span>y</span></p></div>",
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)
}
})
}
}