test_ingest.py
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""上传解析链路的单测,覆盖 v1 的乱码与分块碎片两个问题。"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from app.ingest.chunk import chunk_text # noqa: E402
from app.ingest.filename import repair_filename, sanitize_display_name, stored_name # noqa: E402
from app.ingest.parse import ParseError, decode_text # noqa: E402
class TestFilenameRepair:
"""v1 把 GBK 字节按 latin-1 解出来的文件名直接落盘写库,页面上就是乱码。"""
def test_repairs_gbk_mojibake(self):
original = "需求文档-问界与鸿蒙门店运营数据分析平台(1).md"
mojibake = original.encode("gb18030").decode("latin-1")
assert repair_filename(mojibake) == original
def test_repairs_utf8_mojibake(self):
original = "需求文档分析.md"
mojibake = original.encode("utf-8").decode("latin-1")
assert repair_filename(mojibake) == original
@pytest.mark.parametrize("name", [
"需求文档-问界(1).md", "mini.md", "report 2024.pdf",
"Ünïcode.txt", "café-menu.pdf", "Müller Bericht.docx",
])
def test_leaves_valid_names_alone(self, name):
"""正常文件名(包括带重音的欧洲语言)绝不能被"修"坏。"""
assert repair_filename(name) == name
def test_strips_path_and_control_chars(self):
assert sanitize_display_name("../../etc/passwd.md") == "passwd.md"
assert sanitize_display_name("a\x00b.txt") == "a_b.txt"
def test_stored_name_is_ascii(self):
name = stored_name("需求文档.md", "deadbeefdeadbeef")
assert name == "deadbeefdeadbeef.md"
assert name.isascii()
def test_stored_name_rejects_weird_extension(self):
assert stored_name("x.tar.gz-evil", "abc123") == "abc123"
class TestDecodeText:
"""v1 用 errors="ignore",GBK 文件会被静默吞字。这里必须严格。"""
def test_utf8(self):
text, enc = decode_text("盯防周期".encode("utf-8"))
assert text == "盯防周期" and enc == "utf-8"
def test_utf8_bom(self):
text, enc = decode_text("盯防周期".encode("utf-8-sig"))
assert text == "盯防周期" and enc == "utf-8-sig"
def test_gbk_is_not_silently_mangled(self):
source = "门店运营数据分析平台,兜底协议。"
text, enc = decode_text(source.encode("gb18030"))
assert text == source
assert enc == "gb18030"
assert "�" not in text
def test_undecodable_raises_instead_of_dropping_bytes(self):
with pytest.raises(ParseError):
decode_text(bytes([0xC0, 0x80, 0xFF, 0xFE, 0x00, 0x01] * 40))
class TestChunking:
def test_ids_are_scoped_per_document(self):
"""v1 用全局 c0/c1,多文档必然冲突。"""
text = "# 标题\n\n" + ("内容。" * 400)
a = chunk_text(text, doc_id=1, chunk_tokens=200)
b = chunk_text(text, doc_id=2, chunk_tokens=200)
assert a[0]["id"] == "d1:c0"
assert b[0]["id"] == "d2:c0"
assert not ({c["id"] for c in a} & {c["id"] for c in b})
def test_merges_tiny_sections_instead_of_fragmenting(self):
"""v1 遇到标题就强制断开,一堆小节会被切成一堆碎片。"""
text = "\n\n".join(f"## 第{i}节\n\n这一节只有一句话,内容很短。" for i in range(20))
chunks = chunk_text(text, doc_id=1, chunk_tokens=400, min_tokens=100)
assert len(chunks) < 20
assert all(c["tokens"] <= 400 * 1.2 for c in chunks)
def test_keeps_heading_path(self):
text = "# 顶层\n\n## 二级\n\n" + ("正文内容。" * 60)
chunks = chunk_text(text, doc_id=1, chunk_tokens=500)
assert "顶层" in chunks[0]["heading"]
def test_never_exceeds_budget_badly(self):
text = "# T\n\n" + ("很长的一段话没有任何标点" * 300)
chunks = chunk_text(text, doc_id=1, chunk_tokens=300)
assert all(c["tokens"] <= 300 * 1.8 for c in chunks)
def test_empty_input(self):
assert chunk_text("", doc_id=1) == []