Repository navigation
Expand file tree
/
Copy pathtest_path_normalization.cpp
More file actions
176 lines (142 loc) · 5.44 KB
/
Copy pathtest_path_normalization.cpp
File metadata and controls
176 lines (142 loc) · 5.44 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* 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 "test_env.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");
}
// PHASE2 Step 5: is_safe_model_repo (C §1.6).
void test_is_safe_model_repo() {
std::println(" test_is_safe_model_repo...");
using ragger::is_safe_model_repo;
for (const char* ok : {"sentence-transformers/all-MiniLM-L6-v2", "a/b",
"a.b.c/d_e-f.g", "A1/B2"})
CHECK(is_safe_model_repo(ok));
for (const char* bad : {"x/../../../tmp/evil", "../a", "a/..", "a//b",
"a/b/c", "/a", "a/", "a\\b/c", "", "a/b c",
".", "./a", "a/.", "..", "a/.."})
CHECK(!is_safe_model_repo(bad));
// Embedded NUL (built with an explicit length so the NUL is inside).
CHECK(!is_safe_model_repo(std::string("a/b\0c", 5)));
// Over-long.
CHECK(!is_safe_model_repo(std::string(150, 'a') + "/" + std::string(60, 'b')));
CHECK(is_safe_model_repo(std::string(100, 'a') + "/" + std::string(99, 'b'))); // 200 total
std::println(" OK");
}
int main() {
TestEnv env("path_normalization"); // must be first: isolates from ~/.ragger
std::println("Running path normalization tests:");
test_is_safe_model_repo(); // pure function — before the model gate
auto cfg = ragger::config();
assert(cfg.normalize_home_path == true);
// Embedder needed for sqlite::Backend store()
auto model_dir = env.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;
}