qbt-clean 0.151.0

Automated rules-based cleaning of qBittorrent torrents.
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
fn now() -> std::time::SystemTime {
	// Non-epoch so duration subtraction works in both directions.
	std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000)
}

#[tokio::test]
async fn fast_rule_priority_last_config_rule_wins_for_pin() {
	// Two fast rules with conflicting `pin` both match; reverse iteration
	// makes the last-in-config (pin=false) finalize first and win.
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(true),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(false),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());
}

#[tokio::test]
async fn fast_rules_compose_across_independent_fields() {
	// Two fast rules touching different fields (include_in_score vs pin)
	// both apply.
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				include_in_score: Some(false),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(true),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(matches!(res.pinned, Some(crate::PinReason::Explicit)));
	assert!(!res.include_in_score);
}

#[tokio::test]
async fn slow_phase2_resolves_when_phase1_has_multiple_possible_pin_values() {
	// Two slow rules with conflicting `pin`. Phase 1 sees both Some(true)
	// and Some(false) as possible, so it punts to phase 2, which fetches
	// trackers and resolves definitively.
	let t = crate::Torrent {
		category: "linux".into(),
		seeding_time: std::time::Duration::from_secs(1),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		categories_allowed: ["linux".into()].into(),
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					tracker_msg: Some(regex::RegexSet::new([r"^ok$"]).unwrap()),
					..Default::default()
				},
				pin: Some(true),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					tracker_url: Some(regex::RegexSet::new([r"^http://bad/.*$"]).unwrap()),
					..Default::default()
				},
				pin: Some(false),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	// pin matches.
	let g = crate::qbt_mock::global_with_trackers(c.clone(), now(), vec![
		crate::Tracker {
			msg: "ok".into(),
			status: crate::TrackerStatus::Working,
			url: "http://t/".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(matches!(res.pinned, Some(crate::PinReason::Explicit)));

	// unpin matches.
	let g = crate::qbt_mock::global_with_trackers(c.clone(), now(), vec![
		crate::Tracker {
			msg: "nope".into(),
			status: crate::TrackerStatus::Working,
			url: "http://bad/x".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());

	// Both match — last in config wins.
	let g = crate::qbt_mock::global_with_trackers(c.clone(), now(), vec![
		crate::Tracker {
			msg: "ok".into(),
			status: crate::TrackerStatus::Working,
			url: "http://bad/x".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());

	// Neither matches.
	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![
		crate::Tracker {
			msg: "nope".into(),
			status: crate::TrackerStatus::Working,
			url: "http://t/".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());
}

#[tokio::test]
async fn pin_reason_only_from_matching_rule() {
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let non_matching = crate::Rule {
		match_: crate::Match {
			categories: Some(["mac".into()].into()),
			..Default::default()
		},
		pin: Some(true),
		pin_reason: Some("should-not-leak".into()),
		..Default::default()
	};
	let matching = crate::Rule {
		match_: crate::Match {
			categories: Some(["linux".into()].into()),
			..Default::default()
		},
		pin: Some(true),
		pin_reason: Some("keep linux".into()),
		..Default::default()
	};

	// Non-matching rule first.
	let c = crate::Config {
		rules: vec![non_matching.clone(), matching.clone()],
		..crate::Config::empty()
	};
	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert_eq!(res.pinned, Some(crate::PinReason::Explicit));
	assert_eq!(res.pin_reason, Some("keep linux".into()));

	// Non-matching rule last.
	let c = crate::Config {
		rules: vec![matching, non_matching],
		..crate::Config::empty()
	};
	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert_eq!(res.pinned, Some(crate::PinReason::Explicit));
	assert_eq!(res.pin_reason, Some("keep linux".into()));
}

#[tokio::test]
async fn pin_reason_from_applied_pin_surrounded_by_opposite() {
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(true),
				pin_reason: Some("before".into()),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(false),
				pin_reason: Some("applied".into()),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["mac".into()].into()),
					..Default::default()
				},
				pin: Some(true),
				pin_reason: Some("after".into()),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());
	assert_eq!(res.pin_reason, Some("applied".into()));
}

#[tokio::test]
async fn pin_reason_from_applied_pin_true_surrounded_by_false() {
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(false),
				pin_reason: Some("before".into()),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				pin: Some(true),
				pin_reason: Some("applied".into()),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					categories: Some(["mac".into()].into()),
					..Default::default()
				},
				pin: Some(false),
				pin_reason: Some("after".into()),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, false).await.unwrap();
	assert_eq!(res.pinned, Some(crate::PinReason::Explicit));
	assert_eq!(res.pin_reason, Some("applied".into()));
}

#[tokio::test]
async fn group_pinned_torrent_reports_no_pin_reason() {
	let t = crate::Torrent {
		category: "linux".into(),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		rules: vec![crate::Rule {
			match_: crate::Match {
				categories: Some(["linux".into()].into()),
				..Default::default()
			},
			pin: Some(false),
			pin_reason: Some("should-not-leak".into()),
			..Default::default()
		}],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![]);
	let res = g.rules(&t, true).await.unwrap();
	assert_eq!(res.pinned, None);
	assert_eq!(res.pin_reason, None);
}

#[tokio::test]
async fn pin_reason_from_slow_matched_rule() {
	// The deciding pin rule resolves only in the slow (phase 2) path. Its
	// reason must be captured, and a non-matching slow rule's reason must not.
	let t = crate::Torrent {
		category: "linux".into(),
		seeding_time: std::time::Duration::from_secs(1),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		categories_allowed: ["linux".into()].into(),
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					tracker_url: Some(regex::RegexSet::new([r"^http://other/.*$"]).unwrap()),
					..Default::default()
				},
				pin: Some(false),
				pin_reason: Some("should-not-leak".into()),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					tracker_url: Some(regex::RegexSet::new([r"^http://keep/.*$"]).unwrap()),
					..Default::default()
				},
				pin: Some(true),
				pin_reason: Some("slow keep".into()),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![
		crate::Tracker {
			msg: "ok".into(),
			status: crate::TrackerStatus::Working,
			url: "http://keep/x".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert_eq!(res.pinned, Some(crate::PinReason::Explicit));
	assert_eq!(res.pin_reason, Some("slow keep".into()));
}

#[tokio::test]
async fn mixed_fast_and_slow_rules() {
	// A fast rule sets `include_in_score=false` unconditionally; a slow
	// rule raises `seeder_count_min` when its tracker matches. Phase 1
	// can't resolve `seeder_count_min`, so phase 2 fetches trackers and
	// re-evaluates both rules.
	let t = crate::Torrent {
		category: "linux".into(),
		seeders: 5,
		seeding_time: std::time::Duration::from_secs(1),
		..crate::Torrent::zero()
	};

	let c = crate::Config {
		categories_allowed: ["linux".into()].into(),
		rules: vec![
			crate::Rule {
				match_: crate::Match {
					categories: Some(["linux".into()].into()),
					..Default::default()
				},
				include_in_score: Some(false),
				..Default::default()
			},
			crate::Rule {
				match_: crate::Match {
					tracker_msg: Some(regex::RegexSet::new([r"^private$"]).unwrap()),
					..Default::default()
				},
				seeder_count_min: Some(50),
				..Default::default()
			},
		],
		..crate::Config::empty()
	};

	// Slow rule applies; seeder_count_min=50 violated.
	let g = crate::qbt_mock::global_with_trackers(c.clone(), now(), vec![
		crate::Tracker {
			msg: "private".into(),
			status: crate::TrackerStatus::Working,
			url: "http://t/".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(matches!(res.pinned, Some(crate::PinReason::Seeders(5))));
	assert!(!res.include_in_score);

	// Slow rule skipped; only the fast rule applies.
	let g = crate::qbt_mock::global_with_trackers(c, now(), vec![
		crate::Tracker {
			msg: "public".into(),
			status: crate::TrackerStatus::Working,
			url: "http://t/".into(),
		},
	]);
	let res = g.rules(&t, false).await.unwrap();
	assert!(res.pinned.is_none());
	assert!(!res.include_in_score);
}