feat(queue): 添加旧知识库任务入队兼容入口
- 增加 enqueue_kb_job 和 enqueue_clean_job 函数以支持旧 KB 清洗任务入队 - 在 enqueue 函数中处理 KB_CLEAN 和 KB_DIFY_SYNC 类型的任务 - 引入 ValidationError 以处理 job_id 缺失的情况 feat(shipping_notice): 更新库存 RPA 工作流逻辑 - 修改 ensure_inventory_via_rpa 函数以支持手动重试逻辑 - 优化库存导出触发和状态更新的消息提示 fix(i18n): 更新任务未找到的提示信息 - 修改中文和英文提示,明确用户操作要求 feat(shipping_notice): 增强任务解析逻辑 - 添加缺失 Reservation ID 的检查,阻止任务进入确认阶段 - 更新 get_task_status 函数以处理多票会话的任务解析
This commit is contained in:
1 parent
eb0466245b
commit
4cd987eef6
45 files changed
+2748
-1406
No files matched your search
@@ -0,0 +1,241 @@
|
||||
"""scripts/rpa/freight_rpa_worker.py tests.
|
||||
|
||||
覆盖货代 RPA worker 对真实库存导出 trigger JSON 的消费路径:
|
||||
1. 读取 inventory_export_SN-20260629-2EDADC77.json。
|
||||
2. 按 DSV360/AWSDU 库存源分发下载。
|
||||
3. 成功后回调 hxops,并把 trigger 移入 processed。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from openpyxl import load_workbook
|
||||
|
||||
|
||||
def _repo_root() -> Path:
|
||||
return Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
def _load_freight_rpa_worker():
|
||||
module_path = _repo_root() / "scripts" / "rpa" / "freight_rpa_worker.py"
|
||||
spec = importlib.util.spec_from_file_location("freight_rpa_worker_under_test", module_path)
|
||||
assert spec is not None
|
||||
assert spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_inventory_export_trigger_2edadc77_reports_success(monkeypatch, tmp_path):
|
||||
"""基于失败样本 SN-20260629-2EDADC77 验证 RPA 库存导出 trigger 可被正常消费。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
source_trigger = (
|
||||
_repo_root()
|
||||
/ "backend"
|
||||
/ "data"
|
||||
/ "shipping_notice"
|
||||
/ "freight_rpa"
|
||||
/ "inventory_triggers"
|
||||
/ "failed"
|
||||
/ "inventory_export_SN-20260629-2EDADC77.json"
|
||||
)
|
||||
trigger_dir = tmp_path / "inventory_triggers"
|
||||
trigger_dir.mkdir()
|
||||
trigger_path = trigger_dir / source_trigger.name
|
||||
trigger_path.write_text(source_trigger.read_text(encoding="utf-8"), encoding="utf-8")
|
||||
|
||||
drop_dir = tmp_path / "drop"
|
||||
drop_dir.mkdir()
|
||||
processed_dir = trigger_dir / "processed"
|
||||
callbacks: list[tuple[str, dict]] = []
|
||||
download_calls: list[dict] = []
|
||||
|
||||
def fake_download_inventory_with_browser(trigger: dict, target_drop_dir: Path) -> Path:
|
||||
download_calls.append(trigger)
|
||||
out_file = target_drop_dir / "inventory_SN-20260629-2EDADC77_DSV360_AWSDU.xlsx"
|
||||
out_file.write_bytes(b"fake inventory content")
|
||||
return out_file
|
||||
|
||||
monkeypatch.setenv("FREIGHT_RPA_MOCK", "false")
|
||||
monkeypatch.setattr(worker, "_download_inventory_with_browser", fake_download_inventory_with_browser)
|
||||
monkeypatch.setattr(worker, "_post_callback", lambda path, payload: callbacks.append((path, payload)))
|
||||
|
||||
worker._handle_inventory_trigger(trigger_path, drop_dir, processed_dir)
|
||||
|
||||
assert len(download_calls) == 1
|
||||
trigger = download_calls[0]
|
||||
assert trigger["task_no"] == "SN-20260629-2EDADC77"
|
||||
assert trigger["attempt"] == 3
|
||||
assert trigger["rpa_inventory_source"]["source_code"] == "DSV360_AWSDU"
|
||||
assert trigger["rpa_inventory_source"]["source_type"] == "dsv360"
|
||||
assert trigger["rpa_inventory_source"]["query"]["program_label"] == "AWSDU - Prod (JDA)"
|
||||
assert trigger["rpa_inventory_source"]["query"]["owner_code"] == "CADU-0000000100"
|
||||
|
||||
assert callbacks == [
|
||||
(
|
||||
"/api/shipping-notice/rpa/inventory-ready",
|
||||
{
|
||||
"task_no": "SN-20260629-2EDADC77",
|
||||
"success": True,
|
||||
"file_path": str(drop_dir / "inventory_SN-20260629-2EDADC77_DSV360_AWSDU.xlsx"),
|
||||
"source_code": "DSV360_AWSDU",
|
||||
},
|
||||
)
|
||||
]
|
||||
assert not trigger_path.exists()
|
||||
assert (processed_dir / source_trigger.name).is_file()
|
||||
|
||||
processed_payload = json.loads((processed_dir / source_trigger.name).read_text(encoding="utf-8"))
|
||||
assert processed_payload["task_no"] == "SN-20260629-2EDADC77"
|
||||
|
||||
|
||||
def test_inventory_export_trigger_2edadc77_mock_writes_inventory_excel(monkeypatch, tmp_path):
|
||||
"""基于失败样本验证 mock RPA 会真实导出一个可打开的库存 Excel。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
source_trigger = (
|
||||
_repo_root()
|
||||
/ "backend"
|
||||
/ "data"
|
||||
/ "shipping_notice"
|
||||
/ "freight_rpa"
|
||||
/ "inventory_triggers"
|
||||
/ "failed"
|
||||
/ "inventory_export_SN-20260629-2EDADC77.json"
|
||||
)
|
||||
trigger_dir = tmp_path / "inventory_triggers"
|
||||
trigger_dir.mkdir()
|
||||
trigger_path = trigger_dir / source_trigger.name
|
||||
trigger_path.write_text(source_trigger.read_text(encoding="utf-8"), encoding="utf-8")
|
||||
|
||||
drop_dir = tmp_path / "drop"
|
||||
drop_dir.mkdir()
|
||||
processed_dir = trigger_dir / "processed"
|
||||
callbacks: list[tuple[str, dict]] = []
|
||||
|
||||
monkeypatch.setenv("FREIGHT_RPA_MOCK", "true")
|
||||
monkeypatch.delenv("FREIGHT_RPA_SAMPLE_INVENTORY", raising=False)
|
||||
monkeypatch.setattr(worker, "_post_callback", lambda path, payload: callbacks.append((path, payload)))
|
||||
|
||||
worker._handle_inventory_trigger(trigger_path, drop_dir, processed_dir)
|
||||
|
||||
exported_files = list(drop_dir.glob("inventory_SN-20260629-2EDADC77_*.xlsx"))
|
||||
assert len(exported_files) == 1
|
||||
exported = exported_files[0]
|
||||
wb = load_workbook(exported, data_only=True, read_only=True)
|
||||
try:
|
||||
ws = wb.active
|
||||
rows = list(ws.iter_rows(values_only=True))
|
||||
finally:
|
||||
wb.close()
|
||||
|
||||
assert rows[0] == ("料号", "可用数量")
|
||||
assert rows[1] == ("A001", 100)
|
||||
assert rows[2] == ("A002", 50)
|
||||
assert callbacks == [
|
||||
(
|
||||
"/api/shipping-notice/rpa/inventory-ready",
|
||||
{
|
||||
"task_no": "SN-20260629-2EDADC77",
|
||||
"success": True,
|
||||
"file_path": str(exported),
|
||||
"source_code": "DSV360_AWSDU",
|
||||
},
|
||||
)
|
||||
]
|
||||
assert not trigger_path.exists()
|
||||
assert (processed_dir / source_trigger.name).is_file()
|
||||
|
||||
|
||||
def test_open_browser_uses_configured_browser_channel(monkeypatch, tmp_path):
|
||||
"""真实 RPA 可通过本机 Edge/Chrome channel 启动,避免强制下载 Playwright Chromium。"""
|
||||
worker = _load_freight_rpa_worker()
|
||||
observed: dict[str, object] = {}
|
||||
|
||||
class _FakePage:
|
||||
def set_default_timeout(self, value: int) -> None:
|
||||
observed["timeout"] = value
|
||||
|
||||
class _FakeContext:
|
||||
pages = [_FakePage()]
|
||||
|
||||
class _FakeChromium:
|
||||
def launch_persistent_context(self, profile: str, **kwargs):
|
||||
observed["profile"] = profile
|
||||
observed["kwargs"] = kwargs
|
||||
return _FakeContext()
|
||||
|
||||
class _FakePlaywright:
|
||||
chromium = _FakeChromium()
|
||||
|
||||
class _FakeStarter:
|
||||
def start(self):
|
||||
return _FakePlaywright()
|
||||
|
||||
monkeypatch.setenv("FREIGHT_RPA_BROWSER_PROFILE_DIR", str(tmp_path / "profile"))
|
||||
monkeypatch.setenv("FREIGHT_RPA_HEADLESS", "false")
|
||||
monkeypatch.setenv("FREIGHT_RPA_BROWSER_CHANNEL", "msedge")
|
||||
monkeypatch.delenv("FREIGHT_RPA_BROWSER_EXECUTABLE_PATH", raising=False)
|
||||
monkeypatch.setattr(worker, "_require_playwright", lambda: (_FakeStarter, object))
|
||||
|
||||
_playwright, context, page = worker._open_browser(tmp_path / "downloads")
|
||||
|
||||
assert context is not None
|
||||
assert page is not None
|
||||
assert observed["profile"] == str(tmp_path / "profile")
|
||||
assert observed["kwargs"] == {
|
||||
"headless": False,
|
||||
"accept_downloads": True,
|
||||
"downloads_path": str(tmp_path / "downloads"),
|
||||
"channel": "msedge",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.rpa_e2e
|
||||
def test_inventory_export_trigger_2edadc77_downloads_real_file(monkeypatch, tmp_path):
|
||||
"""
|
||||
手动集成测试:使用真实浏览器 Driver 从 DSV360 导出库存文件。
|
||||
|
||||
默认跳过;仅当 FREIGHT_RPA_E2E=true 时执行。执行前需准备 DSV 登录态或账号密码:
|
||||
- FREIGHT_RPA_MOCK=false
|
||||
- FREIGHT_RPA_HEADLESS=false(建议首次调试)
|
||||
- FREIGHT_RPA_BROWSER_PROFILE_DIR=<已登录 profile,可选>
|
||||
- FREIGHT_RPA_DSV_USERNAME / FREIGHT_RPA_DSV_PASSWORD(可选)
|
||||
"""
|
||||
if str(os.getenv("FREIGHT_RPA_E2E") or "").strip().lower() != "true":
|
||||
pytest.skip("set FREIGHT_RPA_E2E=true to run real freight RPA export")
|
||||
|
||||
worker = _load_freight_rpa_worker()
|
||||
source_trigger = (
|
||||
_repo_root()
|
||||
/ "backend"
|
||||
/ "data"
|
||||
/ "shipping_notice"
|
||||
/ "freight_rpa"
|
||||
/ "inventory_triggers"
|
||||
/ "failed"
|
||||
/ "inventory_export_SN-20260629-2EDADC77.json"
|
||||
)
|
||||
trigger = json.loads(source_trigger.read_text(encoding="utf-8"))
|
||||
drop_dir = tmp_path / "drop"
|
||||
drop_dir.mkdir()
|
||||
|
||||
monkeypatch.setenv("FREIGHT_RPA_MOCK", "false")
|
||||
if not os.getenv("FREIGHT_RPA_HEADLESS"):
|
||||
monkeypatch.setenv("FREIGHT_RPA_HEADLESS", "false")
|
||||
if not os.getenv("FREIGHT_RPA_BROWSER_PROFILE_DIR"):
|
||||
monkeypatch.setenv(
|
||||
"FREIGHT_RPA_BROWSER_PROFILE_DIR",
|
||||
str(tmp_path / "browser_profile"),
|
||||
)
|
||||
|
||||
out_file = worker._download_inventory_with_browser(trigger, drop_dir)
|
||||
|
||||
assert out_file.is_file()
|
||||
assert out_file.parent == drop_dir
|
||||
assert out_file.suffix.lower() in {".xlsx", ".xls", ".csv"}
|
||||
assert out_file.stat().st_size > 0
|
||||
Reference in new issue
Block a user