Class: SpecGuardian::AiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_guardian/ai_client.rb

Class Method Summary collapse

Class Method Details

.create_prompt(source_code, framework, file_type) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spec_guardian/ai_client.rb', line 54

def self.create_prompt(source_code, framework, file_type)
  framework_name = framework == :rspec ? 'RSpec' : 'Minitest'
  style = SpecGuardian.configuration.test_style

  <<~PROMPT
    Generate a comprehensive Rails #{framework_name} test for the following #{file_type} code.
    The test should follow best practices for Rails testing with #{framework_name}.

    Test style preference: #{style}

    Here's the source code to test:

    ```ruby
    #{source_code}
    ```

    Please generate only the test code without any explanations. The output should be valid Ruby that can be saved directly to a test file. Remove comments. Do not include code formatting markdown like '```ruby'. Do not test rails internals like associations, scopes or table columns.
  PROMPT
end

.detect_file_type(file_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spec_guardian/ai_client.rb', line 36

def self.detect_file_type(file_path)
  if file_path.include?('/models/')
    :model
  elsif file_path.include?('/controllers/')
    :controller
  elsif file_path.include?('/views/')
    :view
  elsif file_path.include?('/helpers/')
    :helper
  elsif file_path.include?('/mailers/')
    :mailer
  elsif file_path.include?('/jobs/')
    :job
  else
    :other
  end
end

.generate_test(source_code, framework, file_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spec_guardian/ai_client.rb', line 5

def self.generate_test(source_code, framework, file_path)
  client = Anthropic::Client.new(
    access_token: SpecGuardian.configuration.api_key
  )

  # File type detection
  file_type = detect_file_type(file_path)

  # Create the prompt
  prompt = create_prompt(source_code, framework, file_type)
  model = SpecGuardian.configuration.ai_model
  max_tokens = SpecGuardian.configuration.max_tokens || 20_000

  # Make API call
  response = client.messages(
    parameters: {
      model: model,
      max_tokens: max_tokens,
      messages: [
        {
          role: 'user',
          content: prompt
        }
      ]
    }
  )

  # Extract and return the test code
  response['content'][0]['text']
end