{
This harness script is spawned as a subprocess by spec_helper.rb and runs the
SUT app, registering handlers per fixture. It loads all fixtures, creates
handlers that return expected responses, and serves on a configured port.
Context variables (passed from Ruby codegen):
- imports: list of module names to import (e.g., ["sample_app"])
- app_class: full constant path to app class (e.g., "SampleApp::App")
- method_enum_module: module path for Method enum (e.g., "SampleApp::Method")
- register_route_method: app method to register routes (e.g., "route")
- run_method: serve entrypoint (e.g., "run")
- host: binding host (e.g., "127.0.0.1")
- port: binding port (e.g., 8002)
- fixtures_json: raw JSON string with all fixtures (auto-serialized)
require "json"
require "socket"
require "{{ imports[0] }}"
module AppHarness
FIXTURES_JSON = {{ fixtures_json }}
FIXTURES = JSON.parse(FIXTURES_JSON, symbolize_names: true)
APP = {{ app_class }}.new
FIXTURES.each do |fixture_id, fixture|
http = fixture[:http]
next unless http
handler_config = http[:handler] || {}
route = handler_config[:route] || "/"
method_str = (handler_config[:method] || "GET").upcase
body_schema = handler_config[:body_schema]
expected = http[:expected_response] || {}
expected_status = expected[:status_code] || 200
expected_body = expected[:body]
expected_headers = expected[:headers] || {}
handler_fn = lambda do |*_args, **_kwargs|
{
status_code: expected_status,
"{{ response_body_field }}": expected_body,
headers: expected_headers
}
end
full_route = "/fixtures/#{fixture_id}#{route}"
method_str_upper = method_str.upcase
method_val = method_str_upper.capitalize
next if method_val.empty?
builder = ::{{ route_builder_class }}.new(method_val, full_route)
if body_schema
builder = builder.{{ route_builder_schema_setter }}(JSON.dump(body_schema))
end
middleware_config = handler_config[:middleware] || {}
middleware_dispatch = {
"cors" => ->(cfg) { builder.cors(cfg) }
}
middleware_config.each do |mw_name, mw_cfg|
next unless mw_cfg
applicator = middleware_dispatch[mw_name.to_s]
next unless applicator
builder = applicator.call(mw_cfg)
end
APP.{{ register_route_method }}(builder, &handler_fn)
end
port = Integer(ENV.fetch("HARNESS_PORT", "{{ port }}"))
attempts = 0
begin
ENV["PORT"] = port.to_s
ENV["HARNESS_PORT"] = port.to_s
puts "HARNESS_PORT=#{port}"
STDOUT.flush
APP.{{ run_method }}
rescue Errno::EADDRINUSE
attempts += 1
raise if attempts >= 10
port = rand(40000..60000)
retry
end
end