summaryrefslogtreecommitdiff
path: root/tests/unit/test_amsats.py
blob: d985a6e03d2a3683aeacbe462d3a0aaa28cde81f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import unittest
from unittest import mock
from chirp.sources import amsats


class TestAmsatsSatNOGS(unittest.TestCase):
    def setUp(self):
        self.satnogs = amsats.SatNOGS()

    def test_item_to_memory_mode_mapping(self):
        test_cases = [
            ('FMN', 'NFM'),
            ('DSB', 'AM'),
            ('AM', 'AM'),
            ('CW', 'CW'),
            ('USB', 'USB'),
            ('LSB', 'LSB'),
            ('GMSK', 'DIG'),
            ('GFSK', 'DIG'),
            ('MSK', 'DIG'),
            ('BPSK', 'DIG'),
            ('DVB-S2', 'DIG'),
            ('AHRPT', 'DIG'),
            ('LoRa', 'DIG'),
            ('SSTV', 'DIG'),
        ]

        for orig_mode, expected_mode in test_cases:
            item = {
                'downlink_low': 435000000,
                'mode': orig_mode,
                'type': 'Transmitter',
                '_satellite': {'name': 'TestSat', 'norad_cat_id': '12345'}
            }
            mem = self.satnogs.item_to_memory(item)
            self.assertEqual(mem.mode, expected_mode,
                             f"Failed mapping {orig_mode}")

            # Check comment for non-native modes
            if expected_mode in ['DIG', 'AM'] and orig_mode != expected_mode:
                self.assertIn(f"Mode:{orig_mode}", mem.comment)

            # Check for fixed URL
            self.assertIn("https://db.satnogs.org/satellite/12345",
                          mem.comment)
            # Check for removed NORAD: prefix
            self.assertFalse(mem.comment.startswith("NORAD:"))

    def test_item_to_memory_native_mode_redundancy(self):
        # Native modes shouldn't have "Mode:XXX" in comment
        item = {
            'downlink_low': 145000000,
            'mode': 'FM',
            'type': 'Transmitter',
            '_satellite': {'name': 'TestSat', 'norad_cat_id': '54321'}
        }
        mem = self.satnogs.item_to_memory(item)
        self.assertEqual(mem.mode, 'FM')
        self.assertNotIn("Mode:FM", mem.comment)

    @mock.patch('chirp.sources.amsats.SatNOGS._fetch_all')
    def test_do_fetch_filtering(self, mock_fetch):
        # Setup mocks
        mock_fetch.side_effect = [
            # Satellites
            [{'sat_id': 'SAT1', 'name': 'Sat 1', 'norad_cat_id': '1'}],
            # Transmitters
            [
                {'sat_id': 'SAT1', 'mode': 'FM', 'downlink_low': 100},
                {'sat_id': 'SAT1', 'mode': 'GMSK', 'downlink_low': 200},
                {'sat_id': 'SAT1', 'mode': None, 'downlink_low': 300},
            ],
            # Satellites second call
            [{'sat_id': 'SAT1', 'name': 'Sat 1', 'norad_cat_id': '1'}],
            # Transmitters second call
            [
                {'sat_id': 'SAT1', 'mode': 'FM', 'downlink_low': 100},
                {'sat_id': 'SAT1', 'mode': 'GMSK', 'downlink_low': 200},
                {'sat_id': 'SAT1', 'mode': None, 'downlink_low': 300},
            ]
        ]

        # Test FM filter
        status = mock.MagicMock()
        self.satnogs.do_fetch(status, {'modes': ['FM']})
        self.assertEqual(len(self.satnogs._memories), 1)
        self.assertEqual(self.satnogs._memories[0].mode, 'FM')

        # Reset and test GMSK filter
        self.satnogs._memories = []
        self.satnogs.do_fetch(status, {'modes': ['GMSK']})
        self.assertEqual(len(self.satnogs._memories), 1)
        self.assertEqual(self.satnogs._memories[0].mode, 'DIG')


class TestAmsatsRadioAmateur(unittest.TestCase):
    def test_item_to_memory_restyling(self):
        ra_sat = amsats.RadioAmateurSatellites()
        item = {
            'name': 'Sat1',
            'norad_id': '123',
            'satnogs_id': '123',
            'downlink': '145.800',
            'mode': 'FM',
        }
        mem = ra_sat.item_to_memory(item)
        self.assertEqual(mem.mode, 'FM')
        self.assertIn("https://db.satnogs.org/satellite/123", mem.comment)
        self.assertFalse(mem.comment.startswith("NORAD:"))
        # RadioAmateurSatellites still has Mode: in comment as per my
        # implementation
        self.assertIn("Mode:FM", mem.comment)