Back to Repositories

Testing ANSI Color Code Removal Functionality in AutoGPT

This test suite validates the color code removal functionality in AutoGPT’s logging utilities, ensuring proper handling of ANSI escape sequences in console output. The tests verify the accurate cleaning of colored text while preserving the original content structure.

Test Coverage Overview

The test suite provides comprehensive coverage for the remove_color_codes function, examining various text input scenarios.

Key test cases include:
  • Command output with color formatting
  • Multi-language text with embedded URLs
  • Empty strings and plain text
  • Nested and complex ANSI color sequences
  • Error message formatting

Implementation Analysis

The implementation uses pytest’s parametrize decorator to efficiently test multiple input-output pairs. This approach enables systematic verification of the color code removal logic across different text patterns and formatting combinations.

The testing pattern leverages direct assertion comparison between raw input and expected clean output, ensuring precise transformation of ANSI-coded text.

Technical Details

Testing tools and configuration:
  • pytest framework for test execution
  • parametrize decorator for data-driven testing
  • ANSI escape sequence handling
  • String manipulation verification
  • Unicode text support

Best Practices Demonstrated

The test suite exemplifies several testing best practices including comprehensive input variation coverage, clear test case organization, and efficient test case management.

Notable practices:
  • Parametrized test cases for maintainability
  • Edge case coverage
  • Clear input-output mapping
  • Consistent assertion patterns

significant-gravitas/autogpt

autogpt_platform/autogpt_libs/autogpt_libs/logging/test_utils.py

            
import pytest

from .utils import remove_color_codes


@pytest.mark.parametrize(
    "raw_text, clean_text",
    [
        (
            "COMMAND = \x1b[36mbrowse_website\x1b[0m  "
            "ARGUMENTS = \x1b[36m{'url': 'https://www.google.com',"
            " 'question': 'What is the capital of France?'}\x1b[0m",
            "COMMAND = browse_website  "
            "ARGUMENTS = {'url': 'https://www.google.com',"
            " 'question': 'What is the capital of France?'}",
        ),
        (
            "{'Schaue dir meine Projekte auf github () an, als auch meine Webseiten': "
            "'https://github.com/Significant-Gravitas/AutoGPT,"
            " https://discord.gg/autogpt und https://twitter.com/Auto_GPT'}",
            "{'Schaue dir meine Projekte auf github () an, als auch meine Webseiten': "
            "'https://github.com/Significant-Gravitas/AutoGPT,"
            " https://discord.gg/autogpt und https://twitter.com/Auto_GPT'}",
        ),
        ("", ""),
        ("hello", "hello"),
        ("hello\x1B[31m world", "hello world"),
        ("\x1B[36mHello,\x1B[32m World!", "Hello, World!"),
        (
            "\x1B[1m\x1B[31mError:\x1B[0m\x1B[31m file not found",
            "Error: file not found",
        ),
    ],
)
def test_remove_color_codes(raw_text, clean_text):
    assert remove_color_codes(raw_text) == clean_text