Files
mini-openclaw-v2/backend/test_assistant_materials.py
qiaoqichaoandClaude Code d56c699ce6 chore(v2): 从兴邦预审v2白名单抽取独立可运行基座
按 docs/v2/05-implementation-plan.md §1 候选迁移表与 claude_use/T00 白名单,
从 55e2cdb 抽取 224 个文件(2.48 MB)到独立目录 F:/mini-OpenClaw-v2。

- 排除:凭据(.env/server-deploy.conf/旧DELIVERY.md)、运行数据(xingbang_data 等
  4 个 SQLite)、依赖(venv/node_modules 符号链接)、归档(_backup*/test-evidence/
  ori_file)、Office 临时文件
- 目标目录独立 git 仓库,初始恢复点;Gitea 远程与分支待 OQ-06 Q2 定案
- 未部署、未启动服务、未调用模型

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-29 12:58:26 +08:00

74 lines
4.1 KiB
Python

import io
import unittest
import zipfile
from unittest.mock import patch
from services.assistant_materials import material, apply_candidate, demo_assets
from services.skill_executor import execute_skill
class MaterialTests(unittest.TestCase):
def test_docx_paragraph_table_locators(self):
from docx import Document
doc = Document()
doc.add_paragraph("collection.evidence:到账记录")
table = doc.add_table(rows=1, cols=2)
table.cell(0, 0).text = "due_diligence.liabilities"
table.cell(0, 1).text = "13200"
buffer = io.BytesIO()
doc.save(buffer)
result = material("P", "材料.docx", buffer.getvalue(), "tester")
locators = [c["locator"] for c in result["chunks"]]
self.assertEqual(locators, ["正文第1段", "表1第1行"])
self.assertEqual(len(result["candidates"]), 2)
self.assertNotIn("页", "".join(locators))
def test_xlsx_formula_without_cache_is_not_calculated(self):
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as z:
z.writestr("xl/workbook.xml", '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="财务" sheetId="1" r:id="rId1"/></sheets></workbook>')
z.writestr("xl/_rels/workbook.xml.rels", '<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>')
z.writestr("xl/worksheets/sheet1.xml", '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>financial.total_liabilities</t></is></c><c r="B1"><f>SUM(B2:B3)</f></c></row></sheetData></worksheet>')
result = material("P", "财报.xlsx", buffer.getvalue(), "tester")
self.assertEqual(result["status"], "partial")
self.assertIn("财务!B1公式无缓存", result["warnings"][0])
self.assertIsNone(result["candidates"][0]["value"])
def test_pdf_page_and_scan_failure(self):
import fitz
with fitz.open() as pdf:
pdf.new_page().insert_text((40, 50), "collection.evidence: receipt-001")
pdf.new_page()
content = pdf.tobytes()
with patch.dict("os.environ", {"XINGBANG_OCR_TESSDATA": ""}):
result = material("P", "材料.pdf", content, "tester")
self.assertEqual([c["locator"] for c in result["chunks"]], ["第1页", "第2页"])
self.assertEqual(result["status"], "partial")
self.assertIn("OCR", result["warnings"][0])
def test_asset_dedup_does_not_inflate_amount(self):
result = demo_assets({"id": "P", "facts": {"lease_asset_value": "5400", "duplicate_asset_count": 1}})
candidates = {c["field"]: c for c in result["candidates"]}
self.assertEqual(candidates["lease.total_value"]["value"], "5400")
self.assertEqual(candidates["lease.duplicate_count"]["value"], "1")
self.assertTrue(candidates["lease.total_value"]["formula_version"])
def test_normalization_keeps_raw_and_rejects_mixed_currency(self):
project = {"id": "P"}
candidate = {"field": "financial.total_liabilities", "value": "132000000.01", "unit": "元", "currency": "CNY", "period": "2025", "scope": "合并"}
evidence = apply_candidate(project, candidate, "tester", "核对")
self.assertEqual(evidence["value"], "13200.000001")
self.assertEqual(evidence["raw_value"], "132000000.01")
self.assertEqual(evidence["unit"], "万元")
skill = {"id": "ratio", "version": "1", "engine": "ratio", "numerator": "a", "denominator": "b", "output": "r"}
with self.assertRaises(ValueError):
execute_skill(skill, {"a": "10", "b": "5"}, {"a": [{**evidence, "id": "a"}], "b": [{**evidence, "id": "b", "currency": "USD"}]})
def test_bad_zip_and_unsupported_macro_are_rejected(self):
for name in ("broken.docx", "macro.xlsm", "unknown.exe"):
with self.assertRaises(ValueError):
material("P", name, b"invalid", "tester")
if __name__ == "__main__":
unittest.main()