from pathlib import Path from types import SimpleNamespace from openpyxl import Workbook from app.services.knowledge_cleaning.edkb_adapter import build_clean_result from app.services.knowledge_cleaning.tasks import _normalized_source_filename def test_build_clean_result_generates_markdown_for_xlsx(tmp_path): workbook = Workbook() sheet = workbook.active sheet.title = 'Sheet1' sheet.append(['部门', '指标']) sheet.append(['供应链', '能耗']) source_path = tmp_path / 'sample.xlsx' workbook.save(source_path) target_dir = tmp_path / 'output' result = build_clean_result(source_path, target_dir) cleaned_path = Path(result['cleaned_file_path']) assert result['mode'] == 'edkb_system' assert cleaned_path == target_dir / 'document.md' assert cleaned_path.exists() assert result['cleaned_preview_path'] == str(cleaned_path) content = cleaned_path.read_text(encoding='utf-8') assert content.strip() assert '供应链' in content assert '能耗' in content # Narrow Excel tables should expose Dify-friendly row boundaries. assert '\n\n|' in content or '| 供应链 | 能耗 |' in content def test_normalized_source_filename_infers_xlsx_extension(tmp_path): workbook = Workbook() workbook.active.append(['部门', '指标']) source_with_ext = tmp_path / 'sample.xlsx' workbook.save(source_with_ext) source_without_ext = tmp_path / 'xlsx' source_with_ext.rename(source_without_ext) version = SimpleNamespace(source_file_name='xlsx', source_file_ext=None) assert _normalized_source_filename(version, source_without_ext) == 'xlsx.xlsx'