// http-status-envelope.el — acceptance test for the __status__ HTTP envelope.
//
// Before fix: a handler returning {"__status__":401,"error":"unauthorized"}
// went out as an HTTP 200 with the JSON body verbatim, so Cloud Run logs were
// full of 200s for what should have been 4xx/5xx.
//
// After fix: when the response body's FIRST key is __status__, the runtime
// reads the integer value as the HTTP status code and strips the marker from
// the body before sending it to the client.
//
// Verify with curl:
//   curl -i http://localhost:8081/auth   -> HTTP/1.1 401 Unauthorized
//   curl -i http://localhost:8081/health -> HTTP/1.1 200 OK
//   curl -i http://localhost:8081/oops   -> HTTP/1.1 503 Service Unavailable

fn handle(method: String, path: String, body: String) -> String {
    if path == "/auth" {
        return "{\"__status__\":401,\"error\":\"unauthorized\"}"
    }
    if path == "/oops" {
        return "{\"__status__\":503,\"error\":\"degraded\"}"
    }
    if path == "/health" {
        return "{\"ok\":true}"
    }
    return "{\"__status__\":404,\"error\":\"not found\"}"
}

fn main() -> Int {
    http_set_handler("handle")
    http_serve(8081, "handle")
    return 0
}
