Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

12.0 KB
Raw Blame
"""The appshot editor's way back: an edited picture replaces the held appshot."""

from __future__ import annotations

import io

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from PIL import Image

from dodaios.appshot.store import Appshot, AppshotStore, get_store


def _shot(shot_id: str = "s1") -> Appshot:
    return Appshot(
        id=shot_id,
        image=b"jpeg",
        mime="image/jpeg",
        width=800,
        height=600,
        label="selected area",
        app_name="Editor",
        note="APPSHOT: evidence",
        ui_text="",
        trigger="hotkey",
        taken_at=1.0,
    )


def _png(width: int = 40, height: int = 30) -> bytes:
    buffer = io.BytesIO()
    Image.new("RGB", (width, height), (200, 30, 30)).save(buffer, "PNG")
    return buffer.getvalue()


def test_an_edit_reaches_the_last_and_the_waiting_appshot() -> None:
    store = AppshotStore()
    store.remember(_shot(), keep_s=60)
    store.park(_shot(), ttl_s=60)

    edited = store.replace_image("s1", b"png", "image/png", 40, 30)

    assert edited is not None and edited.note == "APPSHOT: evidence", "the context note stays"
    assert store.latest().image == b"png" and store.latest().mime == "image/png"
    pending = store.take_pending()
    assert pending.image == b"png" and (pending.width, pending.height) == (40, 30)


def test_an_edit_for_a_gone_or_other_appshot_changes_nothing() -> None:
    store = AppshotStore()
    store.remember(_shot("s1"), keep_s=60)
    assert store.replace_image("other", b"png", "image/png", 1, 1) is None
    assert store.latest().image == b"jpeg"
    assert AppshotStore().replace_image("s1", b"png", "image/png", 1, 1) is None


# -- several appshots at hand: the corner card stack ---------------------------


def test_the_store_keeps_the_last_few_newest_first() -> None:
    from dodaios.appshot.store import MAX_RECENT

    store = AppshotStore()
    for n in range(MAX_RECENT + 1):
        store.remember(_shot(f"s{n}"), keep_s=60)

    assert store.latest().id == f"s{MAX_RECENT}"
    assert store.get("s1") is not None and store.get("s1").id == "s1"
    assert store.get("s0") is None, "the oldest beyond the stack is dropped"


def test_an_older_appshot_can_be_edited_and_marked() -> None:
    store = AppshotStore()
    store.remember(_shot("old"), keep_s=60)
    store.remember(_shot("new"), keep_s=60)

    assert store.replace_image("old", b"png", "image/png", 4, 3) is not None
    store.mark_delivered("old", "voice")

    assert store.get("old").image == b"png" and store.get("old").delivered_to == "voice"
    assert store.latest().id == "new" and store.latest().image == b"jpeg"


def test_each_kept_appshot_expires_on_its_own() -> None:
    now = [0.0]
    store = AppshotStore(clock=lambda: now[0])
    store.remember(_shot("old"), keep_s=10)
    now[0] = 5.0
    store.remember(_shot("new"), keep_s=10)
    now[0] = 11.0

    assert store.get("old") is None and store.get("new") is not None
    store.remember(_shot("x"), keep_s=0)
    assert store.latest() is None, "keeping nothing forgets every picture"


@pytest.fixture
def client():
    from dodaios.ui.web.appshot_routes import router

    app = FastAPI()
    app.include_router(router)
    app.state.bus = None
    store = get_store()
    store.clear()
    store.remember(_shot("s1"), keep_s=60)
    yield TestClient(app)
    store.clear()


def test_the_route_takes_a_png_and_serves_it_back(client) -> None:
    png = _png()
    response = client.put("/api/appshot/latest/image?id=s1", content=png)

    assert response.status_code == 200, response.text
    assert response.json()["appshot"]["width"] == 40
    served = client.get("/api/appshot/latest/image")
    assert served.content == png and served.headers["content-type"] == "image/png"


def test_the_route_serves_an_older_card_its_own_picture(client) -> None:
    get_store().remember(_shot("s2"), keep_s=60)
    assert client.put("/api/appshot/latest/image?id=s1", content=_png()).status_code == 200

    assert client.get("/api/appshot/latest/image?id=s1").content == _png()
    assert client.get("/api/appshot/latest/image").content == b"jpeg", "the last one by default"
    assert client.get("/api/appshot/latest/image?id=gone").content == b"jpeg"


