diff --git a/backend/tests/test_crane_report_export.py b/backend/tests/test_crane_report_export.py index be182bbf..2b60d053 100644 --- a/backend/tests/test_crane_report_export.py +++ b/backend/tests/test_crane_report_export.py @@ -596,3 +596,34 @@ def test_wait_and_click_scheduled_download_skips_fallback_on_click_failure(tmp_p download_dir=str(tmp_path), ) assert fallback_called["value"] is False + + +def test_row_matches_scheduled_report_normalizes_name_form(monkeypatch): + """行名 InboundDetails.xlsx 与期望名 Inbound Details 应规范化后相等(收料轮询 not found 根因)。""" + module = _load_export() + texts = ["InboundDetails.xlsx", "2026/9/18 22:02:53", "accf9483fabe", "Download"] + + class _Cell: + def __init__(self, text): + self._text = text + + def inner_text(self, timeout=None): + return self._text + + cells = MagicMock() + cells.count.return_value = len(texts) + cells.nth.side_effect = lambda i: _Cell(texts[i]) + row = MagicMock() + row.locator.return_value = cells + row.is_visible.return_value = True + + assert module._row_matches_scheduled_report( + row, description="accf9483fabe", expected_report_name="Inbound Details" + ) is True + # [断言意图] 描述不一致时不得匹配(唯一性由 UUID 保证) + assert module._row_matches_scheduled_report( + row, description="other", expected_report_name="Inbound Details" + ) is False + assert module._row_matches_scheduled_report( + row, description="accf9483fabe", expected_report_name="OnHandDetails.xlsx" + ) is False diff --git a/scripts/freight_rpa/connectors/crane/crane_report_export.py b/scripts/freight_rpa/connectors/crane/crane_report_export.py index 3279388f..717fa900 100644 --- a/scripts/freight_rpa/connectors/crane/crane_report_export.py +++ b/scripts/freight_rpa/connectors/crane/crane_report_export.py @@ -887,6 +887,22 @@ def _resolve_scheduled_report_file_name( return env_value or _CRANE_SCHEDULED_REPORT_NAME_DEFAULT +def _normalize_scheduled_report_name(value: Any) -> str: + """ + 规范化 Scheduled Reports 行名/期望名,用于相等比较。 + + 核心逻辑:门户行名形如 InboundDetails.xlsx,而适配器/配置侧为 Inbound Details 形态; + 直接精确比较永远不等(收料轮询曾因此 25 轮全部 not found)。统一去空白、忽略大小写、 + 去掉 .xlsx/.xls 扩展名后再比较。 + """ + text = str(value or "").strip().lower() + if text.endswith(".xlsx"): + text = text[: -len(".xlsx")] + elif text.endswith(".xls"): + text = text[: -len(".xls")] + return "".join(text.split()) + + def _row_matches_scheduled_report( row: Any, *, @@ -894,15 +910,15 @@ def _row_matches_scheduled_report( expected_report_name: str, ) -> bool: """ - 判断行是否为目标导出:Name 精确匹配且 Description 为本次 UUID。 + 判断行是否为目标导出:Name 规范化后相等且 Description 为本次 UUID。 核心逻辑:表按 Submitted 降序,自上而下取首条匹配即最近一笔。 """ - wanted_name = str(expected_report_name or "").strip() + wanted_name = _normalize_scheduled_report_name(expected_report_name) wanted_description = str(description or "").strip() if not wanted_name or not wanted_description: return False - name = _read_scheduled_row_cell(row, _CRANE_SCHEDULED_COL_NAME) + name = _normalize_scheduled_report_name(_read_scheduled_row_cell(row, _CRANE_SCHEDULED_COL_NAME)) if name != wanted_name: return False