test(thumbnails): make the no-JPEG-XL-encoder fixture authoritative (#371)

{available:false} stood in only the cached detection, and every settings
read probes a cached None again, finding the host's real encoder. The
fixture now stands in for the detector itself and reset restores it; a
unit test proves the route against a detector that finds an encoder.
This commit is contained in:
Vito Sansevero
2026-10-27 18:27:36 -07:00
parent 7fc36283f8
commit ba22b3721c
2 changed files with 72 additions and 4 deletions
+14 -2
View File
@@ -35,7 +35,8 @@ read. These per-server hooks only change what a System test needs on top:
previews for Convert outdated;
- ``POST /__test__/system/jxl-encoder`` ``{available}`` (#317) makes the
server read as having (``cjxl``) or lacking a JPEG XL encoder without
probing; reset restores the thumbnail settings and the real detection.
probing, whatever the host has installed (#371: ``false`` stands in for the
detector itself); reset restores the thumbnail settings and the real detection.
- ``POST /__test__/system/duplicate-prompts`` (S7) adds two prompts whose
text differs only in case and spacing (a duplicate prompt text group);
@@ -118,6 +119,11 @@ def register_system_commands(app, routes, api, root: Path):
}
from py.api import thumbnail_encode
real_detect = thumbnail_encode._detect
def _no_encoder():
return None
monitor = getattr(image_monitor, "_monitor_instance", None)
failures: dict[str, int] = {}
@@ -147,6 +153,7 @@ def register_system_commands(app, routes, api, root: Path):
api._health_report = None
for (owner, name), value in host_values.items():
setattr(owner, name, value)
thumbnail_encode._detect = real_detect
thumbnail_encode.reset_encoders()
image_monitor._monitor_instance = monitor
api.__dict__.pop("_library_resolver", None)
@@ -298,7 +305,12 @@ def register_system_commands(app, routes, api, root: Path):
available = data.get("available")
if not isinstance(available, bool):
raise web.HTTPBadRequest()
# Test-only: stand in the cached detection (no probe).
# Test-only: the fixture, not the host, decides (#371). Without an encoder the
# detector itself stands in, so a re-probe (a "none found" is probed again on
# every settings read) cannot find the host's real encoder; with one the cached
# answer stands, and a failed write still probes the real detector as before.
# Reset restores the real detector.
thumbnail_encode._detect = real_detect if available else _no_encoder
with thumbnail_encode._detect_lock:
thumbnail_encode._detected = "cjxl" if available else None
return web.json_response({"success": True})
+58 -2
View File
@@ -1,12 +1,26 @@
"""The System browser hooks only ever reset their own server's config.json (#249 S3)."""
"""The System browser hooks only ever reset their own server's config.json (#249 S3)
and decide JPEG XL availability whatever the host has installed (#371)."""
import asyncio
import os
import sys
import tempfile
import types
import unittest
from pathlib import Path
from unittest import mock
from tests.browser.workspace_system_fixture import register_system_commands
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer
_mock_server = mock.MagicMock()
_mock_server.PromptServer.instance.routes = mock.MagicMock()
sys.modules.setdefault("server", _mock_server)
from py.api import thumbnail_encode # noqa: E402
from tests.browser.workspace_system_fixture import ( # noqa: E402
register_system_commands,
)
class TestWorkspaceSystemFixture(unittest.TestCase):
@@ -33,6 +47,48 @@ class TestWorkspaceSystemFixture(unittest.TestCase):
register_system_commands(None, None, None, self.root)
self.assertEqual(elsewhere.read_bytes(), b'{"real": true}')
def test_no_encoder_holds_even_when_the_host_has_one(self):
"""``{available: false}`` survives the settings read's re-probe (#371)."""
config = self.root / "config.json"
env = {**os.environ, "PROMPT_MANAGER_CONFIG_PATH": str(config)}
api = types.SimpleNamespace()
app, routes = web.Application(), web.RouteTableDef()
real_detect = thumbnail_encode._detect
with mock.patch.dict(os.environ, env, clear=True):
reset = register_system_commands(app, routes, api, self.root)
app.add_routes(routes)
self.addCleanup(thumbnail_encode.reset_encoders)
self.addCleanup(setattr, thumbnail_encode, "_detect", real_detect)
async def scenario():
async with TestClient(TestServer(app)) as client:
async def post(available):
response = await client.post(
"/__test__/system/jxl-encoder", json={"available": available}
)
self.assertEqual(response.status, 200)
await post(False)
status = thumbnail_encode.encoder_status()["jxl"]
self.assertFalse(status["available"])
self.assertIsNone(
thumbnail_encode.detect_jxl_encoder(recheck_missing=True)
)
await post(True)
status = thumbnail_encode.encoder_status()["jxl"]
self.assertEqual(status["encoder"], "cjxl")
await post(False)
# The host's real detector finds an encoder (Pillow can save JPEG XL).
with mock.patch.object(thumbnail_encode, "_pillow_can_save_jxl", lambda: True):
asyncio.run(scenario())
# Reset gives detection back to the host.
reset()
self.assertIs(thumbnail_encode._detect, real_detect)
status = thumbnail_encode.encoder_status()["jxl"]
self.assertEqual(status["encoder"], "pillow")
if __name__ == "__main__":
unittest.main()