Files
darth-veitcher-comfydv/tests
James VeitchandClaude Sonnet 5 1f138e5991 feat(llm): detect refusal/deflection responses and retry with a new seed (#36)
Some models (observed with an abliterated Qwen variant) answer a request
they judge "politically sensitive" with hedging refusal language instead of
erroring or returning blank — neither of which the existing blank-response
or schema-validation retry triggers catch, so the refusal just passed
through as a normal result.

Adds a hybrid detector to _llm/retry.py: a free lexical/regex pass catches
blatant refusals ("I cannot generate...") without any network call; a
response that's short and/or hedge-y enough to be ambiguous additionally
gets an embedding-similarity check against canonical refusal exemplars, via
a new optional LLMProvider.embed() (Ollama's /api/embed, llama-server's
/v1/embeddings) — deliberately model-agnostic to the backend, since this is
a model-behavior concern, not a backend one. A detected refusal is treated
exactly like a blank response or validation failure: retried with a bumped
seed (existing next_seed()/RETRY_BACKOFF_SECS machinery), never returned to
the caller.

Wired into all four retry loops: OllamaProvider.chat()/chat_structured(),
LlamaCppProvider.chat(), and the shared _llm/chat.py chat_structured() used
by LlamaCppProvider.chat_structured(). New OllamaOptionRefusalRetry node
(same composable OLLAMA_OPTIONS chain as OllamaOptionDisableThinking)
configures it: enabled toggle, optional embedding_model (blank = lexical-
only), similarity threshold.


Claude-Session: https://claude.ai/code/session_01YArD9ZjBWKsAvazmS48amA

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-29 17:11:08 +01:00
..

Tests

This directory contains comprehensive pytest tests for the comfydv package.

Running Tests

# Run all tests
uv run pytest

# Run with verbose output
uv run pytest -v

# Run with coverage report
uv run pytest --cov=src/comfydv --cov-report=html

# Run specific test file
uv run pytest tests/test_format_string.py

# Run specific test class
uv run pytest tests/test_format_string.py::TestVariableExtraction

# Run specific test
uv run pytest tests/test_format_string.py::TestVariableExtraction::test_extract_simple_single_variable

# Run tests matching a pattern
uv run pytest -k "jinja2"

Coverage reports are available in htmlcov/index.html after running with --cov-report=html.

Test Coverage

Current: 75% (203 statements total, 51 missed)

  • format_string.py: 78% coverage
  • __init__.py: 100% coverage
  • circuit_breaker.py: 68% coverage
  • random_choice.py: 60% coverage
  • utils.py: 75% coverage

Test Structure

conftest.py

Contains pytest configuration, fixtures, and mocks for ComfyUI dependencies:

  • Mock ComfyUI modules (comfy, server, folder_paths, aiohttp)
  • Uses pytest_configure hook to install mocks before test collection
  • Provides format_string_class fixture using importlib to directly load module
  • Fixtures for test data and class instances
  • Pytest hooks for early mock installation

test_format_string.py

Comprehensive test suite for the FormatString node with 47 tests organized into classes:

  • TestVariableExtraction (12 tests): Variable extraction from templates
  • TestSimpleFormatting (4 tests): Python format string rendering
  • TestJinja2Formatting (5 tests): Jinja2 template rendering
  • TestDynamicOutputs (6 tests): Dynamic output configuration
  • TestOutputConsistency (3 tests): Outputs match RETURN_TYPES/RETURN_NAMES
  • TestInputTypes (4 tests): INPUT_TYPES method validation
  • TestIsChanged (5 tests): Cache invalidation logic
  • TestStatePersistence (2 tests): State saving/loading
  • TestEdgeCases (4 tests): Error handling and edge cases
  • TestTimeNowFunction (2 tests): time_now utility function

Mocking Strategy

The tests use importlib.util to directly load the format_string.py module, bypassing the package __init__.py which has ComfyUI dependencies. This allows testing without ComfyUI installation while maintaining the root __init__.py for ComfyUI extension discovery.

Writing Tests

Example test

def test_new_feature(self, format_string_class, sample_data):
    """Test that new feature works correctly."""
    result = format_string_class.some_method(sample_data["name"])
    assert result == expected_value

Available fixtures

  • format_string_class: Fresh FormatString class with reset state
  • sample_templates: Dictionary of sample template strings
  • sample_data: Dictionary of sample data for templates

Test naming convention

Use descriptive names: test_<what>_<condition>_<expected>

Troubleshooting

Debugging failed tests

# Run with verbose output
uv run pytest -vv --tb=long

# Run with pdb debugger
uv run pytest --pdb

Test isolation

Each test is independent. The format_string_class fixture provides a fresh instance with reset state.

Future Improvements

  • Add integration tests with actual ComfyUI installation
  • Add JavaScript tests for frontend functionality
  • Increase coverage for circuit_breaker and random_choice nodes
  • Add property-based testing with Hypothesis