#!/usr/bin/env python3 """Regressions for AGP screenshot output collection, not visual parity tests.""" import base64 import importlib.util import json from pathlib import Path import tempfile import unittest SPEC = importlib.util.spec_from_file_location("integration", Path(__file__).with_name("test-integration.py")) HARNESS = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(HARNESS) PNG = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jJ1sAAAAASUVORK5CYII=") class ScreenshotEvidenceTests(unittest.TestCase): def setUp(self): base = "/tmp/opencode" if Path("/tmp/opencode").exists() else None self.temporary = tempfile.TemporaryDirectory(prefix="yuvomi-evidence-", dir=base) self.addCleanup(self.temporary.cleanup) self.output = Path(self.temporary.name) self.additional, self.screenshots = HARNESS.prepare_android_screenshots(self.output) def fixture(self, device="fixture(AVD) - 15", contents=PNG): image = self.additional / "connected" / device / "native-dashboard.png" image.parent.mkdir(parents=True, exist_ok=True) image.write_bytes(contents) return image def test_collects_actual_agp_variant_path_without_adb_or_installed_app(self): image = self.fixture() HARNESS.collect_native_screenshot(self.additional, self.screenshots) self.assertEqual((self.screenshots / image.name).read_bytes(), PNG) self.assertEqual(self.additional.name, "integrationAndroidTest") def test_missing_screenshot_is_not_success(self): with self.assertRaisesRegex(AssertionError, "Missing or ambiguous"): HARNESS.collect_native_screenshot(self.additional, self.screenshots) def test_rejects_run_as_error_even_when_command_reported_zero_exit(self): self.fixture(contents=b"run-as: unknown package: community.yuvomi.mobile.integration\n") with self.assertRaisesRegex(AssertionError, "Invalid native"): HARNESS.collect_native_screenshot(self.additional, self.screenshots) def test_rejects_ambiguous_device_evidence(self): self.fixture("first") self.fixture("second") with self.assertRaisesRegex(AssertionError, "ambiguous"): HARNESS.collect_native_screenshot(self.additional, self.screenshots) def test_preparation_removes_only_known_stale_capture(self): image = self.fixture() HARNESS.collect_native_screenshot(self.additional, self.screenshots) unrelated = image.with_name("other-output.txt") unrelated.write_text("preserve") HARNESS.prepare_android_screenshots(self.output) self.assertFalse(image.exists()) self.assertFalse((self.screenshots / image.name).exists()) self.assertEqual(unrelated.read_text(), "preserve") def gallery(self): parent = self.fixture().parent for name in HARNESS.SCREENSHOTS: (parent / name).write_bytes(PNG) (parent / "viewport.json").write_text(json.dumps({"widthPx": 720, "heightPx": 1184, "density": 2})) return parent def test_collects_complete_allowlisted_gallery(self): parent = self.gallery() (parent / "private.txt").write_text("not exported") HARNESS.collect_native_screenshot(self.additional, self.screenshots, gallery=True) self.assertEqual({p.name for p in self.screenshots.iterdir()}, set(HARNESS.VISUAL_OUTPUTS)) def test_partial_gallery_is_not_exported(self): parent = self.gallery() (parent / "notes-dark.png").unlink() with self.assertRaisesRegex(AssertionError, "Missing or ambiguous"): HARNESS.collect_native_screenshot(self.additional, self.screenshots, gallery=True) self.assertFalse(self.screenshots.exists()) def test_clears_every_known_gallery_file_but_preserves_unrelated(self): parent = self.gallery() HARNESS.collect_native_screenshot(self.additional, self.screenshots, gallery=True) (parent / "other.png").write_bytes(PNG) HARNESS.prepare_android_screenshots(self.output) self.assertEqual([p.name for p in parent.iterdir()], ["other.png"]) self.assertEqual(list(self.screenshots.iterdir()), []) def test_rejects_gallery_split_across_devices(self): parent = self.gallery() other = parent.with_name("second") other.mkdir() (parent / "notes-dark.png").rename(other / "notes-dark.png") with self.assertRaisesRegex(AssertionError, "Mixed-device"): HARNESS.collect_native_screenshot(self.additional, self.screenshots, gallery=True) if __name__ == "__main__": unittest.main()