"""DocumentProfiler 测试(intent-understanding-layer 任务 4)。 覆盖需求 2.1–2.4 / 2.6:表类型识别、稳定性时序标注、多文档聚合、保留时间防护。 """ from __future__ import annotations from kernel.task_sheet import TableType from kernel.understanding import DocumentProfiler class _MockLLM: def __init__(self, out): self._out = out def profile_and_extract(self, text, goal): return self._out SL0010_TEXT = """=== File: SL-0010-3.xlsx === 有关物质检测结果 样品名称 名称 保留时间min RRT 总杂% SL-0010-20μg 右美托咪定 25.440 1.00 0.00 含量均匀度 含量% 99.19 """ def test_heuristic_profile_no_llm_sl0010(): profiler = DocumentProfiler(llm=None) profile = profiler.profile(SL0010_TEXT, ["SL-0010-3.xlsx"]) types = {t.table_type for t in profile.tables} assert TableType.RELATED_SUBSTANCES in types assert TableType.CONTENT_UNIFORMITY in types assert profile.has_stability_time_series is False assert any("未检测到稳定性" in n for n in profile.notes) def test_llm_profile_respects_retention_guard(): # LLM 错误地把含保留时间列的表标为稳定性时序 → 防护必须强制改回 False。 llm = _MockLLM({ "tables": [{ "table_type": "related_substances", "title": "有关物质", "source_document": "SL-0010-3.xlsx", "is_stability_time_series": True, "columns": ["保留时间min", "总杂%"], }], "intent": "descriptive_summary", "items": [], }) profile = DocumentProfiler(llm).profile(SL0010_TEXT, ["SL-0010-3.xlsx"]) assert profile.tables[0].is_stability_time_series is False assert profile.has_stability_time_series is False def test_multi_document_aggregation(): text = ( "=== File: a.xlsx ===\n有关物质检测结果\n总杂%\n0.1\n" "=== File: b.xlsx ===\n溶出曲线\n累积溶出度%\n95\n" ) profile = DocumentProfiler(llm=None).profile(text, ["a.xlsx", "b.xlsx"]) srcs = {t.source_document for t in profile.tables} assert "a.xlsx" in srcs assert "b.xlsx" in srcs def test_stability_time_series_detected_from_llm(): llm = _MockLLM({ "tables": [{ "table_type": "stability_time_series", "title": "长期稳定性", "is_stability_time_series": True, "columns": ["时间点(月)", "总杂%"], }], "intent": "shelf_life_extrapolation", "items": [], }) profile = DocumentProfiler(llm).profile("=== File: s.xlsx ===\n长期稳定性\n", ["s.xlsx"]) assert profile.has_stability_time_series is True