Module: Minitest::Smartdiff::ClassMethods

Defined in:
lib/minitest/smartdiff.rb

Instance Method Summary collapse

Instance Method Details

#model(model = nil) ⇒ Object



105
106
107
108
# File 'lib/minitest/smartdiff.rb', line 105

def model(model = nil)
  @model ||= model
  @model ||= "gpt-3.5-turbo"
end

#openai(config = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/minitest/smartdiff.rb', line 37

def openai(config = {})
  @openai ||= OpenAI::Client.new(
    {
      organization_id: "",
      log_errors: true,
      access_token: ENV['OPENAI_KEY'],
      uri_base: ENV['OPENAI_URI_BASE'],
    }.merge(config)
  )
end

#prompt(string = nil) ⇒ Object



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
# File 'lib/minitest/smartdiff.rb', line 48

def prompt(string = nil)
  @prompt ||= string
  @prompt ||= ERB.new <<~DEFAULT
    You are Minitest::Smartdiff - the smartest differ that ever existed.
    Your task is to find subtle but important differences in two pieces of data,
    the expected, and the actual.  When you find the difference describe the
    difference as precisely as possible, and provide samples from the provided
    input.

    Be as succinct as possible, only point out the differences in the two strings.

    Example Output
    <% if mode == :json || mode == :object %>
      Make sure to provide JSONPath to the location of the differences.

      Expected:
        {
          usage: {
            prompt_tokens: 9
          }
        }

      Actual:
        {
          usage: {
            prompt_tokens: 11
          }
        }

      1. In the usage object, the number of tokens used is different:
        - JSONPath: $.usage.prompt_tokens
        - Expected: 9
        - Actual: 11
    <% else %>
      Make sure to provide a concise description of the surrounding context for the differences.

      Expected:
        The quick brown dog jumped over the lazy red fox.

      Actual:
        The quick red dog jumped over the lazy brown fox.

      1. The word "brown" in the expected text describes the dog, but in the actual text the word "red" is used.
      2. The word "red" in the expected text describes the fox,  is in the actual text the word "brown" is used.
    <% end %>

    This is very important to my career, I've got children to feed and a
    mortgage to pay - so be very careful to only show differences.

    Expected:
      <%= expected %>

    Actual
      <%= actual %>
  DEFAULT
end

#smart_diffable?(exp, act) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/minitest/smartdiff.rb', line 117

def smart_diffable?(exp, act)
  return false if exp.class != act.class
  return false unless [Hash, String, Array].include?(exp.class)

  if exp.is_a?(String)
    if valid_json?(exp) && valid_json?(act)
      :json
    else
      :text
    end
  else
    :object
  end
end

#valid_json?(str) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/minitest/smartdiff.rb', line 110

def valid_json?(str)
  rep = JSON.parse(str)
  rep.is_a?(Hash) || rep.is_a?(Array)
rescue => e
  false
end