264 lines
9.3 KiB
Python
264 lines
9.3 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def test_collect_run_regression_artifacts_keeps_logs_state_and_debug_images(tmp_path, monkeypatch):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
logs = root / "logs"
|
|
state = root / "douyin" / "core" / "state"
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
ocr = root / "pic" / "ocr" / "fudai"
|
|
cfg = root / "config"
|
|
for path in (logs, state, debug, ocr, cfg):
|
|
path.mkdir(parents=True)
|
|
|
|
(logs / "unified_2026-06-19_120000.log").write_text("open\n参与成功\n", encoding="utf-8")
|
|
(logs / "detail_2026-06-19.log").write_text("detail\n", encoding="utf-8")
|
|
(logs / "screenshot_calls.jsonl").write_text("a\nb\nc\n", encoding="utf-8")
|
|
(state / "room_pos_map.json").write_text('{"1":{"has_fudai":true}}', encoding="utf-8")
|
|
(state / "reservations.json").write_text('{"reservations":[]}', encoding="utf-8")
|
|
(cfg / "config.json").write_text('{"debug":true}', encoding="utf-8")
|
|
for idx, name in enumerate(
|
|
[
|
|
"20260619_120001_px_fdicon_ok_x123.png",
|
|
"20260619_120002_ocr_福袋详情综合标注_detail_regions.png",
|
|
"20260619_120003_ocr_开奖结果按钮_postocr.png",
|
|
]
|
|
):
|
|
p = debug / name
|
|
p.write_bytes(bytes([idx + 1]) * 10)
|
|
(ocr / "福袋倒计时_分钟.png").write_bytes(b"minute")
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="unit_run",
|
|
started_at=0.0,
|
|
exit_code=0,
|
|
device_id="device1",
|
|
max_debug_images=10,
|
|
)
|
|
|
|
out = Path(result["output_dir"])
|
|
assert (out / "manifest.json").exists()
|
|
assert (out / "logs" / "unified_2026-06-19_120000.log").exists()
|
|
assert (out / "logs" / "screenshot_calls.tail.jsonl").read_text(encoding="utf-8") == "a\nb\nc\n"
|
|
assert (out / "state" / "room_pos_map.json").exists()
|
|
assert (out / "config" / "config.json").exists()
|
|
assert len(list((out / "debug_captures").rglob("*.png"))) == 3
|
|
assert any(p.name == "福袋倒计时_分钟.png" for p in (out / "ocr").rglob("*.png"))
|
|
|
|
manifest = json.loads((out / "manifest.json").read_text(encoding="utf-8"))
|
|
assert manifest["run_id"] == "unit_run"
|
|
assert manifest["exit_code"] == 0
|
|
assert manifest["counts"]["debug_images"] == 3
|
|
|
|
|
|
def test_collect_run_regression_artifacts_limits_debug_images(tmp_path, monkeypatch):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
debug.mkdir(parents=True)
|
|
for idx in range(5):
|
|
p = debug / f"20260619_12000{idx}_px_pxstage_ov_ok_1.png"
|
|
p.write_bytes(bytes([idx]) * 5)
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="limited",
|
|
started_at=0.0,
|
|
max_debug_images=2,
|
|
)
|
|
|
|
out = Path(result["output_dir"])
|
|
copied = sorted(p.name for p in (out / "debug_captures").rglob("*.png"))
|
|
assert len(copied) == 2
|
|
assert copied == [
|
|
"20260619_120003_px_pxstage_ov_ok_1.png",
|
|
"20260619_120004_px_pxstage_ov_ok_1.png",
|
|
]
|
|
|
|
|
|
def test_collect_run_regression_artifacts_keeps_only_run_window_logs(tmp_path, monkeypatch):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
logs = root / "logs"
|
|
logs.mkdir(parents=True)
|
|
old_log = logs / "unified_2026-06-18.log"
|
|
current_log = logs / "unified_2026-06-19.log"
|
|
old_log.write_text("old\n", encoding="utf-8")
|
|
current_log.write_text("current\n", encoding="utf-8")
|
|
old_ts = 100.0
|
|
current_ts = 300.0
|
|
old_log.touch()
|
|
current_log.touch()
|
|
import os
|
|
|
|
os.utime(old_log, (old_ts, old_ts))
|
|
os.utime(current_log, (current_ts, current_ts))
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(run_id="log_window", started_at=200.0)
|
|
|
|
out = Path(result["output_dir"])
|
|
assert not (out / "logs" / old_log.name).exists()
|
|
assert (out / "logs" / current_log.name).exists()
|
|
|
|
|
|
def test_collect_run_regression_artifacts_promotes_long_term_error_samples(tmp_path, monkeypatch):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
logs = root / "logs"
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
logs.mkdir(parents=True)
|
|
debug.mkdir(parents=True)
|
|
|
|
(logs / "unified_2026-06-19.log").write_text(
|
|
"\n".join(
|
|
[
|
|
"[FDICON] 点击后进入优惠券,可能是福袋 icon 与优惠券入口位置交换",
|
|
"[DETAIL_OPEN_VERDICT] detail_not_recognized",
|
|
"未找到福袋图标,但当前画面疑似有福袋",
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
for name in [
|
|
"20260619_px_fdicon_false_left_coupon_x162.png",
|
|
"20260619_px_pxstage_miss_no_fudai.png",
|
|
"20260619_detail_DETAIL_OPEN_VERDICT_detail_not_recognized.png",
|
|
"20260619_px_fdicon_ok_normal.png",
|
|
]:
|
|
(debug / name).write_bytes(name.encode("utf-8"))
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="sample_pool",
|
|
started_at=0.0,
|
|
max_debug_images=10,
|
|
max_long_term_samples_per_category=2,
|
|
)
|
|
|
|
out = Path(result["output_dir"])
|
|
manifest = json.loads((out / "manifest.json").read_text(encoding="utf-8"))
|
|
categories = {item["category"] for item in manifest["long_term_samples"]["samples"]}
|
|
assert "ui_position_swap_or_overlay" in categories
|
|
assert "miss_or_not_found" in categories
|
|
assert "detail_stage_anomaly" in categories
|
|
assert manifest["long_term_samples"]["notes"]["ui_position_swap_or_overlay"].startswith(
|
|
"可能是 UI 位置交换"
|
|
)
|
|
sample_root = root / "artifacts" / "fudai_regression_samples"
|
|
assert (sample_root / "index.jsonl").exists()
|
|
assert len(list(sample_root.rglob("*.png"))) >= 3
|
|
|
|
|
|
def test_collect_run_regression_artifacts_sample_only_skips_full_run_package(tmp_path, monkeypatch):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
debug.mkdir(parents=True)
|
|
(debug / "20260619_px_fdicon_false_left_coupon_x162.png").write_bytes(b"coupon")
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="sample_only",
|
|
started_at=0.0,
|
|
full_run_package=False,
|
|
max_debug_images=10,
|
|
)
|
|
|
|
assert result["output_dir"] == ""
|
|
assert result["manifest"] == ""
|
|
assert result["counts"]["long_term_samples"] == 1
|
|
assert not (root / "artifacts" / "fudai_regression_runs" / "sample_only").exists()
|
|
assert (root / "artifacts" / "fudai_regression_samples" / "index.jsonl").exists()
|
|
|
|
|
|
def test_collect_run_regression_artifacts_does_not_promote_normal_stage_images_from_global_log(
|
|
tmp_path, monkeypatch
|
|
):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
logs = root / "logs"
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
logs.mkdir(parents=True)
|
|
debug.mkdir(parents=True)
|
|
|
|
(logs / "unified_2026-06-19.log").write_text(
|
|
"DETAIL_DATA_VERDICT ok\n未找到福袋图标,退出预约直接参与\n",
|
|
encoding="utf-8",
|
|
)
|
|
(debug / "20260619_px_pxstage_ov_ok_1.png").write_bytes(b"normal-stage")
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="normal_stage",
|
|
started_at=0.0,
|
|
full_run_package=False,
|
|
max_debug_images=10,
|
|
)
|
|
|
|
assert result["counts"]["long_term_samples"] == 0
|
|
assert not (root / "artifacts" / "fudai_regression_samples" / "index.jsonl").exists()
|
|
|
|
|
|
def test_collect_run_regression_artifacts_promotes_pixel_confirm_failed_as_miss_sample(
|
|
tmp_path, monkeypatch
|
|
):
|
|
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
|
|
|
root = tmp_path
|
|
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
|
debug.mkdir(parents=True)
|
|
(debug / "20260619_px_fdicon_confirm_failed_x162.png").write_bytes(b"confirm-failed")
|
|
|
|
monkeypatch.setattr(
|
|
"douyin.testing.regression_collector.path_config.get_project_root",
|
|
lambda: str(root),
|
|
)
|
|
|
|
result = collect_run_regression_artifacts(
|
|
run_id="confirm_failed",
|
|
started_at=0.0,
|
|
full_run_package=False,
|
|
max_debug_images=10,
|
|
)
|
|
|
|
assert result["counts"]["long_term_samples"] == 1
|
|
sample_root = root / "artifacts" / "fudai_regression_samples"
|
|
index_text = (sample_root / "index.jsonl").read_text(encoding="utf-8")
|
|
assert '"category": "miss_or_not_found"' in index_text
|