CLI Design - amosproj/amos2025ss04-ai-driven-testing GitHub Wiki

Command-Line Interface (CLI) Design

The project includes a Command-Line Interface (CLI) to provide users and developers with a direct way to interact with the backend API's functionalities from the terminal. This document outlines the design of backend/cli.py.

1. Framework

  • The CLI is built using Typer, a modern Python library for building CLIs, known for its ease of use, type hints integration, and automatic help generation.

2. Core Functionality

The CLI acts as a client to the backend FastAPI service. It does not contain the core logic for test generation or LLM interaction itself. Instead, it:

  1. Parses command-line arguments and options.
  2. Constructs appropriate JSON payloads for the backend API.
  3. Makes HTTP requests to the backend API endpoints.
  4. Receives and displays responses from the API, including handling streaming data.

3. Key Components and Commands

  • Main Typer Application (app = typer.Typer()): Initializes the CLI application.
  • Asynchronous HTTP Client (httpx.AsyncClient): Used to make asynchronous HTTP requests to the backend API, aligning with the API's asynchronous nature.

Commands:

  1. models Command:

    • Purpose: To list the LLM models available through the backend API.
    • Design:
      • Makes a GET request to the API's /models endpoint.
      • Parses the JSON response (a list of model strings).
      • Displays the list to the console, typically with some formatting (e.g., using the rich library for better output).
  2. generate Command:

    • Purpose: To request test case generation from the backend API.
    • Design:
      • Accepts various command-line options (e.g., --code-path, --model-name, --dependencies-path, --testing-framework, --output-path, LLM override options).
      • Reads the content of the specified code file(s).
      • Constructs a JSON payload matching the RequestModel expected by the API's /generate-test endpoint.
      • Makes an asynchronous POST request to the API.
      • Streaming Response Handling: Crucially, it iterates over the API's streaming response (response.aiter_text()) and prints each chunk of text to the console as it arrives. This provides real-time feedback to the user.
      • If an --output-path is specified, it accumulates the full streamed response and writes it to the designated file.

4. Error Handling

  • The CLI includes try...except blocks to catch potential errors during API interaction, such as:
    • httpx.HTTPStatusError: For HTTP error responses from the API (e.g., 4xx, 5xx).
    • httpx.RequestError: For network issues or problems connecting to the API.
    • File I/O errors when reading code files or writing output files.
  • Error messages are printed to the console to inform the user.

5. User Experience

  • Help Messages: Typer automatically generates help messages for the CLI and its commands (e.g., python cli.py --help, python cli.py generate --help).
  • Formatted Output: The rich library is used for more visually appealing and readable output in the terminal (e.g., for lists, success/error messages).
  • Progress/Streaming: For the generate command, streaming output directly to the console provides immediate feedback and shows progress.

6. File Structure (cli.py)

  • Imports necessary libraries (typer, httpx, asyncio, pathlib, rich, etc.).
  • Defines the Typer app.
  • Defines asynchronous functions for each command (e.g., async def generate_test_command(...)).
  • Uses Typer decorators (@app.command()) to register these functions as CLI commands.
  • Includes a if __name__ == "__main__": block to run the Typer application.