AutomatedTDDRspecAgent

InfoGenerateCreated ByPackages

This code defines a module `Sublayer::Agents` containing a class `RspecAgent`, which is a specialized agent derived from a base agent class. The agent automates the process of monitoring, testing, and modifying an implementation file based on related test files using RSpec, a testing tool for Ruby.

1. **Initialization**: The `RspecAgent` is initialized with paths to an implementation file and a test file. It sets an initial state indicating whether tests are passing or not (`@tests_passing = false`).

2. **Trigger**: The agent is triggered to act when either the implementation file or the test file changes.

3. **Check Status**: The `check_status` method runs the tests using the `rspec` command. It captures the output (`stdout`), error (`stderr`), and status of the test run. It prints the standard output and updates the `@tests_passing` state based on whether the tests passed or failed.

4. **Goal Condition**: The agent has a goal condition wherein it checks if all tests are passing (`@tests_passing == true`).

5. **Step Execution**: If the tests are not passing, the `step` method modifies the implementation file to try to make the tests pass. It reads the contents of the implementation and test files, generates a modified version of the implementation file, and writes this modified version back to the implementation file.

**Noteworthy Details**:
- The code automates a test-driven development (TDD) workflow.
- The `Sublayer::Actions::RunTestCommandAction` and `Sublayer::Generators::ModifiedImplementationToPassTestsGenerator` classes are used to run tests and generate modified implementations, respectively.
- The agent prints information to the console about the steps it is taking and the status of the tests.

module Sublayer
  module Agents
    class RspecAgent < Base
      def initialize(implementation_file_path:, test_file_path:)
        @implementation_file_path = implementation_file_path
        @test_file_path = test_file_path
        @tests_passing = false
      end

      trigger_on_files_changed do
        [@implementation_file_path, @test_file_path]
      end

      check_status do
        stdout, stderr, status = Sublayer::Actions::RunTestCommandAction.new( test_command: "rspec #{@test_file_path}").call
        puts stdout

        @test_output = stdout
        @tests_passing = (status.exitstatus == 0)
        @tests_passing ? puts("Tests passing") : puts("Tests failing")
      end

      goal_condition do
        @tests_passing == true
      end

      step do
        puts "Taking a step"
        modified_implementation = Sublayer::Generators::ModifiedImplementationToPassTestsGenerator.new(
          implementation_file_contents: File.read(@implementation_file_path),
          test_file_contents: File.read(@test_file_path),
          test_output: @test_output
        ).generate

        Sublayer::Actions::WriteFileAction.new( file_contents: modified_implementation, file_path: @implementation_file_path).call
      end
    end
  end
end