80 lines
4.1 KiB
C++
80 lines
4.1 KiB
C++
#include <iostream>
|
|
#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 << "Expected response to contain '" << needle << "' but got:\n"
|
|
<< haystack << "\nContext: " << context << "\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
KomMcpServer server;
|
|
register_default_tools(server);
|
|
|
|
const std::string upsertReq = R"({"namespace":"tests","items":[{"text":"hello world","tags":["greeting"],"embedding":[0.1,0.2,0.3]},{"text":"hola mundo","embedding":[0.05,0.1,0.15]}]})";
|
|
std::string upsertResp = server.dispatch("kom.memory.v1.upsert_memory", upsertReq);
|
|
if (!expect_contains(upsertResp, "\"upserted\":2", "upsert_memory count")) return 1;
|
|
if (!expect_contains(upsertResp, "\"status\":\"ok\"", "upsert_memory status")) return 1;
|
|
|
|
const std::string saveReq = R"({"namespace":"tests","key":"greeting","content":{"text":"remember this","confidence":0.98},"tags":["context","demo"]})";
|
|
std::string saveResp = server.dispatch("kom.memory.v1.save_context", saveReq);
|
|
if (!expect_contains(saveResp, "\"id\"", "save_context id")) return 1;
|
|
if (!expect_contains(saveResp, "\"created_at\"", "save_context timestamp")) return 1;
|
|
|
|
const std::string saveReqNoNamespace = R"({"key":"nogreeting","content":{"text":"remember this without namespace"},"tags":["context","demo"]})";
|
|
std::string saveRespNoNamespace = server.dispatch("kom.memory.v1.save_context", saveReqNoNamespace);
|
|
if (!expect_contains(saveRespNoNamespace, "\"error\":{\"code\":\"bad_request\",\"message\":\"namespace is required\"}", "save_context without namespace should fail")) return 1;
|
|
|
|
const std::string recallReq = R"({"namespace":"tests","key":"greeting","limit":2})";
|
|
std::string recallResp = server.dispatch("kom.memory.v1.recall_context", recallReq);
|
|
if (!expect_contains(recallResp, "\"items\"", "recall_context items")) return 1;
|
|
if (!expect_contains(recallResp, "\"confidence\":0.98", "recall_context returns payload")) return 1;
|
|
|
|
std::string firstId;
|
|
const std::string idsAnchor = "\"ids\":[\"";
|
|
auto idsPos = upsertResp.find(idsAnchor);
|
|
if (idsPos != std::string::npos) {
|
|
idsPos += idsAnchor.size();
|
|
auto endPos = upsertResp.find("\"", idsPos);
|
|
if (endPos != std::string::npos) {
|
|
firstId = upsertResp.substr(idsPos, endPos - idsPos);
|
|
}
|
|
}
|
|
if (firstId.empty()) {
|
|
std::cerr << "Failed to extract first item id from upsert response: " << upsertResp << "\n";
|
|
return 1;
|
|
}
|
|
|
|
const std::string searchReq = R"({"namespace":"tests","query":{"text":"hello","k":3}})";
|
|
std::string searchResp = server.dispatch("kom.memory.v1.search_memory", searchReq);
|
|
if (!expect_contains(searchResp, "\"matches\"", "search_memory matches key")) return 1;
|
|
if (!expect_contains(searchResp, "\"text\":\"hello world\"", "search_memory returns stored text")) return 1;
|
|
|
|
const std::string vectorReq = R"({"namespace":"tests","query":{"embedding":[0.1,0.2,0.3],"k":1}})";
|
|
std::string vectorResp = server.dispatch("kom.memory.v1.search_memory", vectorReq);
|
|
if (!expect_contains(vectorResp, "\"id\":\""+firstId+"\"", "vector search returns stored id")) return 1;
|
|
|
|
const std::string snapshotResp = server.dispatch("kom.meta.v1.project_snapshot", "{}");
|
|
if (!expect_contains(snapshotResp, "\"sections\"", "snapshot sections key")) return 1;
|
|
|
|
const std::string exportReq = R"({"namespace":"tests","destination":"/tmp/example.enc"})";
|
|
std::string exportResp = server.dispatch("kom.local.v1.backup.export_encrypted", exportReq);
|
|
if (!expect_contains(exportResp, "\"status\":\"queued\"", "export status")) return 1;
|
|
if (!expect_contains(exportResp, "\"artifact\"", "export artifact path")) return 1;
|
|
|
|
const std::string importReq = R"({"namespace":"tests","source":"/tmp/example.enc"})";
|
|
std::string importResp = server.dispatch("kom.local.v1.backup.import_encrypted", importReq);
|
|
if (!expect_contains(importResp, "\"status\":\"ok\"", "import status")) return 1;
|
|
|
|
return 0;
|
|
}
|