@pytest.mark.parametrize(
    ("body", "status"),
    [(b"", 413), (b"not an image", 400)],
)
def test_the_route_refuses_what_is_not_a_png(client, body: bytes, status: int) -> None:
    assert client.put("/api/appshot/latest/image?id=s1", content=body).status_code == status
    assert get_store().latest().image == b"jpeg"


def test_the_route_refuses_an_expired_appshot(client) -> None:
    assert client.put("/api/appshot/latest/image?id=gone", content=_png()).status_code == 404


# -- copy: the editor's PNG goes to the OS clipboard natively ----------------


def _copy_client(monkeypatch: pytest.MonkeyPatch, *, native: bool, accepted: bool = True):
    from dodaios.platform import clipboard_image
    from dodaios.ui.web.appshot_routes import router

    copied: list[bytes] = []

    def fake_write(png: bytes) -> bool:
        copied.append(png)
        return accepted

    monkeypatch.setattr(clipboard_image, "write_png", fake_write)
    app = FastAPI()
    app.include_router(router)
    app.state.native_file_actions = native
    return TestClient(app), copied


def test_copy_hands_the_png_to_the_native_clipboard(monkeypatch: pytest.MonkeyPatch) -> None:
    client, copied = _copy_client(monkeypatch, native=True)
    png = _png()

    response = client.post("/api/appshot/clipboard", content=png)

    assert response.status_code == 200, response.text
    assert copied == [png]


def test_copy_is_desktop_only(monkeypatch: pytest.MonkeyPatch) -> None:
    """On a browser/headless server the clipboard would be the server's."""
    client, copied = _copy_client(monkeypatch, native=False)

    assert client.post("/api/appshot/clipboard", content=_png()).status_code == 404
    assert copied == []


def test_copy_refuses_what_is_not_a_png(monkeypatch: pytest.MonkeyPatch) -> None:
    client, copied = _copy_client(monkeypatch, native=True)

    assert client.post("/api/appshot/clipboard", content=b"GIF89a....").status_code == 400
    assert client.post("/api/appshot/clipboard", content=b"").status_code == 413
    assert copied == []


def test_copy_says_when_the_os_refused(monkeypatch: pytest.MonkeyPatch) -> None:
    client, _copied = _copy_client(monkeypatch, native=True, accepted=False)

    response = client.post("/api/appshot/clipboard", content=_png())

    assert response.status_code == 503
    assert response.json()["detail"] == "native-clipboard-unavailable"


def test_clipboard_image_needs_a_display(monkeypatch: pytest.MonkeyPatch) -> None:
    from types import SimpleNamespace

    from dodaios.platform import clipboard_image

    monkeypatch.setattr(
        clipboard_image, "detect_capabilities", lambda: SimpleNamespace(display_present=False)
    )
    assert clipboard_image.write_png(_png()) is False
    assert clipboard_image.write_png(b"not a png") is False


def test_clipboard_image_dib_is_a_bitmap_without_the_file_header() -> None:
    from dodaios.platform.clipboard_image import png_to_dib

    dib = png_to_dib(_png(40, 30))
    # BITMAPINFOHEADER: its own size first, then width and height.
    assert int.from_bytes(dib[0:4], "little") == 40
    assert int.from_bytes(dib[4:8], "little") == 40
    assert int.from_bytes(dib[8:12], "little", signed=True) == 30


@pytest.mark.parametrize(
    ("wayland", "tools", "expected"),
    [
        ("wayland-0", {"wl-copy", "xclip"}, "wl-copy"),
        ("", {"wl-copy", "xclip"}, "xclip"),
        ("", {"wl-copy"}, "wl-copy"),
        ("", set(), None),
    ],
)
def test_clipboard_image_picks_the_linux_tool(
    monkeypatch: pytest.MonkeyPatch, wayland: str, tools: set[str], expected: str | None
) -> None:
    from dodaios.platform import clipboard_image

    ran: list[list[str]] = []
    monkeypatch.setenv("WAYLAND_DISPLAY", wayland)
    monkeypatch.setattr(
        clipboard_image.shutil, "which", lambda name: f"/usr/bin/{name}" if name in tools else None
    )
    monkeypatch.setattr(
        clipboard_image, "_run", lambda command, data=None: (ran.append(command) or True, "")
    )

    ok = clipboard_image._write_linux(_png()).ok  # noqa: SLF001

    assert ok is (expected is not None)
    assert [command[0] for command in ran] == ([f"/usr/bin/{expected}"] if expected else [])


