microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
.github/skills/experimental/powerpoint/tests/conftest.py
134lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | """Shared fixtures for PowerPoint skill tests.""" |
| 4 | |
| 5 | import os |
| 6 | import struct |
| 7 | import zlib |
| 8 | from pathlib import Path |
| 9 | |
| 10 | import pytest |
| 11 | from hypothesis import HealthCheck, settings |
| 12 | from pptx import Presentation |
| 13 | from pptx.util import Inches, Pt |
| 14 | |
| 15 | # Hypothesis profiles |
| 16 | settings.register_profile( |
| 17 | "ci", |
| 18 | max_examples=200, |
| 19 | derandomize=True, |
| 20 | deadline=None, |
| 21 | database=None, |
| 22 | print_blob=True, |
| 23 | suppress_health_check=[HealthCheck.too_slow], |
| 24 | ) |
| 25 | settings.register_profile( |
| 26 | "dev", |
| 27 | max_examples=50, |
| 28 | deadline=None, |
| 29 | ) |
| 30 | settings.load_profile("ci" if os.environ.get("CI") else "dev") |
| 31 | |
| 32 | |
| 33 | # Plain functions callable from @given-decorated Hypothesis tests, |
| 34 | # which cannot use pytest fixtures. |
| 35 | |
| 36 | |
| 37 | def make_blank_presentation(): |
| 38 | """Create a fresh Presentation with standard widescreen dimensions.""" |
| 39 | prs = Presentation() |
| 40 | prs.slide_width = Inches(13.333) |
| 41 | prs.slide_height = Inches(7.5) |
| 42 | return prs |
| 43 | |
| 44 | |
| 45 | def make_blank_slide(): |
| 46 | """Create a blank slide on a fresh presentation.""" |
| 47 | prs = make_blank_presentation() |
| 48 | layout = prs.slide_layouts[6] |
| 49 | return prs.slides.add_slide(layout) |
| 50 | |
| 51 | |
| 52 | def _minimal_png_bytes() -> bytes: |
| 53 | """Create a minimal valid 1x1 red PNG in memory.""" |
| 54 | |
| 55 | def _chunk(chunk_type, data): |
| 56 | c = chunk_type + data |
| 57 | crc = struct.pack(">I", zlib.crc32(c) & 0xFFFFFFFF) |
| 58 | return struct.pack(">I", len(data)) + c + crc |
| 59 | |
| 60 | signature = b"\x89PNG\r\n\x1a\n" |
| 61 | ihdr_data = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0) |
| 62 | raw_row = b"\x00\xff\x00\x00" # filter byte + RGB |
| 63 | idat_data = zlib.compress(raw_row) |
| 64 | return ( |
| 65 | signature |
| 66 | + _chunk(b"IHDR", ihdr_data) |
| 67 | + _chunk(b"IDAT", idat_data) |
| 68 | + _chunk(b"IEND", b"") |
| 69 | ) |
| 70 | |
| 71 | |
| 72 | # Fixtures |
| 73 | |
| 74 | |
| 75 | @pytest.fixture() |
| 76 | def blank_presentation(): |
| 77 | """Fresh Presentation with standard widescreen dimensions.""" |
| 78 | return make_blank_presentation() |
| 79 | |
| 80 | |
| 81 | @pytest.fixture() |
| 82 | def blank_slide(blank_presentation): |
| 83 | """Blank slide added to a fresh presentation.""" |
| 84 | layout = blank_presentation.slide_layouts[6] # Blank layout |
| 85 | return blank_presentation.slides.add_slide(layout) |
| 86 | |
| 87 | |
| 88 | @pytest.fixture() |
| 89 | def sample_textbox(blank_slide): |
| 90 | """Slide with a textbox containing known text and formatting.""" |
| 91 | txBox = blank_slide.shapes.add_textbox(Inches(1), Inches(1), Inches(4), Inches(1)) |
| 92 | tf = txBox.text_frame |
| 93 | tf.text = "Sample Text" |
| 94 | tf.paragraphs[0].runs[0].font.size = Pt(18) |
| 95 | tf.paragraphs[0].runs[0].font.bold = True |
| 96 | return txBox |
| 97 | |
| 98 | |
| 99 | @pytest.fixture() |
| 100 | def sample_shape(blank_slide): |
| 101 | """Slide with a rectangle shape having fill and text.""" |
| 102 | from pptx.enum.shapes import MSO_SHAPE |
| 103 | |
| 104 | shape = blank_slide.shapes.add_shape( |
| 105 | MSO_SHAPE.RECTANGLE, |
| 106 | Inches(2), |
| 107 | Inches(2), |
| 108 | Inches(3), |
| 109 | Inches(2), |
| 110 | ) |
| 111 | shape.text = "Shape Text" |
| 112 | shape.fill.solid() |
| 113 | from pptx.dml.color import RGBColor |
| 114 | |
| 115 | shape.fill.fore_color.rgb = RGBColor(0x00, 0x78, 0xD4) |
| 116 | return shape |
| 117 | |
| 118 | |
| 119 | @pytest.fixture() |
| 120 | def sample_image_path(tmp_path): |
| 121 | """Minimal valid PNG file at a temporary path.""" |
| 122 | img = tmp_path / "test.png" |
| 123 | img.write_bytes(_minimal_png_bytes()) |
| 124 | return img |
| 125 | |
| 126 | |
| 127 | @pytest.fixture(scope="session") |
| 128 | def powerpoint_fixture_dir() -> Path: |
| 129 | return Path(__file__).parent / "fixtures" |
| 130 | |
| 131 | |
| 132 | @pytest.fixture(scope="session") |
| 133 | def minimal_test_fixture_path(powerpoint_fixture_dir: Path) -> Path: |
| 134 | return powerpoint_fixture_dir / "minimal_test_fixture.pptx" |
| 135 | |