Files
ai_academy/internal/reranking/passage_code_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
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
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (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 & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (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
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

133 lines
4.1 KiB
Go

package reranking
import (
"context"
"testing"
"academy/internal/types"
)
func TestCleanPassagePreservesFencedCode(t *testing.T) {
t.Parallel()
tests := []struct {
name, input, want string
}{
{
name: "XML-only evidence must not become empty",
input: "```xml\n<refund-policy days=\"7\"/>\n```",
want: "<refund-policy days=\"7\"/>",
},
{
name: "URLs and formatting-like operators are code",
input: "```python\nurl = \"https://api.example.com/v1\"\nvalue = 2 ** 3 ** 2\n```",
want: "url = \"https://api.example.com/v1\"\nvalue = 2 ** 3 ** 2",
},
{
name: "literal Markdown is code",
input: "```markdown\n# Heading\n- item\n| A | B |\n| --- | --- |\n" +
"![alt](image.png)\n[label](https://example.com)\n```",
want: "# Heading\n- item\n| A | B |\n| --- | --- |\n" +
"![alt](image.png)\n[label](https://example.com)",
},
{
name: "code blank lines and math delimiters survive",
input: "```text\nbefore\n\n\n\nafter $$price$$\n```",
want: "before\n\n\n\nafter $$price$$",
},
{
name: "longer outer fence keeps shorter inner fence",
input: "````markdown\n```xml\n<policy/>\n```\n````",
want: "```xml\n<policy/>\n```",
},
{
name: "tilde fences",
input: "~~~xml\n<policy/>\n~~~",
want: "<policy/>",
},
{
name: "mismatched delimiter is code",
input: "```text\n~~~\n<policy/>\n```",
want: "~~~\n<policy/>",
},
{
name: "closing fence cannot carry an info string",
input: "```text\n```not-a-close\n<policy/>\n```",
want: "```not-a-close\n<policy/>",
},
{
name: "longer closing fence",
input: "```xml\n<policy/>\n`````",
want: "<policy/>",
},
{
name: "unclosed fence protects remaining code",
input: "```xml\n<policy/>\nhttps://api.example.com",
want: "<policy/>\nhttps://api.example.com",
},
{
name: "CRLF code lines",
input: "```xml\r\n<one/>\r\n<two/>\r\n```\r\n",
want: "<one/>\r\n<two/>",
},
{
name: "indented fence and CRLF closing whitespace",
input: " ~~~xml\r\n<policy/>\r\n ~~~ \t\r\n",
want: "<policy/>",
},
{
name: "only prose is cleaned between code blocks",
input: "## Example\n```xml\n<one/>\n```\n**Explanation** [docs](https://example.com)\n" +
"~~~shell\ncurl https://api.example.com\n~~~\n> End",
want: "Example\n<one/>\nExplanation docs\ncurl https://api.example.com\nEnd",
},
{
name: "inline triple backticks do not open a block",
input: "Text ```not a block\n**bold**",
want: "Text ```not a block\nbold",
},
{
name: "four-space indented fences are not fenced blocks",
input: " ```xml\n<policy/>\n ```",
want: "```xml\n\n ```",
},
{
name: "backticks in a backtick info string invalidate the opening",
input: "```bad`info\n**bold**",
want: "```bad`info\nbold",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CleanPassage(tt.input); got != tt.want {
t.Errorf("CleanPassage() = %q, want %q", got, tt.want)
}
})
}
}
func TestRerankReceivesFencedCodeWithoutMarkdownCleaning(t *testing.T) {
t.Parallel()
content := "```xml\n<refund-policy days=\"7\"/>\n```"
model := &stubReranker{scores: []float64{0.9}}
input := []*types.SearchResult{{
ID: "policy", Content: content, KnowledgeTitle: "API policy",
ContextHeader: "Refunds", ChunkType: string(types.ChunkTypeText), Score: 0.8,
}}
result := Rerank(context.Background(), model, "refund policy days", input,
Options{Threshold: 0.3})
want := "API policy\n\nRefunds\n\n<refund-policy days=\"7\"/>"
if len(model.documents) != 1 || model.documents[0] != want {
t.Fatalf("model received %q, want one passage %q", model.documents, want)
}
if len(result.Results) != 1 || result.Results[0].ID != "policy" {
t.Fatalf("code-only candidate was dropped: %+v", result)
}
if input[0].Content != content || result.Results[0].Content != content {
t.Fatal("passage cleaning changed the stored/returned chunk content")
}
if got := EnrichedPassage(context.Background(), input[0]); got != "<refund-policy days=\"7\"/>" {
t.Fatalf("MMR enrichment lost the code evidence: %q", got)
}
}