Files
ai-agent/sidecar/test_media.py
Gabriel VidalandClaude Opus 5.5 dff31ad430 feat(images): an image Claude read opens in a browser tab, from any machine
An image Read on a worker (the Orus Mac) showed "Not previewable", or a
false "No longer available" for a Mac /private/tmp/claude-501/ path read as
an expired lab scratchpad. The transcript already holds the bitmap the model
saw, so the hub serves it (GET /api/conversation-image), and the card:

- previews the lab's own file when it serves one, else that copy (a cleared
  scratchpad falls through to it too); worker paths never map onto the lab;
- header: Open in a new tab, Copy path, and Original ↗ while the run's
  worker is online (the hub fetches the path the call read from the worker's
  new GET /media; a lab file outside the served roots links host-file/raw).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 15:23:11 +01:00

70 lines
2.4 KiB
Python

"""/media: an image a run on this worker read, served to the hub at full size."""
import os
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
import media
H = {"Authorization": "Bearer t"}
def _auth(h):
if h != "Bearer t":
raise HTTPException(401, "unauthorized")
@pytest.fixture()
def client(monkeypatch):
monkeypatch.setattr(media, "IS_WORKER", True)
app = FastAPI()
app.include_router(media.make_router(_auth))
return TestClient(app)
def test_serves_an_image_with_its_type(client, tmp_path):
(tmp_path / "shot.PNG").write_bytes(b"\x89PNG data")
r = client.get("/media", params={"path": str(tmp_path / "shot.PNG")}, headers=H)
assert r.status_code == 200
assert r.content == b"\x89PNG data"
assert r.headers["content-type"] == "image/png"
assert r.headers["x-content-type-options"] == "nosniff"
def test_needs_the_hub_token(client, tmp_path):
(tmp_path / "a.png").write_bytes(b"x")
assert client.get("/media", params={"path": str(tmp_path / "a.png")}).status_code == 401
def test_only_existing_raster_images(client, tmp_path):
(tmp_path / "notes.txt").write_text("secret")
(tmp_path / "logo.svg").write_text("<svg/>")
(tmp_path / "dir.png").mkdir()
for name in ("notes.txt", "logo.svg", "dir.png", "gone.png"):
r = client.get("/media", params={"path": str(tmp_path / name)}, headers=H)
assert r.status_code == 404, name
assert client.get("/media", params={"path": "rel/a.png"}, headers=H).status_code == 404
def test_a_link_named_like_an_image_is_judged_by_its_target(client, tmp_path):
(tmp_path / "id_rsa").write_text("KEY")
os.symlink(tmp_path / "id_rsa", tmp_path / "shot.png")
assert client.get("/media", params={"path": str(tmp_path / "shot.png")},
headers=H).status_code == 404
def test_too_big_is_refused(client, tmp_path, monkeypatch):
monkeypatch.setattr(media, "MAX_BYTES", 4)
(tmp_path / "big.jpg").write_bytes(b"12345")
assert client.get("/media", params={"path": str(tmp_path / "big.jpg")},
headers=H).status_code == 404
def test_the_host_sidecar_refuses(client, tmp_path, monkeypatch):
monkeypatch.setattr(media, "IS_WORKER", False)
(tmp_path / "a.png").write_bytes(b"x")
assert client.get("/media", params={"path": str(tmp_path / "a.png")},
headers=H).status_code == 409