Repository navigation
Expand file tree
/
Copy pathtest_memory.cpp
More file actions
145 lines (115 loc) · 3.79 KB
/
Copy pathtest_memory.cpp
File metadata and controls
145 lines (115 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* RaggerMemory facade tests
*
* Requires ONNX model files at the configured model_dir.
*/
#include "config.h"
#include "memory.h"
#include <algorithm>
#include <cassert>
#include <filesystem>
#include <iostream>
#include <print>
#include <stdexcept>
namespace fs = std::filesystem;
static const std::string TEMP_DB1 = "/tmp/ragger_test_mem1.db";
static const std::string TEMP_DB2 = "/tmp/ragger_test_mem2.db";
static void cleanup(const std::string& path) {
fs::remove(path);
fs::remove(path + "-wal");
fs::remove(path + "-shm");
}
static void cleanup_all() {
cleanup(TEMP_DB1);
cleanup(TEMP_DB2);
}
void test_store_and_search() {
std::println(" test_store_and_search...");
cleanup_all();
ragger::RaggerMemory mem(TEMP_DB1);
mem.store("The Eiffel Tower is located in Paris, France.");
mem.store("Photosynthesis converts sunlight into energy in plants.");
auto resp = mem.search("Where is the Eiffel Tower?", 5, 0.0f);
assert(!resp.results.empty());
assert(resp.results[0].text.find("Eiffel") != std::string::npos);
mem.close();
cleanup_all();
std::println(" OK");
}
void test_store_with_tags() {
std::println(" test_store_with_tags...");
cleanup_all();
ragger::RaggerMemory mem(TEMP_DB1);
mem.store("Reference: HTTP status 200 means OK.", {{"tags", {"reference"}}});
mem.store("Memory: I had coffee this morning.", {{"tags", {"diary"}}});
// Lean v2 has no collection filter; verify tags round-trip. With only two
// near-equidistant candidates, min-max score normalization can be
// volatile enough to reorder results on a tiny raw-cosine gap (a known
// property of the blend, not specific to this doc pair) -- so find the
// HTTP row by content rather than assuming it lands at rank 0, and check
// its tags round-tripped correctly.
auto resp = mem.search("HTTP status", 5, 0.0f);
assert(!resp.results.empty());
auto it = std::find_if(resp.results.begin(), resp.results.end(),
[](const auto& r) { return r.text.find("HTTP") != std::string::npos; });
assert(it != resp.results.end());
assert((*it).metadata["tags"] == "reference");
mem.close();
cleanup_all();
std::println(" OK");
}
void test_count() {
std::println(" test_count...");
cleanup_all();
ragger::RaggerMemory mem(TEMP_DB1);
assert(mem.count() == 0);
mem.store("First item.");
mem.store("Second item.");
mem.store("Third item.");
assert(mem.count() == 3);
mem.close();
cleanup_all();
std::println(" OK");
}
void test_delete() {
std::println(" test_delete...");
cleanup_all();
ragger::RaggerMemory mem(TEMP_DB1);
std::string id1 = mem.store("To be deleted.");
mem.store("To be kept.");
assert(mem.count() == 2);
bool deleted = mem.delete_memory(std::stoi(id1));
assert(deleted);
assert(mem.count() == 1);
mem.close();
cleanup_all();
std::println(" OK");
}
void test_empty_db_path_throws() {
std::println(" test_empty_db_path_throws...");
bool threw = false;
try {
ragger::RaggerMemory mem("");
} catch (const std::invalid_argument&) {
threw = true;
}
assert(threw);
std::println(" OK");
}
int main() {
ragger::init_config();
auto model_dir = ragger::config().resolved_model_dir();
if (!fs::exists(model_dir + "/model.onnx") && !fs::exists(model_dir + "/onnx/model.onnx")) {
std::cerr << "Skipping memory tests: model not found at " << model_dir << "\n";
std::println("test_memory: SKIPPED (no model)");
return 0;
}
std::println("Running memory facade tests:");
test_store_and_search();
test_store_with_tags();
test_count();
test_delete();
test_empty_db_path_throws();
std::println("test_memory: all passed");
return 0;
}