|
13 | 13 | from __future__ import annotations |
14 | 14 |
|
15 | 15 | import json |
| 16 | +from fractions import Fraction |
16 | 17 | from pathlib import Path |
17 | 18 |
|
18 | 19 | import pytest |
@@ -1106,6 +1107,128 @@ def test_every_measured_preset_round_trips_at_this_duration(self, tmp_path, dura |
1106 | 1107 | ) |
1107 | 1108 |
|
1108 | 1109 |
|
| 1110 | +class TestOnlyTheTwoRoundingsOfAHalfAreAccepted: |
| 1111 | + """A scaled half has two legitimate values, and they are not symmetric. |
| 1112 | +
|
| 1113 | + `_scaled` rounds a half up, so the value it writes is the *upper* rounding |
| 1114 | + and PowerPoint's alternative is the lower one. Allowing |
| 1115 | + `abs(written - expected) <= 1` also accepted the value one above, which |
| 1116 | + neither writer emits ā so a hand-edited `@delay` of 99 was taken for the 98 |
| 1117 | + sdpm had written, and the next build put 98 back over it. |
| 1118 | +
|
| 1119 | + The three cases are the ones the review measured. Each `source Ć ratio` is |
| 1120 | + an exact half, which is the only place any slack applies at all. |
| 1121 | + """ |
| 1122 | + |
| 1123 | + #: category, effect, duration, the value sdpm writes, element, attribute. |
| 1124 | + HALVES = [ |
| 1125 | + ("entrance", "bounce", 300, 98, "animScale", "delay"), |
| 1126 | + ("entrance", "drop", 875, 137, "anim", "dur"), |
| 1127 | + ("exit", "boomerang", 201, 101, "anim", "delay"), |
| 1128 | + ] |
| 1129 | + |
| 1130 | + def _retime(self, element, attribute, before, after): |
| 1131 | + def mutate(slide): |
| 1132 | + changed = 0 |
| 1133 | + for node in slide.iter(f"{{{NS['p']}}}{element}"): |
| 1134 | + ctn = node.find(f"{{{NS['p']}}}cBhvr").find(f"{{{NS['p']}}}cTn") |
| 1135 | + if attribute == "dur": |
| 1136 | + target = ctn |
| 1137 | + else: |
| 1138 | + conditions = ctn.find(f"{{{NS['p']}}}stCondLst") |
| 1139 | + target = ( |
| 1140 | + None if conditions is None |
| 1141 | + else conditions.find(f"{{{NS['p']}}}cond") |
| 1142 | + ) |
| 1143 | + if target is None or target.get(attribute) != str(before): |
| 1144 | + continue |
| 1145 | + target.set(attribute, str(after)) |
| 1146 | + changed += 1 |
| 1147 | + assert changed, ( |
| 1148 | + f"the fixture must carry p:{element}/@{attribute}={before}" |
| 1149 | + ) |
| 1150 | + return mutate |
| 1151 | + |
| 1152 | + def _read_at(self, tmp_path, case, offset): |
| 1153 | + category, effect, duration, written, element, attribute = case |
| 1154 | + package = _package(tmp_path, _slide(animations={ |
| 1155 | + "mainSequence": [_effect( |
| 1156 | + preset={"category": category, "effect": effect}, |
| 1157 | + timing={"durationMs": duration}, |
| 1158 | + )], |
| 1159 | + "interactiveSequences": [], |
| 1160 | + }), name=f"{category}-{effect}-{written + offset}.pptx") |
| 1161 | + mutate = ( |
| 1162 | + (lambda _slide: None) if offset == 0 |
| 1163 | + else self._retime(element, attribute, written, written + offset) |
| 1164 | + ) |
| 1165 | + return _read(_mutate(package, mutate)) |
| 1166 | + |
| 1167 | + @pytest.mark.parametrize("case", HALVES, ids=lambda c: f"{c[0]}.{c[1]}@{c[2]}") |
| 1168 | + def test_the_value_above_the_upper_rounding_is_refused(self, tmp_path, case): |
| 1169 | + motion = self._read_at(tmp_path, case, +1) |
| 1170 | + assert motion.animations is None, ( |
| 1171 | + f"{case[3] + 1} is neither rounding of the product, so taking it for " |
| 1172 | + f"{case[3]} would write {case[3]} back over a hand edit" |
| 1173 | + ) |
| 1174 | + assert motion.raw_timing is not None |
| 1175 | + |
| 1176 | + @pytest.mark.parametrize("case", HALVES, ids=lambda c: f"{c[0]}.{c[1]}@{c[2]}") |
| 1177 | + def test_both_roundings_of_the_half_still_import(self, tmp_path, case): |
| 1178 | + """The refusal must not cost the case the slack exists for. |
| 1179 | +
|
| 1180 | + PowerPoint's float precision splits an exact half both ways, so the |
| 1181 | + lower rounding is a file PowerPoint really writes. |
| 1182 | + """ |
| 1183 | + for offset in (-1, 0): |
| 1184 | + motion = self._read_at(tmp_path, case, offset) |
| 1185 | + assert motion.animations is not None, ( |
| 1186 | + f"{case[3] + offset} is a rounding of the product: " |
| 1187 | + f"{motion.raw_timing.reasons[0] if motion.raw_timing else ''}" |
| 1188 | + ) |
| 1189 | + |
| 1190 | + def test_no_half_anywhere_in_the_catalogue_accepts_the_value_above(self, tmp_path): |
| 1191 | + """The three cases above are named; this is every one of them. |
| 1192 | +
|
| 1193 | + Which timing lands on an exact half depends on the ratio, so the cases |
| 1194 | + cannot be enumerated by hand ā the same reason the duration sweep |
| 1195 | + covers the whole catalogue. 64 locations across 97 presets at three |
| 1196 | + durations; 27 of them imported semantically before the fix. |
| 1197 | + """ |
| 1198 | + from sdpm.engine.timing.writer import _is_visibility_set, _scaled |
| 1199 | + |
| 1200 | + leaked, checked = [], 0 |
| 1201 | + for key in SCALABLE_MEASURED: |
| 1202 | + preset, measured = _measured_preset(key) |
| 1203 | + entry = next(item for item in CATALOG["guiItems"] if item["key"] == key) |
| 1204 | + for duration in (300, 875, 1234): |
| 1205 | + if duration == measured: |
| 1206 | + continue |
| 1207 | + ratio = Fraction(duration, measured) |
| 1208 | + for behavior in entry["ooxml"]["behaviors"]: |
| 1209 | + if _is_visibility_set(behavior): |
| 1210 | + continue |
| 1211 | + element = str(behavior.get("element", ""))[len("p:"):] |
| 1212 | + for attribute, field in (("dur", "durMs"), ("delay", "delayMs")): |
| 1213 | + source = behavior.get(field) |
| 1214 | + if source is None: |
| 1215 | + continue |
| 1216 | + if (Fraction(int(source)) * ratio).denominator != 2: |
| 1217 | + continue |
| 1218 | + checked += 1 |
| 1219 | + written = _scaled(int(source), ratio) |
| 1220 | + case = (preset["category"], preset["effect"], duration, |
| 1221 | + written, element, attribute) |
| 1222 | + if self._read_at(tmp_path, case, +1).animations is not None: |
| 1223 | + leaked.append(f"{key}@{duration} p:{element}/@{attribute} " |
| 1224 | + f"{written} -> {written + 1}") |
| 1225 | + assert checked, "the catalogue must produce some exact halves to check" |
| 1226 | + assert not leaked, ( |
| 1227 | + f"{len(leaked)} of {checked} half timings accepted a value neither " |
| 1228 | + f"writer produces: {leaked[:10]}" |
| 1229 | + ) |
| 1230 | + |
| 1231 | + |
1109 | 1232 | class TestAMeasuredTimingMustBeAnInteger: |
1110 | 1233 | """A recorded timing that is absent or non-numeric is a change, not a gap. |
1111 | 1234 |
|
|
0 commit comments