|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rack/test" |
| 4 | + |
| 5 | +describe Hooks::App::HealthEndpoint do |
| 6 | + include Rack::Test::Methods |
| 7 | + |
| 8 | + def app |
| 9 | + described_class |
| 10 | + end |
| 11 | + |
| 12 | + before do |
| 13 | + # Mock API start_time for consistent uptime calculation |
| 14 | + allow(Hooks::App::API).to receive(:start_time).and_return(Time.parse("2024-12-31T23:59:00Z")) |
| 15 | + end |
| 16 | + |
| 17 | + describe "GET /" do |
| 18 | + it "returns health status as JSON" do |
| 19 | + get "/" |
| 20 | + |
| 21 | + expect(last_response.status).to eq(200) |
| 22 | + expect(last_response.headers["Content-Type"]).to include("application/json") |
| 23 | + end |
| 24 | + |
| 25 | + it "includes health status in response" do |
| 26 | + get "/" |
| 27 | + |
| 28 | + response_data = JSON.parse(last_response.body) |
| 29 | + expect(response_data["status"]).to eq("healthy") |
| 30 | + end |
| 31 | + |
| 32 | + it "includes timestamp in ISO8601 format" do |
| 33 | + get "/" |
| 34 | + |
| 35 | + response_data = JSON.parse(last_response.body) |
| 36 | + expect(response_data["timestamp"]).to eq(TIME_MOCK) |
| 37 | + end |
| 38 | + |
| 39 | + it "includes version information" do |
| 40 | + get "/" |
| 41 | + |
| 42 | + response_data = JSON.parse(last_response.body) |
| 43 | + expect(response_data["version"]).to eq(Hooks::VERSION) |
| 44 | + end |
| 45 | + |
| 46 | + it "includes uptime in seconds" do |
| 47 | + get "/" |
| 48 | + |
| 49 | + response_data = JSON.parse(last_response.body) |
| 50 | + expect(response_data["uptime_seconds"]).to be_a(Integer) |
| 51 | + expect(response_data["uptime_seconds"]).to eq(60) # 1 minute difference |
| 52 | + end |
| 53 | + |
| 54 | + it "returns valid JSON structure" do |
| 55 | + get "/" |
| 56 | + |
| 57 | + expect { JSON.parse(last_response.body) }.not_to raise_error |
| 58 | + |
| 59 | + response_data = JSON.parse(last_response.body) |
| 60 | + expect(response_data).to have_key("status") |
| 61 | + expect(response_data).to have_key("timestamp") |
| 62 | + expect(response_data).to have_key("version") |
| 63 | + expect(response_data).to have_key("uptime_seconds") |
| 64 | + end |
| 65 | + |
| 66 | + it "calculates uptime correctly" do |
| 67 | + # Test with different start time |
| 68 | + different_start = Time.parse("2024-12-31T23:58:30Z") |
| 69 | + allow(Hooks::App::API).to receive(:start_time).and_return(different_start) |
| 70 | + |
| 71 | + get "/" |
| 72 | + |
| 73 | + response_data = JSON.parse(last_response.body) |
| 74 | + expect(response_data["uptime_seconds"]).to eq(90) # 1.5 minutes difference |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments