From 2c506d935e34f2be64880b38a08a83b2f2ecc4af Mon Sep 17 00:00:00 2001 From: Lukas Bielesch Date: Thu, 12 Jun 2025 14:52:37 +0200 Subject: [PATCH] chore(tests): replace hardcoded strings with translations add new translation function format implement abort test for delizia and eckhart --- .../test_shamir_persistence.py | 72 +++++++++++++------ tests/translations.py | 13 ++++ 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/tests/persistence_tests/test_shamir_persistence.py b/tests/persistence_tests/test_shamir_persistence.py index bbeb37df13..63e18e9ea1 100644 --- a/tests/persistence_tests/test_shamir_persistence.py +++ b/tests/persistence_tests/test_shamir_persistence.py @@ -14,12 +14,11 @@ # You should have received a copy of the License along with this library. # If not, see . -import pytest - from trezorlib import device from trezorlib.debuglink import DebugLink, LayoutType from trezorlib.messages import RecoveryStatus +from .. import translations as TR from ..click_tests import common, recovery from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6 from ..device_handler import BackgroundDeviceHandler @@ -40,8 +39,8 @@ def test_abort(core_emulator: Emulator): debug = device_handler.debuglink() features = device_handler.features() - if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart): - pytest.skip("abort not supported on T3T1") + # if debug.layout_type is LayoutType.Delizia: + # pytest.skip("abort not supported on T3T1") assert features.recovery_status == RecoveryStatus.Nothing @@ -49,24 +48,36 @@ def test_abort(core_emulator: Emulator): recovery.confirm_recovery(debug) layout = debug.read_layout() - assert "number of words" in layout.text_content() + assert TR.recovery__num_of_words in layout.text_content() debug = _restart(device_handler, core_emulator) features = device_handler.features() assert features.recovery_status == RecoveryStatus.Recovery - assert "number of words" in debug.read_layout().text_content() + assert TR.recovery__num_of_words in debug.read_layout().text_content() # clicking at 24 in word choice recovery.select_number_of_words(debug, 24) # Cancelling the backup - assert "Enter your backup" in debug.read_layout().text_content() - layout = common.go_back(debug) + text_content = debug.read_layout().text_content() + assert any( + needle in text_content + for needle in [ + TR.recovery__enter_each_word, + TR.recovery__enter_backup, + ] + ) - assert layout.title().lower() in ("abort recovery", "cancel recovery") - for _ in range(layout.page_count()): - common.go_next(debug) + if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart): + # cancel in the menu + debug.click(debug.screen_buttons.menu()) + debug.click(debug.screen_buttons.vertical_menu_items()[0]) + else: + layout = common.go_back(debug) + assert TR.recovery__title_cancel_recovery.lower() in layout.title().lower() + for _ in range(layout.page_count()): + common.go_next(debug) assert debug.read_layout().main_component() == "Homescreen" features = device_handler.features() @@ -145,9 +156,13 @@ def test_recovery_on_old_wallet(core_emulator: Emulator): words = first_share.split(" ") # start entering first share - assert ( - "Enter any share" in debug.read_layout().text_content() - or "Enter each word" in debug.read_layout().text_content() + text_content = debug.read_layout().text_content() + assert any( + needle in text_content + for needle in [ + TR.recovery__enter_any_share, + TR.recovery__enter_each_word, + ] ) debug.press_yes() assert_mnemonic_keyboard(debug) @@ -167,7 +182,16 @@ def test_recovery_on_old_wallet(core_emulator: Emulator): layout = debug.read_layout() # check that we entered the first share successfully - assert "2 more shares needed" in layout.text_content() + text_content = layout.text_content().strip() + + assert ( + TR.format( + "recovery__x_more_shares_needed_template_plural", + count=2, + plural="shares", + ) + in text_content + ) # try entering the remaining shares for share in MNEMONIC_SLIP39_BASIC_20_3of6[1:3]: @@ -185,23 +209,29 @@ def test_recovery_on_old_wallet(core_emulator: Emulator): def test_recovery_multiple_resets(core_emulator: Emulator): def enter_shares_with_restarts(debug: DebugLink) -> None: shares = MNEMONIC_SLIP39_ADVANCED_20 + share_num = [1, 5, 3, 4] + group_num = [2, 3, 3, 3] layout = debug.read_layout() - expected_text = "Enter any share" + expected_text = TR.recovery__enter_any_share if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart): - expected_text = "Enter each word" + expected_text = TR.recovery__enter_each_word remaining = len(shares) - for share in shares: + for idx, share in enumerate(shares): assert expected_text in layout.text_content() layout = recovery.enter_share(debug, share) remaining -= 1 expected_text = ( - "entered" + TR.format( + "recovery__share_from_group_entered_template", + share_num[idx], + group_num[idx], + ) if debug.layout_type is LayoutType.Eckhart - else "You have entered" + else TR.recovery__you_have_entered ) debug = _restart(device_handler, core_emulator) - assert "Wallet recovery completed" in layout.text_content() + assert TR.recovery__wallet_recovered in layout.text_content() device_handler = BackgroundDeviceHandler(core_emulator.client) debug = device_handler.debuglink() diff --git a/tests/translations.py b/tests/translations.py index dcc855b33d..6ae3c40b53 100644 --- a/tests/translations.py +++ b/tests/translations.py @@ -140,6 +140,15 @@ class Translation: re_safe = re.escape(tr) return re.compile(self.FORMAT_STR_RE.sub(r".*?", re_safe)) + def format(self, key: str, *args, **kwargs) -> str: + tr = self.translate(key) + try: + return tr.format(*args, **kwargs) + except (KeyError, IndexError) as e: + raise ValueError( + f"Failed to format translation '{key}' with args={args}, kwargs={kwargs}: {e}" + ) from e + TRANSLATIONS = {lang: Translation(lang) for lang in LANGUAGES} _CURRENT_TRANSLATION.TR = TRANSLATIONS["en"] @@ -154,6 +163,10 @@ def regexp(key: str) -> re.Pattern: return _CURRENT_TRANSLATION.TR.as_regexp(key, _stacklevel=1) +def format(key: str, *args, **kwargs) -> str: + return _CURRENT_TRANSLATION.TR.format(key, *args, **kwargs) + + def __getattr__(key: str) -> str: try: return translate(key, _stacklevel=1)