|
| 1 | +"""Tests for the top-level metadata-formatting dispatcher. |
| 2 | +
|
| 3 | +The interesting cases here aren't the renderers themselves (those are covered |
| 4 | +in ``test_invoke_metadata.py``) but the orchestration: deciding which renderer |
| 5 | +to call and conditionally appending the standalone "Use as Ref Image" button |
| 6 | +to the non-Invoke paths whenever an InvokeAI backend is configured. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from photomap.backend.config import get_config_manager |
| 16 | +from photomap.backend.metadata_formatting import format_metadata |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def clear_invokeai_config(): |
| 21 | + manager = get_config_manager() |
| 22 | + manager.set_invokeai_settings(url=None, username=None, password=None) |
| 23 | + yield |
| 24 | + manager.set_invokeai_settings(url=None, username=None, password=None) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def with_invokeai_url(clear_invokeai_config): |
| 29 | + manager = get_config_manager() |
| 30 | + manager.set_invokeai_settings(url="http://localhost:9090") |
| 31 | + yield "http://localhost:9090" |
| 32 | + manager.set_invokeai_settings(url=None, username=None, password=None) |
| 33 | + |
| 34 | + |
| 35 | +def _filepath() -> Path: |
| 36 | + return Path("/tmp/example.png") |
| 37 | + |
| 38 | + |
| 39 | +class TestNoMetadata: |
| 40 | + def test_renders_placeholder_without_invokeai(self, clear_invokeai_config): |
| 41 | + result = format_metadata(_filepath(), {}, 0, 1) |
| 42 | + assert "No metadata available" in result.description |
| 43 | + assert "invoke-recall-controls" not in result.description |
| 44 | + |
| 45 | + def test_appends_use_ref_button_when_invokeai_configured(self, with_invokeai_url): |
| 46 | + result = format_metadata(_filepath(), {}, 0, 1) |
| 47 | + assert "No metadata available" in result.description |
| 48 | + assert 'data-recall-mode="use_ref"' in result.description |
| 49 | + # No Recall/Remix without parameters to recall. |
| 50 | + assert 'data-recall-mode="recall"' not in result.description |
| 51 | + assert 'data-recall-mode="remix"' not in result.description |
| 52 | + |
| 53 | + |
| 54 | +class TestExifMetadata: |
| 55 | + EXIF = {"Make": "Canon", "Model": "EOS R5", "FNumber": 2.8} |
| 56 | + |
| 57 | + def test_no_button_without_invokeai(self, clear_invokeai_config): |
| 58 | + result = format_metadata(_filepath(), self.EXIF, 0, 1) |
| 59 | + assert "Canon" in result.description |
| 60 | + assert "invoke-recall-controls" not in result.description |
| 61 | + |
| 62 | + def test_use_ref_button_added_when_invokeai_configured(self, with_invokeai_url): |
| 63 | + result = format_metadata(_filepath(), self.EXIF, 0, 1) |
| 64 | + assert "Canon" in result.description |
| 65 | + assert 'data-recall-mode="use_ref"' in result.description |
| 66 | + assert 'data-recall-mode="recall"' not in result.description |
| 67 | + assert 'data-recall-mode="remix"' not in result.description |
| 68 | + |
| 69 | + |
| 70 | +class TestInvokeMetadata: |
| 71 | + INVOKE = { |
| 72 | + "metadata_version": 3, |
| 73 | + "app_version": "3.5.0", |
| 74 | + "positive_prompt": "anything", |
| 75 | + "seed": 1, |
| 76 | + "model": {"model_name": "m"}, |
| 77 | + } |
| 78 | + |
| 79 | + def test_no_buttons_without_invokeai(self, clear_invokeai_config): |
| 80 | + result = format_metadata(_filepath(), self.INVOKE, 0, 1) |
| 81 | + assert "anything" in result.description |
| 82 | + assert "invoke-recall-controls" not in result.description |
| 83 | + |
| 84 | + def test_full_recall_group_when_invokeai_configured(self, with_invokeai_url): |
| 85 | + result = format_metadata(_filepath(), self.INVOKE, 0, 1) |
| 86 | + assert 'data-recall-mode="recall"' in result.description |
| 87 | + assert 'data-recall-mode="remix"' in result.description |
| 88 | + assert 'data-recall-mode="use_ref"' in result.description |
| 89 | + # The use_ref button should be inside the same control group as the |
| 90 | + # recall buttons (one container, three buttons), not a duplicate |
| 91 | + # standalone container appended afterwards. |
| 92 | + assert result.description.count('class="invoke-recall-controls"') == 1 |
0 commit comments