#!/usr/bin/env ruby require 'json' require 'net/http' require 'socket' def request(method, path, body=nil) body_json = JSON.generate(body) if body uri = URI("http://localhost:50080#{path}") response = Net::HTTP.start(uri.host, uri.port) do |http| request = Net::HTTP.const_get(method.capitalize).new(uri) request.body = body_json request['Content-Type'] = 'application/json' http.request(request) end if response.code.to_i >= 400 raise "Request failed with status #{response.code}: #{response.body}" end JSON.parse(response.body) end def run_tests file_dir = File.dirname(__FILE__) cmd = "cargo run" # Run the command in a separate process openai_endpoint = "https://api.openai.com" openai_api_key = ENV.fetch("OPENAI_API_KEY") pid = Process.spawn({ "OPENAI_ENDPOINT" => openai_endpoint, "OPENAI_API_KEY" => openai_api_key, }, cmd) puts "Running command: #{cmd} (PID: #{pid})" # Wait for the process to start listening on port 50080 connected = false while !connected do begin TCPSocket.open('localhost', 50080) { connected = true } rescue Errno::ECONNREFUSED sleep 0.3 end end puts "Running tests..." response = request(:post, '/v1/openai/v1/chat/completions', { "model" => "gpt-4o", "messages" => [{ "role" => "user", "content" => "Hello, how are you?" }], }) raise "Expected response, got #{response.inspect}" unless response.dig("choices", 0, "message", "content") ensure # Kill the process Process.kill('TERM', pid) if pid end run_tests puts "\n\nAll tests passed!\n\n"