Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions tests/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ def test_build_report_summary_aggregates_snapshot_and_report():
summary = _build_report_summary(_sample_snapshot(), _sample_report_files(), steps)

assert set(summary) == {"pages", "assets", "totals", "steps_seconds", "top_savings"}
assert summary["pages"] == {"count": 1, "bytes": 2000}
assert summary["pages"] == {
"count": 1, "bytes": 2000,
"files": [{"path": "index.html", "bytes": 2000}],
}

assets = summary["assets"]
assert list(assets) == ["images", "css", "js", "fonts", "videos", "html", "other"]
Expand Down Expand Up @@ -401,7 +404,32 @@ def test_build_report_summary_without_report_marks_everything_unchanged():
"files": 2, "original_bytes": 1500, "final_bytes": 1500, "saved_pct": 0.0,
}
assert not summary["top_savings"]
assert summary["pages"] == {"count": 1, "bytes": 1000}
assert summary["pages"] == {
"count": 1, "bytes": 1000,
"files": [{"path": "index.html", "bytes": 1000}],
}


def test_build_report_summary_pages_files_sorted_and_capped():
"""pages.files is sorted by path and capped at 200; count/bytes cover all."""
from jobs import _build_report_summary

snapshot = {
f"pages/{i:03d}.html": {"bytes": 100, "kind": "html"}
for i in reversed(range(205))
}

summary = _build_report_summary(snapshot, [], {})

pages = summary["pages"]
assert pages["count"] == 205
assert pages["bytes"] == 20500
files = pages["files"]
assert len(files) == 200
paths = [f["path"] for f in files]
assert paths == sorted(paths)
assert paths[0] == "pages/000.html"
assert all(f == {"path": p, "bytes": 100} for f, p in zip(files, paths))


def test_build_report_summary_empty_snapshot_saved_pct_zero():
Expand All @@ -416,6 +444,7 @@ def test_build_report_summary_empty_snapshot_saved_pct_zero():
assert all(
bucket["count"] == 0 for bucket in summary["assets"].values()
)
assert summary["pages"] == {"count": 0, "bytes": 0, "files": []}


# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -471,6 +500,9 @@ def fake_run(cmd, **_kwargs):
"count": 1, "original_bytes": 1000, "final_bytes": 300,
}
assert report["pages"]["count"] == 1
assert report["pages"]["files"] == [
{"path": "index.html", "bytes": len("<html> <body>hi</body> </html>")},
]
assert report["top_savings"] == [
{"path": "photo.jpg", "original_bytes": 1000, "final_bytes": 300},
]
31 changes: 29 additions & 2 deletions worker/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ def _build_opt_cmd(workdir: pathlib.Path, opts: dict, hostname: str = "") -> lis
"other": "other",
}

# Cap on the pages.files list in the build report — the result travels
# through Redis/the API and is stored by clients, so it must stay bounded.
# count/bytes still reflect ALL pages.
_PAGES_FILES_CAP = 200


def _snapshot_workdir(workdir: pathlib.Path) -> dict[str, dict]:
"""
Expand Down Expand Up @@ -393,6 +398,22 @@ def _accumulate_entry(
savings.append({"path": path, "original_bytes": original, "final_bytes": final})


def _page_files(snapshot: dict[str, dict]) -> list[dict]:
"""
Per-page list for the build report: one {"path", "bytes"} entry per HTML
file in the snapshot (bytes = scraped/original size), sorted by path and
capped at _PAGES_FILES_CAP — the result travels through Redis/the API and
is stored by clients, so the list must stay bounded while the pages
count/bytes still cover ALL pages.
"""
pages = [
{"path": rel, "bytes": info["bytes"]}
for rel, info in sorted(snapshot.items())
if info["kind"] == "html"
]
return pages[:_PAGES_FILES_CAP]


def _build_report_summary(
snapshot: dict[str, dict],
report_files: list,
Expand All @@ -409,7 +430,9 @@ def _build_report_summary(
snapshot walk and the bundle contributes sum(sources) → bundle size;
- everything else is unchanged: final bytes = snapshot bytes;
- HTML files additionally feed "pages" (count + scraped/original bytes;
the minification delta itself lives in assets.html).
the minification delta itself lives in assets.html) and "pages.files",
a per-page list of {path, bytes} (scraped size) sorted by path and
capped at _PAGES_FILES_CAP entries; count/bytes cover ALL pages.
"""
buckets = {
name: {"count": 0, "original_bytes": 0, "final_bytes": 0}
Expand Down Expand Up @@ -465,7 +488,11 @@ def _build_report_summary(

html_bucket = buckets["html"]
return {
"pages": {"count": html_bucket["count"], "bytes": html_bucket["original_bytes"]},
"pages": {
"count": html_bucket["count"],
"bytes": html_bucket["original_bytes"],
"files": _page_files(snapshot),
},
"assets": assets,
"totals": {
"files": total_files,
Expand Down
Loading