# -- drag out: a file for the native drag bridge -------------------------------


def test_drag_file_is_written_once_and_old_ones_are_swept(
    monkeypatch: pytest.MonkeyPatch, tmp_path
) -> None:
    import os

    from dodaios.appshot import dragfile

    monkeypatch.setattr(dragfile.tempfile, "gettempdir", lambda: str(tmp_path))
    folder = dragfile.drag_folder()
    folder.mkdir()
    stale = folder / "appshot-old.png"
    stale.write_bytes(b"x")
    os.utime(stale, (1_000.0, 1_000.0))

    first = dragfile.write_drag_file(_png(), now=1_000.0 + dragfile.MAX_AGE_S + 5)
    second = dragfile.write_drag_file(_png(), now=1_000.0 + dragfile.MAX_AGE_S + 5)

    assert first is not None and second is not None and first != second
    assert first.read_bytes() == _png()
    assert not stale.exists()


def test_drag_route_returns_a_path_on_the_desktop_only(
    monkeypatch: pytest.MonkeyPatch, tmp_path
) -> None:
    from dodaios.appshot import dragfile
    from dodaios.ui.web.appshot_routes import router

    monkeypatch.setattr(dragfile.tempfile, "gettempdir", lambda: str(tmp_path))
    app = FastAPI()
    app.include_router(router)
    app.state.native_file_actions = True
    client = TestClient(app)

    response = client.post("/api/appshot/drag-file", content=_png())
    assert response.status_code == 200, response.text
    path = response.json()["path"]
    assert path.startswith(str(tmp_path)) and path.endswith(".png")
    assert client.post("/api/appshot/drag-file", content=b"nope").status_code == 400

    app.state.native_file_actions = False
    assert client.post("/api/appshot/drag-file", content=_png()).status_code == 404


# -- saved in the editor: the picture flies back into the corner ---------------


def test_the_card_route_hands_the_flight_start_through(monkeypatch: pytest.MonkeyPatch) -> None:
    from dodaios.appshot import card_actions
    from dodaios.ui.web.appshot_routes import router

    seen: list = []

    async def fake_return(fly_from=None, *, shot_id=""):
        seen.append((fly_from, shot_id))
        return True

    monkeypatch.setattr(card_actions, "return_to_corner", fake_return)
    app = FastAPI()
    app.include_router(router)
    client = TestClient(app)

    saved = client.post(
        "/api/appshot/latest/card", json={"id": "a1b2c3d4", "fly_from": [100, 80, 640, 400]}
    )
    closed = client.post("/api/appshot/latest/card")

    assert saved.json() == {"shown": True} and closed.json() == {"shown": True}
    assert seen == [([100.0, 80.0, 640.0, 400.0], "a1b2c3d4"), (None, "")]
    assert client.post("/api/appshot/latest/card", json={"fly_from": [1, 2]}).status_code == 422


async def test_the_card_command_carries_the_flight_start(monkeypatch: pytest.MonkeyPatch) -> None:
    from dodaios.cu.indicator import protocol
    from dodaios.cu.indicator.controller import CUIndicatorController

    class _Proc:
        @staticmethod
        def poll() -> None:
            return None

    controller = CUIndicatorController(bus=None)
    sent: list[tuple[str, dict]] = []
    monkeypatch.setattr(controller, "_border_capability", lambda: (True, ""))
    monkeypatch.setattr(controller, "_spawn_sidecar", lambda: setattr(controller, "_proc", _Proc()))
    monkeypatch.setattr(
        controller,
        "_send_and_wait",
        lambda cmd, _timeout, **fields: sent.append((cmd, fields)) or True,
    )

    flight = [1.0, 2.0, 300.0, 200.0]
    assert await controller.show_card(thumb_b64="t", image_b64="", fly_from=flight)
    assert await controller.show_card(thumb_b64="t", image_b64="")

    cards = [fields for cmd, fields in sent if cmd == protocol.CMD_CARD]
    assert cards[0]["from"] == [1.0, 2.0, 300.0, 200.0]
    assert "from" not in cards[1], "a plain close slides in, it does not fly"