Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(visual-review): cover below-threshold processing (#72183)

Paul D'Ambra813534796aad
diff --git a/products/visual_review/backend/tests/test_tasks.py b/products/visual_review/backend/tests/test_tasks.py
index 1805c42869ae..ff443dc9ee26 100644
--- a/products/visual_review/backend/tests/test_tasks.py
+++ b/products/visual_review/backend/tests/test_tasks.py
@@ -21,11 +21,26 @@ from products.visual_review.backend.facade.enums import (
RunType,
SnapshotResult,
)
-from products.visual_review.backend.models import RunSnapshot
+from products.visual_review.backend.models import RunSnapshot, ToleratedHash
from products.visual_review.backend.tasks.tasks import post_approval_comment, process_run_diffs
from products.visual_review.backend.tests.conftest import PRODUCT_DATABASES, VisualReviewTeamScopedTestMixin
+def _make_png(color: tuple[int, int, int, int], size: tuple[int, int] = (10, 10)) -> bytes:
+ image = Image.new("RGBA", size, color)
+ buffer = io.BytesIO()
+ image.save(buffer, format="PNG")
+ return buffer.getvalue()
+
+
+def _make_png_with_single_changed_pixel() -> bytes:
+ image = Image.new("RGBA", (100, 100), (255, 0, 0, 255))
+ image.putpixel((0, 0), (0, 0, 255, 255))
+ buffer = io.BytesIO()
+ image.save(buffer, format="PNG")
+ return buffer.getvalue()
+
+
@pytest.mark.django_db(databases=PRODUCT_DATABASES)
class TestProcessRunDiffs:
@pytest.fixture
@@ -269,15 +284,9 @@ class TestProcessRunDiffs:
assert any("diff_skipped_missing_artifact" in arg for arg in call_args)
def testprocess_diffs_does_not_repeat_completed_comparisons(self, repo, mocker):
- def png(color: tuple[int, int, int, int]) -> bytes:
- image = Image.new("RGBA", (10, 10), color)
- buffer = io.BytesIO()
- image.save(buffer, format="PNG")
- return buffer.getvalue()
-
stored_bytes = {
- "old_hash": png((255, 0, 0, 255)),
- "new_hash": png((0, 0, 255, 255)),
+ "old_hash": _make_png((255, 0, 0, 255)),
+ "new_hash": _make_png((0, 0, 255, 255)),
}
logic.get_or_create_artifact(repo.id, "old_hash", "visual_review/old_hash")
logic.get_or_create_artifact(repo.id, "new_hash", "visual_review/new_hash")
@@ -319,6 +328,62 @@ class TestProcessRunDiffs:
assert process_diffs(create_result.run_id) == 0
assert compare_images.call_count == 1
+ def test_process_diffs_counts_below_threshold_comparisons(self, repo, mocker):
+ stored_bytes = {
+ "old_hash": _make_png((255, 0, 0, 255), size=(100, 100)),
+ "new_hash": _make_png_with_single_changed_pixel(),
+ }
+ logic.get_or_create_artifact(repo.id, "old_hash", "visual_review/old_hash")
+ logic.get_or_create_artifact(repo.id, "new_hash", "visual_review/new_hash")
+ create_result = api.create_run(
+ CreateRunInput(
+ repo_id=repo.id,
+ run_type=RunType.STORYBOOK,
+ commit_sha="abc123",
+ branch="main",
+ snapshots=[SnapshotManifestItem(identifier="Button", content_hash="new_hash")],
+ baseline_hashes={"Button": "old_hash"},
+ ),
+ team_id=repo.team_id,
+ )
+ with (
+ patch(
+ "products.visual_review.backend.logic._resolve_baselines_with_merge_base",
+ return_value=({"Button": "old_hash"}, 0),
+ ),
+ patch("products.visual_review.backend.tasks.tasks.process_run_diffs.delay"),
+ ):
+ logic.complete_run(create_result.run_id)
+
+ mocker.patch(
+ "products.visual_review.backend.storage.ArtifactStorage.read",
+ autospec=True,
+ side_effect=lambda _storage, content_hash: stored_bytes.get(content_hash),
+ )
+ mocker.patch(
+ "products.visual_review.backend.storage.ArtifactStorage.write",
+ autospec=True,
+ side_effect=lambda _storage, content_hash, content: (
+ stored_bytes.setdefault(content_hash, content) and f"visual_review/{content_hash}"
+ ),
+ )
+
+ assert process_diffs(create_result.run_id) == 1
+
+ snapshot = RunSnapshot.objects.get(run_id=create_result.run_id)
+ assert snapshot.result == SnapshotResult.UNCHANGED
+ assert snapshot.classification_reason == ClassificationReason.BELOW_THRESHOLD
+ assert snapshot.diff_pixel_count is not None
+ assert snapshot.diff_pixel_count > 0
+ assert snapshot.diff_percentage is not None
+ assert snapshot.diff_percentage > 0
+ assert ToleratedHash.objects.filter(
+ repo_id=repo.id,
+ identifier="Button",
+ baseline_hash="old_hash",
+ alternate_hash="new_hash",
+ ).exists()
+
class TestCountProcessedDiffs(VisualReviewTeamScopedTestMixin, BaseTest):
databases = PRODUCT_DATABASES