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
9 changes: 5 additions & 4 deletions novelforge/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@

# Validate that every allowed genre has a name-pool mapping.
_missing_name_genres = ALLOWED_GENRES - _GENRE_GROUP.keys()
assert not _missing_name_genres, (
f"Genres missing from _GENRE_GROUP in names.py: {sorted(_missing_name_genres)}. "
f"Add a style-group mapping for each."
)
if _missing_name_genres:
raise ValueError(
f"Genres missing from _GENRE_GROUP in names.py: {sorted(_missing_name_genres)}. "
f"Add a style-group mapping for each."
)

# ---------------------------------------------------------------------------
# Name pool data
Expand Down
9 changes: 5 additions & 4 deletions novelforge/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@

# Validate that every allowed genre has voice weights.
_missing_voice_genres = ALLOWED_GENRES - _GENRE_VOICE_WEIGHTS.keys()
assert not _missing_voice_genres, (
f"Genres missing from _GENRE_VOICE_WEIGHTS in voice.py: {sorted(_missing_voice_genres)}. "
f"Add a voice weight mapping for each."
)
if _missing_voice_genres:
raise ValueError(
f"Genres missing from _GENRE_VOICE_WEIGHTS in voice.py: {sorted(_missing_voice_genres)}. "
f"Add a voice weight mapping for each."
)


def select_voice_seed(genre: str = "", premise: str = "") -> dict[str, str]:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Tests for the novelforge.names module (genre-aware name pools)."""

import importlib

import pytest

import novelforge.names
import novelforge.validation
from novelforge.names import (
_GENRE_GROUP,
_NAMES,
Expand Down Expand Up @@ -167,3 +171,22 @@ def test_output_mentions_male_female_last(self):
assert "male first names" in result.lower()
assert "female first names" in result.lower()
assert "last names" in result.lower()


# ---------------------------------------------------------------------------
# Genre coverage validation raises ValueError (not silenced by -O flag)
# ---------------------------------------------------------------------------

class TestNameGenreValidation:
"""names.py must raise ValueError for missing genre mappings even under -O."""

def test_raises_value_error_when_genre_missing_from_genre_group(self, monkeypatch):
"""Reloading names.py with an extra ALLOWED_GENRES entry raises ValueError."""
extended = novelforge.validation.ALLOWED_GENRES | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
with pytest.raises(ValueError, match="FakeGenreForTest"):
Comment on lines +185 to +187
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.

The failing importlib.reload(novelforge.names) leaves novelforge.names in a partially re-executed state for the remainder of the test session (reload reuses the existing module dict and does not roll back on exceptions). That can create order-dependent behavior if any later test imports/uses novelforge.names expecting a clean module state. Consider reloading the module back to a known-good state in a finally block (after restoring ALLOWED_GENRES) or using a fixture that always reloads novelforge.names with the standard ALLOWED_GENRES after this test.

Suggested change
extended = novelforge.validation.ALLOWED_GENRES | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
with pytest.raises(ValueError, match="FakeGenreForTest"):
original_allowed_genres = novelforge.validation.ALLOWED_GENRES
extended = original_allowed_genres | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
try:
with pytest.raises(ValueError, match="FakeGenreForTest"):
importlib.reload(novelforge.names)
finally:
monkeypatch.setattr(
novelforge.validation,
"ALLOWED_GENRES",
original_allowed_genres,
)

Copilot uses AI. Check for mistakes.
importlib.reload(novelforge.names)

def test_no_error_when_all_genres_covered(self):
"""Reloading names.py with the standard ALLOWED_GENRES does not raise."""
importlib.reload(novelforge.names)
23 changes: 23 additions & 0 deletions tests/test_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
Tests for novelforge/voice.py — voice seed selection and formatting.
"""

import importlib
import random

import pytest

import novelforge.validation
import novelforge.voice
from novelforge.voice import (
_PREMISE_KEYWORD_BOOST,
_PREMISE_KEYWORDS,
Expand Down Expand Up @@ -212,3 +215,23 @@ def test_embeds_seed_content(self):
assert seed["prose_style"] in prompt
assert seed["emotional_register"] in prompt
assert seed["sensory_preference"] in prompt



# ---------------------------------------------------------------------------
# Genre coverage validation raises ValueError (not silenced by -O flag)
# ---------------------------------------------------------------------------

class TestVoiceGenreValidation:
"""voice.py must raise ValueError for missing genre mappings even under -O."""

def test_raises_value_error_when_genre_missing_from_voice_weights(self, monkeypatch):
"""Reloading voice.py with an extra ALLOWED_GENRES entry raises ValueError."""
extended = novelforge.validation.ALLOWED_GENRES | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
with pytest.raises(ValueError, match="FakeGenreForTest"):
Comment on lines +230 to +232
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.

Similar to names.py: when importlib.reload(novelforge.voice) raises, the module is left partially re-executed (no rollback), which can leak state into later tests and make outcomes order-dependent. It’s safer to restore a clean module state after the assertion (e.g., reload again after monkeypatch is undone, or wrap the reload in try/except + finally that reloads with the original ALLOWED_GENRES).

Suggested change
extended = novelforge.validation.ALLOWED_GENRES | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
with pytest.raises(ValueError, match="FakeGenreForTest"):
original_allowed_genres = novelforge.validation.ALLOWED_GENRES
extended = original_allowed_genres | {"FakeGenreForTest"}
monkeypatch.setattr(novelforge.validation, "ALLOWED_GENRES", extended)
try:
with pytest.raises(ValueError, match="FakeGenreForTest"):
importlib.reload(novelforge.voice)
finally:
monkeypatch.setattr(
novelforge.validation, "ALLOWED_GENRES", original_allowed_genres
)

Copilot uses AI. Check for mistakes.
importlib.reload(novelforge.voice)

def test_no_error_when_all_genres_covered(self):
"""Reloading voice.py with the standard ALLOWED_GENRES does not raise."""
importlib.reload(novelforge.voice)
Loading