Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion novelforge/routes/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,10 @@ def generate_illustrations() -> Response | tuple[Response, int]:
try:
progress_manager.update(token, {"illustration_token": illust_token})
except KeyError:
pass
logger.warning(
"Could not link illustration to novel (progress entry %s not found).",
token,
)

thread = threading.Thread(
target=_run_illustration_generation,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_illustration_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,50 @@ def test_novel_token_records_illustration_token(self, client, mock_llm, monkeypa
assert novel_state is not None
assert novel_state.get("illustration_token") == illust_token

def test_warning_logged_when_novel_progress_entry_missing(
self, client, monkeypatch, caplog
):
"""A warning is emitted (not silently swallowed) when the novel progress
entry is deleted before illustration_token can be linked to it."""
import logging
import novelforge.config as config
import novelforge.routes.export as export_module
from novelforge.progress import progress_manager as pm

monkeypatch.setattr(config, "IMAGE_API_KEY", "key")
# Prevent the background thread from running so the test stays focused.
monkeypatch.setattr(
export_module.threading,
"Thread",
lambda *a, **kw: type("T", (), {"start": lambda s: None, "daemon": True})(),
)

# Patch progress_manager.update so that the first call (linking
# illustration_token onto the novel entry) raises KeyError, simulating
# a deleted entry.
original_update = pm.update

def _raise_on_novel_token(tok, data):
if "illustration_token" in data:
raise KeyError(tok)
return original_update(tok, data)

monkeypatch.setattr(pm, "update", _raise_on_novel_token)

token = "deleted-novel-link"
_done_novel(token)

with caplog.at_level(logging.WARNING, logger="novelforge.routes.export"):
client.post("/generate_illustrations",
data=json.dumps({"token": token}),
content_type="application/json")

warning_msgs = [r.message for r in caplog.records if r.levelno == logging.WARNING]
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

caplog.records contains logging.LogRecord objects, which don’t reliably have a .message attribute populated. This can raise AttributeError or produce unformatted text depending on how pytest formats records. Prefer caplog.messages or record.getMessage() when building warning_msgs so the assertion is stable.

Suggested change
warning_msgs = [r.message for r in caplog.records if r.levelno == logging.WARNING]
warning_msgs = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]

Copilot uses AI. Check for mistakes.
assert any(token in msg for msg in warning_msgs), (
"Expected a warning mentioning the missing progress token, got: "
+ str(warning_msgs)
)


# ---------------------------------------------------------------------------
# Background worker: success flow
Expand Down
Loading