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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Integration tests for githubrepocloner using mock HTTP server
#![allow(clippy::unwrap_used, clippy::expect_used)]
use githubrepocloner::{create_client, clone_repos_with_client_and_url};
/// Helper: create an unauthenticated client for testing
fn test_client() -> reqwest::Client {
create_client(None).unwrap()
}
use mockito::Server;
use tempfile::TempDir;
/// Helper: create a JSON string for a single mock GitHub repo
fn mock_repo_json(name: &str, fork: bool, updated_at: &str, default_branch: &str) -> String {
format!(
r#"{{"name":"{name}","default_branch":"{default_branch}","fork":{fork},"updated_at":"{updated_at}"}}"#
)
}
/// Helper: set up the standard page-2 empty response to stop pagination
async fn mock_empty_page2(server: &mut Server) -> mockito::Mock {
server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=2")
.with_status(200)
.with_header("content-type", "application/json")
.with_body("[]")
.create_async()
.await
}
#[tokio::test]
async fn test_archive_mode_downloads_files() {
let mut server = Server::new_async().await;
let temp_dir = TempDir::new().unwrap();
let clone_folder = temp_dir.path().to_str().unwrap();
// Recent non-fork repo
let repo_json = mock_repo_json("my-project", false, "2026-02-20T12:00:00Z", "main");
let _m1 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=1")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo_json}]"))
.create_async()
.await;
let _m2 = mock_empty_page2(&mut server).await;
// Serve dummy tarball bytes
let _m3 = server
.mock("GET", "/repos/testorg/my-project/tarball/main")
.with_status(200)
.with_header("content-type", "application/gzip")
.with_body(b"\x1f\x8b fake-archive-data")
.create_async()
.await;
let client = test_client();
let result = clone_repos_with_client_and_url(
&client,
&server.url(),
"testorg",
clone_folder,
365,
true, // archive mode
1,
)
.await;
assert!(result.is_ok());
let archive_path = temp_dir.path().join("my-project.tar.gz");
assert!(archive_path.exists(), "Expected archive file to be created");
assert!(
std::fs::metadata(&archive_path).unwrap().len() > 0,
"Archive file should not be empty"
);
}
#[tokio::test]
async fn test_fork_repos_are_skipped() {
let mut server = Server::new_async().await;
let temp_dir = TempDir::new().unwrap();
let clone_folder = temp_dir.path().to_str().unwrap();
let repo_json = mock_repo_json("forked-repo", true, "2026-02-20T12:00:00Z", "main");
let _m1 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=1")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo_json}]"))
.create_async()
.await;
let _m2 = mock_empty_page2(&mut server).await;
// No tarball mock — it should never be requested for a fork
let client = test_client();
let result = clone_repos_with_client_and_url(
&client,
&server.url(),
"testorg",
clone_folder,
365,
true,
1,
)
.await;
assert!(result.is_ok());
let archive_path = temp_dir.path().join("forked-repo.tar.gz");
assert!(!archive_path.exists(), "Fork should not be downloaded");
}
#[tokio::test]
async fn test_old_repos_are_filtered_by_date() {
let mut server = Server::new_async().await;
let temp_dir = TempDir::new().unwrap();
let clone_folder = temp_dir.path().to_str().unwrap();
// Repo last updated in 2020 — well outside the 30-day window
let repo_json = mock_repo_json("old-repo", false, "2020-01-01T12:00:00Z", "main");
let _m1 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=1")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo_json}]"))
.create_async()
.await;
let _m2 = mock_empty_page2(&mut server).await;
let client = test_client();
let result = clone_repos_with_client_and_url(
&client,
&server.url(),
"testorg",
clone_folder,
30, // only repos updated in last 30 days
true,
1,
)
.await;
assert!(result.is_ok());
let archive_path = temp_dir.path().join("old-repo.tar.gz");
assert!(!archive_path.exists(), "Old repo should not be downloaded");
}
#[tokio::test]
async fn test_existing_repo_is_skipped() {
let mut server = Server::new_async().await;
let temp_dir = TempDir::new().unwrap();
let clone_folder = temp_dir.path().to_str().unwrap();
// Pre-create the repo folder so it will be skipped
std::fs::create_dir(temp_dir.path().join("existing-repo")).unwrap();
let repo_json = mock_repo_json("existing-repo", false, "2026-02-20T12:00:00Z", "main");
let _m1 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=1")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo_json}]"))
.create_async()
.await;
let _m2 = mock_empty_page2(&mut server).await;
// No tarball mock — should never be requested for an existing repo
let client = test_client();
let result = clone_repos_with_client_and_url(
&client,
&server.url(),
"testorg",
clone_folder,
365,
true,
1,
)
.await;
assert!(result.is_ok());
let archive_path = temp_dir.path().join("existing-repo.tar.gz");
assert!(!archive_path.exists(), "Existing repo folder should cause skip");
}
#[tokio::test]
async fn test_pagination_fetches_multiple_pages() {
let mut server = Server::new_async().await;
let temp_dir = TempDir::new().unwrap();
let clone_folder = temp_dir.path().to_str().unwrap();
// Page 1: one repo
let repo1 = mock_repo_json("repo-page1", false, "2026-02-20T12:00:00Z", "main");
let _m1 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=1")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo1}]"))
.create_async()
.await;
// Page 2: another repo
let repo2 = mock_repo_json("repo-page2", false, "2026-02-18T12:00:00Z", "main");
let _m2 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=2")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(format!("[{repo2}]"))
.create_async()
.await;
// Page 3: empty (end pagination)
let _m3 = server
.mock("GET", "/orgs/testorg/repos?per_page=100&page=3")
.with_status(200)
.with_header("content-type", "application/json")
.with_body("[]")
.create_async()
.await;
// Tarball mocks for both repos
let _m4 = server
.mock("GET", "/repos/testorg/repo-page1/tarball/main")
.with_status(200)
.with_header("content-type", "application/gzip")
.with_body(b"\x1f\x8b page1-archive")
.create_async()
.await;
let _m5 = server
.mock("GET", "/repos/testorg/repo-page2/tarball/main")
.with_status(200)
.with_header("content-type", "application/gzip")
.with_body(b"\x1f\x8b page2-archive")
.create_async()
.await;
let client = test_client();
let result = clone_repos_with_client_and_url(
&client,
&server.url(),
"testorg",
clone_folder,
365,
true,
1,
)
.await;
assert!(result.is_ok());
assert!(
temp_dir.path().join("repo-page1.tar.gz").exists(),
"Repo from page 1 should be downloaded"
);
assert!(
temp_dir.path().join("repo-page2.tar.gz").exists(),
"Repo from page 2 should be downloaded"
);
}
#[test]
fn test_client_creation() {
let client = create_client(None);
assert!(client.is_ok(), "Should be able to create HTTP client");
}
#[test]
fn test_client_creation_with_token() {
let client = create_client(Some("ghp_testtoken123"));
assert!(client.is_ok(), "Should be able to create authenticated HTTP client");
}
|