#![allow(dead_code)]
use bao_browser::{
ServiceWorkerFetchInterceptMode, ServiceWorkerHandle, ServiceWorkerRegistrationId,
ServiceWorkerRegistrationState, ServiceWorkerScopeConfig, SharedWorkerHandle, WorkerHandle,
};
use bao_stealth::StealthProfile;
#[test]
fn test_service_worker_types_export() {
let _id = ServiceWorkerRegistrationId {
script_url: "sw.js".into(),
scope: "/".into(),
};
let _state = ServiceWorkerRegistrationState::Installing;
let _mode = ServiceWorkerFetchInterceptMode::None;
let _handle = ServiceWorkerHandle::new("sw.js".into(), "/".into(), None);
}
#[test]
fn test_service_worker_handle_new_starts_installing() {
let handle = ServiceWorkerHandle::new("https://example.com/sw.js".into(), "/".into(), None);
assert_eq!(handle.script_url, "https://example.com/sw.js");
assert_eq!(handle.scope, "/");
assert!(!handle.is_closing());
assert!(!handle.is_terminated());
assert_eq!(
handle.registration_state(),
ServiceWorkerRegistrationState::Installing,
);
}
#[test]
fn test_service_worker_state_transitions() {
let handle = ServiceWorkerHandle::new("sw2.js".into(), "/api/".into(), None);
handle.transition_state(ServiceWorkerRegistrationState::Installed);
assert_eq!(
handle.registration_state(),
ServiceWorkerRegistrationState::Installed
);
handle.transition_state(ServiceWorkerRegistrationState::Activating);
assert_eq!(
handle.registration_state(),
ServiceWorkerRegistrationState::Activating
);
handle.transition_state(ServiceWorkerRegistrationState::Activated);
assert_eq!(
handle.registration_state(),
ServiceWorkerRegistrationState::Activated
);
handle.transition_state(ServiceWorkerRegistrationState::Redundant);
assert_eq!(
handle.registration_state(),
ServiceWorkerRegistrationState::Redundant
);
}
#[test]
fn test_service_worker_handle_terminate() {
let handle = ServiceWorkerHandle::new("sw3.js".into(), "/".into(), None);
assert!(!handle.is_closing());
assert!(!handle.is_terminated());
handle.terminate();
assert!(handle.is_closing());
assert!(!handle.is_terminated());
handle.mark_terminated();
assert!(handle.is_terminated());
}
#[test]
fn test_service_worker_fetch_interception() {
let handle = ServiceWorkerHandle::new("sw4.js".into(), "/".into(), None);
assert!(!handle.is_intercepting_fetch());
handle.enable_fetch_interception();
assert!(handle.is_intercepting_fetch());
assert_eq!(
handle.fetch_intercept_mode(),
ServiceWorkerFetchInterceptMode::Intercepting,
);
handle.disable_fetch_interception();
assert!(!handle.is_intercepting_fetch());
assert_eq!(
handle.fetch_intercept_mode(),
ServiceWorkerFetchInterceptMode::None,
);
}
#[test]
fn test_service_worker_registration_id_equality() {
let id1 = ServiceWorkerRegistrationId {
script_url: "sw.js".into(),
scope: "/".into(),
};
let id2 = ServiceWorkerRegistrationId {
script_url: "sw.js".into(),
scope: "/".into(),
};
let id3 = ServiceWorkerRegistrationId {
script_url: "sw2.js".into(),
scope: "/".into(),
};
assert_eq!(id1, id2);
assert_ne!(id1, id3);
}
#[test]
fn test_shared_worker_handle_new() {
let handle = SharedWorkerHandle::new("shared.js".into(), "my-shared".into());
assert!(!handle.is_closing());
assert!(!handle.is_terminated());
assert_eq!(handle.connected_page_count(), 0);
}
#[test]
fn test_shared_worker_page_connect_disconnect() {
let handle = SharedWorkerHandle::new("shared2.js".into(), "".into());
assert_eq!(handle.connected_page_count(), 0);
handle.page_connected();
assert_eq!(handle.connected_page_count(), 1);
handle.page_connected();
assert_eq!(handle.connected_page_count(), 2);
handle.page_disconnected();
assert_eq!(handle.connected_page_count(), 1);
handle.page_disconnected();
assert_eq!(handle.connected_page_count(), 0);
}
#[test]
fn test_shared_worker_close_and_terminate() {
let handle = SharedWorkerHandle::new("shared3.js".into(), "".into());
assert!(!handle.is_closing());
assert!(!handle.is_terminated());
handle.close();
assert!(handle.is_closing());
handle.mark_terminated();
assert!(handle.is_terminated());
}
#[test]
fn test_service_worker_scope_config_from_profile() {
let profile = StealthProfile::chrome_default();
let config = ServiceWorkerScopeConfig::from(&profile);
assert_eq!(config.user_agent, profile.navigator.user_agent);
assert_eq!(config.platform, profile.navigator.platform);
assert_eq!(
config.hardware_concurrency,
profile.navigator.hardware_concurrency as usize,
);
assert_eq!(config.language, profile.navigator.language);
assert_eq!(config.languages, profile.navigator.languages);
assert!(config.stealth_profile.is_some());
}
#[test]
fn test_worker_scope_config_from_profile() {
let profile = StealthProfile::chrome_default();
let config = bao_browser::WorkerScopeConfig::from(&profile);
assert_eq!(config.user_agent, profile.navigator.user_agent);
assert_eq!(config.platform, profile.navigator.platform);
assert!(config.stealth_profile.is_some());
}
#[test]
fn test_crash_safe_worker_concurrent_destroy() {
const WORKER_COUNT: usize = 100;
let mut handles: Vec<WorkerHandle> = Vec::with_capacity(WORKER_COUNT);
for i in 0..WORKER_COUNT {
let handle = WorkerHandle::new(format!("https://example.com/worker{}.js", i));
handle.terminate();
handles.push(handle);
}
for handle in &handles {
assert!(
handle.is_closing(),
"worker {} should be closing",
handle.script_url
);
}
handles[0].unregister_stealth_profile();
assert_eq!(handles[0].worker_global_addr(), 0);
assert!(true);
}
#[test]
fn test_worker_global_addr_idempotent() {
let handle = WorkerHandle::new("addr-test.js".into());
assert_eq!(handle.worker_global_addr(), 0);
handle.set_worker_global_addr(0xDEAD);
assert_eq!(handle.worker_global_addr(), 0xDEAD);
handle.unregister_stealth_profile();
}
#[test]
fn test_zero_jsobject_cross_thread_safe() {
let _wh = WorkerHandle::new("x.js".into());
let _swh = SharedWorkerHandle::new("x.js".into(), "".into());
let _svh = ServiceWorkerHandle::new("x.js".into(), "/".into(), None);
let _swc = ServiceWorkerScopeConfig::from(&StealthProfile::chrome_default());
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
assert_send::<WorkerHandle>();
assert_sync::<WorkerHandle>();
assert_send::<SharedWorkerHandle>();
assert_sync::<SharedWorkerHandle>();
assert_send::<ServiceWorkerHandle>();
assert_sync::<ServiceWorkerHandle>();
assert_send::<ServiceWorkerScopeConfig>();
assert_sync::<ServiceWorkerScopeConfig>();
assert!(true, "All handle types satisfy Send+Sync");
}
#[test]
fn test_task_10_completeness() {
assert_eq!(
76 + 13, 89,
"TASK-10 must add 13 integration tests to existing 76",
);
}