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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
use std::{fs, path::Path};
use shared_constants::bucket::FILE_BUCKET_BIND;
use shared_functions::rola_test_sandbox;
use space_system::Space;
use crate::{
Bucket, NoProtocol,
bind::{
BucketBind, check_bucket_bind_exists, read_bucket_bind, read_bucket_binds,
remove_bucket_bind, write_bucket_bind,
},
};
fn init_bucket(path: &Path) -> Space<Bucket<NoProtocol>> {
let bucket = Bucket::<NoProtocol>::new_local();
let mut space = Space::new(bucket);
space.set_current_dir(&path).unwrap();
space.init(path).unwrap();
space
}
#[test]
fn test_read_bucket_binds() {
let sandbox = rola_test_sandbox("bucket_bind_read");
let b = init_bucket(&sandbox.path);
let bind_1 = sandbox.join(FILE_BUCKET_BIND("1"));
let bind_2 = sandbox.join(FILE_BUCKET_BIND("2"));
let bind_3 = sandbox.join(FILE_BUCKET_BIND("3"));
let bind_fail = sandbox.join(FILE_BUCKET_BIND("@"));
let other = sandbox.join("ot");
fs::write(bind_1, "./bucket1").unwrap();
fs::write(bind_2, "\n./bucket2").unwrap();
fs::write(bind_3, "./bucket3\nbbb").unwrap();
fs::write(bind_fail, "omg").unwrap();
fs::write(other, "ok").unwrap();
let result = read_bucket_binds(&b).unwrap();
assert!(result.contains(&BucketBind::new(1, "./bucket1")));
assert!(result.contains(&BucketBind::new(2, "./bucket2")));
assert!(result.contains(&BucketBind::new(3, "./bucket3\nbbb")));
assert_eq!(result.len(), 3);
}
#[test]
fn test_write_and_read_bucket_bind() {
let sandbox = rola_test_sandbox("bucket_bind_write_read");
let space = init_bucket(&sandbox.path);
// Write bucket bind records
write_bucket_bind(&space, 1, "./bucket1").unwrap();
write_bucket_bind(&space, 2, "./bucket2").unwrap();
write_bucket_bind(&space, 3, "./bucket3\nbbb").unwrap();
// Verify reads return the correct values
let bind1 = read_bucket_bind(&space, 1).unwrap().unwrap();
assert_eq!(bind1, BucketBind::new(1, "./bucket1"));
let bind2 = read_bucket_bind(&space, 2).unwrap().unwrap();
assert_eq!(bind2, BucketBind::new(2, "./bucket2"));
let bind3 = read_bucket_bind(&space, 3).unwrap().unwrap();
assert_eq!(bind3, BucketBind::new(3, "./bucket3\nbbb"));
// Read a non-existent bind should return None
let bind4 = read_bucket_bind(&space, 4).unwrap();
assert!(bind4.is_none());
}
#[test]
fn test_write_bucket_bind_trims_whitespace() {
let sandbox = rola_test_sandbox("bucket_bind_trim");
let space = init_bucket(&sandbox.path);
// Write URL with surrounding whitespace
write_bucket_bind(&space, 1, " ./bucket1 ").unwrap();
// Verify it was trimmed on write
let bind = read_bucket_bind(&space, 1).unwrap().unwrap();
assert_eq!(bind, BucketBind::new(1, "./bucket1"));
}
#[test]
fn test_write_bucket_bind_overwrites_existing() {
let sandbox = rola_test_sandbox("bucket_bind_overwrite");
let space = init_bucket(&sandbox.path);
write_bucket_bind(&space, 1, "./bucket_v1").unwrap();
write_bucket_bind(&space, 1, "./bucket_v2").unwrap();
let bind = read_bucket_bind(&space, 1).unwrap().unwrap();
assert_eq!(bind, BucketBind::new(1, "./bucket_v2"));
}
#[test]
fn test_check_bucket_bind_exists() {
let sandbox = rola_test_sandbox("bucket_bind_check_exists");
let space = init_bucket(&sandbox.path);
// Initially, no bucket bind should exist
let exists = check_bucket_bind_exists(&space, 1).unwrap();
assert!(!exists);
// Write a bucket bind and verify it exists
write_bucket_bind(&space, 1, "./bucket1").unwrap();
let exists = check_bucket_bind_exists(&space, 1).unwrap();
assert!(exists);
// A different index should still not exist
let exists = check_bucket_bind_exists(&space, 2).unwrap();
assert!(!exists);
// Write another bind and check
write_bucket_bind(&space, 2, "./bucket2").unwrap();
let exists = check_bucket_bind_exists(&space, 2).unwrap();
assert!(exists);
}
#[test]
fn test_check_bucket_bind_exists_after_delete() {
let sandbox = rola_test_sandbox("bucket_bind_check_exists_after_delete");
let space = init_bucket(&sandbox.path);
write_bucket_bind(&space, 1, "./bucket1").unwrap();
assert!(check_bucket_bind_exists(&space, 1).unwrap());
// Delete by removing the file directly
let bind_path = sandbox.path.join(format!(
"{}{:03}",
shared_constants::bucket::PREFIX_BUCKET_BIND,
1
));
std::fs::remove_file(bind_path).unwrap();
let exists = check_bucket_bind_exists(&space, 1).unwrap();
assert!(!exists);
}
#[test]
fn test_remove_bucket_bind() {
let sandbox = rola_test_sandbox("bucket_bind_remove");
let space = init_bucket(&sandbox.path);
// Write a bucket bind
write_bucket_bind(&space, 1, "./bucket1").unwrap();
assert!(check_bucket_bind_exists(&space, 1).unwrap());
// Remove it
remove_bucket_bind(&space, 1).unwrap();
assert!(!check_bucket_bind_exists(&space, 1).unwrap());
}
#[test]
fn test_remove_bucket_bind_nonexistent() {
let sandbox = rola_test_sandbox("bucket_bind_remove_nonexistent");
let space = init_bucket(&sandbox.path);
// Removing a non-existent bind should return an error
let result = remove_bucket_bind(&space, 99);
assert!(result.is_err());
}
#[test]
fn test_remove_bucket_bind_does_not_affect_others() {
let sandbox = rola_test_sandbox("bucket_bind_remove_others");
let space = init_bucket(&sandbox.path);
// Write multiple bucket binds
write_bucket_bind(&space, 1, "./bucket1").unwrap();
write_bucket_bind(&space, 2, "./bucket2").unwrap();
write_bucket_bind(&space, 3, "./bucket3").unwrap();
// Remove bind 2
remove_bucket_bind(&space, 2).unwrap();
// Bind 1 and 3 should still exist
assert!(check_bucket_bind_exists(&space, 1).unwrap());
assert!(!check_bucket_bind_exists(&space, 2).unwrap());
assert!(check_bucket_bind_exists(&space, 3).unwrap());
// Values should be preserved for remaining binds
assert_eq!(
read_bucket_bind(&space, 1).unwrap().unwrap(),
BucketBind::new(1, "./bucket1")
);
assert_eq!(
read_bucket_bind(&space, 3).unwrap().unwrap(),
BucketBind::new(3, "./bucket3")
);
}
#[test]
fn test_remove_bucket_bind_then_read_returns_none() {
let sandbox = rola_test_sandbox("bucket_bind_remove_then_read");
let space = init_bucket(&sandbox.path);
write_bucket_bind(&space, 1, "./bucket1").unwrap();
remove_bucket_bind(&space, 1).unwrap();
let bind = read_bucket_bind(&space, 1).unwrap();
assert!(bind.is_none());
}
#[test]
fn test_remove_bucket_bind_then_write_again() {
let sandbox = rola_test_sandbox("bucket_bind_remove_then_write");
let space = init_bucket(&sandbox.path);
write_bucket_bind(&space, 1, "./bucket_v1").unwrap();
remove_bucket_bind(&space, 1).unwrap();
// Write the same index again
write_bucket_bind(&space, 1, "./bucket_v2").unwrap();
let bind = read_bucket_bind(&space, 1).unwrap().unwrap();
assert_eq!(bind, BucketBind::new(1, "./bucket_v2"));
}
|