Class: MockServer::LLM::LlmFailoverBuilder
- Inherits:
-
Object
- Object
- MockServer::LLM::LlmFailoverBuilder
- Defined in:
- lib/mockserver/llm.rb
Instance Method Summary collapse
- #apply_to(client) ⇒ Array<Expectation>
-
#build ⇒ Array<Hash>
A list of expectations.
- #coalesce_failures ⇒ Object private
-
#fail_with(status_code, second = nil) ⇒ self
Add one (or
count) failure attempt(s) with the given status. - #failure_count ⇒ Integer
-
#initialize ⇒ LlmFailoverBuilder
constructor
A new instance of LlmFailoverBuilder.
- #then_respond_with(completion) ⇒ self
- #with_model(model) ⇒ self
- #with_path(path) ⇒ self
- #with_provider(provider) ⇒ self
Constructor Details
#initialize ⇒ LlmFailoverBuilder
Returns a new instance of LlmFailoverBuilder.
744 745 746 747 748 749 750 |
# File 'lib/mockserver/llm.rb', line 744 def initialize @path = nil @provider = nil @model = nil @failures = [] @success_completion = nil end |
Instance Method Details
#apply_to(client) ⇒ Array<Expectation>
844 845 846 |
# File 'lib/mockserver/llm.rb', line 844 def apply_to(client) client.upsert(*build.map { |h| RawExpectation.new(h) }) end |
#build ⇒ Array<Hash>
Returns a list of expectations.
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'lib/mockserver/llm.rb', line 813 def build raise ArgumentError, 'Path must be set' if @path.nil? raise ArgumentError, 'Provider must be set' if @provider.nil? raise ArgumentError, 'At least one failure must be defined' if @failures.empty? raise ArgumentError, 'Success completion must be set via then_respond_with()' if @success_completion.nil? expectations = coalesce_failures.map do |cf| body = cf[:error_body] || LLM.default_error_body(cf[:status_code]) { 'httpRequest' => LLM.post_matcher(@path), 'times' => { 'remainingTimes' => cf[:count], 'unlimited' => false }, 'timeToLive' => { 'unlimited' => true }, 'httpResponse' => { 'statusCode' => cf[:status_code], 'headers' => [{ 'name' => 'Content-Type', 'values' => ['application/json'] }], 'body' => body } } end expectations << { 'httpRequest' => LLM.post_matcher(@path), 'times' => { 'remainingTimes' => 0, 'unlimited' => true }, 'timeToLive' => { 'unlimited' => true }, 'httpLlmResponse' => LLM.build_llm_response(@provider, @model, @success_completion, nil, nil, nil) } expectations end |
#coalesce_failures ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/mockserver/llm.rb', line 799 def coalesce_failures result = [] @failures.each do |spec| last = result.last if last && last[:status_code] == spec[:status_code] && last[:error_body] == spec[:error_body] last[:count] += 1 else result << { status_code: spec[:status_code], error_body: spec[:error_body], count: 1 } end end result end |
#fail_with(status_code, second = nil) ⇒ self
Add one (or count) failure attempt(s) with the given status.
Mirrors the three overloads: fail_with(status), fail_with(status, error_body_string) and fail_with(status, count_int).
775 776 777 778 779 780 781 782 783 784 785 |
# File 'lib/mockserver/llm.rb', line 775 def fail_with(status_code, second = nil) LLM.validate_status_code(status_code) if second.is_a?(Integer) raise ArgumentError, "count must be >= 1, got #{second}" if second < 1 second.times { @failures << { status_code: status_code, error_body: nil } } else @failures << { status_code: status_code, error_body: second.is_a?(String) ? second : nil } end self end |
#failure_count ⇒ Integer
794 795 796 |
# File 'lib/mockserver/llm.rb', line 794 def failure_count @failures.length end |
#then_respond_with(completion) ⇒ self
788 789 790 791 |
# File 'lib/mockserver/llm.rb', line 788 def then_respond_with(completion) @success_completion = completion self end |
#with_model(model) ⇒ self
765 766 767 768 |
# File 'lib/mockserver/llm.rb', line 765 def with_model(model) @model = model self end |
#with_path(path) ⇒ self
753 754 755 756 |
# File 'lib/mockserver/llm.rb', line 753 def with_path(path) @path = path self end |
#with_provider(provider) ⇒ self
759 760 761 762 |
# File 'lib/mockserver/llm.rb', line 759 def with_provider(provider) @provider = provider self end |