Files
eco/tests/test_widget_tray.py
gac berninaandClaude Sonnet 5 59b8d30b53 Add a Log Viewer button to eco's JupyterLab and Voila dashboards
Native menu bars are frontend-only in JupyterLab (same conclusion reached
earlier this session for auto-opening a Console tab -- no server-side hook
exists), so eco desktop's Tools -> Log Viewer menu action has no direct
equivalent there. A button on the dashboard is the closest achievable
analogue, wired through eco.logs's existing prefer= mechanism so all three
front-ends share one code path instead of three parallel ones:

- eco.logs._open_timeline gained a "sidecar" prefer option: renders the
  timeline's HTML into a new eco.widgets.jupyter_sidecar.open_html_in_
  sidecar() panel -- a real, separate dockable Sidecar, matching desktop's
  menu action opening its own window.
- eco.widgets.jupyter_sidecar.NamespaceDashboard (the JupyterLab
  counterpart to EcoDesktopApp) gained a "Log Viewer" button above the
  Namespace launcher, calling eco.logs.widget(prefer="sidecar"); reopening
  replaces (closes) the previous panel rather than piling up, and it's
  torn down in .close() alongside everything else.
- eco.widgets.widget_tray.make_namespace_dashboard (Voila) gained the same
  button, calling eco.logs.widget(prefer="jupyter") into a dedicated
  ipywidgets.Output() -- Voila has no Sidecar shell to dock into, so this
  renders inline instead, same reasoning as its existing WidgetTray.

Verified via scripted construction against fake namespaces (button
present, opens, reopening replaces, torn down on close) and the full test
suite (347/349 -- the 2 failures are pre-existing in
test_config_lazy_init.py, unrelated). Along the way, found and worked
around a real pre-existing test-isolation hazard: IPython's
InteractiveShell.instance() is a process-wide singleton, and an earlier,
unrelated test building a real ipykernel-backed shell leaves a stale one
behind that later display()/Output.clear_output() calls in the same
pytest session crash against -- not something this change caused, but the
first tests to actually exercise that code path, so the new tests
short-circuit the specific stale calls rather than depending on that
global state.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-22 08:43:34 +02:00

61 lines
2.2 KiB
Python

"""Focused coverage for eco.widgets.widget_tray's Log Viewer button (see
eco.widgets.jupyter_sidecar's equivalent for the JupyterLab counterpart) --
the rest of make_namespace_dashboard/NamespaceLauncherWidget is exercised
via scripted manual testing (constructing them needs a real ipywidgets
display context to be fully meaningful; see this session's own testing
notes), not a dedicated suite here yet.
"""
from eco.widgets.widget_tray import make_namespace_dashboard
class _FakeItem:
def widget(self):
import ipywidgets as widgets
return widgets.HTML("fake widget")
class _FakeNamespace:
def __init__(self):
self.initialized_names = {"cam_west"}
self.lazy_names = set()
self.failed_names = set()
self._items = {"cam_west": _FakeItem()}
self._required = set()
def resolve_item(self, name):
return self._items.get(name)
def required_names(self, value=None):
if value is None:
return sorted(self._required)
self._required = set(value)
def test_log_viewer_button_calls_eco_logs_widget_prefer_jupyter(monkeypatch):
"""Must go through eco.logs.widget(prefer="jupyter") -- not a parallel,
duplicated HTML-rendering path -- so it stays consistent with every
other logs.widget() caller (eco desktop's Tools menu,
eco.widgets.jupyter_sidecar's JupyterLab button)."""
import eco.logs
calls = []
monkeypatch.setattr(eco.logs, "widget", lambda prefer: calls.append(prefer))
dash = make_namespace_dashboard(_FakeNamespace())
header, _tray_box = dash.children
_launcher, _status, btn_row, log_output = header.children
_layout_toggle, log_btn = btn_row.children
# A real ipykernel-backed InteractiveShell.instance() left behind by an
# earlier, unrelated test in this same pytest process (a real,
# process-wide IPython singleton) crashes Output.clear_output()'s
# attempt to actually send a clear through it -- irrelevant to what
# this test verifies (that clicking the button calls eco.logs.widget),
# so short-circuit it.
monkeypatch.setattr(log_output, "clear_output", lambda *a, **kw: None)
assert log_btn.description == "Log Viewer"
log_btn.click()
assert calls == ["jupyter"]