|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "minitest/mock" |
| 5 | + |
| 6 | +class AgentAutoRunnerHealthGateTest < ActiveSupport::TestCase |
| 7 | + include ActiveSupport::Testing::TimeHelpers |
| 8 | + |
| 9 | + class FakeWebhookService |
| 10 | + cattr_accessor :wakes, default: [] |
| 11 | + |
| 12 | + def initialize(_user) |
| 13 | + end |
| 14 | + |
| 15 | + def notify_auto_pull_ready(task) |
| 16 | + self.class.wakes << task.id |
| 17 | + true |
| 18 | + end |
| 19 | + |
| 20 | + def notify_auto_pull_ready_with_pipeline(task) |
| 21 | + self.class.wakes << task.id |
| 22 | + true |
| 23 | + end |
| 24 | + |
| 25 | + def notify_runner_summary(_message) |
| 26 | + true |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + setup do |
| 31 | + Rails.cache.clear |
| 32 | + User.update_all(agent_auto_mode: false) |
| 33 | + @user = User.create!( |
| 34 | + email_address: "auto_runner_health_#{SecureRandom.hex(4)}@example.test", |
| 35 | + password: "password123", |
| 36 | + password_confirmation: "password123", |
| 37 | + agent_auto_mode: true, |
| 38 | + openclaw_gateway_url: "http://example.test", |
| 39 | + openclaw_gateway_token: "tok" |
| 40 | + ) |
| 41 | + @board = Board.create!(user: @user, name: "Health Gate Board") |
| 42 | + @task = Task.create!( |
| 43 | + user: @user, |
| 44 | + board: @board, |
| 45 | + name: "Health Gate Task", |
| 46 | + status: :up_next, |
| 47 | + assigned_to_agent: true, |
| 48 | + blocked: false, |
| 49 | + pipeline_enabled: false, |
| 50 | + model: "gemini" |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + test "gateway 503 skips wakes and records skip reason" do |
| 55 | + FakeWebhookService.wakes = [] |
| 56 | + cache = ActiveSupport::Cache::MemoryStore.new |
| 57 | + |
| 58 | + with_healthcheck_enabled do |
| 59 | + stub_gateway_ready(503) do |
| 60 | + travel_to Time.find_zone!("America/Argentina/Buenos_Aires").local(2026, 2, 8, 23, 30, 0) do |
| 61 | + stats = AgentAutoRunnerService.new(openclaw_webhook_service: FakeWebhookService, cache: cache).run! |
| 62 | + assert_equal 0, FakeWebhookService.wakes.length |
| 63 | + assert_equal 1, stats[:queue_skip_reasons][:gateway_not_ready] |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + test "gateway 200 allows wakes" do |
| 70 | + FakeWebhookService.wakes = [] |
| 71 | + cache = ActiveSupport::Cache::MemoryStore.new |
| 72 | + |
| 73 | + with_healthcheck_enabled do |
| 74 | + stub_gateway_ready(200) do |
| 75 | + travel_to Time.find_zone!("America/Argentina/Buenos_Aires").local(2026, 2, 8, 23, 30, 0) do |
| 76 | + stats = AgentAutoRunnerService.new(openclaw_webhook_service: FakeWebhookService, cache: cache).run! |
| 77 | + assert_includes FakeWebhookService.wakes, @task.id |
| 78 | + assert stats[:tasks_woken] >= 1 |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def with_healthcheck_enabled |
| 87 | + original = ENV["OPENCLAW_GATEWAY_HEALTHCHECK"] |
| 88 | + ENV["OPENCLAW_GATEWAY_HEALTHCHECK"] = "true" |
| 89 | + yield |
| 90 | + ensure |
| 91 | + ENV["OPENCLAW_GATEWAY_HEALTHCHECK"] = original |
| 92 | + end |
| 93 | + |
| 94 | + def stub_gateway_ready(code) |
| 95 | + response = Minitest::Mock.new |
| 96 | + response.expect(:code, code.to_s) |
| 97 | + |
| 98 | + http = Minitest::Mock.new |
| 99 | + http.expect(:use_ssl=, nil, [false]) |
| 100 | + http.expect(:open_timeout=, nil, [3]) |
| 101 | + http.expect(:read_timeout=, nil, [3]) |
| 102 | + http.expect(:request, response) { true } |
| 103 | + |
| 104 | + Net::HTTP.stub(:new, ->(_host, _port) { http }) do |
| 105 | + yield |
| 106 | + end |
| 107 | + ensure |
| 108 | + response.verify |
| 109 | + http.verify |
| 110 | + end |
| 111 | +end |
0 commit comments