// Routes: every POST/DELETE under /agent/sessions. Without a credential they // answer 503 google_not_configured; bad input is 422 before any traffic; a // wrong verb is 405. No Google reply is scripted: the mock has no // expectations, so any request that reached it would fail loudly. #include #include #include #include "fw/Http.hpp" #include "support/GoogleRig.hpp" using namespace molo::example::test; namespace { const std::string kEngine = "projects/p1/locations/us-central1/reasoningEngines/1"; const std::string kJson = "application/json"; std::string body(const std::string& extra) { return "{\"engine\":\"" + kEngine + "\"," + extra + "}"; } struct Route { std::string path; std::string valid; // valid input (no credential: expect 503) std::string invalid; // bad input (expect 422) }; } // namespace int main() { setenv("GOOGLE_CLOUD_PROJECT", "p1", 1); const std::vector posts = { {"/agent/sessions/create", body("\"user_id\":\"u1\",\"session_id\":\"chat-1\""), body("\"user_id\":\"u1\",\"session_id\":\"Bad_Id\"")}, {"/agent/sessions/get", body("\"session_id\":\"chat-1\""), body("\"session_id\":\"\"")}, {"/agent/sessions/list", body("\"filter\":\"userId=\\\"u1\\\"\""), "{\"engine\":\"a/b\"}"}, {"/agent/sessions/events/append", body("\"session_id\":\"chat-1\",\"author\":\"user\",\"invocation_id\":\"i1\"," "\"timestamp\":\"2025-01-31T11:30:00Z\",\"text\":\"hi\""), body("\"session_id\":\"chat-1\",\"author\":\"user\",\"invocation_id\":\"i1\"," "\"timestamp\":\"yesterday\"")}, {"/agent/sessions/events/list", body("\"session_id\":\"chat-1\""), body("\"session_id\":\"a/b\"")}, {"/agent/sessions/memories/generate", body("\"scope_key\":\"user_id\",\"scope_value\":\"1\",\"text\":\"I like tea\""), body("\"scope_key\":\"user_id\",\"scope_value\":\"*\",\"text\":\"x\"")}, {"/agent/sessions/memories/retrieve", body("\"scope_key\":\"user_id\",\"scope_value\":\"1\",\"query\":\"tea\",\"top_k\":\"3\""), body("\"scope_key\":\"user_id\",\"scope_value\":\"1\",\"query\":\"tea\",\"top_k\":\"51\"")}, {"/agent/sessions/memories/create", body("\"scope_key\":\"user_id\",\"scope_value\":\"1\",\"fact\":\"likes tea\""), body("\"scope_key\":\"user_id\",\"scope_value\":\"1\",\"fact\":\"\"")}, {"/agent/sessions/memories/get", body("\"memory_id\":\"mem-1\""), body("\"memory_id\":\"1bad\"")}, {"/agent/sessions/memories/list", body("\"filter\":\"topics=\\\"x\\\"\""), "{\"engine\":\"\"}"}, {"/agent/sessions/memories/profiles", body("\"scope_key\":\"user_id\",\"scope_value\":\"1\""), body("\"scope_key\":\"\"")}, {"/agent/sessions/memories/purge", body("\"filter\":\"scope.user_id=\\\"1\\\"\""), body("\"filter\":\"\"")}, }; GoogleRig rig(false); Fixture f(rig.client); for (const Route& r : posts) { auto none = f.client->post(r.path, r.valid, kJson); check(none.status == 503 && contains(none.body, "google_not_configured"), r.path + ": no credential is a loud 503"); auto bad = f.client->post(r.path, r.invalid, kJson); check(bad.status == 422 && contains(bad.body, "validation_error"), r.path + ": bad input is 422"); check(f.client->get(r.path).status == 405, r.path + ": get is 405"); } // Bare engine id resolves through GOOGLE_CLOUD_PROJECT. auto bare = f.client->post("/agent/sessions/get", "{\"engine\":\"1\",\"session_id\":\"chat-1\"}", kJson); check(bare.status == 503, "bare engine id with a project env reaches the client"); unsetenv("GOOGLE_CLOUD_PROJECT"); auto no_project = f.client->post("/agent/sessions/get", "{\"engine\":\"1\",\"session_id\":\"chat-1\"}", kJson); check(no_project.status == 422, "bare engine id without a project is 422"); setenv("GOOGLE_CLOUD_PROJECT", "p1", 1); // Limits: id cap (63), purge guard, dry run default. check(f.client->post("/agent/sessions/get", body("\"session_id\":\"" + std::string(64, 'a') + "\""), kJson) .status == 422, "session id over 63 characters is 422"); check(f.client->post("/agent/sessions/get", body("\"session_id\":\"" + std::string(63, 'a') + "\""), kJson) .status == 503, "session id of exactly 63 characters passes validation"); auto forced = f.client->post( "/agent/sessions/memories/purge", body("\"filter\":\"scope.user_id=\\\"1\\\"\",\"force\":\"true\""), kJson); check(forced.status == 422 && contains(forced.body, "confirm"), "forced purge without confirm is 422"); auto confirmed = f.client->post( "/agent/sessions/memories/purge", body("\"filter\":\"scope.user_id=\\\"1\\\"\",\"force\":\"true\",\"confirm\":\"1\""), kJson); check(confirmed.status == 503, "forced purge with the engine id as confirm reaches the client"); // DELETE routes. using molo::example::fw::Method; auto del_session = f.client->request( Method::Delete, "/agent/sessions/session?engine=" + kEngine + "&id=chat-1"); check(del_session.status == 503 && contains(del_session.body, "google_not_configured"), "delete session: no credential is 503"); check(f.client->request(Method::Delete, "/agent/sessions/session?engine=" + kEngine + "&id=BAD").status == 422, "delete session: bad id is 422"); check(f.client->post("/agent/sessions/session").status == 405, "delete route: post is 405"); auto del_memory = f.client->request( Method::Delete, "/agent/sessions/memories/memory?engine=" + kEngine + "&id=mem-1"); check(del_memory.status == 503, "delete memory: no credential is 503"); check(f.client->request(Method::Delete, "/agent/sessions/memories/memory?engine=" + kEngine + "&id=1x").status == 422, "delete memory: bad id is 422"); check(rig.mock->send_count() == 0, "side effect: nothing reached the network"); // With a credential, validation still runs first and still stops traffic. GoogleRig keyed; Fixture g(keyed.client); check(g.client->post("/agent/sessions/get", body("\"session_id\":\"BAD\""), kJson).status == 422, "credentialed bad input is 422"); check(keyed.mock->send_count() == 0, "rejected input never reached Google"); return finish("agent/sessions calls"); }