外部单号进入异动档自然键,圈档按 T-2 成熟封顶,当前归档可单独重解析。同批带上 Crane 禁整页刷新、ODM 白名单降级、排程抽屉与执行记录日期窗,以及 YAML 库存源下线。 Co-authored-by: Cursor <cursoragent@cursor.com>
753 lines
27 KiB
Python
753 lines
27 KiB
Python
"""scripts/freight_rpa/dsv_report_parameters.py 单元测试。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import importlib.util
|
||
from pathlib import Path
|
||
|
||
|
||
def _load_module():
|
||
path = Path(__file__).resolve().parents[2] / "scripts" / "freight_rpa" / "connectors" / "dsv" / "dsv_report_parameters.py"
|
||
spec = importlib.util.spec_from_file_location("dsv_report_parameters_test", path)
|
||
assert spec and spec.loader
|
||
module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(module)
|
||
return module
|
||
|
||
|
||
def test_fill_dsv_owner_filter_defaults_to_reportviewer_owner_input():
|
||
"""Owner 未配置 selector 时,应写入 ReportViewer Owner 文本框。"""
|
||
module = _load_module()
|
||
filled: dict[str, str] = {}
|
||
|
||
class _FakeText:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def is_visible(self, timeout: int | None = None) -> bool:
|
||
return False
|
||
|
||
class _FakeFrame:
|
||
def evaluate(self, script: str, payload: dict[str, str], timeout: int | None = None) -> bool:
|
||
assert "disabled = false" in script
|
||
filled[payload["selector"]] = payload["value"]
|
||
return True
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
def get_by_text(self, text: str, exact: bool = False) -> _FakeText:
|
||
return _FakeText()
|
||
|
||
module.fill_dsv_filter_if_needed(
|
||
_FakePage(),
|
||
"FREIGHT_RPA_WORKER_DSV_OWNER_SELECTOR",
|
||
"Owner",
|
||
"CADU-0000000100",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
)
|
||
|
||
assert filled == {"#ReportViewerMain_ctl08_ctl03_txtValue": "CADU-0000000100"}
|
||
|
||
|
||
def test_fill_dsv_warehouse_select_uses_default_ddvalue():
|
||
"""Warehouse 未配置 selector 时,应对 ReportViewer select 执行 select_option。"""
|
||
module = _load_module()
|
||
selected: dict[str, str] = {}
|
||
|
||
class _FakeLocator:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def wait_for(self, *args, **kwargs) -> None:
|
||
return None
|
||
|
||
def evaluate(self, script: str, value: str | None = None):
|
||
if value is not None and "removeAttribute" in script:
|
||
selected["value"] = value
|
||
return True
|
||
if "realOptionCount" in script:
|
||
return {
|
||
"hasTarget": True,
|
||
"enabled": False,
|
||
"optionCount": 26,
|
||
"realOptionCount": 25,
|
||
}
|
||
if "aspNetDisabled" in script and "hasTarget" not in script:
|
||
return True
|
||
return False
|
||
|
||
def select_option(self, *, label: str, timeout: int) -> None:
|
||
selected["label"] = label
|
||
|
||
class _FakeReportRootLocator:
|
||
def count(self) -> int:
|
||
return 1
|
||
|
||
class _FakeFrame:
|
||
def locator(self, selector: str) -> _FakeLocator | _FakeReportRootLocator:
|
||
if selector == "#ReportViewerMain":
|
||
return _FakeReportRootLocator()
|
||
assert selector == "#ReportViewerMain_ctl08_ctl17_ddValue"
|
||
return _FakeLocator()
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
def get_by_text(self, text: str, exact: bool = False):
|
||
raise RuntimeError("not visible")
|
||
|
||
module.fill_dsv_filter_if_needed(
|
||
_FakePage(),
|
||
"FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR",
|
||
"Warehouse",
|
||
"BYAWSATL01",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
)
|
||
|
||
assert selected.get("value") == "BYAWSATL01" or selected.get("label") == "BYAWSATL01"
|
||
|
||
|
||
def test_fill_dsv_multi_select_parameter_supports_select_all_checkbox_dropdown():
|
||
"""AWSAMS Warehouse 这类 checkbox 下拉应支持 select_all 和显式 selector。"""
|
||
module = _load_module()
|
||
payloads: list[dict[str, object]] = []
|
||
|
||
class _FakeLocator:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def wait_for(self, *args, **kwargs) -> None:
|
||
return None
|
||
|
||
def input_value(self, timeout: int | None = None) -> str:
|
||
return ""
|
||
|
||
def click(self, timeout: int | None = None, force: bool = False) -> None:
|
||
return None
|
||
|
||
class _FakeReportRootLocator:
|
||
def count(self) -> int:
|
||
return 1
|
||
|
||
class _FakeFrame:
|
||
def locator(self, selector: str):
|
||
if selector == "#ReportViewerMain":
|
||
return _FakeReportRootLocator()
|
||
return _FakeLocator()
|
||
|
||
def evaluate(self, script: str, payload: dict[str, object]) -> bool:
|
||
payloads.append(payload)
|
||
return True
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
ok = module.fill_dsv_multi_select_parameter(
|
||
_FakePage(),
|
||
"Warehouse",
|
||
["RTM"],
|
||
selectors={
|
||
"text": "#ReportViewerMain_ctl08_ctl17_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl17_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl17_divDropDown",
|
||
},
|
||
select_all=True,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
)
|
||
|
||
assert ok is True
|
||
assert payloads[-1]["dropdownSelector"] == "#ReportViewerMain_ctl08_ctl17_divDropDown"
|
||
assert payloads[-1]["selectAll"] is True
|
||
|
||
|
||
def test_resolve_parameter_controls_jda_report_warehouse_multi_value():
|
||
"""JDA 版 On Hand Inventory by Location 的 Warehouse 模板应为多选框下拉(multi_value)。"""
|
||
module = _load_module()
|
||
controls = module.resolve_parameter_controls("On Hand Inventory by Location", "Warehouse")
|
||
assert controls["control"] == "multi_value"
|
||
assert controls["text"] == "#ReportViewerMain_ctl08_ctl17_txtValue"
|
||
assert controls["toggle"] == "#ReportViewerMain_ctl08_ctl17_ctl01"
|
||
assert controls["dropdown"] == "#ReportViewerMain_ctl08_ctl17_divDropDown"
|
||
|
||
|
||
def test_resolve_parameter_controls_3lv_report_warehouse_select():
|
||
"""3lv 报表的 Warehouse 模板应是原生 select(ddValue)。"""
|
||
module = _load_module()
|
||
controls = module.resolve_parameter_controls("On Hand Inventory By Location 3lv", "Warehouse")
|
||
assert controls == {
|
||
"control": "select",
|
||
"selector": "#ReportViewerMain_ctl08_ctl17_ddValue",
|
||
}
|
||
|
||
|
||
def test_resolve_parameter_controls_receipt_detail_warehouse_select():
|
||
"""Receipt Detail 的 Warehouse 应按实机 DOM 使用 ctl27_ddValue(非库存默认 ctl17)。"""
|
||
module = _load_module()
|
||
controls = module.resolve_parameter_controls("Receipt Detail", "Warehouse")
|
||
# 关键断言:与 2026-09-28 Bison/AWSBI 实机 DOM(data-parametername=p_dns_cd_wh)一致。
|
||
assert controls == {
|
||
"control": "select",
|
||
"selector": "#ReportViewerMain_ctl08_ctl27_ddValue",
|
||
}
|
||
|
||
|
||
def test_resolve_parameter_controls_receipt_detail_awsrtm_warehouse_multi_value():
|
||
"""Moerdijk/AWSRTM 收料 Receipt Detail 的 Warehouse 应为 ctl29 multi_value(对齐库存 JDA)。"""
|
||
module = _load_module()
|
||
controls = module.resolve_parameter_controls(
|
||
"Receipt Detail", "Warehouse", program_label="AWSRTM",
|
||
)
|
||
# 关键断言:2026-09-28 实机 DOM——txtValue + glyphui-downarrow + divDropDown。
|
||
assert controls == {
|
||
"control": "multi_value",
|
||
"text": "#ReportViewerMain_ctl08_ctl29_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl29_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl29_divDropDown",
|
||
}
|
||
# 无 program_label 时仍回落 AWSBI 原生 select,避免误伤。
|
||
assert module.resolve_parameter_controls("Receipt Detail", "Warehouse")["control"] == "select"
|
||
|
||
|
||
def test_resolve_parameter_controls_shipment_order_detail_owner_and_warehouse():
|
||
"""出站 Shipment Order Detail 的 Owner/Warehouse 应按实机 DOM 使用 multi_value,而不是库存默认 select。"""
|
||
module = _load_module()
|
||
owner = module.resolve_parameter_controls("Shipment Order Detail", "Owner")
|
||
warehouse = module.resolve_parameter_controls("Shipment Order Detail", "Warehouse")
|
||
# 关键断言:2026-10-19 AWS/Bison/AWSBI 实机——Owner ctl15、Warehouse ctl31,均为 txtValue 下拉。
|
||
assert owner == {
|
||
"control": "multi_value",
|
||
"text": "#ReportViewerMain_ctl08_ctl15_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl15_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl15_divDropDown",
|
||
}
|
||
assert warehouse == {
|
||
"control": "multi_value",
|
||
"text": "#ReportViewerMain_ctl08_ctl31_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl31_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl31_divDropDown",
|
||
}
|
||
|
||
|
||
def test_resolve_parameter_controls_unknown_report_empty():
|
||
"""未登记报表名应返回空配置,由调用方回退参数名默认值(保持旧行为)。"""
|
||
module = _load_module()
|
||
assert module.resolve_parameter_controls("On Hand Inventory by Location XX", "Warehouse") == {}
|
||
assert module.resolve_parameter_controls(None, "Warehouse") == {}
|
||
|
||
|
||
def test_fill_dsv_multi_select_select_all_via_report_template():
|
||
"""selectors 缺省时,应按报表模板解析控件并完成全选(AWSAMS Warehouse 场景)。"""
|
||
module = _load_module()
|
||
payloads: list[dict[str, object]] = []
|
||
|
||
class _FakeLocator:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def wait_for(self, *args, **kwargs) -> None:
|
||
return None
|
||
|
||
def input_value(self, timeout: int | None = None) -> str:
|
||
return ""
|
||
|
||
def click(self, timeout: int | None = None, force: bool = False) -> None:
|
||
return None
|
||
|
||
class _FakeReportRootLocator:
|
||
def count(self) -> int:
|
||
return 1
|
||
|
||
class _FakeFrame:
|
||
def locator(self, selector: str):
|
||
if selector == "#ReportViewerMain":
|
||
return _FakeReportRootLocator()
|
||
return _FakeLocator()
|
||
|
||
def evaluate(self, script: str, payload: dict[str, object]) -> bool:
|
||
payloads.append(payload)
|
||
return True
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
ok = module.fill_dsv_multi_select_parameter(
|
||
_FakePage(),
|
||
"Warehouse",
|
||
["RTM"],
|
||
selectors=None,
|
||
select_all=True,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="On Hand Inventory by Location",
|
||
)
|
||
|
||
assert ok is True
|
||
assert payloads[-1]["dropdownSelector"] == "#ReportViewerMain_ctl08_ctl17_divDropDown"
|
||
assert payloads[-1]["selectAll"] is True
|
||
|
||
|
||
def test_fill_dsv_filter_if_needed_jda_report_uses_template_multi_value():
|
||
"""单一值填充 Warehouse 时,JDA 报表应走模板登记的多选框下拉而非默认 select。"""
|
||
module = _load_module()
|
||
payloads: list[dict[str, object]] = []
|
||
|
||
class _FakeLocator:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def wait_for(self, *args, **kwargs) -> None:
|
||
return None
|
||
|
||
def input_value(self, timeout: int | None = None) -> str:
|
||
return ""
|
||
|
||
def click(self, timeout: int | None = None, force: bool = False) -> None:
|
||
return None
|
||
|
||
class _FakeReportRootLocator:
|
||
def count(self) -> int:
|
||
return 1
|
||
|
||
class _FakeFrame:
|
||
def locator(self, selector: str):
|
||
if selector == "#ReportViewerMain":
|
||
return _FakeReportRootLocator()
|
||
return _FakeLocator()
|
||
|
||
def evaluate(self, script: str, payload: dict[str, object]) -> bool:
|
||
payloads.append(payload)
|
||
return True
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
def get_by_text(self, text: str, exact: bool = False):
|
||
raise RuntimeError("not visible")
|
||
|
||
module.fill_dsv_filter_if_needed(
|
||
_FakePage(),
|
||
"FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR",
|
||
"Warehouse",
|
||
"RTM",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="On Hand Inventory by Location",
|
||
)
|
||
|
||
# payloads 会混入结构性解析传入的 label 字符串("Warehouse"),只断言 dict 载荷。
|
||
dict_payloads = [p for p in payloads if isinstance(p, dict)]
|
||
assert dict_payloads, "应进入多选框下拉填充路径而不是无痕失败"
|
||
assert any(
|
||
payload.get("dropdownSelector") == "#ReportViewerMain_ctl08_ctl17_divDropDown"
|
||
for payload in dict_payloads
|
||
), f"应命中 JDA ctl17 multi_value dropdown,实际 payloads={payloads!r}"
|
||
|
||
|
||
def test_awsrtm_multi_value_miss_must_not_fallback_to_default_ctl17_ddvalue(monkeypatch, capsys):
|
||
"""
|
||
根因回归:默认 Warehouse.selector=ctl17_ddValue 不得在模板 multi_value 命中后残留。
|
||
|
||
[断言意图] multi_value 失败时禁止走 ctl17_ddValue select/fill(08:46 RTM 偶发失败日志形态)。
|
||
"""
|
||
module = _load_module()
|
||
seen: list[tuple[str, str]] = []
|
||
|
||
monkeypatch.setattr(module, '_fill_multi_value_parameter', lambda *args, **kwargs: False)
|
||
|
||
def _fake_fill_select(page, label, value, selector, *, timeout_ms):
|
||
seen.append(('select', selector))
|
||
return False
|
||
|
||
def _fake_fill_input(root, selector, value):
|
||
seen.append(('input', selector))
|
||
return False
|
||
|
||
monkeypatch.setattr(module, '_fill_select_parameter', _fake_fill_select)
|
||
monkeypatch.setattr(module, '_fill_input_via_script', _fake_fill_input)
|
||
monkeypatch.setattr(module, '_find_locator', lambda page, selector: (None, None))
|
||
monkeypatch.setattr(module, '_probe_report_parameter_controls', lambda page: {'roots': 0, 'controls': []})
|
||
|
||
class _FakePage:
|
||
frames = []
|
||
|
||
def get_by_text(self, text: str, exact: bool = False):
|
||
raise RuntimeError('not visible')
|
||
|
||
module.fill_dsv_filter_if_needed(
|
||
_FakePage(),
|
||
'FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR',
|
||
'Warehouse',
|
||
'RTM',
|
||
env=lambda name, default='': default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name='Receipt Detail',
|
||
program_label='AWSRTM',
|
||
)
|
||
# [断言意图] 不得被默认 select 污染;允许回退到 ctl29_txtValue 文本路径。
|
||
assert not any(selector.endswith('ctl17_ddValue') for _, selector in seen), seen
|
||
assert not any(kind == 'select' for kind, _ in seen), seen
|
||
captured = capsys.readouterr()
|
||
assert 'ctl17_ddValue' not in captured.out + captured.err
|
||
|
||
|
||
def test_fill_dsv_filter_if_needed_receipt_awsrtm_uses_ctl29_multi_value():
|
||
"""AWSRTM 收料 Receipt Detail 的 Warehouse 应走 ctl29 multi_value,而非 AWSBI ctl27 select。"""
|
||
module = _load_module()
|
||
payloads: list[dict[str, object]] = []
|
||
|
||
class _FakeLocator:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def wait_for(self, *args, **kwargs) -> None:
|
||
return None
|
||
|
||
def input_value(self, timeout: int | None = None) -> str:
|
||
return ""
|
||
|
||
def click(self, timeout: int | None = None, force: bool = False) -> None:
|
||
return None
|
||
|
||
class _FakeReportRootLocator:
|
||
def count(self) -> int:
|
||
return 1
|
||
|
||
class _FakeFrame:
|
||
def locator(self, selector: str):
|
||
if selector == "#ReportViewerMain":
|
||
return _FakeReportRootLocator()
|
||
return _FakeLocator()
|
||
|
||
def evaluate(self, script: str, payload: dict[str, object]) -> bool:
|
||
payloads.append(payload)
|
||
return True
|
||
|
||
class _FakePage:
|
||
frames = [_FakeFrame()]
|
||
|
||
def get_by_text(self, text: str, exact: bool = False):
|
||
raise RuntimeError("not visible")
|
||
|
||
module.fill_dsv_filter_if_needed(
|
||
_FakePage(),
|
||
"FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR",
|
||
"Warehouse",
|
||
"AWSRTM",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="Receipt Detail",
|
||
program_label="AWSRTM",
|
||
)
|
||
|
||
# payloads 会混入结构性解析传入的 label 字符串,只断言 dict 载荷。
|
||
dict_payloads = [p for p in payloads if isinstance(p, dict)]
|
||
assert dict_payloads, "应进入 AWSRTM multi_value 填充路径"
|
||
assert any(
|
||
payload.get("dropdownSelector") == "#ReportViewerMain_ctl08_ctl29_divDropDown"
|
||
for payload in dict_payloads
|
||
), f"应命中 ctl29 multi_value dropdown,实际 payloads={payloads!r}"
|
||
|
||
|
||
def test_fill_multi_value_js_uses_entry_text_not_bare_text():
|
||
"""multi_value 勾选脚本须用 entry.text,禁止裸引用 text(AWSRTM ReferenceError 根因)。"""
|
||
import inspect
|
||
|
||
module = _load_module()
|
||
source = inspect.getsource(module._fill_multi_value_parameter)
|
||
assert "entry.text" in source
|
||
# 禁止在 checkbox 循环里出现未绑定的 `text`(旧脚本 ReferenceError)。
|
||
assert "desiredValues.includes(text" not in source
|
||
assert "normalize(text)" not in source or "entryText" in source
|
||
|
||
|
||
class _RecordingFrame:
|
||
"""脚本化 frame:evaluate 返回预置结果或抛预置异常,并记录收到的 label。"""
|
||
|
||
def __init__(self, result=None, error: Exception | None = None):
|
||
self._result = result
|
||
self._error = error
|
||
self.calls: list[str] = []
|
||
|
||
def evaluate(self, script: str, label: str):
|
||
self.calls.append(label)
|
||
if self._error is not None:
|
||
raise self._error
|
||
return self._result
|
||
|
||
|
||
class _RecordingPage:
|
||
def __init__(self, frames):
|
||
self.frames = frames
|
||
|
||
|
||
_STRUCTURAL_CONTROLS = {
|
||
"control": "multi_value",
|
||
"text": "#ReportViewerMain_ctl08_ctl15_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl15_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl15_divDropDown",
|
||
}
|
||
|
||
_STRUCTURAL_SELECT = {
|
||
"control": "select",
|
||
"selector": "#ReportViewerMain_ctl08_ctl19_ddValue",
|
||
}
|
||
|
||
|
||
def test_structural_resolver_hit_returns_controls_and_trims_label():
|
||
"""命中 frame 返回 multi_value 三件套(含 control);label 先 trim 再传入页面脚本。"""
|
||
module = _load_module()
|
||
early = _RecordingFrame(result=None)
|
||
hit = _RecordingFrame(result=dict(_STRUCTURAL_CONTROLS))
|
||
page = _RecordingPage([early, hit])
|
||
|
||
got = module.resolve_parameter_controls_structural(page, " Owner ")
|
||
|
||
assert got == _STRUCTURAL_CONTROLS
|
||
assert hit.calls == ["Owner"]
|
||
|
||
|
||
def test_structural_resolver_select_via_label_for_returns_ddvalue():
|
||
"""Cincinnati CVG611 形态:label[for]=ctl19_ddValue 时返回 select,禁止再当 multi_value。
|
||
|
||
[断言意图] 2026-10-20 事故回归——Warehouse 是原生 select,不得落到 ctl17_txtValue
|
||
(已漂移为 Project ID)文本兜底。
|
||
"""
|
||
module = _load_module()
|
||
hit = _RecordingFrame(result=dict(_STRUCTURAL_SELECT))
|
||
page = _RecordingPage([hit])
|
||
|
||
got = module.resolve_parameter_controls_structural(page, "Warehouse")
|
||
|
||
assert got == _STRUCTURAL_SELECT
|
||
assert hit.calls == ["Warehouse"]
|
||
|
||
|
||
def test_structural_resolver_all_frames_miss_or_error_returns_none():
|
||
"""全部 frame 未命中或探测异常(跨域/销毁帧):返回 None 交调用方回退。"""
|
||
module = _load_module()
|
||
page = _RecordingPage([
|
||
_RecordingFrame(result=None),
|
||
_RecordingFrame(error=RuntimeError("execution context destroyed")),
|
||
_RecordingFrame(result=None),
|
||
])
|
||
assert module.resolve_parameter_controls_structural(page, "Owner") is None
|
||
|
||
|
||
def test_structural_resolver_empty_label_short_circuits():
|
||
"""空标签短路:不打扰任何 frame。"""
|
||
module = _load_module()
|
||
frame = _RecordingFrame(result=dict(_STRUCTURAL_CONTROLS))
|
||
page = _RecordingPage([frame])
|
||
assert module.resolve_parameter_controls_structural(page, " ") is None
|
||
assert frame.calls == []
|
||
|
||
|
||
def test_structural_resolver_none_page_returns_none():
|
||
"""无页面上下文(纯函数调用):直接返回 None。"""
|
||
module = _load_module()
|
||
assert module.resolve_parameter_controls_structural(None, "Owner") is None
|
||
|
||
|
||
def test_fill_multi_select_template_miss_falls_back_to_structural(monkeypatch):
|
||
"""接线:报表模板未命中 Owner 时,落到结构性解析并把三件套传入 multi_value 填充。
|
||
|
||
[断言意图] 2026-10-08 事故回归:Owner@Receipt Detail@AWSRTM 未登记 ctl15 模板时,
|
||
不得静默失败弃填(导致全货主巨报表渲染超时),必须结构性解析后正常填充。
|
||
"""
|
||
module = _load_module()
|
||
captured: dict[str, object] = {}
|
||
|
||
def _fake_fill(page, label, values, *, timeout_ms, config_override, select_all):
|
||
captured.update(config_override)
|
||
return True
|
||
|
||
monkeypatch.setattr(module, "resolve_parameter_controls", lambda *a, **k: {})
|
||
probe = _RecordingFrame(result=dict(_STRUCTURAL_CONTROLS))
|
||
page = _RecordingPage([probe])
|
||
monkeypatch.setattr(module, "_fill_multi_value_parameter", _fake_fill)
|
||
|
||
ok = module.fill_dsv_multi_select_parameter(
|
||
page, "Owner", ["CRTM-0000000100"], select_all=False,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="Receipt Detail", program_label="AWSRTM",
|
||
)
|
||
|
||
assert ok is True
|
||
assert captured == {"control": "multi_value", **_STRUCTURAL_CONTROLS}
|
||
assert probe.calls == ["Owner"]
|
||
|
||
|
||
def test_fill_multi_select_structural_preferred_registry_is_fallback(monkeypatch):
|
||
"""优先级:结构性解析优先(ctl 编号随渲染漂移,注册表 ctl 条目只作回退)。
|
||
|
||
[断言意图] 结构性命中时使用结构性三件套、注册表回退不被消费;
|
||
结构性未命中时才回退注册表控件。
|
||
"""
|
||
module = _load_module()
|
||
registry_controls = {
|
||
"text": "#ReportViewerMain_ctl08_ctl29_txtValue",
|
||
"toggle": "#ReportViewerMain_ctl08_ctl29_ctl01",
|
||
"dropdown": "#ReportViewerMain_ctl08_ctl29_divDropDown",
|
||
}
|
||
resolve_calls: list[tuple] = []
|
||
monkeypatch.setattr(
|
||
module, "resolve_parameter_controls",
|
||
lambda *a, **k: (resolve_calls.append(a), dict(registry_controls))[1],
|
||
)
|
||
probe = _RecordingFrame(result=dict(_STRUCTURAL_CONTROLS))
|
||
page = _RecordingPage([probe])
|
||
captured: dict[str, object] = {}
|
||
|
||
def _fake_fill(page, label, values, *, timeout_ms, config_override, select_all):
|
||
captured.update(config_override)
|
||
return True
|
||
|
||
monkeypatch.setattr(module, "_fill_multi_value_parameter", _fake_fill)
|
||
|
||
ok = module.fill_dsv_multi_select_parameter(
|
||
page, "Warehouse", ["RTM"], select_all=False,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="Receipt Detail", program_label="AWSRTM",
|
||
)
|
||
|
||
assert ok is True
|
||
assert captured.get("text") == _STRUCTURAL_CONTROLS["text"] # 结构性解析优先生效
|
||
assert resolve_calls == [] # 结构性命中:注册表回退未被消费
|
||
|
||
# 结构性未命中 → 回退注册表控件
|
||
probed = _RecordingFrame(result=None)
|
||
page = _RecordingPage([probed])
|
||
captured.clear()
|
||
ok = module.fill_dsv_multi_select_parameter(
|
||
page, "Warehouse", ["RTM"], select_all=False,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="Receipt Detail", program_label="AWSRTM",
|
||
)
|
||
assert ok is True
|
||
assert captured.get("text") == registry_controls["text"] # 回退到注册表
|
||
assert probed.calls == ["Warehouse"]
|
||
|
||
|
||
def test_fill_warehouse_uses_structural_select_not_stale_txtvalue(monkeypatch, capsys):
|
||
"""Warehouse 结构性命中 select 时走 ddValue,禁止 JDA 模板 ctl17_txtValue 文本兜底。
|
||
|
||
[断言意图] 复现 2026-10-20 Cincinnati:模板仍写 multi_value@ctl17,但页面 Warehouse
|
||
是 ctl19_ddValue;不得把 BYAWSCV611 写进漂移后的 Project ID。
|
||
"""
|
||
module = _load_module()
|
||
page = _RecordingPage([_RecordingFrame(result=dict(_STRUCTURAL_SELECT))])
|
||
selected: dict[str, str] = {}
|
||
|
||
class _FakeText:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def is_visible(self, timeout: int | None = None) -> bool:
|
||
return False
|
||
|
||
page.get_by_text = lambda text, exact=False: _FakeText() # type: ignore[attr-defined]
|
||
|
||
def _fake_select(page_obj, label, value, selector, *, timeout_ms):
|
||
selected["selector"] = selector
|
||
selected["value"] = value
|
||
return True
|
||
|
||
def _fake_multi(*_a, **_k):
|
||
raise AssertionError("select 形态不得走 multi_value")
|
||
|
||
monkeypatch.setattr(module, "_fill_select_parameter", _fake_select)
|
||
monkeypatch.setattr(module, "_fill_multi_value_parameter", _fake_multi)
|
||
|
||
ok = module.fill_dsv_filter_if_needed(
|
||
page,
|
||
"FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR",
|
||
"Warehouse",
|
||
"BYAWSCV611",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="On Hand Inventory By Location",
|
||
program_label="CVG611",
|
||
)
|
||
|
||
assert ok is True
|
||
assert selected == {
|
||
"selector": "#ReportViewerMain_ctl08_ctl19_ddValue",
|
||
"value": "BYAWSCV611",
|
||
}
|
||
captured = capsys.readouterr()
|
||
assert "ctl17_txtValue" not in captured.out + captured.err
|
||
|
||
|
||
def test_fill_warehouse_refuses_stale_txtvalue_when_structural_miss(monkeypatch, capsys):
|
||
"""结构性未命中且模板 multi_value 失败时:Warehouse 拒绝 txtValue 文本兜底并返回 False。"""
|
||
module = _load_module()
|
||
page = _RecordingPage([_RecordingFrame(result=None)])
|
||
|
||
class _FakeText:
|
||
first = None
|
||
|
||
def __init__(self) -> None:
|
||
self.first = self
|
||
|
||
def is_visible(self, timeout: int | None = None) -> bool:
|
||
return False
|
||
|
||
page.get_by_text = lambda text, exact=False: _FakeText() # type: ignore[attr-defined]
|
||
monkeypatch.setattr(module, "_fill_multi_value_parameter", lambda *_a, **_k: False)
|
||
monkeypatch.setattr(module, "_probe_report_parameter_controls", lambda *_a, **_k: {"controls": []})
|
||
filled_via_script: list[str] = []
|
||
monkeypatch.setattr(
|
||
module,
|
||
"_fill_input_via_script",
|
||
lambda root, selector, text: filled_via_script.append(selector) or True,
|
||
)
|
||
|
||
ok = module.fill_dsv_filter_if_needed(
|
||
page,
|
||
"FREIGHT_RPA_WORKER_DSV_WAREHOUSE_SELECTOR",
|
||
"Warehouse",
|
||
"BYAWSCV611",
|
||
env=lambda name, default="": default,
|
||
browser_timeout_ms=lambda: 3_000,
|
||
report_name="On Hand Inventory By Location",
|
||
)
|
||
|
||
assert ok is False
|
||
assert filled_via_script == []
|
||
captured = capsys.readouterr()
|
||
assert "refusing stale txtValue fallback" in captured.err
|
||
|
||
|
||
def test_multi_value_already_set_set_equality_not_substring():
|
||
"""2026-10-08 Owner@AWSRTM 事故根因回归:SSRS 多选框默认全选,txtValue 恒为全量 CSV,
|
||
目标值必为该 CSV 的子串——必须用集合相等判定,子串包含会恒误判"已设置"并跳过下拉勾选。"""
|
||
module = _load_module()
|
||
all_owners_csv = ",".join(f"CRTM-0000000{n:03d}" for n in range(5, 449))
|
||
|
||
# [断言意图] 目标值是全选 CSV 的"子串"但非"全集"——不得判已设置(旧 bug 行为)。
|
||
assert module._multi_value_already_set(all_owners_csv, ["CRTM-0000000100"]) is False
|
||
# 完全相等的选中集合才允许跳过。
|
||
assert module._multi_value_already_set("CRTM-0000000100", ["CRTM-0000000100"]) is True
|
||
assert module._multi_value_already_set("CRTM-0000000100, CRTM-0000000101",
|
||
["CRTM-0000000101", "CRTM-0000000100"]) is True
|
||
# 边界:空当前值/空目标不判已设置;大小写与空白不敏感。
|
||
assert module._multi_value_already_set("", ["CRTM-0000000100"]) is False
|
||
assert module._multi_value_already_set(all_owners_csv, []) is False
|
||
assert module._multi_value_already_set(" crtm-0000000100 ", ["CRTM-0000000100"]) is True
|