hermes-agent/tests/tui_gateway/test_image_history_display.py
finn763 23de088b6a fix(tui_gateway): project verified native image turns as display refs
Native-vision user turns persist inline base64 in history text, so the
served user text never equals the Desktop optimistic @image ref and the
optimistic bubble is re-appended below every finished reply.

Display-only projection in _history_to_messages for role=user rows:
verified local image parts become @image: refs, stored/model content
untouched. Ambiguous or missing images keep inline form.

The session store's text-only projection appends one `[screenshot]`
line per image part, so flattened rows are projected too: trailing
placeholders are peeled (one per projected ref, else unprojected) and
dropped, mirroring the Desktop's extractImageRefs.

Closes #120083
2026-09-29 19:14:53 -05:00

61 lines
3.1 KiB
Python

"""Native image history projection regression for #120083."""
import base64
from tui_gateway import server
def test_verified_native_image_history_reconciles_without_inline_payload(tmp_path):
image = tmp_path / "sample shot.png"
image.write_bytes(b"image bytes")
url = "data:image/png;base64," + base64.b64encode(image.read_bytes()).decode()
path = str(image)
parts = [{"type": "image_url", "image_url": {"url": url}}]
legacy = [{"type": "text", "text": f"Caption\n\n[Image attached at: {path}]"}, *parts,
{"type": "text", "text": "<memory-context>private recall</memory-context>"}]
current = [{"type": "text", "text": f"Caption\n@image:`{path}`"}, *parts]
history = [{"role": "user", "content": legacy}, {"role": "assistant", "content": "Done"},
{"role": "user", "content": current}]
displayed = server._history_to_messages(history)
assert displayed == [{"role": "user", "text": f"Caption\n\n@image:`{path}`"},
{"role": "assistant", "text": "Done"},
{"role": "user", "text": f"Caption\n@image:`{path}`"}]
assert history[0]["content"] is legacy
assert history[2]["content"] is current
def test_flattened_screenshot_placeholder_still_projects(tmp_path):
"""The store's text-only projection appends one `[screenshot]` per image part; those rows must project too."""
image = tmp_path / "sample shot.png"
image.write_bytes(b"image bytes")
url = "data:image/png;base64," + base64.b64encode(image.read_bytes()).decode()
path = str(image)
parts = [{"type": "image_url", "image_url": {"url": url}}]
legacy = [{"type": "text", "text": f"Caption\n\n[Image attached at: {path}]\n[screenshot]"}, *parts]
current = [{"type": "text", "text": f"Caption\n@image:`{path}`\n[screenshot]"}, *parts]
history = [{"role": "user", "content": legacy}, {"role": "user", "content": current}]
displayed = server._history_to_messages(history)
assert displayed == [{"role": "user", "text": f"Caption\n\n@image:`{path}`"},
{"role": "user", "text": f"Caption\n@image:`{path}`"}]
def test_screenshot_placeholder_without_image_part_stays_unprojected(tmp_path):
"""Text-only rows carry no inline payload to verify against, so they keep whatever was stored."""
image = tmp_path / "shot.png"
image.write_bytes(b"image bytes")
row = {"role": "user", "content": [{"type": "text", "text": f"Caption\n@image:{image}\n[screenshot]"}]}
assert server._history_to_messages([row])[0]["text"] == f"Caption\n@image:{image}\n[screenshot]"
def test_unavailable_or_mismatched_image_keeps_inline_payload(tmp_path):
image = tmp_path / "shot.png"
image.write_bytes(b"new bytes")
url = "data:image/png;base64," + base64.b64encode(b"old bytes").decode()
parts = [{"type": "text", "text": f"Caption\n@image:{image}"},
{"type": "image_url", "image_url": {"url": url}}]
row = {"role": "user", "content": parts}
assert url in server._history_to_messages([row])[0]["text"]
image.unlink()
assert url in server._history_to_messages([row])[0]["text"]