SPB Git

Repository Public

test_deep_capture.py 6.5 KB 131 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project          : SPIDER7# File             : tests/integration/test_deep_capture.py8# Module           : Tests9# Repository       : https://git.spboucher.ai/spider10# Project Website  : Not specified11# Created          : 2026-12-0412# Last Modified    : 2026-12-0413#14# Author           : Simon-Pierre Boucher15# Position         : Professor of Finance16# Institution      : Université du Québec en Outaouais (UQO)17# Organization     : SP Boucher18# Website          : https://spboucher.ai19# Git              : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development      : dev@spboucher.ai23# Support          : support@spboucher.ai24# Security/Admin   : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License          : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""Browser path end to end: render, tiles, states, OCR, evidence — through the real gateway and worker."""42import pytest4344pytestmark = [pytest.mark.pg, pytest.mark.browser]454647async def _deep(stack, path, **over):48    body = {"url": stack.url(path), "capture": {"full_page": True, "tiles": True, "interactive_states": True},49            "extract": {"formats": ["markdown", "json"], "ocr": True}} | over50    r = await stack.client.post("/v1/deep-capture", json=body)51    assert r.status_code == 202, r.text52    job = await stack.wait(r.json()["job_id"], timeout=180)53    return job, await stack.results(job["job_id"])545556async def test_deep_capture_tabs_states_and_artifacts(stack):57    await stack.start_worker(caps=("http", "browser", "ocr"), concurrency=1)58    job, items = await _deep(stack, "/ui/tabs")59    assert job["status"] == "succeeded", job60    it = items[0]61    kinds = [a["kind"] for a in it["artifacts"]]62    assert {"viewport", "full_page", "tile", "dom", "html", "replay", "capture_manifest"} <= set(kinds), kinds63    for a in it["artifacts"]:64        if a["kind"] in ("viewport", "tile", "full_page"):65            css, dpr = a["meta"]["css"], a["meta"]["dpr"]66            assert a["mime"] == "image/png" and abs(a["width"] - css["w"] * dpr) <= 1 and abs(a["height"] - css["h"] * dpr) <= 167            got = await stack.client.get(a["url"])68            assert got.status_code == 200 and got.headers["x-spider-sha256"] == a["sha256"]69    # every tab panel was revealed by a recorded, read-only action70    acted = [s for s in it.get("states", []) if s["actions"]]71    assert len(acted) >= 2, it.get("states")72    for s in acted:73        assert s["actions"][-1]["type"] == "click" and s["dom_hash_before"] != s["dom_hash_after"]74    assert stack.site.state.side_effects == []75    assert all(q.method in ("GET", "HEAD") for q in stack.site.state.requests)767778async def test_danger_page_no_side_effect(stack):79    await stack.start_worker(caps=("http", "browser"), concurrency=1)80    job, items = await _deep(stack, "/ui/danger", extract={"formats": ["markdown"], "ocr": False})81    assert job["status"] == "succeeded", job82    skipped = items[0]["meta"]["skipped_actions"]83    assert len(skipped) >= 5 and all(s["reason"] for s in skipped)84    assert stack.site.state.side_effects == [], stack.site.state.side_effects85    assert all(q.method in ("GET", "HEAD") for q in stack.site.state.requests)868788async def test_tall_page_tiles_cover_document(stack):89    await stack.start_worker(caps=("http", "browser"), concurrency=1)90    job, items = await _deep(stack, "/dyn/tall", extract={"formats": ["markdown"], "ocr": False},91                             capture={"full_page": True, "tiles": True, "interactive_states": False})92    assert job["status"] in ("succeeded", "partial"), job93    tiles = sorted((a for a in items[0]["artifacts"] if a["kind"] == "tile"), key=lambda a: a["meta"]["css"]["y"])94    doc_h = items[0]["meta"]["browser"]["doc_height"]95    assert doc_h >= 20000 and tiles[0]["meta"]["css"]["y"] == 096    covered = 097    for t in tiles:98        css = t["meta"]["css"]99        assert css["y"] <= covered + 1, "gap between tiles"100        covered = max(covered, css["y"] + css["h"])101    assert covered >= doc_h - 2102    assert any(a["kind"] == "mosaic" for a in items[0]["artifacts"]) or any(a["kind"] == "full_page" for a in items[0]["artifacts"])103104105async def test_browser_crawl_discovers_spa_routes(stack):106    await stack.start_worker(caps=("http", "browser"), concurrency=1)107    r = await stack.client.post("/v1/crawl", json={108        "url": stack.url("/spa/"), "render": "browser", "use_sitemaps": False,109        "budgets": {"max_pages": 12, "max_depth": 2}, "policy": {"include_paths": ["^/spa/"]},110        "capture": {"full_page": False, "tiles": False, "viewport": True}, "extract": {"formats": ["markdown"]}})111    job = await stack.wait(r.json()["job_id"], timeout=240)112    front = (await stack.client.get(f"/v1/jobs/{job['job_id']}/frontier", params={"limit": 500})).json()["items"]113    paths = {f["url"][len(stack.site.base_url):] for f in front if f["status"] == "fetched"}114    assert {"/spa/items", "/spa/about"} <= paths, paths115    assert any(p.startswith("/spa/items/") for p in paths), paths116117118async def test_ocr_reads_pixel_only_text_with_evidence(stack):119    from spider.ocr.client import available120    if not available():121        pytest.skip("spider-ocr binary not built on this machine")122    await stack.start_worker(caps=("http", "browser", "ocr"), concurrency=1)123    job, items = await _deep(stack, "/dyn/canvas", capture={"full_page": True, "tiles": True, "interactive_states": False})124    assert job["status"] == "succeeded", job125    it = items[0]126    assert it["meta"]["ocr"]["status"] == "done" and it["meta"]["ocr"]["lines"] > 0, it["meta"]["ocr"]127    ocr = next(a for a in it["artifacts"] if a["kind"] == "ocr_json")128    data = (await stack.client.get(ocr["url"])).json()129    shas = {a["sha256"] for a in it["artifacts"]}130    assert all(ln["capture_sha256"] in shas for ln in data["lines"])   # every OCR line points to a stored capture131
Files main
Menu

Theme