metal-kompanion/tests/mcp/test_memory_exchange.cpp

76 lines
2.5 KiB
C++

#include <iostream>
#include <optional>
#include <string>
#include "mcp/KomMcpServer.hpp"
#include "mcp/RegisterTools.hpp"
namespace {
bool expect_contains(const std::string& haystack, const std::string& needle, const std::string& context) {
if (haystack.find(needle) == std::string::npos) {
std::cerr << "[memory-exchange] Expected \"" << needle << "\" in " << context << " but got:\n"
<< haystack << std::endl;
return false;
}
return true;
}
std::optional<std::string> extract_field(const std::string& json, const std::string& key) {
const std::string pattern = "\"" + key + "\":\"";
auto pos = json.find(pattern);
if (pos == std::string::npos) {
return std::nullopt;
}
pos += pattern.size();
auto end = json.find('"', pos);
if (end == std::string::npos) {
return std::nullopt;
}
return json.substr(pos, end - pos);
}
} // namespace
int main() {
KomMcpServer server;
register_default_tools(server);
const std::string saveReq =
R"({"namespace":"tests","key":"exchange-demo","content":"memory-exchange note","tags":["unit","memory"],"ttl_seconds":120})";
std::string saveResp = server.dispatch("kom.memory.v1.save_context", saveReq);
if (!expect_contains(saveResp, "\"id\"", "save_context response") ||
!expect_contains(saveResp, "\"created_at\"", "save_context response")) {
return 1;
}
auto savedId = extract_field(saveResp, "id");
if (!savedId || savedId->empty()) {
std::cerr << "[memory-exchange] Failed to parse id from save_context response\n";
return 1;
}
const std::string recallReq =
R"({"namespace":"tests","key":"exchange-demo","limit":5})";
std::string recallResp = server.dispatch("kom.memory.v1.recall_context", recallReq);
if (!expect_contains(recallResp, "\"items\"", "recall_context response") ||
!expect_contains(recallResp, "\"memory-exchange note\"", "recall_context returns content")) {
return 1;
}
if (!savedId->empty() && !expect_contains(recallResp, *savedId, "recall_context preserves ids")) {
return 1;
}
const std::string searchReq =
R"({"namespace":"tests","query":{"text":"memory-exchange","k":3}})";
std::string searchResp = server.dispatch("kom.memory.v1.search_memory", searchReq);
if (!expect_contains(searchResp, "\"matches\"", "search_memory response") ||
!expect_contains(searchResp, "\"memory-exchange note\"", "search_memory finds saved text")) {
return 1;
}
return 0;
}