pub fn pip_toggle_js() -> &'static str {
r#"(function() {
// Find the most relevant video to PiP. Preference order:
// 1. currently playing video
// 2. video with audio
// 3. first video element
const videos = Array.from(document.querySelectorAll('video'));
if (videos.length === 0) return { ok: false, reason: 'no-video' };
const playing = videos.find(v => !v.paused && !v.ended);
const target = playing || videos.find(v => !v.muted) || videos[0];
// Toggle: if already in PiP, exit; otherwise request.
if (document.pictureInPictureElement === target) {
document.exitPictureInPicture()
.catch(e => console.warn('[buffr] exitPiP failed', e));
return { ok: true, action: 'exit' };
}
target.requestPictureInPicture()
.catch(e => console.warn('[buffr] requestPiP failed', e));
return { ok: true, action: 'enter' };
})()"#
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pip_toggle_js_contains_required_calls() {
let js = pip_toggle_js();
assert!(
js.contains("requestPictureInPicture"),
"JS must call requestPictureInPicture"
);
assert!(
js.contains("exitPictureInPicture"),
"JS must call exitPictureInPicture"
);
assert!(
js.contains("pictureInPictureElement"),
"JS must check pictureInPictureElement for toggle detection"
);
}
#[test]
fn pip_toggle_js_handles_no_video() {
let js = pip_toggle_js();
assert!(
js.contains("no-video"),
"JS must short-circuit with 'no-video' when no <video> elements exist"
);
}
}