import tempfile import unittest from datetime import datetime, timezone from pathlib import Path, PurePosixPath from archive_clients.config import RootMapping from archive_clients.routes import discover_routes from archive_control.v1 import route_pb2 class RouteDiscoveryTests(unittest.TestCase): def test_pairwise_routes_are_discovered_and_unsafe_folders_ignored(self): with tempfile.TemporaryDirectory() as directory: roots = RootMapping(PurePosixPath("/sync"), Path(directory)) configuration = {"folders": [ { "id": "route-b", "path": "/sync/routes/b", "type": "sendreceive", "devices": [{"deviceID": "LOCAL"}, {"deviceID": "PEER"}], "label": "Archive Control route-b", }, { "id": "route-a", "path": "/sync/routes/a", "type": "sendonly", "devices": [{"deviceID": "LOCAL"}, {"deviceID": "PEER"}], }, { "id": "outside", "path": "/other/path", "type": "sendreceive", "devices": [], }, ]} routes = discover_routes( configuration, "LOCAL", roots, True, datetime(2026, 1, 2, tzinfo=timezone.utc), ) self.assertEqual([route.route_id for route in routes], ["route-a", "route-b"]) self.assertEqual(routes[0].state, route_pb2.ROUTE_STATE_UNSUPPORTED) self.assertEqual(routes[1].state, route_pb2.ROUTE_STATE_DISCOVERED) self.assertEqual(list(routes[1].peer_syncthing_device_ids), ["PEER"]) self.assertEqual(routes[1].local_relative_path, "routes/b") self.assertFalse(routes[1].archive_control_created) def test_home_relative_paths_are_normalized_under_api_root(self): with tempfile.TemporaryDirectory() as directory: roots = RootMapping(PurePosixPath("/var/syncthing"), Path(directory)) routes = discover_routes( {"folders": [{ "id": "DownloadsSync", "path": "~/DownloadsSync", "type": "sendreceive", "devices": [ {"deviceID": "LOCAL"}, {"deviceID": "ARCHIVE"}, ], }]}, "LOCAL", roots, True, ) self.assertEqual(len(routes), 1) self.assertEqual(routes[0].route_id, "DownloadsSync") self.assertEqual(routes[0].local_relative_path, "DownloadsSync") self.assertEqual(routes[0].state, route_pb2.ROUTE_STATE_DISCOVERED) if __name__ == "__main__": unittest.main()