|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +BDD-style tests for the backend_filesystem_program_settings.py file. |
| 4 | +
|
| 5 | +This file is part of ArduPilot Methodic Configurator. https://github.com/ArduPilot/MethodicConfigurator |
| 6 | +
|
| 7 | +SPDX-FileCopyrightText: 2024-2026 Amilcar do Carmo Lucas <amilcar.lucas@iav.de> |
| 8 | +
|
| 9 | +SPDX-License-Identifier: GPL-3.0-or-later |
| 10 | +""" |
| 11 | + |
| 12 | +from typing import Any |
| 13 | +from unittest.mock import patch |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from ardupilot_methodic_configurator.backend_filesystem_program_settings import ProgramSettings |
| 18 | + |
| 19 | +# pylint: disable=redefined-outer-name |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_settings_file() -> dict[str, Any]: |
| 24 | + """Fixture providing realistic settings data for testing.""" |
| 25 | + return { |
| 26 | + "Format version": 1, |
| 27 | + "display_usage_popup": { |
| 28 | + "workflow_explanation": True, |
| 29 | + "component_editor": False, |
| 30 | + }, |
| 31 | + "connection_history": ["COM3", "tcp:127.0.0.1:5760"], |
| 32 | + "directory_selection": { |
| 33 | + "template_dir": "/path/to/template", |
| 34 | + "new_base_dir": "/path/to/base", |
| 35 | + "vehicle_dir": "/path/to/vehicle", |
| 36 | + }, |
| 37 | + "auto_open_doc_in_browser": True, |
| 38 | + "gui_complexity": "simple", |
| 39 | + "motor_test": { |
| 40 | + "duration": 2, |
| 41 | + "throttle_pct": 10, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +class TestConnectionHistoryManagement: |
| 47 | + """Test user connection history management workflows.""" |
| 48 | + |
| 49 | + def test_user_can_store_new_connection(self, mock_settings_file: dict[str, Any]) -> None: |
| 50 | + """ |
| 51 | + User can save a new connection string to history. |
| 52 | +
|
| 53 | + GIVEN: A user successfully connects to a device |
| 54 | + WHEN: The connection is stored |
| 55 | + THEN: It should appear at the top of the connection history |
| 56 | + """ |
| 57 | + # Arrange: Mock settings and file operations (GIVEN) |
| 58 | + with ( |
| 59 | + patch.object(ProgramSettings, "_get_settings_as_dict", return_value=mock_settings_file), |
| 60 | + patch.object(ProgramSettings, "_set_settings_from_dict") as mock_set, |
| 61 | + ): |
| 62 | + # Act: User stores a new connection (WHEN) |
| 63 | + ProgramSettings.store_connection("udp:192.168.1.100:14550") |
| 64 | + |
| 65 | + # Assert: New connection added to top of history (THEN) |
| 66 | + mock_set.assert_called_once() |
| 67 | + saved_settings = mock_set.call_args[0][0] |
| 68 | + assert saved_settings["connection_history"][0] == "udp:192.168.1.100:14550" |
| 69 | + |
| 70 | + def test_user_can_store_multiple_connections_in_order(self, mock_settings_file: dict[str, Any]) -> None: |
| 71 | + """ |
| 72 | + User can store multiple connections and they maintain order. |
| 73 | +
|
| 74 | + GIVEN: A clean connection history |
| 75 | + WHEN: The user stores multiple connection strings |
| 76 | + THEN: The connections should be stored in most-recent-first order |
| 77 | + """ |
| 78 | + # Arrange: Start with empty history (GIVEN) |
| 79 | + mock_settings_file["connection_history"] = [] |
| 80 | + |
| 81 | + with ( |
| 82 | + patch.object(ProgramSettings, "_get_settings_as_dict", return_value=mock_settings_file), |
| 83 | + patch.object(ProgramSettings, "_set_settings_from_dict") as mock_set, |
| 84 | + ): |
| 85 | + # Act: User stores multiple connections (WHEN) |
| 86 | + ProgramSettings.store_connection("COM1") |
| 87 | + ProgramSettings.store_connection("COM2") |
| 88 | + ProgramSettings.store_connection("COM3") |
| 89 | + |
| 90 | + # Assert: Connections in correct order (THEN) |
| 91 | + # We check the very last call to see the final state of history |
| 92 | + final_settings = mock_set.call_args[0][0] |
| 93 | + history = final_settings["connection_history"] |
| 94 | + assert history[0] == "COM3" # Most recent first |
| 95 | + assert history[1] == "COM2" |
| 96 | + assert history[2] == "COM1" |
0 commit comments