139 lines
4.9 KiB
Python
139 lines
4.9 KiB
Python
import json
|
|
import subprocess
|
|
import sys
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
def _write_receipt(root, *, platform, manifest_id, title, url, http_status=200):
|
|
receipt_dir = root / platform / manifest_id
|
|
tiles_dir = receipt_dir / "tiles"
|
|
tiles_dir.mkdir(parents=True)
|
|
screenshot = receipt_dir / "screenshot.png"
|
|
screenshot.write_bytes(b"png")
|
|
files = [{"kind": "screenshot", "path": str(screenshot)}]
|
|
for index in range(4):
|
|
tile = tiles_dir / f"tile-{index}.png"
|
|
tile.write_bytes(b"tile")
|
|
files.append({"kind": "tile", "path": str(tile)})
|
|
receipt = {
|
|
"generated_at": datetime.now(timezone.utc).isoformat(),
|
|
"status": "captured",
|
|
"manifest_id": manifest_id,
|
|
"http_status": http_status,
|
|
"capture_target": {
|
|
"platform": platform,
|
|
"url": url,
|
|
"crawler": "PixelRAGMarketplaceSearch.visual_fallback",
|
|
},
|
|
"page_metrics": {
|
|
"title": title,
|
|
"final_url": url,
|
|
},
|
|
"tile_plan": {
|
|
"planned_tile_count": 4,
|
|
"emitted_tile_count": 4,
|
|
},
|
|
"files": files,
|
|
}
|
|
path = receipt_dir / "capture_receipt.json"
|
|
path.write_text(json.dumps(receipt, ensure_ascii=False), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def test_pixelrag_rag_candidate_replay_splits_eligible_and_blocked_receipts(tmp_path):
|
|
from services.pixelrag_rag_candidate_replay_service import (
|
|
POLICY,
|
|
build_pixelrag_rag_candidate_replay_readback,
|
|
)
|
|
|
|
_write_receipt(
|
|
tmp_path,
|
|
platform="shopee_tw",
|
|
manifest_id="shopee-ok",
|
|
title="Shopee 防曬乳",
|
|
url="https://shopee.tw/search?keyword=%E9%98%B2%E6%9B%AC%E4%B9%B3",
|
|
)
|
|
_write_receipt(
|
|
tmp_path,
|
|
platform="coupang_tw",
|
|
manifest_id="coupang-403",
|
|
title="Access Denied",
|
|
url="https://www.tw.coupang.com/search?q=iphone",
|
|
http_status=403,
|
|
)
|
|
|
|
payload = build_pixelrag_rag_candidate_replay_readback(artifact_root=tmp_path)
|
|
by_platform = {item["platform"]: item for item in payload["candidates"]}
|
|
|
|
assert payload["policy"] == POLICY
|
|
assert payload["status"] == "warning"
|
|
assert payload["summary"]["receipt_count"] == 2
|
|
assert payload["summary"]["eligible_count"] == 1
|
|
assert payload["summary"]["blocked_count"] == 1
|
|
assert payload["summary"]["visual_barrier_count"] == 1
|
|
assert by_platform["shopee_tw"]["eligible_for_rag_candidate_replay"] is True
|
|
assert by_platform["shopee_tw"]["promotion_boundary"]["direct_ai_insights_write_allowed"] is False
|
|
assert "Shopee 防曬乳" in by_platform["shopee_tw"]["rag_candidate_text"]
|
|
assert by_platform["coupang_tw"]["eligible_for_rag_candidate_replay"] is False
|
|
assert by_platform["coupang_tw"]["visual_barrier_reason"] == "http_status_403"
|
|
assert by_platform["coupang_tw"]["next_machine_action"] == "run_platform_probe_or_use_structured_api"
|
|
assert payload["replay_contract"]["blocked_pages_are_not_product_data"] is True
|
|
assert payload["controlled_apply"]["db_write"] is False
|
|
|
|
|
|
def test_pixelrag_rag_candidate_replay_cli_outputs_machine_readable_json(tmp_path):
|
|
_write_receipt(
|
|
tmp_path,
|
|
platform="shopee_tw",
|
|
manifest_id="shopee-ok",
|
|
title="Shopee 防曬乳",
|
|
url="https://shopee.tw/search?keyword=sunscreen",
|
|
)
|
|
|
|
completed = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"scripts/ops/report_pixelrag_rag_candidate_replay.py",
|
|
"--artifact-root",
|
|
str(tmp_path),
|
|
"--platform",
|
|
"shopee_tw",
|
|
],
|
|
capture_output=True,
|
|
check=False,
|
|
text=True,
|
|
)
|
|
|
|
assert completed.returncode == 0
|
|
payload = json.loads(completed.stdout)
|
|
assert payload["summary"]["receipt_count"] == 1
|
|
assert payload["summary"]["eligible_count"] == 1
|
|
assert payload["candidates"][0]["platform"] == "shopee_tw"
|
|
|
|
|
|
def test_pixelrag_rag_candidate_replay_route_returns_readback(tmp_path, monkeypatch):
|
|
from flask import Flask
|
|
from routes import system_public_routes as routes
|
|
from services import pixelrag_rag_candidate_replay_service as service
|
|
|
|
_write_receipt(
|
|
tmp_path,
|
|
platform="shopee_tw",
|
|
manifest_id="shopee-ok",
|
|
title="Shopee 防曬乳",
|
|
url="https://shopee.tw/search?keyword=sunscreen",
|
|
)
|
|
monkeypatch.setattr(service, "DEFAULT_ARTIFACT_ROOT", str(tmp_path))
|
|
|
|
app = Flask(__name__)
|
|
with app.test_request_context(
|
|
"/api/ai-automation/pixelrag-rag-candidate-replay?platform=shopee_tw"
|
|
):
|
|
response = routes.ai_automation_pixelrag_rag_candidate_replay_api.__wrapped__()
|
|
payload = response.get_json()
|
|
|
|
assert payload["policy"] == "read_only_pixelrag_rag_candidate_replay_v1"
|
|
assert payload["summary"]["receipt_count"] == 1
|
|
assert payload["summary"]["eligible_count"] == 1
|
|
assert payload["replay_contract"]["writes_database"] is False
|