🔍 Code Extractor

function test_compact_formatter

Maturity: 42

A test function that demonstrates the CompactResponseFormatter's ability to compress verbose LLM responses by converting a lengthy mathematical explanation into a more compact format.

File:
/tf/active/vicechatdev/e-ink-llm/test_improvements.py
Lines:
13 - 61
Complexity:
simple

Purpose

This function serves as a unit test and demonstration for the CompactResponseFormatter class. It takes a verbose mathematical response (solving a quadratic equation) and shows how the formatter can reduce its length while preserving essential information. The test outputs both the original and compressed versions along with compression statistics, making it useful for validating the formatter's effectiveness and for documentation purposes.

Source Code

def test_compact_formatter():
    """Test the compact response formatter"""
    print("🧪 Testing Compact Response Formatter")
    print("=" * 50)
    
    formatter = CompactResponseFormatter()
    
    # Test with a verbose mathematical response
    verbose_response = """
    I can see that you have written a quadratic equation that needs to be solved.
    
    The equation x² + 5x + 6 = 0 can be solved using the quadratic formula or by factoring.
    
    Let me solve this step by step using factoring:
    
    First, I need to find two numbers that multiply to 6 and add to 5.
    Those numbers are 3 and 2, because 3 × 2 = 6 and 3 + 2 = 5.
    
    Therefore, I can factor the equation as: (x + 3)(x + 2) = 0
    
    Using the zero product property, either x + 3 = 0 or x + 2 = 0.
    
    This gives us:
    x + 3 = 0  →  x = -3
    x + 2 = 0  →  x = -2
    
    So the solutions are x = -3 and x = -2.
    
    To verify: 
    For x = -3: (-3)² + 5(-3) + 6 = 9 - 15 + 6 = 0 ✓
    For x = -2: (-2)² + 5(-2) + 6 = 4 - 10 + 6 = 0 ✓
    
    Therefore, both solutions are correct.
    """
    
    print("ORIGINAL (Verbose):")
    print(f"Length: {len(verbose_response)} characters")
    print(verbose_response)
    
    print("\n" + "→" * 50 + "\n")
    
    compact_response = formatter.parse_llm_response_to_compact(verbose_response)
    
    print("COMPACT FORMAT:")
    print(f"Length: {len(compact_response)} characters")
    print(f"Compression: {len(compact_response)/len(verbose_response)*100:.0f}%")
    print(compact_response)
    
    print("\n" + "=" * 50)

Return Value

This function does not return any value (implicitly returns None). It performs its work by printing output to the console, including the original verbose response, the compacted version, character counts, and compression percentage.

Dependencies

  • compact_formatter
  • session_manager
  • asyncio
  • pathlib

Required Imports

import asyncio
from pathlib import Path
from compact_formatter import CompactResponseFormatter
from session_manager import SessionManager

Usage Example

# Ensure the required modules are in the Python path
from compact_formatter import CompactResponseFormatter

# Run the test function
test_compact_formatter()

# Expected output:
# - Prints a header with test name
# - Shows original verbose response with character count
# - Shows compacted response with character count and compression percentage
# - Demonstrates the formatter's ability to reduce response length

Best Practices

  • This is a test/demo function meant to be run directly, not imported and called in production code
  • The function uses print statements for output, making it suitable for console-based testing but not for automated test suites that require assertions
  • The verbose_response is hardcoded as a multi-line string, making it easy to modify for testing different types of content
  • Consider converting this to a proper unit test using pytest or unittest framework for integration into CI/CD pipelines
  • The function demonstrates good testing practice by showing both input and output with metrics (character count, compression ratio)
  • Note that asyncio and Path are imported but not used in this function, suggesting they may be used by other functions in the same file

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class CompactResponseFormatter 74.6% similar

    A formatter class that converts verbose LLM responses into compact, symbol-rich text optimized for e-ink displays by using Unicode symbols, mathematical notation, and abbreviated formatting.

    From: /tf/active/vicechatdev/e-ink-llm/compact_formatter.py
  • function demo_improvement_comparison 63.6% similar

    A demonstration function that displays a before-and-after comparison of response formatting improvements, showing the evolution from verbose to compact, symbol-rich formatting optimized for e-ink displays.

    From: /tf/active/vicechatdev/e-ink-llm/test_improvements.py
  • function demo_hybrid_response 53.7% similar

    Demonstrates end-to-end hybrid response processing by converting an LLM response containing text and graphics placeholders into a formatted PDF document.

    From: /tf/active/vicechatdev/e-ink-llm/demo_hybrid_mode.py
  • function main_v75 52.9% similar

    Asynchronous test runner function that executes a suite of tests for the E-Ink LLM Assistant application, including tests for compact formatting, session management, and improvement comparisons.

    From: /tf/active/vicechatdev/e-ink-llm/test_improvements.py
  • function test_markdown_processing 51.7% similar

    A test function that validates markdown processing capabilities by testing content parsing, element extraction, and HTML conversion functionality.

    From: /tf/active/vicechatdev/vice_ai/test_markdown.py
← Back to Browse