|
| 1 | +# Copyright 2026 Dimensional Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import builtins |
| 18 | +import socket |
| 19 | +from typing import Any |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from dimos.core.run_registry import RunEntry |
| 24 | +from dimos.visualization.rerun.constants import RERUN_GRPC_PORT |
| 25 | +from dimos.visualization.rerun.topic_monitor import ( |
| 26 | + PortAllocationError, |
| 27 | + TopicMonitorDependencyError, |
| 28 | + TopicMonitorSidecar, |
| 29 | + _require_visualization_dependencies, |
| 30 | + allocate_monitor_ports, |
| 31 | + open_selector_url, |
| 32 | + resolve_run_context, |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +def test_allocate_monitor_ports_avoids_default_rerun_ports() -> None: |
| 37 | + ports = allocate_monitor_ports(start=RERUN_GRPC_PORT, end=RERUN_GRPC_PORT + 20) |
| 38 | + |
| 39 | + assert RERUN_GRPC_PORT not in { |
| 40 | + ports.rerun_grpc, |
| 41 | + ports.rerun_web, |
| 42 | + ports.selector_frontend, |
| 43 | + ports.selector_api, |
| 44 | + ports.reflex_backend, |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def test_allocate_monitor_ports_fails_for_explicit_conflict() -> None: |
| 49 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 50 | + sock.bind(("127.0.0.1", 0)) |
| 51 | + port = sock.getsockname()[1] |
| 52 | + sock.listen(1) |
| 53 | + try: |
| 54 | + with pytest.raises(PortAllocationError): |
| 55 | + allocate_monitor_ports(port_base=port) |
| 56 | + finally: |
| 57 | + sock.close() |
| 58 | + |
| 59 | + |
| 60 | +def test_topic_monitor_urls_use_allocated_connected_viewer_url() -> None: |
| 61 | + sidecar = TopicMonitorSidecar( |
| 62 | + host="127.0.0.1", |
| 63 | + ports=allocate_monitor_ports(start=11150, end=11180), |
| 64 | + ) |
| 65 | + |
| 66 | + urls = sidecar.urls |
| 67 | + |
| 68 | + assert str(sidecar.ports.selector_frontend) in urls.selector |
| 69 | + assert str(sidecar.ports.selector_api) in urls.selector_api |
| 70 | + assert str(sidecar.ports.rerun_grpc) in urls.rerun_connect |
| 71 | + assert "url=rerun%2Bhttp" in urls.rerun_viewer |
| 72 | + |
| 73 | + |
| 74 | +def test_resolve_run_context_uses_latest_or_bus_only(monkeypatch: pytest.MonkeyPatch) -> None: |
| 75 | + entry = RunEntry( |
| 76 | + run_id="20260610-test", |
| 77 | + pid=123, |
| 78 | + blueprint="demo", |
| 79 | + started_at="now", |
| 80 | + log_dir="/tmp/logs", |
| 81 | + ) |
| 82 | + monkeypatch.setattr( |
| 83 | + "dimos.visualization.rerun.topic_monitor.get_most_recent", |
| 84 | + lambda alive_only=True: entry, |
| 85 | + ) |
| 86 | + |
| 87 | + context = resolve_run_context() |
| 88 | + |
| 89 | + assert context.entry is entry |
| 90 | + assert "20260610-test" in context.label |
| 91 | + |
| 92 | + monkeypatch.setattr( |
| 93 | + "dimos.visualization.rerun.topic_monitor.get_most_recent", |
| 94 | + lambda alive_only=True: None, |
| 95 | + ) |
| 96 | + |
| 97 | + assert resolve_run_context().entry is None |
| 98 | + |
| 99 | + |
| 100 | +def test_resolve_run_context_fails_for_missing_explicit_run( |
| 101 | + monkeypatch: pytest.MonkeyPatch, |
| 102 | +) -> None: |
| 103 | + monkeypatch.setattr( |
| 104 | + "dimos.visualization.rerun.topic_monitor.list_runs", lambda alive_only=True: [] |
| 105 | + ) |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match="No active DimOS run"): |
| 108 | + resolve_run_context("missing") |
| 109 | + |
| 110 | + |
| 111 | +def test_open_selector_url_is_non_fatal() -> None: |
| 112 | + assert open_selector_url("http://localhost", opener=lambda _url: False) is False |
| 113 | + |
| 114 | + def boom(_url: str) -> bool: |
| 115 | + raise RuntimeError("no browser") |
| 116 | + |
| 117 | + assert open_selector_url("http://localhost", opener=boom) is False |
| 118 | + |
| 119 | + |
| 120 | +def test_missing_dependency_error_mentions_visualization_extra( |
| 121 | + monkeypatch: pytest.MonkeyPatch, |
| 122 | +) -> None: |
| 123 | + real_import = builtins.__import__ |
| 124 | + |
| 125 | + def fake_import(name: str, *args: Any, **kwargs: Any) -> object: |
| 126 | + if name == "reflex": |
| 127 | + raise ImportError("missing reflex") |
| 128 | + return real_import(name, *args, **kwargs) |
| 129 | + |
| 130 | + monkeypatch.setattr(builtins, "__import__", fake_import) |
| 131 | + |
| 132 | + with pytest.raises(TopicMonitorDependencyError, match="uv sync --extra visualization"): |
| 133 | + _require_visualization_dependencies() |
0 commit comments