feat(agent): 立账凭证检核

(cherry picked from commit f9e2fb5efc1ec7bf4f6e52e08dd3591b4e77edd9)
This commit is contained in:
sophie committed 2026-08-06 10:05:46 +08:00
1 parent 9bcbf9d211
commit f615e56bb9
70 files changed
+4968 -108

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""backend/tests/voucher_check — 立账凭证检核单测包。"""
@@ -0,0 +1,30 @@
"""
backend/tests/voucher_check/test_period_and_incremental.py
用途:自然月边界与增量跳过决策相关单测。
依赖:pytest、erp_repository.current_period_bounds。
逻辑:固定时钟注入,验证 Asia/Shanghai 月初/下月边界。
"""
from __future__ import annotations
from datetime import datetime
from zoneinfo import ZoneInfo
from app.services.voucher_check.erp_repository import current_period_bounds
def test_current_period_bounds_shanghai_august():
now = datetime(2026, 8, 15, 10, 0, 0, tzinfo=ZoneInfo("Asia/Shanghai"))
period_month, next_month, period_start, next_start = current_period_bounds(now)
assert period_month.isoformat() == "2026-08-01"
assert next_month.isoformat() == "2026-09-01"
assert period_start.isoformat().startswith("2026-08-01T00:00:00")
assert next_start.isoformat().startswith("2026-09-01T00:00:00")
def test_current_period_bounds_december_rollover():
now = datetime(2026, 12, 31, 23, 30, 0, tzinfo=ZoneInfo("Asia/Shanghai"))
period_month, next_month, _, _ = current_period_bounds(now)
assert period_month.isoformat() == "2026-12-01"
assert next_month.isoformat() == "2027-01-01"
@@ -0,0 +1,106 @@
"""
backend/tests/voucher_check/test_share_and_fingerprint.py
用途:立账凭证检核目录模板、文件名精确匹配与指纹稳定性单测。
依赖:pytest、临时目录、voucher_check 领域模块。
逻辑:不连 Oracle/共享盘;覆盖精确匹配、0 字节齐全、子目录不计、指纹变化。
"""
from __future__ import annotations
from datetime import date
from pathlib import Path
import pytest
from app.core.errors import ValidationError
from app.services.voucher_check.config import (
RequiredFileRule,
validate_directory_template,
)
from app.services.voucher_check.fingerprint import build_material_fingerprint
from app.services.voucher_check.share_scanner import (
FileMeta,
match_required_files,
scan_invoice_folder,
)
def test_directory_template_rejects_unknown_placeholder():
with pytest.raises(ValidationError) as exc:
validate_directory_template("{root}/{YYYY}/{MM}/{invoice_no}/{extra}")
assert exc.value.code == "VOUCHER_CHECK.CONFIG_INVALID"
def test_exact_filename_match_and_zero_byte_counts():
rules = (
RequiredFileRule(
code="price_check_sheet",
name="核价单",
required=True,
accepted_filenames=("核价单.pdf",),
),
RequiredFileRule(
code="purchase_order",
name="采购单",
required=True,
accepted_filenames=("采购单.pdf",),
),
)
files = [
FileMeta(name="核价单.pdf", size=0, mtime_ns=1),
FileMeta(name="采购单.pdf", size=10, mtime_ns=2),
]
matched, missing = match_required_files(files, rules, case_sensitive=True)
assert matched["price_check_sheet"] == "核价单.pdf"
assert matched["purchase_order"] == "采购单.pdf"
assert missing == []
def test_subdir_same_name_does_not_count(tmp_path: Path):
"""直接子文件才命中;子目录内同名文件不算。"""
from app.services.voucher_check.config import VoucherCheckConfig
root = tmp_path / "vouche"
inv_dir = root / "2026" / "08" / "INV001"
inv_dir.mkdir(parents=True)
nested = inv_dir / "nested"
nested.mkdir()
(nested / "核价单.pdf").write_bytes(b"x")
(inv_dir / "采购单.pdf").write_bytes(b"y")
cfg = VoucherCheckConfig(
scope_code="HXJM",
directory_template="{root}/{YYYY}/{MM}/{invoice_no}",
required_files=(
RequiredFileRule("price_check_sheet", "核价单", True, ("核价单.pdf",)),
RequiredFileRule("purchase_order", "采购单", True, ("采购单.pdf",)),
),
filename_case_sensitive=True,
notify_missing_enabled=False,
notify_failure_enabled=False,
same_day_dedup_enabled=False,
local_root=str(root),
share_unc="",
healthcheck_relative_path=".hxops-share-ready",
erp_dsn="x",
erp_username="u",
erp_password="p",
)
result = scan_invoice_folder(
cfg,
root=str(root),
period_month=date(2026, 8, 1),
invoice_no="INV001",
)
assert result.result == "missing"
assert any(m["code"] == "price_check_sheet" for m in result.missing_materials)
assert result.matched_files.get("purchase_order") == "采购单.pdf"
def test_fingerprint_stable_and_changes_on_mtime():
a = build_material_fingerprint([("a.pdf", 1, 100), ("b.pdf", 2, 200)])
b = build_material_fingerprint([("b.pdf", 2, 200), ("a.pdf", 1, 100)])
assert a == b
c = build_material_fingerprint([("a.pdf", 1, 101), ("b.pdf", 2, 200)])
assert a != c
@@ -0,0 +1,52 @@
"""
backend/tests/voucher_check/test_tool_auth_and_openapi.py
用途:立账凭证检核 Tool OpenAPI 静态规格校验(不依赖 Flask venv)。
依赖:pytest、openapi_base.json。
逻辑:直接读 JSON,校验 operationId 与 securitySchemes。
"""
from __future__ import annotations
import json
from pathlib import Path
_SPEC = (
Path(__file__).resolve().parents[2]
/ "app"
/ "services"
/ "voucher_check"
/ "openapi_base.json"
)
def test_openapi_base_contains_expected_operations():
spec = json.loads(_SPEC.read_text(encoding="utf-8"))
paths = spec["paths"]
assert (
paths["/api/accounting-voucher-check/tools/tasks/{task_id}/missing"]["get"]["operationId"]
== "list_voucher_missing"
)
assert (
paths["/api/accounting-voucher-check/tools/runs/{run_id}/items"]["get"]["operationId"]
== "list_voucher_run_items"
)
assert (
paths["/api/accounting-voucher-check/tools/invoices/{invoice_no}/latest"]["get"]["operationId"]
== "get_voucher_invoice_latest"
)
assert "BearerAuth" in spec["components"]["securitySchemes"]
assert "IntegrationKey" in spec["components"]["securitySchemes"]
def test_docs_openapi_export_in_sync():
docs = (
Path(__file__).resolve().parents[3]
/ "docs"
/ "openapi"
/ "accounting_voucher_check_tools_openapi.json"
)
assert docs.is_file(), "请运行 python backend/scripts/export_voucher_check_openapi.py"
exported = json.loads(docs.read_text(encoding="utf-8"))
base = json.loads(_SPEC.read_text(encoding="utf-8"))
assert exported["paths"].keys() == base["paths"].keys()