tests — testing

Module: tests-testing Cohesion: 0.80 Members: 0

tests — testing

The src/testing module provides a suite of intelligent, automated capabilities designed to enhance code quality, streamline testing workflows, and facilitate Test-Driven Development (TDD) for AI agents and developers. It integrates with various programming languages and testing/linting frameworks, offering a programmatic interface to manage and interact with these critical development processes.

This documentation focuses on the core functionalities exposed by the src/testing modules, which are validated by the provided test files.


1. Auto-Lint Module

The Auto-Lint module (src/testing/auto-lint.ts) is responsible for managing automatic code linting. It detects available linters in a project, applies configurations, executes linting processes, and provides structured results.

Purpose

To provide a unified interface for integrating and managing various code linters (e.g., ESLint, Prettier, Ruff) within a project, allowing for automated code quality checks and potential auto-fixes.

Key Components

The central class for orchestrating linting operations. It extends EventEmitter to provide a reactive interface.

Initializes the manager for a given projectRoot, optionally overriding default configurations.

An object containing predefined configurations for common linters (e.g., eslint, prettier, ruff, clippy, golangci, rubocop). Each entry specifies the linter's name, supported file extensions, and common configFiles for detection.

The default configuration object for AutoLintManager, defining sensible defaults for properties like enabled, autoFix, failOnError, maxErrors, timeout, and the list of linters to use.

Defines the structure for a single linting issue, including file, line, column, message, rule, severity (error, warning, info), and fixable status.

Defines the aggregated result of a linting run by a specific linter, including success, lists of errors and warnings, fixedCount, duration, and the linter name.

Core Functionality

  1. Configuration Management: Allows dynamic adjustment of linting behavior through getConfig() and updateConfig().
  2. Linter Detection: Automatically identifies which linters are configured and available in the project based on BUILTIN_LINTERS and project files.
  3. Result Processing: Collects and structures linting output into LintResult objects.
  4. LLM Integration: Provides a specialized formatResultsForLLM() method to present linting outcomes clearly and concisely to AI agents.
  5. Eventing: Emits lint:start (data: { file: string; linter: string }) and lint:complete (data: { file: string; result: LintResult }) events, enabling other modules to react to linting lifecycle events.

Usage and Integration

The AutoLintManager is typically initialized once per project using initializeAutoLint or retrieved via getAutoLintManager. It serves as a programmatic interface for AI agents or other system components to trigger linting, retrieve results, and configure linting settings. The event system allows for real-time feedback and integration into broader development workflows.


2. Auto-Test Module

The Auto-Test module (src/testing/auto-test.ts) manages the execution and reporting of automated tests using various testing frameworks.

Purpose

To provide a consistent way to detect, run, and interpret results from different testing frameworks (e.g., Jest, Vitest, pytest) within a project, supporting automated testing and coverage collection.

Key Components

The central class for managing testing operations, also extending EventEmitter.

Initializes the manager for a given projectRoot, with optional custom configurations.

An object containing predefined configurations for common testing frameworks (e.g., jest, vitest, pytest, cargo, go, rspec). Each entry specifies the framework's name, command (for execution), and common configFiles for detection.

The default configuration object for AutoTestManager, defining sensible defaults for properties like enabled, runOnSave, runRelatedTests, collectCoverage, timeout, maxTestFiles, and watchMode.

Defines the structure for a single test case, including name, suite, status (passed, failed, skipped), duration, and optional file, error, and stack trace.

Defines the aggregated result of a test run, including success, passed, failed, skipped, total test counts, duration, a list of individual tests, the framework used, and optional coverage data.

Core Functionality

  1. Configuration Management: Allows dynamic adjustment of testing behavior.
  2. Framework Detection: Automatically identifies the testing framework used in the project.
  3. Result Management: Stores and retrieves the results of the last test run.
  4. LLM Integration: Provides formatResultsForLLM() to summarize test outcomes for AI agents.
  5. Eventing: Emits test:start (data: { type: string }) and test:complete (data: TestResult) events, allowing for real-time monitoring and integration.

Usage and Integration

Similar to AutoLintManager, the AutoTestManager is typically initialized once per project. It provides the means for AI agents or other system components to trigger test runs, retrieve detailed results, and configure testing parameters. The formatResultsForLLM method is particularly important for AI agents to understand test failures and guide remediation.


3. TDD Mode Module

The TDD Mode module (src/testing/tdd-mode.ts) implements a stateful manager to guide an AI agent through a structured Test-Driven Development (TDD) workflow.

Purpose

To enforce and manage the TDD cycle (Red-Green-Refactor) for AI agents, ensuring that tests are written before implementation, and providing context-rich prompts at each stage.

Key Components

The central class for managing the TDD workflow, also extending EventEmitter.

Initializes the manager for a given projectRoot, with optional custom configurations.

An object providing language-specific templates for generating tests, edge cases, and mocks (e.g., typescript, python, go, rust). Each template includes the target framework and the actual template strings.

The default configuration object for TDDModeManager, defining properties like maxIterations, autoApproveTests, generateEdgeCases, generateMocks, testCoverage, and language.

A union type representing the possible states of the TDD workflow (e.g., idle, requirements, reviewing-tests, implementing, testing, refactoring, completed, failed).

Defines the structure for the outcome of a TDD cycle.

Core Functionality

  1. State Machine: Manages the TDD workflow through a defined sequence of states, ensuring adherence to the TDD process.
  2. Configuration Management: Allows dynamic adjustment of TDD parameters.
  3. Prompt Generation: generateTestPrompt() is a critical function for instructing an LLM to generate tests based on the current TDD context.
  4. Workflow Control: Methods like startCycle(), cancelCycle(), reset(), recordGeneratedTests(), and approveTests() drive the state transitions and manage the TDD session.
  5. Eventing: Emits cycle:started (data: { requirements: string }), cycle:cancelled, and tests:generated (data: { tests: string[]; files: string[] }) events.

TDD Workflow (State Machine)

The TDDModeManager implements a state machine to guide the TDD process. The core transitions, as validated by tests, are:

graph TD
    A[Idle] --> B{startCycle};
    B --> C[Requirements];
    C --> D{recordGeneratedTests};
    D --> E[Reviewing Tests];
    E --> F{approveTests};
    F --> G[Implementing];

    B -- cancelCycle --> A;
    C -- cancelCycle --> A;
    D -- cancelCycle --> A;
    E -- cancelCycle --> A;
    G -- reset --> A;

Usage and Integration

The TDDModeManager is the primary interface for an AI agent to engage in a structured TDD process. It ensures that the AI follows the TDD principles, generating tests first, then implementing the code, and providing clear prompts and state tracking throughout. The generateTestPrompt method is crucial for instructing the AI at the test generation phase.


4. Common Patterns & Integration Points

The src/testing modules share several architectural patterns and integration points: