Repository navigation
Expand file tree
/
Copy pathtest_path_normalization.cpp
More file actions
154 lines (122 loc) · 4.37 KB
/
Copy pathtest_path_normalization.cpp
File metadata and controls
154 lines (122 loc) · 4.37 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
146
147
148
149
150
151
152
153
154
/**
* Path normalization tests
*
* Tests that $HOME/ is replaced with ~/ in stored text.
* Uses store + load_all to verify normalization through the public API
* (normalize_path is a private static method in sqlite::Backend).
*/
#include "config.h"
#include "embedder.h"
#include "sqlite/backend.h"
#include <cassert>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <print>
namespace fs = std::filesystem;
static const std::string TEMP_DB = "/tmp/ragger_test_pathnorm.db";
static void cleanup() {
fs::remove(TEMP_DB);
fs::remove(TEMP_DB + "-wal");
fs::remove(TEMP_DB + "-shm");
}
void test_normalize_home_in_stored_text(ragger::Embedder& emb) {
std::println(" test_normalize_home_in_stored_text...");
cleanup();
ragger::sqlite::Backend db(emb, TEMP_DB);
const char* home = std::getenv("HOME");
assert(home != nullptr);
std::string input = std::string(home) + "/Documents/test.md";
db.store(input);
auto all = db.load_all();
assert(all.size() == 1);
assert(all[0].text == "~/Documents/test.md");
db.close();
cleanup();
std::println(" OK");
}
void test_normalize_preserves_non_home_paths(ragger::Embedder& emb) {
std::println(" test_normalize_preserves_non_home_paths...");
cleanup();
ragger::sqlite::Backend db(emb, TEMP_DB);
std::string input = "/usr/local/bin/python";
db.store(input);
auto all = db.load_all();
assert(all.size() == 1);
assert(all[0].text == "/usr/local/bin/python");
db.close();
cleanup();
std::println(" OK");
}
void test_normalize_multiple_occurrences(ragger::Embedder& emb) {
std::println(" test_normalize_multiple_occurrences...");
cleanup();
ragger::sqlite::Backend db(emb, TEMP_DB);
const char* home = std::getenv("HOME");
assert(home != nullptr);
std::string input = "Source: " + std::string(home) + "/a.md and " +
std::string(home) + "/b.md";
db.store(input);
auto all = db.load_all();
assert(all.size() == 1);
// Both occurrences should be replaced
assert(all[0].text == "Source: ~/a.md and ~/b.md");
// Original home path should not appear
assert(all[0].text.find(home) == std::string::npos);
db.close();
cleanup();
std::println(" OK");
}
void test_normalize_partial_match_not_replaced(ragger::Embedder& emb) {
std::println(" test_normalize_partial_match_not_replaced...");
cleanup();
ragger::sqlite::Backend db(emb, TEMP_DB);
const char* home = std::getenv("HOME");
assert(home != nullptr);
// Home path without trailing component — should NOT be replaced
// (normalize_path looks for HOME + "/", not HOME alone)
std::string input = "Path is " + std::string(home);
db.store(input);
auto all = db.load_all();
assert(all.size() == 1);
assert(all[0].text == input); // unchanged
db.close();
cleanup();
std::println(" OK");
}
void test_normalize_embedded_in_longer_text(ragger::Embedder& emb) {
std::println(" test_normalize_embedded_in_longer_text...");
cleanup();
ragger::sqlite::Backend db(emb, TEMP_DB);
const char* home = std::getenv("HOME");
assert(home != nullptr);
std::string input = "I stored notes at " + std::string(home) +
"/Projects/notes.md for future reference.";
db.store(input);
auto all = db.load_all();
assert(all.size() == 1);
assert(all[0].text == "I stored notes at ~/Projects/notes.md for future reference.");
db.close();
cleanup();
std::println(" OK");
}
int main() {
std::println("Running path normalization tests:");
ragger::init_config();
auto cfg = ragger::config();
assert(cfg.normalize_home_path == true);
// Embedder needed for sqlite::Backend store()
auto model_dir = cfg.resolved_model_dir();
if (!std::filesystem::exists(model_dir + "/model.onnx") && !std::filesystem::exists(model_dir + "/onnx/model.onnx")) {
std::cerr << "Skipping path normalization tests: model not found at " << model_dir << "\n";
return 0;
}
ragger::Embedder emb(model_dir);
test_normalize_home_in_stored_text(emb);
test_normalize_preserves_non_home_paths(emb);
test_normalize_multiple_occurrences(emb);
test_normalize_partial_match_not_replaced(emb);
test_normalize_embedded_in_longer_text(emb);
std::println("test_path_normalization: all passed");
return 0;
}