use super::tracker::RetryReason;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct RetryDirective {
pub rotate_ua: bool,
pub rotate_proxy: bool,
pub change_viewport: bool,
pub enable_stealth: bool,
pub force_browser: bool,
}
impl RetryDirective {
#[must_use]
pub fn for_attempt(reason: RetryReason, attempt: u32) -> Self {
if reason == RetryReason::FeatureToggle && attempt == 0 {
return Self {
rotate_ua: true,
..Self::default()
};
}
match attempt {
0 => Self::default(), 1 => Self {
rotate_ua: true,
..Self::default()
},
2 => Self {
rotate_ua: true,
rotate_proxy: true,
change_viewport: true,
..Self::default()
},
_ => Self {
rotate_ua: true,
rotate_proxy: true,
change_viewport: true,
enable_stealth: true,
force_browser: true,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn attempt_0_all_false() {
let d = RetryDirective::for_attempt(RetryReason::Transient, 0);
assert!(!d.rotate_ua);
assert!(!d.rotate_proxy);
assert!(!d.change_viewport);
assert!(!d.enable_stealth);
assert!(!d.force_browser);
}
#[test]
fn attempt_1_only_ua() {
let d = RetryDirective::for_attempt(RetryReason::Transient, 1);
assert!(d.rotate_ua);
assert!(!d.rotate_proxy);
assert!(!d.change_viewport);
assert!(!d.enable_stealth);
assert!(!d.force_browser);
}
#[test]
fn attempt_2_ua_proxy_viewport() {
let d = RetryDirective::for_attempt(RetryReason::Transient, 2);
assert!(d.rotate_ua);
assert!(d.rotate_proxy);
assert!(d.change_viewport);
assert!(!d.enable_stealth);
assert!(!d.force_browser);
}
#[test]
fn attempt_3_all_enabled() {
let d = RetryDirective::for_attempt(RetryReason::Transient, 3);
assert!(d.rotate_ua);
assert!(d.rotate_proxy);
assert!(d.change_viewport);
assert!(d.enable_stealth);
assert!(d.force_browser);
}
#[test]
fn attempt_high_all_enabled() {
let d = RetryDirective::for_attempt(RetryReason::AntiBot, 10);
assert!(d.rotate_ua);
assert!(d.rotate_proxy);
assert!(d.change_viewport);
assert!(d.enable_stealth);
assert!(d.force_browser);
}
#[test]
fn feature_toggle_attempt_0_rotates_ua() {
let d = RetryDirective::for_attempt(RetryReason::FeatureToggle, 0);
assert!(d.rotate_ua, "FeatureToggle attempt=0 must rotate UA");
assert!(!d.rotate_proxy);
assert!(!d.change_viewport);
assert!(!d.enable_stealth);
assert!(!d.force_browser);
}
#[test]
fn feature_toggle_attempt_1_follows_general() {
let d = RetryDirective::for_attempt(RetryReason::FeatureToggle, 1);
assert!(d.rotate_ua);
assert!(!d.rotate_proxy);
}
#[test]
fn antibot_follows_general_strategy() {
let d0 = RetryDirective::for_attempt(RetryReason::AntiBot, 0);
assert!(!d0.rotate_ua);
let d1 = RetryDirective::for_attempt(RetryReason::AntiBot, 1);
assert!(d1.rotate_ua);
let d3 = RetryDirective::for_attempt(RetryReason::AntiBot, 3);
assert!(d3.force_browser, "AntiBot attempt=3 must force browser");
assert!(d3.enable_stealth, "AntiBot attempt=3 must enable stealth");
}
#[test]
fn all_reasons_same_directive_at_attempt_2() {
let d_trans = RetryDirective::for_attempt(RetryReason::Transient, 2);
let d_ft = RetryDirective::for_attempt(RetryReason::FeatureToggle, 2);
let d_ab = RetryDirective::for_attempt(RetryReason::AntiBot, 2);
assert_eq!(d_trans, d_ft);
assert_eq!(d_trans, d_ab);
}
#[test]
fn default_all_false() {
let d = RetryDirective::default();
assert!(!d.rotate_ua);
assert!(!d.rotate_proxy);
assert!(!d.change_viewport);
assert!(!d.enable_stealth);
assert!(!d.force_browser);
}
}