Existing directories were reported as success:true while the preview pane opened nothing. Fail closed with an explicit error and do not emit preview.open. HTTP(S) URLs and regular files are unchanged.
83 lines
2.1 KiB
Python
83 lines
2.1 KiB
Python
"""Tests for the GUI-surface ``open_preview`` tool."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from tools import desktop_ui, open_preview_tool as op
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_emitter():
|
|
"""Each test controls the emitter; never leak one across tests."""
|
|
desktop_ui.set_emitter(None)
|
|
yield
|
|
desktop_ui.set_emitter(None)
|
|
|
|
|
|
|
|
|
|
def test_emitter_failure_is_reported():
|
|
def _boom(*_a):
|
|
raise RuntimeError("no window")
|
|
|
|
desktop_ui.set_emitter(_boom)
|
|
assert "no window" in json.loads(op.open_preview_tool("https://x.example"))["error"]
|
|
|
|
|
|
def _capture_emits():
|
|
emitted: list = []
|
|
|
|
def _emit(_sid, event, payload):
|
|
emitted.append((event, payload))
|
|
|
|
desktop_ui.set_emitter(_emit)
|
|
return emitted
|
|
|
|
|
|
def test_existing_directory_is_an_error_not_success(tmp_path):
|
|
"""#95853: a directory must not report success while opening nothing."""
|
|
emitted = _capture_emits()
|
|
folder = tmp_path / "Active"
|
|
folder.mkdir()
|
|
|
|
result = json.loads(op.open_preview_tool(str(folder)))
|
|
|
|
assert "error" in result
|
|
assert "director" in result["error"].lower()
|
|
assert result.get("success") is not True
|
|
assert emitted == []
|
|
|
|
|
|
def test_existing_file_still_opens(tmp_path):
|
|
emitted = _capture_emits()
|
|
path = tmp_path / "notes.md"
|
|
path.write_text("hi", encoding="utf-8")
|
|
|
|
result = json.loads(op.open_preview_tool(str(path)))
|
|
|
|
assert result["success"] is True
|
|
assert result["url"] == str(path)
|
|
assert emitted == [("preview.open", {"url": str(path), "label": ""})]
|
|
|
|
|
|
def test_https_url_is_not_treated_as_a_directory():
|
|
emitted = _capture_emits()
|
|
result = json.loads(op.open_preview_tool("https://example.com/docs"))
|
|
|
|
assert result["success"] is True
|
|
assert emitted[0][0] == "preview.open"
|
|
|
|
|
|
def test_file_uri_directory_is_an_error(tmp_path):
|
|
emitted = _capture_emits()
|
|
folder = tmp_path / "docs"
|
|
folder.mkdir()
|
|
uri = folder.resolve().as_uri()
|
|
|
|
result = json.loads(op.open_preview_tool(uri))
|
|
|
|
assert "error" in result
|
|
assert "director" in result["error"].lower()
|
|
assert emitted == []
|