# Conflicts: # backend/app/api/chat.py # backend/app/services/shipping_notice/chat_actions.py
143 lines
5.1 KiB
Python
143 lines
5.1 KiB
Python
"""
|
|
tests/test_notice_intake.py
|
|
|
|
用途:验证发货通知附件统一接入服务的抽取、截断与失败分流语义。
|
|
依赖:notice_upload.intake_notice_attachment、truncate_notice_text。
|
|
逻辑:保证单票/批量/重传三条入口共用同一 intake 结果模型,避免行为漂移。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
def test_truncate_notice_text_appends_warning(app):
|
|
"""正文超长时应截断并追加 notice_text_truncated warning。"""
|
|
from app.services.shipping_notice.notice_upload import truncate_notice_text
|
|
|
|
with app.app_context():
|
|
app.config["SHIPPING_NOTICE_NOTICE_TEXT_MAX_CHARS"] = 10
|
|
text, warnings = truncate_notice_text("abcdefghijklmnop", [])
|
|
|
|
assert text == "abcdefghij"
|
|
assert warnings == ["notice_text_truncated:10"]
|
|
|
|
|
|
def test_intake_notice_attachment_accepts_txt(app, tmp_path):
|
|
"""任务级接入应落盘、抽取并标记 accepted。"""
|
|
from app.services.shipping_notice.notice_upload import intake_notice_attachment
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
with app.app_context():
|
|
result = intake_notice_attachment(
|
|
filename="notice-a.txt",
|
|
raw="客户:ACME\nA001 x 10".encode("utf-8"),
|
|
source_file_type="text/plain",
|
|
storage="task",
|
|
task_no="SN-INTAKE-001",
|
|
)
|
|
|
|
assert result.accepted is True
|
|
assert result.status == "accepted"
|
|
assert "A001" in result.text
|
|
assert result.to_notice_item()["source_file_name"] == "notice-a.txt"
|
|
assert result.to_notice_upload_meta()["filename"] == "notice-a.txt"
|
|
assert result.saved_path is not None
|
|
assert result.saved_path.exists()
|
|
|
|
|
|
def test_intake_notice_attachment_empty_text_is_rejected(app, tmp_path, monkeypatch):
|
|
"""抽取空文本时应标记 empty_text,供批量入口分流到 rejected_files。"""
|
|
from app.services.shipping_notice import notice_upload
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
monkeypatch.setattr(
|
|
notice_upload,
|
|
"extract_notice_text",
|
|
lambda saved_path: ("", {"warnings": ["ocr_empty"], "file_type": "pdf"}),
|
|
)
|
|
|
|
with app.app_context():
|
|
result = notice_upload.intake_notice_attachment(
|
|
filename="scan.pdf",
|
|
raw=b"%PDF-1.4",
|
|
source_file_type="application/pdf",
|
|
storage="temp",
|
|
temp_request_id="req-empty",
|
|
temp_index=1,
|
|
)
|
|
|
|
assert result.accepted is False
|
|
assert result.status == "empty_text"
|
|
rejected = result.to_rejected_file_summary()
|
|
assert rejected["reason"] == "empty_text"
|
|
assert rejected["warnings"] == ["ocr_empty"]
|
|
|
|
|
|
def test_intake_temp_notice_uploads_splits_accepted_and_rejected(app, tmp_path, monkeypatch):
|
|
"""批量接入应分别返回 accepted / rejected,并生成统一 file_summary。"""
|
|
from app.services.shipping_notice import notice_upload
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
|
|
def fake_extract(saved_path):
|
|
name = str(saved_path).replace("\\", "/")
|
|
if name.endswith("/notice-a.txt"):
|
|
return "客户:ACME\nA001 x 10", {"warnings": [], "file_type": "txt"}
|
|
return "", {"warnings": ["ocr_empty"], "file_type": "pdf"}
|
|
|
|
monkeypatch.setattr(notice_upload, "extract_notice_text", fake_extract)
|
|
|
|
class _Upload:
|
|
def __init__(self, name: str, raw: bytes, mimetype: str):
|
|
self.filename = name
|
|
self._raw = raw
|
|
self.mimetype = mimetype
|
|
|
|
def read(self):
|
|
return self._raw
|
|
|
|
uploads = [
|
|
_Upload("notice-a.txt", b"ok", "text/plain"),
|
|
_Upload("notice-b.pdf", b"%PDF-1.4", "application/pdf"),
|
|
]
|
|
|
|
with app.app_context():
|
|
accepted, rejected = notice_upload.intake_temp_notice_uploads("req-batch", uploads)
|
|
summary = notice_upload.build_notice_file_summary(
|
|
total_files=len(uploads),
|
|
accepted=accepted,
|
|
rejected=rejected,
|
|
)
|
|
|
|
assert len(accepted) == 1
|
|
assert len(rejected) == 1
|
|
assert summary["accepted_count"] == 1
|
|
assert summary["rejected_count"] == 1
|
|
assert summary["accepted_files"][0]["filename"] == "notice-a.txt"
|
|
assert summary["rejected_files"][0]["filename"] == "notice-b.pdf"
|
|
|
|
|
|
def test_intake_notice_attachment_extract_failure(app, tmp_path, monkeypatch):
|
|
"""抽取异常时应标记 extract_failed 并保留 notice_extract_failed warning。"""
|
|
from app.services.shipping_notice import notice_upload
|
|
|
|
app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path)
|
|
monkeypatch.setattr(
|
|
notice_upload,
|
|
"extract_notice_text",
|
|
lambda saved_path: (_ for _ in ()).throw(RuntimeError("ocr down")),
|
|
)
|
|
|
|
with app.app_context():
|
|
result = notice_upload.intake_notice_attachment(
|
|
filename="broken.pdf",
|
|
raw=BytesIO(b"%PDF").read(),
|
|
storage="task",
|
|
task_no="SN-INTAKE-FAIL",
|
|
)
|
|
|
|
assert result.accepted is False
|
|
assert result.status == "extract_failed"
|
|
assert result.warnings[0].startswith("notice_extract_failed:")
|