Class: RailsAiContext::Tools::GetTestInfo

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/get_test_info.rb

Constant Summary

Constants inherited from BaseTool

BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, cache_key, cached_context, config, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, inherited, not_found_response, paginate, rails_app, registered_tools, reset_all_caches!, reset_cache!, session_queries, session_record, session_reset!, set_call_params, text_response

Class Method Details

.call(model: nil, controller: nil, detail: "standard", server_context: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rails_ai_context/tools/get_test_info.rb', line 31

def self.call(model: nil, controller: nil, detail: "standard", server_context: nil)
  data = cached_context[:tests]
  return text_response("Test introspection not available. Add :tests to introspectors.") unless data
  return text_response("Test introspection failed: #{data[:error]}") if data[:error]

  # Specific model tests
  if model
    return text_response(find_test_file(model, :model, detail))
  end

  # Specific controller tests
  if controller
    return text_response(find_test_file(controller, :controller, detail))
  end

  case detail
  when "summary"
    lines = [ "# Test Infrastructure", "" ]
    lines << "- **Framework:** #{data[:framework]}"
    lines << "- **Factories:** #{data[:factories][:count]} files" if data[:factories]
    lines << "- **Fixtures:** #{data[:fixtures][:count]} files" if data[:fixtures]
    if data[:test_files]&.any?
      total = data[:test_files].values.sum { |v| v[:count] }
      lines << "- **Test files:** #{total} across #{data[:test_files].size} categories"
    end
    lines << "- **CI:** #{data[:ci_config].join(', ')}" if data[:ci_config]&.any?
    text_response(lines.join("\n"))

  when "standard"
    lines = [ "# Test Infrastructure", "" ]
    lines << "- **Framework:** #{data[:framework]}"
    lines << "- **Factories:** #{data[:factories][:location]} (#{data[:factories][:count]} files)" if data[:factories]
    lines << "- **Fixtures:** #{data[:fixtures][:location]} (#{data[:fixtures][:count]} files)" if data[:fixtures]
    lines << "- **System tests:** #{data[:system_tests][:location]}" if data[:system_tests]
    lines << "- **CI:** #{data[:ci_config].join(', ')}" if data[:ci_config]&.any?
    lines << "- **Coverage:** #{data[:coverage]}" if data[:coverage]

    if data[:test_count_by_category].is_a?(Hash) && data[:test_count_by_category].any?
      lines << "" << "## Test Counts by Category"
      data[:test_count_by_category].each do |cat, count|
        lines << "- #{cat}: #{count}"
      end
    end

    if data[:test_files]&.any?
      lines << "" << "## Test Files"
      data[:test_files].each do |cat, info|
        lines << "- #{cat}: #{info[:count]} files (#{info[:location]})"
      end
    end

    if data[:factory_traits]&.any?
      lines << "" << "## Factory Traits"
      data[:factory_traits].first(15).each do |trait|
        lines << "- #{trait}"
      end
    end

    if data[:test_helpers]&.any?
      lines << "" << "## Test Helpers"
      data[:test_helpers].each { |h| lines << "- `#{h}`" }
    end

    # Generate a test template based on app patterns
    template = generate_test_template(data)
    lines.concat(template) if template.any?

    text_response(lines.join("\n"))

  when "full"
    lines = [ "# Test Infrastructure (Full Detail)", "" ]
    lines << "- **Framework:** #{data[:framework]}"
    lines << "- **CI:** #{data[:ci_config].join(', ')}" if data[:ci_config]&.any?
    lines << "- **Coverage:** #{data[:coverage]}" if data[:coverage]

    if data[:test_count_by_category].is_a?(Hash) && data[:test_count_by_category].any?
      lines << "" << "## Test Counts by Category"
      data[:test_count_by_category].each do |cat, count|
        lines << "- #{cat}: #{count}"
      end
    end

    if data[:factory_traits]&.any?
      lines << "" << "## Factory Traits"
      data[:factory_traits].first(20).each do |trait|
        lines << "- #{trait}"
      end
    end

    if data[:fixture_names]&.any?
      lines << "" << "## Fixtures"
      parsed_fixtures = parse_all_fixture_contents
      if parsed_fixtures.any?
        parsed_fixtures.each do |file, entries|
          lines << "- **#{file}:**"
          entries.each do |entry_name, attrs|
            attr_str = attrs.map { |k, v| "#{k}: #{v}" }.join(", ")
            lines << "  - `#{entry_name}`: #{attr_str}"
          end
        end

        # Fixture relationships section
        relationships = extract_fixture_relationships(parsed_fixtures)
        if relationships.any?
          lines << "" << "## Fixture Relationships"
          relationships.each do |parent, children|
            lines << "- **#{parent}** ← #{children.join(', ')}"
          end
        end
      else
        # Fallback to simple names if parsing fails
        data[:fixture_names].each do |file, names|
          lines << "- **#{file}:** #{names.join(', ')}"
        end
      end
    end

    if data[:factory_names]&.any?
      lines << "" << "## Factories"
      data[:factory_names].each do |file, names|
        detail_str = parse_factory_details(file)
        if detail_str
          lines << detail_str
        else
          lines << "- **#{file}:** #{names.join(', ')}"
        end
      end
    end

    if data[:test_helper_setup]&.any?
      lines << "" << "## Test Helper Setup"
      data[:test_helper_setup].each { |m| lines << "- `#{m}`" }
    end

    if data[:test_files]&.any?
      lines << "" << "## Test Files"
      data[:test_files].each do |cat, info|
        lines << "- #{cat}: #{info[:count]} files (#{info[:location]})"
      end
    end

    if data[:test_helpers]&.any?
      lines << "" << "## Test Helper Files"
      data[:test_helpers].each { |h| lines << "- `#{h}`" }
    end
    text_response(lines.join("\n"))

  else
    text_response("Unknown detail level: #{detail}. Use summary, standard, or full.")
  end
end

.max_test_file_sizeObject



183
184
185
# File 'lib/rails_ai_context/tools/get_test_info.rb', line 183

def self.max_test_file_size
  RailsAiContext.configuration.max_test_file_size
end