外部单号进入异动档自然键,圈档按 T-2 成熟封顶,当前归档可单独重解析。同批带上 Crane 禁整页刷新、ODM 白名单降级、排程抽屉与执行记录日期窗,以及 YAML 库存源下线。 Co-authored-by: Cursor <cursoragent@cursor.com>
281 lines
11 KiB
Python
281 lines
11 KiB
Python
"""crane_navigation 报表页导航测试。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import importlib.util
|
||
from pathlib import Path
|
||
from unittest.mock import MagicMock
|
||
|
||
import pytest
|
||
|
||
|
||
def _load_navigation():
|
||
module_path = Path(__file__).resolve().parents[2] / "scripts" / "freight_rpa" / "connectors" / "crane" / "crane_navigation.py"
|
||
spec = importlib.util.spec_from_file_location("crane_navigation_test", module_path)
|
||
assert spec and spec.loader
|
||
module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(module)
|
||
return module
|
||
|
||
|
||
class _FakeLocator:
|
||
def __init__(self, *, visible: bool = True) -> None:
|
||
self.visible = visible
|
||
self.clicked = False
|
||
self.hovered = False
|
||
|
||
@property
|
||
def first(self) -> "_FakeLocator":
|
||
return self
|
||
|
||
def is_visible(self, timeout: int = 0) -> bool:
|
||
return self.visible
|
||
|
||
def click(self, timeout: int = 0, force: bool = False) -> None:
|
||
self.clicked = True
|
||
|
||
def hover(self, timeout: int = 0) -> None:
|
||
self.hovered = True
|
||
|
||
|
||
class _FakePage:
|
||
def __init__(
|
||
self,
|
||
*,
|
||
url: str = "https://wms-portal.craneww.com/#/",
|
||
visible_tokens: tuple[str, ...] = ("on-hand-details-query", "On Hand Details", "Queries", "navbar"),
|
||
visible_role_names: tuple[str, ...] = ("On Hand Details", "Queries"),
|
||
) -> None:
|
||
self.url = url
|
||
self.frames: list[_FakePage] = []
|
||
self.goto_calls: list[str] = []
|
||
self.evaluate_calls: list[tuple[str, object]] = []
|
||
self.reload_calls: list[int] = []
|
||
self.locator_selectors: list[str] = []
|
||
self._visible_tokens = visible_tokens
|
||
self._visible_role_names = visible_role_names
|
||
|
||
def locator(self, selector: str) -> _FakeLocator:
|
||
self.locator_selectors.append(selector)
|
||
visible = any(token in selector for token in self._visible_tokens)
|
||
return _FakeLocator(visible=visible)
|
||
|
||
def get_by_role(self, role: str, name: str, exact: bool = False) -> _FakeLocator:
|
||
return _FakeLocator(visible=name in self._visible_role_names)
|
||
|
||
def goto(self, url: str, wait_until: str = "", timeout: int = 0) -> None:
|
||
self.goto_calls.append(url)
|
||
self.url = url
|
||
|
||
def reload(self, wait_until: str = "", timeout: int = 0) -> None:
|
||
self.reload_calls.append(1)
|
||
|
||
def evaluate(self, script: str, arg: object = None) -> None:
|
||
self.evaluate_calls.append((script, arg))
|
||
if isinstance(arg, str):
|
||
self.url = f"https://wms-portal.craneww.com/#/{arg.lstrip('/')}"
|
||
|
||
def wait_for_load_state(self, state: str = "", timeout: int = 0) -> None:
|
||
return None
|
||
|
||
|
||
def test_navigate_prefers_dashboard_tile(monkeypatch):
|
||
"""应优先尝试首页目标报表卡片。"""
|
||
module = _load_navigation()
|
||
page = _FakePage()
|
||
calls: list[str] = []
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url: False)
|
||
monkeypatch.setattr(
|
||
module,
|
||
"_click_dashboard_report_tile",
|
||
lambda *args, **kwargs: calls.append("dashboard") or True,
|
||
)
|
||
monkeypatch.setattr(module, "_click_navbar_report_item", lambda *args, **kwargs: calls.append("navbar") or True)
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", lambda *args, **kwargs: True)
|
||
|
||
mode = module.navigate_to_crane_report(
|
||
page,
|
||
"https://wms-portal.craneww.com/#/on-hand-details-query",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=5_000,
|
||
)
|
||
|
||
assert mode == "dashboard_tile"
|
||
assert calls == ["dashboard"]
|
||
assert page.goto_calls == []
|
||
|
||
|
||
def test_navigate_falls_back_to_navbar_when_dashboard_misses(monkeypatch):
|
||
"""首页卡片未进入报表时应回退顶部 Queries 菜单。"""
|
||
module = _load_navigation()
|
||
page = _FakePage()
|
||
wait_calls = {"n": 0}
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url: False)
|
||
monkeypatch.setattr(module, "_click_dashboard_report_tile", lambda *args, **kwargs: True)
|
||
monkeypatch.setattr(module, "_click_navbar_report_item", lambda *args, **kwargs: True)
|
||
|
||
def _wait(page_arg, entry_url, *, browser_timeout_ms):
|
||
wait_calls["n"] += 1
|
||
return wait_calls["n"] >= 2
|
||
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", _wait)
|
||
|
||
mode = module.navigate_to_crane_report(
|
||
page,
|
||
"https://wms-portal.craneww.com/#/on-hand-details-query",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=5_000,
|
||
)
|
||
|
||
assert mode == "navbar_menu"
|
||
assert wait_calls["n"] == 2
|
||
|
||
|
||
def test_direct_goto_last_resort_assigns_hash_in_place(monkeypatch):
|
||
"""卡片与菜单均失败后:goto entry_url 未就绪时只允许就地赋 hash,禁止回根地址。"""
|
||
module = _load_navigation()
|
||
page = _FakePage(url="https://wms-portal.craneww.com/#/on-hand-details-query")
|
||
entry = "https://wms-portal.craneww.com/#/outbound-details-query"
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url_arg: False)
|
||
monkeypatch.setattr(module, "_click_dashboard_report_tile", lambda *args, **kwargs: False)
|
||
monkeypatch.setattr(module, "_click_navbar_report_item", lambda *args, **kwargs: False)
|
||
|
||
def _wait(page_arg, entry_url_arg, *, browser_timeout_ms):
|
||
# [断言意图] 就地赋 hash 之后表单才视为就绪;此前一律未就绪
|
||
return len(page.evaluate_calls) > 0
|
||
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", _wait)
|
||
|
||
mode = module.navigate_to_crane_report(
|
||
page,
|
||
entry,
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=5_000,
|
||
)
|
||
|
||
assert mode == "direct_goto"
|
||
assert page.goto_calls == [entry]
|
||
assert page.reload_calls == []
|
||
assert not any(call.rstrip("/") == "https://wms-portal.craneww.com" for call in page.goto_calls)
|
||
assert page.evaluate_calls and "outbound-details-query" in str(page.evaluate_calls[0][1])
|
||
|
||
|
||
def test_navigation_never_lands_on_origin_when_form_never_ready(monkeypatch):
|
||
"""表单始终未就绪也应抛 RuntimeError,且全程不得 goto 根地址或 reload(会强制登出)。"""
|
||
module = _load_navigation()
|
||
page = _FakePage(url="https://wms-portal.craneww.com/#/on-hand-details-query")
|
||
entry = "https://wms-portal.craneww.com/#/outbound-details-query"
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url_arg: False)
|
||
monkeypatch.setattr(module, "_click_dashboard_report_tile", lambda *args, **kwargs: False)
|
||
monkeypatch.setattr(module, "_click_navbar_report_item", lambda *args, **kwargs: False)
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", lambda *args, **kwargs: False)
|
||
|
||
with pytest.raises(RuntimeError, match="navigation failed"):
|
||
module.navigate_to_crane_report(
|
||
page,
|
||
entry,
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=5_000,
|
||
)
|
||
|
||
# [断言意图] 失败收场也只允许一次 goto(entry):根地址/reload 一旦发生即登出
|
||
assert page.goto_calls == [entry]
|
||
assert page.reload_calls == []
|
||
assert not any(call.rstrip("/") == "https://wms-portal.craneww.com" for call in page.goto_calls)
|
||
|
||
|
||
def test_is_crane_on_report_route_reads_entry_hash():
|
||
"""应从 entry_url hash 判断当前是否已在目标报表路由。"""
|
||
module = _load_navigation()
|
||
page = _FakePage(url="https://wms-portal.craneww.com/#/on-hand-details-query")
|
||
entry = "https://wms-portal.craneww.com/#/on-hand-details-query"
|
||
assert module.is_crane_on_report_route(page, entry) is True
|
||
|
||
|
||
def test_homepage_is_not_report_form_ready():
|
||
"""登录后首页 /#/ 不应被误判为报表表单已就绪。"""
|
||
module = _load_navigation()
|
||
page = _FakePage(url="https://wms-portal.craneww.com/#/")
|
||
entry = "https://wms-portal.craneww.com/#/on-hand-details-query"
|
||
assert module.is_crane_on_homepage(page) is True
|
||
assert module.is_crane_report_form_ready(page, entry) is False
|
||
|
||
|
||
def test_report_route_without_controls_is_not_ready(monkeypatch):
|
||
"""仅有 hash 路由但无 Run Report 控件时仍不算 ready。"""
|
||
module = _load_navigation()
|
||
page = _FakePage(url="https://wms-portal.craneww.com/#/on-hand-details-query")
|
||
entry = "https://wms-portal.craneww.com/#/on-hand-details-query"
|
||
monkeypatch.setattr(module, "_has_report_form_controls", lambda page_arg: False)
|
||
assert module.is_crane_report_form_ready(page, entry) is False
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
("route", "label"),
|
||
(
|
||
("on-hand-details-query", "On Hand Details"),
|
||
("inbound-details-query", "Inbound Details"),
|
||
("outbound-details-query", "Outbound Details"),
|
||
),
|
||
)
|
||
def test_navigate_targets_tile_by_entry_route(route, label, monkeypatch):
|
||
"""首页卡片/菜单选择器必须由 entry 路由推导,禁止硬编码 On Hand Details。"""
|
||
module = _load_navigation()
|
||
# [构造] 门户对授权用户渲染目标报表卡片:当前路由/标签的选择器可见
|
||
page = _FakePage(
|
||
url="https://wms-portal.craneww.com/#/",
|
||
visible_tokens=(route, label, "Queries"),
|
||
)
|
||
entry = f"https://wms-portal.craneww.com/#/{route}"
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url_arg: False)
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", lambda *args, **kwargs: True)
|
||
|
||
mode = module.navigate_to_crane_report(
|
||
page,
|
||
entry,
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=5_000,
|
||
)
|
||
|
||
assert mode == "dashboard_tile"
|
||
joined = "\n".join(page.locator_selectors)
|
||
assert label in joined
|
||
# [断言意图] 出站/入站导航不得再查询 On Hand Details 选择器(2026-10-20 死循环根因之一);
|
||
# 路由推导由 test_direct_goto_last_resort(evaluate 参数)单独覆盖
|
||
for other in {"On Hand Details", "Inbound Details", "Outbound Details"} - {label}:
|
||
assert other not in joined
|
||
|
||
|
||
def test_navbar_fallback_uses_entry_route_label(monkeypatch):
|
||
"""首页卡片不可见时,经 Queries 菜单点击的路由项也须由 entry 推导。"""
|
||
module = _load_navigation()
|
||
# [构造] 卡片全部不可见(token 只含 navbar),仅 Queries 下拉项可见;
|
||
# 首页等待用短超时避免 shell 未就绪时白等 5s
|
||
page = _FakePage(
|
||
url="https://wms-portal.craneww.com/#/",
|
||
visible_tokens=("navbar",),
|
||
visible_role_names=("Queries",),
|
||
)
|
||
entry = "https://wms-portal.craneww.com/#/outbound-details-query"
|
||
|
||
monkeypatch.setattr(module, "is_crane_report_form_ready", lambda page_arg, entry_url_arg: False)
|
||
monkeypatch.setattr(module, "_wait_for_report_navigation", lambda *args, **kwargs: True)
|
||
|
||
mode = module.navigate_to_crane_report(
|
||
page,
|
||
entry,
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=1_000,
|
||
)
|
||
|
||
assert mode == "navbar_menu"
|
||
joined = "\n".join(page.locator_selectors)
|
||
# [断言意图] 菜单回退路径同样按 entry 推导标签,禁止再查询 On Hand Details 选择器
|
||
assert '.navbar-dropdown a.navbar-item:has-text("Outbound Details")' in joined
|
||
assert "On Hand Details" not in joined
|