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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Profile types: ProfileKind enum, Platform enum, PerFieldOverride struct,
//! plus the StealthProfile builder.
use std::path::{Path, PathBuf};
use crate::error::StealthError;
use crate::fingerprint::{Fingerprint, UserAgentMetadata};
use crate::persona::specs::ScreenSpec;
/// Stealth modes shipped by the library.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProfileKind {
/// No stealth applied. Browser is launched stock; no JS patches, no UA scrub.
Off,
/// Launch flags + UA scrub (HeadlessChrome → Chrome). No JS bootstrap.
/// Safe against `Function.prototype.toString` detection. Default.
Native,
/// Native + Navigator-prototype JS patches. Passes sannysoft. Detectable
/// by sophisticated bots that probe `toString` on Navigator getters.
Spoofed,
}
/// JS `navigator.platform` value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Platform {
Win32,
MacIntel,
LinuxX86_64,
}
impl Platform {
/// Map to the `navigator.platform` string Chrome reports for that OS.
#[must_use]
pub fn js_string(self) -> &'static str {
match self {
Platform::Win32 => "Win32",
Platform::MacIntel => "MacIntel",
Platform::LinuxX86_64 => "Linux x86_64",
}
}
/// CDP `userAgentMetadata.platform` value (no version).
#[must_use]
pub fn ch_platform(self) -> &'static str {
match self {
Platform::Win32 => "Windows",
Platform::MacIntel => "macOS",
Platform::LinuxX86_64 => "Linux",
}
}
/// UA-string OS token (the bit inside parentheses).
#[must_use]
pub fn ua_token(self) -> &'static str {
match self {
Platform::Win32 => "Windows NT 10.0; Win64; x64",
Platform::MacIntel => "Macintosh; Intel Mac OS X 10_15_7",
Platform::LinuxX86_64 => "X11; Linux x86_64",
}
}
}
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub(crate) struct PerFieldOverride {
pub memory_gb: Option<u32>,
pub cpu_count: Option<u32>,
pub chrome_major: Option<u32>,
pub platform: Option<Platform>,
pub timezone: Option<String>,
pub locale: Option<String>,
pub languages: Option<Vec<String>>,
pub ua_string: Option<String>,
pub ua_metadata: Option<UserAgentMetadata>,
pub screen: Option<ScreenSpec>,
}
/// Stealth configuration passed to `BrowserBuilder::stealth(...)`.
#[derive(Debug, Clone)]
pub struct StealthProfile {
pub(crate) kind: ProfileKind,
pub(crate) extra_flags: Vec<String>,
pub(crate) fingerprint_override: Option<Fingerprint>,
pub(crate) per_field: PerFieldOverride,
pub(crate) bypass_csp: bool,
// Wired by `BrowserBuilder::stealth` in Task 17.
#[allow(dead_code)]
pub(crate) user_data_dir: Option<PathBuf>,
}
impl StealthProfile {
/// No stealth: stock browser launch.
///
/// Use this when you want a bare-bones Chrome with none of the launch
/// flags or CDP overrides applied — e.g. when verifying that a problem
/// reproduces in vanilla Chrome.
///
/// ```
/// use zendriver_stealth::StealthProfile;
/// let p = StealthProfile::off();
/// assert!(p.build_flags().is_empty());
/// ```
#[must_use]
pub fn off() -> Self {
Self {
kind: ProfileKind::Off,
extra_flags: Vec::new(),
fingerprint_override: None,
per_field: PerFieldOverride::default(),
bypass_csp: false,
user_data_dir: None,
}
}
/// Launch flags + UA scrub + Emulation overrides. No JS bootstrap.
///
/// Safe against `Function.prototype.toString` detection (it doesn't
/// patch any prototype getter). The default when stealth is requested.
///
/// ```
/// use zendriver_stealth::StealthProfile;
/// let p = StealthProfile::native();
/// assert!(!p.build_flags().is_empty());
/// ```
#[must_use]
pub fn native() -> Self {
Self {
kind: ProfileKind::Native,
extra_flags: Vec::new(),
fingerprint_override: None,
per_field: PerFieldOverride::default(),
bypass_csp: false,
user_data_dir: None,
}
}
/// `native` + Navigator-prototype JS patches. Passes the sannysoft
/// detection battery.
///
/// Sets [`bypass_csp`](Self::bypass_csp) on by default so the bootstrap
/// script can install on pages with strict CSP headers.
///
/// ```
/// use zendriver_stealth::StealthProfile;
/// let p = StealthProfile::spoofed();
/// assert!(p.bypass_csp_enabled());
/// ```
#[must_use]
pub fn spoofed() -> Self {
Self {
kind: ProfileKind::Spoofed,
extra_flags: Vec::new(),
fingerprint_override: None,
per_field: PerFieldOverride::default(),
bypass_csp: true, // default ON for spoofed; see spec assumption #2
user_data_dir: None,
}
}
/// Override the auto-detected [`Fingerprint`] wholesale.
///
/// Use this when you need to pin a specific Chrome version / platform /
/// hardware combination across runs (e.g. to keep request fingerprints
/// stable across CI invocations).
#[must_use]
pub fn fingerprint(mut self, f: Fingerprint) -> Self {
self.fingerprint_override = Some(f);
self
}
/// Override the reported `navigator.deviceMemory` (in GB).
///
/// Per the [HTML Device Memory spec][spec] the value Chrome reports
/// is one of `{1, 2, 4, 8}` (Chrome does not expose fractional values
/// to JS). The resolver snaps `gb` to the nearest valid integer at
/// or below it:
///
/// | Input `gb` | Reported |
/// |-----------:|---------:|
/// | `0` or `1` | `1` |
/// | `2` or `3` | `2` |
/// | `4`–`7` | `4` |
/// | `8`+ | `8` |
///
/// Snap-down (not nearest-round) matches Chrome's own behavior and
/// avoids inflating the reported value above what the host plausibly
/// has. Anything outside `{1, 2, 4, 8}` is itself a stealth tell
/// because real browsers never report it.
///
/// [spec]: https://www.w3.org/TR/device-memory/
#[must_use]
pub fn memory_gb(mut self, gb: u32) -> Self {
self.per_field.memory_gb = Some(gb);
self
}
/// Override the reported `navigator.hardwareConcurrency` (CPU count).
///
/// Clamped to `2..=32` at resolve time — values outside that range are
/// implausibly low/high and trip simple heuristics.
#[must_use]
pub fn cpu_count(mut self, n: u32) -> Self {
self.per_field.cpu_count = Some(n);
self
}
/// Override the reported Chrome major version (e.g. `125`).
#[must_use]
pub fn chrome_version(mut self, major: u32) -> Self {
self.per_field.chrome_major = Some(major);
self
}
/// Override the reported [`Platform`] (`navigator.platform` + UA OS
/// token + UAM `platform`).
#[must_use]
pub fn platform(mut self, p: Platform) -> Self {
self.per_field.platform = Some(p);
self
}
/// Override the reported locale (e.g. `"en-US"`, `"fr-FR"`).
///
/// Sends `Emulation.setLocaleOverride` and adds `--lang=...` to the
/// launch flags.
#[must_use]
pub fn locale(mut self, l: impl Into<String>) -> Self {
self.per_field.locale = Some(l.into());
self
}
/// Override the reported language list (drives `navigator.languages` +
/// q-weighted `Accept-Language`). When unset, derived from
/// [`locale`](Self::locale).
#[must_use]
pub fn languages(mut self, langs: impl IntoIterator<Item = String>) -> Self {
self.per_field.languages = Some(langs.into_iter().collect());
self
}
/// Override the reported timezone via `Emulation.setTimezoneOverride`
/// (IANA name, e.g. `"America/Los_Angeles"`).
#[must_use]
pub fn timezone(mut self, tz: impl Into<String>) -> Self {
self.per_field.timezone = Some(tz.into());
self
}
/// Override the reported User-Agent string verbatim.
///
/// Skips the composed-from-fingerprint step — prefer
/// [`platform`](Self::platform) + [`chrome_version`](Self::chrome_version)
/// unless you need an exact UA string.
#[must_use]
pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
self.per_field.ua_string = Some(ua.into());
self
}
/// Override the reported UA-CH (`userAgentMetadata`) wholesale.
///
/// Skips the composed-from-fingerprint step entirely — the resolved
/// [`Fingerprint::ua_metadata`] equals exactly what's passed here.
/// Prefer this over hand-composing individual UA-CH fields when you
/// need an exact, pre-built [`UserAgentMetadata`] (e.g. captured from a
/// real browser). For persona-driven, field-wise UA-CH overrides that
/// fall back to the derived value per sub-field, use a
/// [`Persona`](crate::Persona)'s `ua.ua_metadata`
/// ([`UaMetadata`](crate::UaMetadata)) instead, threaded via
/// [`StealthObserver::with_persona`](crate::StealthObserver::with_persona).
#[must_use]
pub fn user_agent_metadata(mut self, m: UserAgentMetadata) -> Self {
self.per_field.ua_metadata = Some(m);
self
}
/// Override the reported screen / device-metrics
/// (`Emulation.setDeviceMetricsOverride`) wholesale.
///
/// Replaces the observer's fixed 1920x1080 default. Like
/// [`user_agent_metadata`](Self::user_agent_metadata), this is a
/// profile-level pin; a [`Persona`](crate::Persona)'s `screen` field
/// (threaded via
/// [`StealthObserver::with_persona`](crate::StealthObserver::with_persona))
/// takes precedence when both are set.
#[must_use]
pub fn screen(mut self, s: ScreenSpec) -> Self {
self.per_field.screen = Some(s);
self
}
/// Toggle `Page.setBypassCSP`. Default `true` for [`spoofed`](Self::spoofed),
/// `false` for [`native`](Self::native) / [`off`](Self::off).
#[must_use]
pub fn bypass_csp(mut self, on: bool) -> Self {
self.bypass_csp = on;
self
}
/// Add a single extra Chrome launch flag (e.g. `"--proxy-server=..."`).
#[must_use]
pub fn arg(mut self, flag: impl Into<String>) -> Self {
self.extra_flags.push(flag.into());
self
}
/// Add a batch of Chrome launch flags.
#[must_use]
pub fn args(mut self, flags: impl IntoIterator<Item = String>) -> Self {
self.extra_flags.extend(flags);
self
}
// Consumed by `StealthObserver` in Task 13.
#[allow(dead_code)]
pub(crate) fn kind(&self) -> ProfileKind {
self.kind
}
/// Resolve the final [`Fingerprint`]: either the explicit override or an
/// auto-detected baseline, with per-field tweaks (`platform`, `locale`,
/// `memory_gb`, …) applied on top.
///
/// `chrome_exe` is probed for its Chrome major: by reading the binary's PE
/// version resource on Windows, and by running `--version` on Unix (Windows
/// Chrome answers `--version` by launching a browser that never exits, so it
/// must not be executed there). Either probe failing falls back to a
/// baked-in default, so the resolver never errors solely on Chrome being
/// unavailable.
///
/// # Errors
/// Returns [`StealthError::ChromeVersionDetect`] when the Chrome probe
/// fails *and* no override is provided, and [`StealthError::SystemInfo`]
/// when total-RAM detection fails.
// `StealthError` is large because `PatchFailed` wraps `CallError` (~152B).
// Boxing it would cross the Task 5 file scope; bypass per-fn instead.
#[allow(clippy::result_large_err)]
pub fn resolve_fingerprint(&self, chrome_exe: &Path) -> Result<Fingerprint, StealthError> {
let mut fp = match &self.fingerprint_override {
Some(fp) => fp.clone(),
None => Fingerprint::auto_detect(chrome_exe)?,
};
// The Accept-Encoding the *real* binary advertises, captured before the
// claimed-major override below (used only to warn on an uncorrectable skew).
let binary_major = fp.chrome_major;
if let Some(p) = self.per_field.platform {
fp.platform = p;
}
if let Some(c) = self.per_field.chrome_major {
fp.chrome_major = c;
fp.chrome_full = format!("{c}.0.6099.234"); // synthesize a full version if user only set major
}
if let Some(n) = self.per_field.cpu_count {
fp.cpu_count = n.clamp(2, 32);
}
if let Some(g) = self.per_field.memory_gb {
// Snap-down to the nearest spec-valid value Chrome exposes
// to JS — see `memory_gb` doc table.
let snapped = match g {
0..=1 => 1,
2..=3 => 2,
4..=7 => 4,
_ => 8,
};
if snapped != g {
tracing::debug!(
requested = g,
chosen = snapped,
"stealth memory_gb snapped to nearest navigator.deviceMemory spec value",
);
}
fp.memory_gb = snapped;
}
// Always recompose so `ua_metadata.{platform_version, architecture,
// bitness}` track any `platform` / `chrome_major` overrides applied
// above. Then, if the user supplied an explicit UA string, replace
// the freshly composed `ua_string` with it (UAM remains coherent
// with the overridden platform).
fp.recompose();
if let Some(ref ua) = self.per_field.ua_string {
fp.ua_string = ua.clone();
}
// Wholesale UA-CH pin: mirrors the `ua_string` override immediately
// above — when set, it REPLACES the recompose()-derived UAM outright
// (no field-wise fallback; that's what `Persona`'s `ua_metadata` is
// for, resolved in the observer against this fingerprint instead).
if let Some(ref uam) = self.per_field.ua_metadata {
fp.ua_metadata = uam.clone();
}
// Screen has no "derived" baseline (unlike UA-CH) — `Fingerprint`
// never had a screen concept before this, so leaving it `None` here
// is exactly today's behavior (the observer's fixed 1920x1080).
if let Some(s) = self.per_field.screen {
fp.screen = Some(s);
}
if let Some(ref tz) = self.per_field.timezone {
fp.timezone = Some(tz.clone());
}
if let Some(ref locale) = self.per_field.locale {
fp.locale = Some(locale.clone());
}
if let Some(ref langs) = self.per_field.languages {
fp.languages = Some(langs.clone());
}
// Accept-Encoding coherence is observable but NOT correctable over CDP:
// Chrome's network service owns the header and ignores
// `Network.setExtraHTTPHeaders` for it (verified, Chrome 148). When a
// pinned `chrome_version` straddles the `zstd`/Chrome-123 boundary vs the
// launched binary, the request advertises the binary's encodings — an
// Accept-Encoding vs User-Agent mismatch we can only warn about.
if let Some(coherent) = crate::headers::accept_encoding_skew(binary_major, fp.chrome_major)
{
tracing::warn!(
claimed_major = fp.chrome_major,
binary_major,
coherent_accept_encoding = coherent,
"stealth: claimed Chrome major advertises a different Accept-Encoding \
than the launched binary; Chrome controls this header and CDP cannot \
override it, so requests will leak the binary's encodings — pin \
chrome_version to the binary's major to stay coherent",
);
}
Ok(fp)
}
/// Composed Chrome launch flag list: per-profile defaults plus any
/// extras added via [`arg`](Self::arg) / [`args`](Self::args), with a
/// `--lang=<locale>` flag injected when [`locale`](Self::locale) is set.
///
/// ```
/// use zendriver_stealth::StealthProfile;
/// let flags = StealthProfile::native().locale("fr-FR").build_flags();
/// assert!(flags.iter().any(|f| f == "--lang=fr-FR"));
/// ```
pub fn build_flags(&self) -> Vec<String> {
let mut flags = crate::flags::flags_for_profile(self.kind);
if let Some(ref locale) = self.per_field.locale {
flags.push(format!("--lang={locale}"));
}
flags.extend(self.extra_flags.iter().cloned());
flags
}
/// Whether `Page.setBypassCSP` will be sent for this profile. Defaults
/// to `true` for [`spoofed`](Self::spoofed) and `false` otherwise; the
/// [`bypass_csp`](Self::bypass_csp) setter toggles it explicitly.
pub fn bypass_csp_enabled(&self) -> bool {
self.bypass_csp
}
/// Returns the input-realism profile appropriate for this stealth profile.
/// `spoofed` returns realistic timings; `native` and `off` return zero-overhead.
#[must_use]
pub fn input_profile(&self) -> crate::InputProfile {
match self.kind {
ProfileKind::Spoofed => crate::InputProfile::spoofed(),
ProfileKind::Native | ProfileKind::Off => crate::InputProfile::native(),
}
}
}
#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn platform_js_string_matches_chrome_output() {
assert_eq!(Platform::Win32.js_string(), "Win32");
assert_eq!(Platform::MacIntel.js_string(), "MacIntel");
assert_eq!(Platform::LinuxX86_64.js_string(), "Linux x86_64");
}
#[test]
fn platform_ch_platform_uses_no_version() {
assert_eq!(Platform::MacIntel.ch_platform(), "macOS");
}
#[test]
fn platform_ua_token_includes_arch() {
assert!(Platform::Win32.ua_token().contains("Win64; x64"));
}
}
#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod profile_tests {
use super::*;
use crate::fingerprint::UserAgentMetadata;
#[test]
fn off_profile_has_no_flags_no_patches() {
let p = StealthProfile::off();
assert_eq!(p.kind, ProfileKind::Off);
assert!(p.build_flags().is_empty());
}
#[test]
fn native_profile_has_flags_no_patches() {
let p = StealthProfile::native();
assert_eq!(p.kind, ProfileKind::Native);
assert!(!p.build_flags().is_empty());
}
#[test]
fn spoofed_profile_default_bypass_csp_on() {
let p = StealthProfile::spoofed();
assert!(p.bypass_csp_enabled());
}
#[test]
fn builder_chains_set_fields() {
let p = StealthProfile::spoofed()
.memory_gb(16)
.cpu_count(10)
.chrome_version(125)
.platform(Platform::MacIntel)
.locale("en-US")
.timezone("America/Los_Angeles")
.arg("--proxy-server=http://x");
assert_eq!(p.per_field.memory_gb, Some(16));
assert_eq!(p.per_field.cpu_count, Some(10));
assert_eq!(p.per_field.chrome_major, Some(125));
assert_eq!(p.per_field.platform, Some(Platform::MacIntel));
assert_eq!(p.per_field.locale.as_deref(), Some("en-US"));
assert_eq!(p.per_field.timezone.as_deref(), Some("America/Los_Angeles"));
assert!(
p.extra_flags
.contains(&"--proxy-server=http://x".to_string())
);
}
#[test]
fn build_flags_includes_locale_arg_when_set() {
let flags = StealthProfile::native().locale("fr-FR").build_flags();
assert!(flags.iter().any(|f| f == "--lang=fr-FR"));
}
#[test]
fn spoofed_profile_uses_spoofed_input_profile() {
let ip = StealthProfile::spoofed().input_profile();
assert!(ip.typo_rate > 0.0);
}
#[test]
fn native_profile_uses_native_input_profile() {
let ip = StealthProfile::native().input_profile();
assert_eq!(ip.typo_rate, 0.0);
}
#[test]
fn off_profile_uses_native_input_profile() {
let ip = StealthProfile::off().input_profile();
assert_eq!(ip.typo_rate, 0.0);
}
#[test]
fn resolve_fingerprint_with_explicit_override_skips_autodetect() {
let fp = Fingerprint {
platform: Platform::Win32,
chrome_major: 120,
chrome_full: "120.0.6099.234".into(),
cpu_count: 8,
memory_gb: 8,
ua_string: String::new(),
ua_metadata: UserAgentMetadata::realistic(Platform::Win32, 120, "120.0.6099.234"),
timezone: None,
locale: None,
languages: None,
screen: None,
};
let p = StealthProfile::native()
.fingerprint(fp.clone())
.platform(Platform::MacIntel);
// Pass a path that doesn't exist; if it tried to probe, it'd fail.
let resolved = p
.resolve_fingerprint(std::path::Path::new("/nonexistent"))
.unwrap();
assert_eq!(resolved.platform, Platform::MacIntel); // per-field override applied
}
#[test]
fn profile_languages_resolve_into_fingerprint() {
let profile = StealthProfile::native().languages(["fr-FR".into(), "fr".into()]);
let fp = Fingerprint {
platform: Platform::Win32,
chrome_major: 120,
chrome_full: "120.0.6099.234".into(),
cpu_count: 8,
memory_gb: 8,
ua_string: String::new(),
ua_metadata: UserAgentMetadata::realistic(Platform::Win32, 120, "120.0.6099.234"),
timezone: None,
locale: None,
languages: None,
screen: None,
};
let profile = profile.fingerprint(fp);
let resolved = profile
.resolve_fingerprint(std::path::Path::new("/nonexistent-chrome"))
.expect("resolve ok");
assert_eq!(resolved.languages.unwrap(), vec!["fr-FR", "fr"]);
}
fn bare_fp() -> Fingerprint {
Fingerprint {
platform: Platform::Win32,
chrome_major: 120,
chrome_full: "120.0.6099.234".into(),
cpu_count: 8,
memory_gb: 8,
ua_string: String::new(),
ua_metadata: UserAgentMetadata::realistic(Platform::Win32, 120, "120.0.6099.234"),
timezone: None,
locale: None,
languages: None,
screen: None,
}
}
#[test]
fn custom_ua_metadata_replaces_recompose_derived_when_supplied() {
let custom = UserAgentMetadata::realistic(Platform::MacIntel, 150, "150.0.7500.0");
let profile = StealthProfile::native()
.fingerprint(bare_fp())
.user_agent_metadata(custom.clone());
let resolved = profile
.resolve_fingerprint(std::path::Path::new("/nonexistent"))
.unwrap();
// Equals the SUPPLIED value exactly, not the recompose()-derived one
// (which would be `realistic(Win32, 120, ...)` from `bare_fp()`).
assert_eq!(resolved.ua_metadata, custom);
}
#[test]
fn absent_ua_metadata_override_keeps_recompose_derived_behavior() {
let profile = StealthProfile::native().fingerprint(bare_fp());
let resolved = profile
.resolve_fingerprint(std::path::Path::new("/nonexistent"))
.unwrap();
// No override → today's behavior: recompose()-derived from platform
// + chrome_major, i.e. matches `UserAgentMetadata::realistic` for the
// fingerprint's own (unoverridden) platform/version.
assert_eq!(
resolved.ua_metadata,
UserAgentMetadata::realistic(Platform::Win32, 120, "120.0.6099.234")
);
}
#[test]
fn custom_screen_resolves_onto_fingerprint_when_supplied() {
let screen = ScreenSpec {
width: 1536,
height: 864,
device_pixel_ratio: 1.25,
};
let profile = StealthProfile::native()
.fingerprint(bare_fp())
.screen(screen);
let resolved = profile
.resolve_fingerprint(std::path::Path::new("/nonexistent"))
.unwrap();
assert_eq!(resolved.screen, Some(screen));
}
#[test]
fn absent_screen_override_leaves_fingerprint_screen_none() {
let profile = StealthProfile::native().fingerprint(bare_fp());
let resolved = profile
.resolve_fingerprint(std::path::Path::new("/nonexistent"))
.unwrap();
// No override → today's behavior: no screen field influence at all.
assert_eq!(resolved.screen, None);
}
}