From ad9b3e5af5ae3631b931927047faeeec4bb51f4b Mon Sep 17 00:00:00 2001 From: Atishyy27 Date: Sat, 14 Nov 2026 04:49:23 +0530 Subject: [PATCH] fix(coordinates): honor an explicit zero swipe/scroll duration parse_swipe_parameters picked the duration with an or-chain, so an integer 0 was falsy and replaced by the 800 ms default while the string "0" survived as 0 ms. Select the first key whose value is usable as a duration instead; empty or invalid values still fall back to the default. --- artemis/utils/coordinates.py | 17 +++--- .../unit/utils/test_parse_swipe_parameters.py | 53 +++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 tests/unit/utils/test_parse_swipe_parameters.py diff --git a/artemis/utils/coordinates.py b/artemis/utils/coordinates.py index fc90a8f3f..14cfcbb9c 100644 --- a/artemis/utils/coordinates.py +++ b/artemis/utils/coordinates.py @@ -39,12 +39,17 @@ def parse_swipe_parameters( coordinates = args.get("coordinates") or args.get("normalized_coordinates") action = args.get("action") gesture = args.get("gesture") - duration = ( - args.get("duration") - or args.get("duration_ms") - or args.get("time_in_ms") - or default_duration - ) + duration = default_duration + for _key in ("duration", "duration_ms", "time_in_ms"): + _value = args.get(_key) + if _value is None or isinstance(_value, bool): + continue + try: + int(_value) + except (ValueError, TypeError): + continue + duration = _value + break else: direction = None start = None diff --git a/tests/unit/utils/test_parse_swipe_parameters.py b/tests/unit/utils/test_parse_swipe_parameters.py new file mode 100644 index 000000000..53c1c0278 --- /dev/null +++ b/tests/unit/utils/test_parse_swipe_parameters.py @@ -0,0 +1,53 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for duration handling in parse_swipe_parameters.""" + +from artemis.utils.coordinates import parse_swipe_parameters + + +def test_integer_zero_duration_is_honored_not_defaulted(): + # An explicit integer 0 must be honored (0ms), matching how the string "0" + # is already treated. Previously integer 0 was falsy and silently replaced + # by the default, so the same value behaved differently across types. + _, _, from_int = parse_swipe_parameters({"direction": "up", "duration": 0}) + _, _, from_str = parse_swipe_parameters({"direction": "up", "duration": "0"}) + assert from_int == 0 + assert from_str == 0 + + +def test_missing_duration_uses_default(): + _, _, duration = parse_swipe_parameters({"direction": "up"}) + assert duration == 800 + + +def test_duration_precedence_prefers_duration_key(): + _, _, duration = parse_swipe_parameters( + {"direction": "up", "duration": 100, "duration_ms": 200} + ) + assert duration == 100 + + +def test_unusable_higher_priority_duration_falls_through(): + # An empty string in the higher-priority key is not a usable duration, so + # it must fall through to the next key rather than being taken and dropped. + _, _, duration = parse_swipe_parameters({"duration": "", "duration_ms": 500}) + assert duration == 500 + + +def test_boolean_duration_is_ignored(): + # Booleans are not durations; they must not be honored (True/False), and a + # usable lower-priority key should still win. + _, _, duration = parse_swipe_parameters({"duration": False, "duration_ms": 500}) + assert duration == 500