hex/src/meeting/detection.rs

3399 lines
131 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::time::{Duration, Instant};
mod detector;
pub use detector::{MeetingDetector, OfferId};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::app_settings::{
KNOWN_MEETING_PROVIDERS, MORE_MEETING_PROVIDERS, MeetingPolicy, MeetingProviderChoice,
};
use crate::dictation::browser_page::BrowserPages;
use crate::dictation::context::ContextSnapshot;
use crate::microphone_activity::ActiveMicrophoneApplication;
/// A known call service must keep the microphone for about 2 s.
const STABLE_FOR: Duration = Duration::from_secs(2);
/// A browser using the microphone on a page that is not a known call site
/// must keep it for about 20 s: voice notes and dictation fields open the
/// microphone for a few seconds, calls hold it for minutes.
const BROWSER_STABLE_FOR: Duration = Duration::from_secs(20);
/// An offered call stays snoozed until its app has been off the microphone
/// for about a minute, so one call is offered once.
const ABSENT_FOR_RESET: Duration = Duration::from_secs(60);
/// About three seconds without the call app on the microphone.
const CALL_ENDED_AFTER: Duration = Duration::from_secs(3);
/// Observations further apart than this (a sleeping Mac, a stalled loop) do
/// not make one run: the clock restarts rather than counting the gap.
const MAX_OBSERVATION_GAP: Duration = Duration::from_secs(10);
/// A call that drops off one microphone check (or two, on a slow loop) and
/// is back right after keeps its time toward the offer (#835).
const DROPOUT_GRACE: Duration = Duration::from_secs(2);
/// A page the browser showed while it held the microphone still names the
/// call this long after it was last read.
const BROWSER_PAGE_MEMORY: Duration = Duration::from_secs(60);
/// Websites whose pages are calls whenever a browser holds the microphone on
/// them. Google Meet is separate: only its meeting paths are calls. Teams
/// pages are the Microsoft Teams app's service, so they carry its provider
/// key rather than one of their own (see [`provider_key_for_site`]).
const CALL_SITES: [(&str, MeetingSource); 7] = [
("banshee.app", MeetingSource::Banshee),
("web.whatsapp.com", MeetingSource::WhatsApp),
("app.v2.gather.town", MeetingSource::Gather),
("app.gather.town", MeetingSource::Gather),
("teams.microsoft.com", MeetingSource::Teams),
("teams.cloud.microsoft", MeetingSource::Teams),
("teams.live.com", MeetingSource::Teams),
];
/// The other call websites Hex names, with the label an offer's "…"
/// saves as that site's provider. FaceTime pages have their own choice,
/// shown apart from the app's name.
const MORE_CALL_SITES: [(&str, MeetingSource, &str); 4] = [
(
"voice.google.com",
MeetingSource::GoogleVoice,
"Google Voice",
),
("messenger.com", MeetingSource::Messenger, "Messenger"),
(
"facetime.apple.com",
MeetingSource::FaceTime,
"FaceTime (web)",
),
("web.groupme.com", MeetingSource::GroupMe, "GroupMe"),
];
/// How long a condition has held across observations, by the monotonic
/// clock, so a slow loop iteration cannot stretch a threshold.
#[derive(Clone, Copy, Debug)]
struct Run {
since: Instant,
last: Instant,
}
impl Run {
fn start(now: Instant) -> Self {
Self {
since: now,
last: now,
}
}
/// Records an observation and returns how long the run has lasted.
fn extend(&mut self, now: Instant) -> Duration {
if now.saturating_duration_since(self.last) > MAX_OBSERVATION_GAP {
self.since = now;
}
self.last = now;
now.saturating_duration_since(self.since)
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct MeetingCandidate {
pub key: String,
pub title: String,
pub source: MeetingSource,
/// Canonical bundle identifier of the application using the microphone.
#[serde(default)]
pub application: Option<String>,
/// Whether `title` names this meeting rather than its app.
#[serde(default)]
pub named: bool,
/// Website host of the browser tab, when the browser adapter knows it.
#[serde(default)]
pub site: Option<String>,
/// Display name of the application using the microphone.
#[serde(default)]
pub application_name: Option<String>,
/// The call's own meeting code when its page names one (a Google Meet
/// code), so two calls in one app are told apart (#1113).
#[serde(default)]
pub call: Option<String>,
}
fn known_provider_label(key: &str) -> Option<&'static str> {
KNOWN_MEETING_PROVIDERS
.iter()
.find(|(known, _)| *known == key)
.map(|(_, label)| *label)
.or_else(|| {
CALL_SITES
.iter()
.find(|(host, _)| *host == key)
.map(|(_, source)| source.label())
})
.or_else(|| {
MORE_MEETING_PROVIDERS
.iter()
.find(|(known, _)| *known == key)
.map(|(_, label)| *label)
})
.or_else(|| {
MORE_CALL_SITES
.iter()
.find(|(host, _, _)| *host == key)
.map(|(_, _, label)| *label)
})
.or_else(|| is_webex_host(key).then_some(MeetingSource::Webex.label()))
}
/// The provider key a website's calls use. Teams pages are the Microsoft
/// Teams app's own service, so the app's choice covers them instead of a
/// separate row per website host.
pub(crate) fn provider_key_for_site(host: &str) -> &str {
if matches!(
host,
"teams.microsoft.com" | "teams.cloud.microsoft" | "teams.live.com"
) {
"com.microsoft.teams2"
} else {
host
}
}
/// A website or app whose calls share one answer (Ask each time, Record
/// automatically, or Don't offer).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MeetingProvider {
pub key: String,
pub label: String,
}
impl MeetingProvider {
pub fn choose(self, policy: MeetingPolicy) -> MeetingProviderChoice {
MeetingProviderChoice {
key: self.key,
label: self.label,
policy,
}
}
}
/// What a stable detected call leads to.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Detection {
Offer(MeetingCandidate),
/// The provider is set to Always record.
Record(MeetingCandidate),
}
impl MeetingCandidate {
pub fn manual() -> Self {
Self {
key: "manual".into(),
title: MeetingSource::Manual.label().into(),
source: MeetingSource::Manual,
application: None,
named: false,
site: None,
application_name: None,
call: None,
}
}
/// A manual recording, named `title` when one is given.
pub fn manual_titled(title: Option<String>) -> Self {
match title.map(|title| title.trim().to_owned()) {
Some(title) if !title.is_empty() => Self {
title,
named: true,
..Self::manual()
},
_ => Self::manual(),
}
}
pub fn recording_start(&self) -> crate::meeting::MeetingStart {
crate::meeting::MeetingStart {
title: self.named.then(|| self.title.clone()),
app: self.app_label(),
application: self.application.clone(),
calendar: None,
title_source: if self.source == MeetingSource::Manual {
crate::meeting::TitleSource::User
} else {
crate::meeting::TitleSource::Call
},
}
}
/// What the call is in: the service, else the website, else the browser.
fn app_label(&self) -> Option<String> {
match self.source {
MeetingSource::Manual => None,
MeetingSource::BrowserCall => {
self.site.clone().or_else(|| self.application_name.clone())
}
source => Some(source.label().into()),
}
}
/// The offer capsule's headline.
pub fn offer_title(&self) -> String {
if self.named {
return self.title.clone();
}
match self.source {
MeetingSource::BrowserCall => match (&self.site, &self.application_name) {
(Some(site), _) => format!("{site} call detected"),
(None, Some(application)) => format!("{application} call detected"),
(None, None) => "Call detected".into(),
},
MeetingSource::SlackHuddle => "Slack huddle detected".into(),
source => format!("{} call detected", source.label()),
}
}
/// Whose call this is, for the user's answer: the website when known,
/// else the app. Known services use the
/// name Settings lists them under; a Teams page is the Teams app's
/// service, so it carries the app's key.
pub fn provider(&self) -> Option<MeetingProvider> {
let site = self.site.as_deref().map(provider_key_for_site);
let key = site.or(self.application.as_deref())?.to_string();
let label = known_provider_label(&key)
.map(str::to_string)
.or_else(|| self.site.clone())
.or_else(|| self.application_name.clone())
.unwrap_or_else(|| key.clone());
Some(MeetingProvider { key, label })
}
/// Whether this call may record automatically: not for a browser whose
/// page is unknown, which would record every page.
pub fn can_always_record(&self) -> bool {
self.provider().is_some()
&& !(self.source == MeetingSource::BrowserCall && self.site.is_none())
}
/// Whether the call is on a website rather than in an app of its own.
pub fn on_web(&self) -> bool {
self.site.is_some()
|| crate::microphone_activity::is_browser_id(self.application.as_deref())
|| matches!(
self.source,
MeetingSource::BrowserCall | MeetingSource::GoogleMeet | MeetingSource::Banshee
)
}
/// The user's answer for this call, or `default` when it has none.
pub fn policy(
&self,
choices: &[MeetingProviderChoice],
default: MeetingPolicy,
) -> MeetingPolicy {
self.own_policy(choices).unwrap_or(default)
}
/// The answer saved for this call: the website's when it has one, else
/// the app's (a browser's answer covers its other sites). A Teams page
/// keeps an answer other than Ask saved for its own earlier host key; an
/// Ask there no longer shadows the Teams app's answer.
fn own_policy(&self, choices: &[MeetingProviderChoice]) -> Option<MeetingPolicy> {
let chosen = |key: Option<&str>| {
let key = key?;
choices
.iter()
.find(|choice| choice.key == key)
.map(|choice| choice.policy)
};
let raw = self.site.as_deref();
let site = raw.map(provider_key_for_site);
let legacy = if site == raw {
chosen(raw)
} else {
// Ask is stored as a real entry, so an older host row left on Ask
// must not shadow a newer choice under the mapped key.
chosen(raw).filter(|policy| *policy != MeetingPolicy::Ask)
};
legacy
.or_else(|| chosen(site))
.or_else(|| chosen(self.application.as_deref()))
}
fn required_stability(&self) -> Duration {
match self.source {
// WhatsApp also takes the microphone for voice notes (#835), and
// Gather can hold it while you sit in a space (#855). So can the
// other messaging apps, and Messenger and
// GroupMe send voice clips too.
MeetingSource::BrowserCall
| MeetingSource::WhatsApp
| MeetingSource::Gather
| MeetingSource::Signal
| MeetingSource::Telegram
| MeetingSource::WeChat
| MeetingSource::Line
| MeetingSource::Element
| MeetingSource::Messenger
| MeetingSource::GroupMe => BROWSER_STABLE_FOR,
_ => STABLE_FOR,
}
}
}
/// Notices when the call application behind a recording stops using the
/// microphone, so the user can choose to stop. It never stops anything.
#[derive(Default)]
pub struct CallPresence {
absent: Option<Run>,
prompted: bool,
}
impl CallPresence {
/// Returns true once per absence that lasts long enough.
pub fn observe(&mut self, present: bool) -> bool {
self.observe_at(present, Instant::now())
}
fn observe_at(&mut self, present: bool, now: Instant) -> bool {
if present {
self.absent = None;
self.prompted = false;
return false;
}
let absent = self.absent.get_or_insert_with(|| Run::start(now));
if absent.extend(now) >= CALL_ENDED_AFTER && !self.prompted {
self.prompted = true;
return true;
}
false
}
}
/// How long an automatic recording keeps going after its call ended, under
/// Stop when the call ends (#835).
pub const AUTO_STOP_GRACE: Duration = Duration::from_secs(30);
/// How long a browser's page must name a different call before an automatic
/// recording treats its own call as over: long enough that a tab previewed
/// on the way to another one never stops anything.
pub const CALL_CHANGED_AFTER: Duration = Duration::from_secs(4);
/// How long detection keeps the call a page named just before a call-switch
/// stop: the browser stays on the microphone, so a recording has to start
/// that call well within this even though its reads still name the ended one.
const SWITCHED_CALL_MEMORY: Duration = Duration::from_secs(120);
/// Stop when the call ends: the deadline at which a recording Hex
/// started on its own stops, armed when [`CallPresence`] reports the call
/// ended (or [`CallSwitch`] reports another call's page) and dropped when the
/// call app comes back or the user keeps recording.
#[derive(Default)]
pub struct AutoStop {
deadline: Option<Instant>,
}
impl AutoStop {
/// The call ended: stop after the grace.
pub fn arm(&mut self) {
self.arm_at(Instant::now());
}
fn arm_at(&mut self, now: Instant) {
self.deadline = Some(now + AUTO_STOP_GRACE);
}
/// Whether a stop is counting: armed, and its grace has not run out.
pub fn armed(&self) -> bool {
self.deadline.is_some()
}
/// The call app came back, the user kept recording, or the toggle is
/// off: nothing stops. Returns whether a stop was pending.
pub fn disarm(&mut self) -> bool {
self.deadline.take().is_some()
}
/// Whether the grace has run out; true once, then disarmed.
pub fn due(&mut self) -> bool {
self.due_at(Instant::now())
}
fn due_at(&mut self, now: Instant) -> bool {
if self.deadline.is_some_and(|deadline| now >= deadline) {
self.deadline = None;
return true;
}
false
}
}
/// A different call's code showing on a recording's browser page, and how
/// long it has shown.
struct ChangedCall {
code: String,
run: Run,
/// It already asked the controller to stop this recording; the same code
/// never asks twice.
reported: bool,
}
/// Stop when the call ends in a browser that never releases the
/// microphone between calls (Arc, #1775): the call code the recording started
/// with, and another code showing on the same browser's page for
/// [`CALL_CHANGED_AFTER`], so one recording does not span two calls. It never
/// stops anything; the watcher owns the grace.
///
/// The page read is the browser's active tab (with Automation), else the
/// first call page found in any of its windows, so a tab that names no call
/// (`None`) never ends a recording: only a page that positively names a
/// different call does. `None` neither starts nor clears a run; the recorded
/// code showing again does.
#[derive(Default)]
pub struct CallSwitch {
/// The call's code when its recording started; without one nothing is
/// watched.
recorded: Option<String>,
/// A different code showing since.
changed: Option<ChangedCall>,
/// A different code the user kept recording through: it never ends this
/// recording.
kept: Option<String>,
}
impl CallSwitch {
pub fn new(recorded: Option<String>) -> Self {
Self {
recorded,
..Self::default()
}
}
/// A recording detected in place of another call's stopped one: the code
/// the page kept naming is already kept, so it never ends this recording.
pub fn with_kept(recorded: Option<String>, kept: Option<String>) -> Self {
Self {
kept,
..Self::new(recorded)
}
}
/// The code the recording started with, when its page named one.
pub fn recorded(&self) -> Option<&str> {
self.recorded.as_deref()
}
/// Whether a different call's code is showing: its count is under way, or
/// it already asked to stop.
pub fn changed(&self) -> bool {
self.changed.is_some()
}
/// The page names `current` (the browser's call code: its active tab with
/// Automation, else the first call page found in any of its windows;
/// `None` when none names one): true once, when another code has shown for
/// [`CALL_CHANGED_AFTER`].
pub fn observe(&mut self, current: Option<&str>) -> bool {
self.observe_at(current, Instant::now())
}
/// [`Self::observe`] at `now`; the watcher's tests drive it on their own
/// clock.
pub(crate) fn observe_at(&mut self, current: Option<&str>, now: Instant) -> bool {
let Some(current) = current else {
return false;
};
let Some(recorded) = self.recorded.as_deref() else {
return false;
};
if current == recorded || self.kept.as_deref() == Some(current) {
self.changed = None;
return false;
}
if self
.changed
.as_ref()
.is_none_or(|changed| changed.code != current)
{
self.changed = Some(ChangedCall {
code: current.to_string(),
run: Run::start(now),
reported: false,
});
}
let Some(changed) = self.changed.as_mut() else {
return false;
};
if changed.reported {
return false;
}
if changed.run.extend(now) >= CALL_CHANGED_AFTER {
changed.reported = true;
return true;
}
false
}
/// Keep recording: the different code showing now never ends this
/// recording, however long it stays. Called with nothing counting (the
/// capsule's Keep recording after a microphone release), it forgets
/// nothing: an earlier kept code still never ends this recording.
pub fn keep(&mut self) {
if let Some(changed) = self.changed.take() {
self.kept = Some(changed.code);
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MeetingSource {
Manual,
GoogleMeet,
Banshee,
Zoom,
Teams,
SlackHuddle,
FaceTime,
WhatsApp,
Gather,
Discord,
BrowserCall,
// After `BrowserCall` so its generated key stays the same as before
// these were added.
Signal,
Telegram,
WeChat,
Line,
Webex,
Element,
Phone,
GoogleVoice,
Messenger,
GroupMe,
}
impl MeetingSource {
pub fn label(self) -> &'static str {
match self {
Self::Manual => "Meeting",
Self::GoogleMeet => "Google Meet",
Self::Banshee => "Banshee",
Self::Zoom => "Zoom",
Self::Teams => "Microsoft Teams",
Self::SlackHuddle => "Slack Huddle",
Self::FaceTime => "FaceTime",
Self::WhatsApp => "WhatsApp",
Self::Gather => "Gather",
Self::Discord => "Discord",
Self::BrowserCall => "Browser call",
Self::Signal => "Signal",
Self::Telegram => "Telegram",
Self::WeChat => "WeChat",
Self::Line => "LINE",
Self::Webex => "Webex",
Self::Element => "Element",
Self::Phone => "Phone",
Self::GoogleVoice => "Google Voice",
Self::Messenger => "Messenger",
Self::GroupMe => "GroupMe",
}
}
}
/// The call `context` describes.
pub fn candidate_from_context(context: &ContextSnapshot) -> Option<MeetingCandidate> {
let application = context.application.as_deref()?;
let application_lower = application.to_ascii_lowercase();
let title = context.window_title.as_deref().unwrap_or("").trim();
let title_lower = title.to_ascii_lowercase();
if is_browser_application(&application_lower)
&& let Some(url) = &context.browser_url
&& is_google_meet_call(url)
{
let mut meet = named(candidate(
MeetingSource::GoogleMeet,
url.as_str(),
&clean_google_meet_title(title),
));
meet.call = google_meet_url_code(url);
return Some(meet);
}
// No page address (Automation off, no web area behind another app): a
// Meet call's tab title still names it (#827).
if is_browser_application(&application_lower)
&& context.browser_url.is_none()
&& is_google_meet_call_title(title)
{
let mut meet = named(candidate(
MeetingSource::GoogleMeet,
&format!("{GOOGLE_MEET_SITE} {title}"),
&clean_google_meet_title(title),
));
meet.site = Some(GOOGLE_MEET_SITE.into());
meet.call = google_meet_title_code(title);
return Some(meet);
}
if is_browser_application(&application_lower)
&& let Some(host) = context.browser_host()
&& let Some(source) = call_site_source(host)
{
return Some(candidate(source, &call_site_host(host), ""));
}
if application_lower.contains("zoom")
&& (title_lower.contains("meeting") || title_lower.contains("webinar"))
{
return Some(candidate(MeetingSource::Zoom, title, title));
}
if application_lower.contains("teams")
&& (title_lower.contains("meeting") || title_lower.contains("call"))
{
return Some(candidate(MeetingSource::Teams, title, title));
}
if application_lower == "slack" && title_lower.contains("huddle") {
return Some(candidate(MeetingSource::SlackHuddle, title, title));
}
if application_lower == "facetime" && !title.is_empty() && title_lower != "facetime" {
return Some(named(candidate(MeetingSource::FaceTime, title, title)));
}
None
}
fn named(mut candidate: MeetingCandidate) -> MeetingCandidate {
candidate.named = candidate.title != candidate.source.label();
candidate
}
pub fn candidate_from_microphone(
application: &ActiveMicrophoneApplication,
context: &ContextSnapshot,
) -> Option<MeetingCandidate> {
let canonical_bundle = application.canonical_bundle_id()?;
let application_name = crate::microphone_activity::canonical_application_name(canonical_bundle)
.map(str::to_string)
.unwrap_or_else(|| application.name.clone());
if application.is_browser() {
let matches = context_matches_browser(canonical_bundle, &application.name, context);
let site = matches
.then(|| context.browser_host())
.flatten()
.map(call_site_host);
if matches && let Some(mut candidate) = candidate_from_context(context) {
candidate.application = Some(canonical_bundle.into());
candidate.application_name = Some(application_name);
candidate.site = site.or(candidate.site.take());
return Some(candidate);
}
let mut candidate = candidate(MeetingSource::BrowserCall, canonical_bundle, "");
candidate.application = Some(canonical_bundle.into());
candidate.application_name = Some(application_name);
candidate.site = site;
return Some(candidate);
}
let (source, title) = match canonical_bundle {
"us.zoom.xos" => (MeetingSource::Zoom, "Zoom meeting"),
"com.microsoft.teams2" => (MeetingSource::Teams, "Microsoft Teams meeting"),
"com.tinyspeck.slackmacgap" => (MeetingSource::SlackHuddle, "Slack huddle"),
"com.apple.FaceTime" => (MeetingSource::FaceTime, "FaceTime call"),
"net.whatsapp.WhatsApp" => (MeetingSource::WhatsApp, "WhatsApp call"),
"com.gather.GatherV2" => (MeetingSource::Gather, "Gather call"),
"com.hnc.Discord" => (MeetingSource::Discord, "Discord call"),
_ => more_call_app(canonical_bundle)?,
};
let mut candidate = candidate(source, canonical_bundle, title);
candidate.application = Some(canonical_bundle.into());
candidate.application_name = Some(application_name);
Some(candidate)
}
/// The other call apps Hex knows, and the title each call gets.
fn more_call_app(canonical_bundle: &str) -> Option<(MeetingSource, &'static str)> {
Some(match canonical_bundle {
"org.whispersystems.signal-desktop" => (MeetingSource::Signal, "Signal call"),
"ru.keepcoder.Telegram" => (MeetingSource::Telegram, "Telegram call"),
"com.tencent.xinWeChat" => (MeetingSource::WeChat, "WeChat call"),
"jp.naver.line.mac" => (MeetingSource::Line, "LINE call"),
"com.webex.meetingmanager" => (MeetingSource::Webex, "Webex meeting"),
"im.riot.app" => (MeetingSource::Element, "Element call"),
"com.apple.mobilephone" => (MeetingSource::Phone, "Phone call"),
_ => return None,
})
}
/// A website host as a site key: lowercase, without `www.`, and a Webex
/// subdomain as `webex.com`, so one choice covers every Webex page.
fn call_site_host(host: &str) -> String {
let host = site_host(host);
if is_webex_host(&host) {
"webex.com".into()
} else {
host
}
}
/// Whether a website host is Webex's: `webex.com` or any of its subdomains.
fn is_webex_host(host: &str) -> bool {
host == "webex.com" || host.ends_with(".webex.com")
}
/// A website host as a site key: lowercase, without `www.`.
fn site_host(host: &str) -> String {
host.strip_prefix("www.")
.unwrap_or(host)
.to_ascii_lowercase()
}
/// The call service a browser page on `host` belongs to, if it is a known
/// call site.
fn call_site_source(host: &str) -> Option<MeetingSource> {
let host = site_host(host);
if let Some((_, source)) = CALL_SITES.iter().find(|(known, _)| *known == host) {
return Some(*source);
}
if is_webex_host(&host) {
return Some(MeetingSource::Webex);
}
MORE_CALL_SITES
.iter()
.find(|(known, _, _)| *known == host)
.map(|(_, source, _)| *source)
}
/// The last page a browser showed while it held the microphone. The call's
/// site is read from the browser's tabs, which fails now and then (a timed
/// out AppleScript, a browser without an adapter); the page seen a moment
/// ago still names the call, but not one seen minutes ago.
#[derive(Default)]
pub struct BrowserPageMemory {
last: Option<(&'static str, ContextSnapshot, Instant)>,
}
impl BrowserPageMemory {
/// The page to judge `application`'s call by: `context` when it shows the
/// browser's page, else the page remembered from the last 60 s.
pub fn recall(
&mut self,
application: &ActiveMicrophoneApplication,
context: ContextSnapshot,
now: Instant,
) -> ContextSnapshot {
let Some(bundle) = application
.canonical_bundle_id()
.filter(|_| application.is_browser())
else {
self.last = None;
return context;
};
if context_matches_browser(bundle, &application.name, &context)
&& context.browser_url.is_some()
{
self.last = Some((bundle, context.clone(), now));
return context;
}
match &self.last {
Some((remembered, page, at))
if *remembered == bundle
&& now.saturating_duration_since(*at) <= BROWSER_PAGE_MEMORY =>
{
page.clone()
}
_ => context,
}
}
/// No browser holds the microphone, so the next call starts fresh.
pub fn forget(&mut self) {
self.last = None;
}
}
fn is_browser_application(application_lower: &str) -> bool {
// Arc's, Aside's, and Zen's names are too short or too common to match as
// a substring: `Citizen`, `Seaside`, and `Zendesk` are not browsers.
application_lower == "arc"
|| application_lower == "aside"
|| application_lower == "zen"
|| application_lower.starts_with("zen ")
|| [
"brave", "chrome", "safari", "firefox", "edge", "vivaldi", "helium", "ardia",
]
.iter()
.any(|browser| application_lower.contains(browser))
}
fn context_matches_browser(
canonical_bundle: &str,
application_name: &str,
context: &ContextSnapshot,
) -> bool {
let Some(foreground) = context.application.as_deref() else {
return false;
};
if foreground.contains(application_name) || application_name.contains(foreground) {
return true;
}
let lower = foreground.to_ascii_lowercase();
match canonical_bundle {
"com.brave.Browser" => lower.contains("brave"),
"com.google.Chrome" => lower.contains("chrome"),
"com.apple.Safari" => lower.contains("safari"),
"org.mozilla.firefox" => lower.contains("firefox"),
"com.microsoft.edgemac" => lower.contains("edge"),
"company.thebrowser.Browser" => lower == "arc",
"at.studio.AsideBrowser" => lower == "aside",
"com.vivaldi.Vivaldi" => lower.contains("vivaldi"),
"net.imput.helium" => lower.contains("helium"),
"com.ardia.browser" => lower.contains("ardia"),
"app.zen-browser.zen" => lower == "zen" || lower.starts_with("zen "),
_ => false,
}
}
fn candidate(source: MeetingSource, identity: &str, title: &str) -> MeetingCandidate {
MeetingCandidate {
key: opaque_key(source, identity),
title: if title.is_empty() {
source.label().into()
} else {
title.into()
},
source,
application: None,
named: false,
site: None,
application_name: None,
call: None,
}
}
fn opaque_key(source: MeetingSource, identity: &str) -> String {
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
identity.hash(&mut hasher);
format!("meeting-{:016x}", hasher.finish())
}
/// The page that names the call a browser shows, from every page its windows
/// expose: the first address that names a call, else the first address, and a
/// title that names a call outranks another window's address. Windows makes
/// this choice over its browsers' UI Automation pages; macOS makes the same
/// one over its Accessibility pages.
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub fn call_page(pages: &BrowserPages) -> (Option<Url>, Option<String>) {
let (url, title) = crate::dictation::browser_page::choose(
pages,
|url| web_url(url).is_some_and(|url| names_call(&url)),
title_names_call,
);
let title_names_call = title.as_deref().is_some_and(title_names_call);
let url = url
.as_deref()
.and_then(web_url)
.filter(|url| names_call(url) || !title_names_call);
(url, title)
}
/// An http(s) page address.
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
fn web_url(url: &str) -> Option<Url> {
Url::parse(url)
.ok()
.filter(|url| matches!(url.scheme(), "http" | "https"))
}
/// Whether a page address is a call this detection knows: a Google Meet
/// call or another known call site.
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub fn names_call(url: &Url) -> bool {
is_google_meet_call(url) || url.host_str().and_then(call_site_source).is_some()
}
/// Whether a browser tab's title names a call (a Google Meet call's).
#[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
pub fn title_names_call(title: &str) -> bool {
is_google_meet_call_title(title)
}
fn is_google_meet_call(url: &Url) -> bool {
if url.host_str() != Some("meet.google.com") {
return false;
}
let path = url.path().trim_matches('/');
!path.is_empty() && path != "landing" && path != "new"
}
const GOOGLE_MEET_SITE: &str = "meet.google.com";
pub(crate) const BROWSER_TITLE_SUFFIXES: &[&str] = &[
" - Audio playing - Brave",
" - Brave",
" - Brave Browser",
" - Google Chrome",
" — Mozilla Firefox",
" - Microsoft Edge",
" - Safari",
" - Arc",
" - Aside",
" - Vivaldi",
" - Helium",
" - Ardia",
// Zen is Firefox-based: titles are `<page> — <brandFullName>` on Windows
// and Linux, with no brand suffix on macOS.
" — Zen Browser",
" — Zen Twilight",
];
const GOOGLE_MEET_TITLE_PREFIXES: [&str; 3] = ["Meet - ", "Meet – ", "Meet — "];
/// Whether a browser tab title is a Google Meet call's ("Meet - abc-defg-hij",
/// "Meet – Design sync"); the landing page is just "Google Meet".
fn is_google_meet_call_title(title: &str) -> bool {
let cleaned = clean_title(title, BROWSER_TITLE_SUFFIXES);
GOOGLE_MEET_TITLE_PREFIXES.into_iter().any(|prefix| {
cleaned
.strip_prefix(prefix)
.is_some_and(|rest| !rest.trim().is_empty())
})
}
fn clean_google_meet_title(title: &str) -> String {
let mut cleaned = clean_title(title, BROWSER_TITLE_SUFFIXES);
cleaned = clean_title(cleaned, &[" - Google Meet", " | Google Meet"]);
for prefix in GOOGLE_MEET_TITLE_PREFIXES {
if let Some(rest) = cleaned.strip_prefix(prefix) {
cleaned = rest.trim();
break;
}
}
if cleaned.eq_ignore_ascii_case("Google Meet")
|| cleaned.eq_ignore_ascii_case("Meet")
|| is_google_meet_code(cleaned)
{
return String::new();
}
cleaned.to_string()
}
/// A Meet call's code from its address: the path segment shaped like one, so
/// query strings (`?authuser=0`) and the `/meet/` prefix Arc adds when it
/// opens a Calendar link never make one call look like two. A nickname path
/// without a code-shaped segment (`/team-sync`) is its own code, as before.
fn google_meet_url_code(url: &Url) -> Option<String> {
let mut fallback = None;
for segment in url.path_segments()?.map(str::to_ascii_lowercase) {
if segment.is_empty() {
continue;
}
if is_google_meet_code(&segment) {
return Some(segment);
}
fallback.get_or_insert(segment);
}
fallback
}
/// A Meet call's code from a tab title that shows only the code
/// ("Meet - abc-defg-hij"); a named title carries none.
fn google_meet_title_code(title: &str) -> Option<String> {
let cleaned = clean_title(title, BROWSER_TITLE_SUFFIXES);
GOOGLE_MEET_TITLE_PREFIXES.into_iter().find_map(|prefix| {
let rest = cleaned.strip_prefix(prefix)?.trim();
is_google_meet_code(rest).then(|| rest.to_string())
})
}
fn is_google_meet_code(value: &str) -> bool {
let mut parts = value.split('-');
let (Some(first), Some(second), Some(third), None) =
(parts.next(), parts.next(), parts.next(), parts.next())
else {
return false;
};
first.len() == 3
&& second.len() == 4
&& third.len() == 3
&& [first, second, third]
.into_iter()
.all(|part| part.bytes().all(|byte| byte.is_ascii_lowercase()))
}
pub(crate) fn clean_title<'a>(title: &'a str, suffixes: &[&str]) -> &'a str {
suffixes
.iter()
.find_map(|suffix| title.strip_suffix(suffix))
.unwrap_or(title)
.trim()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::meeting::window_title::normalize_window_title;
fn context(application: &str, url: Option<&str>, title: &str) -> ContextSnapshot {
ContextSnapshot {
application: Some(application.into()),
browser_url: url.map(|url| Url::parse(url).unwrap()),
window_title: Some(title.into()),
input_revision: None,
screen_terms: Vec::new(),
caret_text: None,
bundle_name: None,
}
}
const STEP_MS: u64 = 500;
/// A detector fed on a fake monotonic clock, like the watcher loop does.
struct Watch {
detector: MeetingDetector,
base: Instant,
elapsed_ms: u64,
wall_ms: i64,
/// The answer for calls without their own.
default: MeetingPolicy,
}
impl Watch {
fn new() -> Self {
Self {
detector: MeetingDetector::default(),
base: Instant::now(),
elapsed_ms: 0,
wall_ms: 0,
default: MeetingPolicy::Ask,
}
}
/// Observes `candidate` every `step_ms` for `ms`, the first at the
/// current time, and returns each detection with the time it came.
fn run_every(
&mut self,
candidate: Option<&MeetingCandidate>,
choices: &[MeetingProviderChoice],
ms: u64,
step_ms: u64,
) -> Vec<(u64, Detection)> {
let mut found = Vec::new();
let end = self.elapsed_ms + ms;
while self.elapsed_ms < end {
let now = self.base + Duration::from_millis(self.elapsed_ms);
if let Some(detection) = self.detector.detect_at(
candidate.cloned(),
choices,
self.default,
now,
self.wall_ms,
) {
found.push((self.elapsed_ms, detection));
}
self.elapsed_ms += step_ms;
}
found
}
fn run(
&mut self,
candidate: Option<&MeetingCandidate>,
choices: &[MeetingProviderChoice],
ms: u64,
) -> Vec<(u64, Detection)> {
self.run_every(candidate, choices, ms, STEP_MS)
}
/// Time passes with no observation, as when the Mac sleeps.
fn skip(&mut self, ms: u64) {
self.elapsed_ms += ms;
}
/// The detector's clock, as `detect_at` sees it.
fn now(&self) -> Instant {
self.base + Duration::from_millis(self.elapsed_ms)
}
/// The page named `candidate` just before a call-switch stop.
fn switch_to(&mut self, ended: Option<String>, candidate: MeetingCandidate) {
let now = self.now();
self.detector.call_switched_at(ended, candidate, now);
}
/// Observes until the candidate has been stable for just under its
/// threshold, then once more; returns what that last observation
/// detected.
fn stable(
&mut self,
candidate: &MeetingCandidate,
choices: &[MeetingProviderChoice],
) -> Option<Detection> {
let required = candidate.required_stability().as_millis() as u64;
assert!(self.run(Some(candidate), choices, required).is_empty());
self.run(Some(candidate), choices, STEP_MS)
.pop()
.map(|(_, detection)| detection)
}
/// Off the microphone for longer than the snooze.
fn leave(&mut self) {
let ms = ABSENT_FOR_RESET.as_millis() as u64 + STEP_MS;
assert!(self.run(None, &[], ms).is_empty());
}
}
/// When each detection came, asserting every one is an offer.
fn offered(found: &[(u64, Detection)]) -> Vec<u64> {
found
.iter()
.map(|(at, detection)| {
assert!(matches!(detection, Detection::Offer(_)), "{detection:?}");
*at
})
.collect()
}
#[test]
fn recognizes_calls_without_treating_landing_pages_as_meetings() {
assert!(
candidate_from_context(&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet"
))
.is_some()
);
assert!(
candidate_from_context(&context(
"Brave Browser",
Some("https://meet.google.com/landing"),
"Google Meet"
))
.is_none()
);
assert!(candidate_from_context(&context("zoom.us", None, "Zoom Workplace")).is_none());
assert!(candidate_from_context(&context("zoom.us", None, "Zoom Meeting")).is_some());
let banshee = candidate_from_context(&context(
"Brave Browser",
Some("https://www.banshee.app/room-1"),
"Room 1",
))
.unwrap();
assert_eq!(banshee.source, MeetingSource::Banshee);
assert!(
candidate_from_context(&context(
"Brave Browser",
Some("https://notbanshee.app/"),
"Not Banshee",
))
.is_none()
);
}
#[test]
fn gather_calls_are_detected_in_its_app_and_browser() {
for bundle_id in [
"com.gather.GatherV2",
"com.gather.GatherV2.helper.Renderer",
"com.gather.Gather",
"Gather.exe",
] {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: bundle_id.into(),
name: "Gather".into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default())
.unwrap_or_else(|| panic!("{bundle_id} is Gather"));
assert_eq!(call.source, MeetingSource::Gather);
assert_eq!(call.application.as_deref(), Some("com.gather.GatherV2"));
// Sitting in a space can hold the microphone, so about 20 s.
assert_eq!(call.required_stability(), BROWSER_STABLE_FOR);
}
for url in [
"https://app.v2.gather.town/space/abc",
"https://app.gather.town/app/x",
] {
let web =
candidate_from_microphone(&brave(), &context("Brave Browser", Some(url), "Gather"))
.unwrap();
assert_eq!(web.source, MeetingSource::Gather, "{url}");
}
assert_eq!(known_provider_label("com.gather.GatherV2"), Some("Gather"));
}
#[test]
fn discord_calls_are_detected_on_every_desktop() {
for bundle_id in [
"com.hnc.Discord",
"com.hnc.Discord.helper",
"com.hnc.Discord.helper.Renderer",
"com.hnc.DiscordCanary",
"com.hnc.DiscordPTB",
"Discord.exe",
"DiscordCanary.exe",
"DiscordPTB.exe",
"Discord",
"discord",
"discord-canary",
"com.discordapp.Discord",
] {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: bundle_id.into(),
name: "Discord".into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default())
.unwrap_or_else(|| panic!("{bundle_id} is Discord"));
assert_eq!(call.source, MeetingSource::Discord);
assert_eq!(call.application.as_deref(), Some("com.hnc.Discord"));
assert_eq!(call.required_stability(), STABLE_FOR);
}
assert_eq!(known_provider_label("com.hnc.Discord"), Some("Discord"));
}
#[test]
fn whatsapp_calls_are_detected_after_a_voice_note_would_have_ended() {
let mut offers = Vec::new();
for (bundle_id, name) in [
("net.whatsapp.WhatsApp", "WhatsApp"),
("desktop.WhatsApp", "WhatsApp"),
("WhatsApp.Root.exe", "WhatsApp"),
("whatsapp.exe", "WhatsApp"),
] {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: bundle_id.into(),
name: name.into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default())
.unwrap_or_else(|| panic!("{bundle_id} is WhatsApp"));
assert_eq!(call.source, MeetingSource::WhatsApp);
assert_eq!(call.application.as_deref(), Some("net.whatsapp.WhatsApp"));
offers.push(call);
}
// Voice notes hold the microphone too, so it takes about 20 s.
let mut watch = Watch::new();
assert!(watch.run(Some(&offers[0]), &[], 19_500).is_empty());
assert_eq!(watch.run(Some(&offers[0]), &[], 1_000).len(), 1);
let web = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://web.whatsapp.com/"),
"WhatsApp",
),
)
.unwrap();
assert_eq!(web.source, MeetingSource::WhatsApp);
assert_eq!(web.required_stability(), BROWSER_STABLE_FOR);
assert_eq!(
known_provider_label("net.whatsapp.WhatsApp"),
Some("WhatsApp")
);
}
#[test]
fn more_call_apps_are_named_with_their_websites() {
for (bundle_id, source, canonical, title, stability) in [
(
"org.whispersystems.signal-desktop",
MeetingSource::Signal,
"org.whispersystems.signal-desktop",
"Signal call",
BROWSER_STABLE_FOR,
),
(
"org.whispersystems.signal-desktop.helper.Renderer",
MeetingSource::Signal,
"org.whispersystems.signal-desktop",
"Signal call",
BROWSER_STABLE_FOR,
),
(
"ru.keepcoder.Telegram",
MeetingSource::Telegram,
"ru.keepcoder.Telegram",
"Telegram call",
BROWSER_STABLE_FOR,
),
(
"com.tdesktop.Telegram",
MeetingSource::Telegram,
"ru.keepcoder.Telegram",
"Telegram call",
BROWSER_STABLE_FOR,
),
(
"org.telegram.desktop",
MeetingSource::Telegram,
"ru.keepcoder.Telegram",
"Telegram call",
BROWSER_STABLE_FOR,
),
(
"com.tencent.xinWeChat",
MeetingSource::WeChat,
"com.tencent.xinWeChat",
"WeChat call",
BROWSER_STABLE_FOR,
),
(
"jp.naver.line.mac",
MeetingSource::Line,
"jp.naver.line.mac",
"LINE call",
BROWSER_STABLE_FOR,
),
(
"com.webex.meetingmanager",
MeetingSource::Webex,
"com.webex.meetingmanager",
"Webex meeting",
STABLE_FOR,
),
(
"com.cisco.webexmeetingsapp",
MeetingSource::Webex,
"com.webex.meetingmanager",
"Webex meeting",
STABLE_FOR,
),
(
"im.riot.app",
MeetingSource::Element,
"im.riot.app",
"Element call",
BROWSER_STABLE_FOR,
),
(
"com.apple.mobilephone",
MeetingSource::Phone,
"com.apple.mobilephone",
"Phone call",
STABLE_FOR,
),
] {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: bundle_id.into(),
name: "Call".into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default())
.unwrap_or_else(|| panic!("{bundle_id} is a call app"));
assert_eq!(call.source, source, "{bundle_id}");
assert_eq!(call.application.as_deref(), Some(canonical), "{bundle_id}");
assert_eq!(call.title, title, "{bundle_id}");
assert_eq!(call.required_stability(), stability, "{bundle_id}");
}
for (url, source, site) in [
(
"https://voice.google.com/u/0/calls",
MeetingSource::GoogleVoice,
"voice.google.com",
),
(
"https://www.messenger.com/t/ana",
MeetingSource::Messenger,
"messenger.com",
),
(
"https://facetime.apple.com/join/abc",
MeetingSource::FaceTime,
"facetime.apple.com",
),
(
"https://web.groupme.com/chats/1",
MeetingSource::GroupMe,
"web.groupme.com",
),
(
"https://meetings.webex.com/mw",
MeetingSource::Webex,
"webex.com",
),
(
"https://www.webex.com/meet",
MeetingSource::Webex,
"webex.com",
),
] {
let page = context("Brave Browser", Some(url), "A call");
let call = candidate_from_microphone(&brave(), &page).unwrap();
assert_eq!(call.source, source, "{url}");
assert_eq!(call.site.as_deref(), Some(site), "{url}");
}
}
#[test]
fn a_telegram_voice_note_is_not_offered_but_a_sustained_call_is() {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: "ru.keepcoder.Telegram".into(),
name: "Telegram".into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default())
.expect("Telegram is a call app");
let mut watch = Watch::new();
assert!(watch.run(Some(&call), &[], 19_500).is_empty());
assert_eq!(watch.run(Some(&call), &[], 1_000).len(), 1);
}
#[test]
fn a_messenger_voice_clip_is_not_offered_but_a_sustained_call_is() {
let page = context(
"Brave Browser",
Some("https://www.messenger.com/t/ana"),
"Messenger",
);
let call = candidate_from_microphone(&brave(), &page).unwrap();
assert_eq!(call.source, MeetingSource::Messenger);
// A voice clip holds the microphone too, so it takes about 20 s.
let mut watch = Watch::new();
assert!(watch.run(Some(&call), &[], 19_500).is_empty());
assert_eq!(watch.run(Some(&call), &[], 1_000).len(), 1);
// GroupMe sends voice clips as well.
let groupme = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://web.groupme.com/chats/1"),
"GroupMe",
),
)
.unwrap();
assert_eq!(groupme.source, MeetingSource::GroupMe);
assert_eq!(groupme.required_stability(), BROWSER_STABLE_FOR);
assert_eq!(call.required_stability(), BROWSER_STABLE_FOR);
}
#[test]
fn teams_pages_use_the_teams_apps_provider_and_facetime_its_own() {
let teams = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://teams.live.com/meet/123"),
"A call",
),
)
.unwrap();
assert_eq!(teams.source, MeetingSource::Teams);
// The page is named after the service and shares the app's provider.
assert_eq!(teams.offer_title(), "Microsoft Teams call detected");
assert_eq!(teams.site.as_deref(), Some("teams.live.com"));
let provider = teams.provider().unwrap();
assert_eq!(provider.key, "com.microsoft.teams2");
assert_eq!(provider.label, "Microsoft Teams");
// The Teams app's choice covers its pages.
let app = MeetingProviderChoice {
key: "com.microsoft.teams2".into(),
label: "Microsoft Teams".into(),
policy: MeetingPolicy::AlwaysRecord,
};
assert_eq!(
teams.policy(std::slice::from_ref(&app), MeetingPolicy::Ask),
MeetingPolicy::AlwaysRecord
);
// A choice other than Ask saved for the page's own host before pages
// shared the app's key still applies, and wins over the app's.
let web = MeetingProviderChoice {
key: "teams.live.com".into(),
label: "Teams (web)".into(),
policy: MeetingPolicy::NeverOffer,
};
assert_eq!(
teams.policy(std::slice::from_ref(&web), MeetingPolicy::Ask),
MeetingPolicy::NeverOffer
);
assert_eq!(
teams.policy(&[app.clone(), web], MeetingPolicy::Ask),
MeetingPolicy::NeverOffer,
"the page's own saved choice wins"
);
// Ask is stored as a real entry; an older host row left on Ask does
// not shadow the app's choice.
let asked_web = MeetingProviderChoice {
key: "teams.live.com".into(),
label: "Teams (web)".into(),
policy: MeetingPolicy::Ask,
};
assert_eq!(
teams.policy(&[app, asked_web], MeetingPolicy::Ask),
MeetingPolicy::AlwaysRecord
);
let facetime = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://facetime.apple.com/join/abc"),
"A call",
),
)
.unwrap();
assert_eq!(facetime.source, MeetingSource::FaceTime);
assert_eq!(facetime.offer_title(), "FaceTime call detected");
let provider = facetime.provider().unwrap();
assert_eq!(provider.key, "facetime.apple.com");
assert_eq!(provider.label, "FaceTime (web)");
assert_eq!(
known_provider_label("teams.live.com"),
known_provider_label("com.microsoft.teams2"),
"Teams pages and the app share one name"
);
assert_ne!(
known_provider_label("facetime.apple.com"),
known_provider_label("com.apple.FaceTime")
);
}
#[test]
fn teams_web_calls_are_the_teams_service() {
let always = MeetingProviderChoice {
key: "com.microsoft.teams2".into(),
label: "Microsoft Teams".into(),
policy: MeetingPolicy::AlwaysRecord,
};
for (url, host) in [
(
"https://teams.microsoft.com/meet/123?p=abc",
"teams.microsoft.com",
),
(
"https://teams.microsoft.com/v2/?meetingjoin=true#/meet/123?anon=true",
"teams.microsoft.com",
),
("https://teams.microsoft.com/v2/", "teams.microsoft.com"),
("https://teams.cloud.microsoft/v2/", "teams.cloud.microsoft"),
] {
let call = candidate_from_microphone(&arc(), &context("Arc", Some(url), "Weekly sync"))
.unwrap_or_else(|| panic!("{url} is a Teams call"));
assert_eq!(call.source, MeetingSource::Teams, "{url}");
assert_eq!(call.site.as_deref(), Some(host), "{url}");
assert_eq!(call.required_stability(), STABLE_FOR, "{url}");
let provider = call.provider().unwrap();
assert_eq!(provider.key, "com.microsoft.teams2", "{url}");
assert_eq!(provider.label, "Microsoft Teams", "{url}");
assert_eq!(
call.policy(std::slice::from_ref(&always), MeetingPolicy::Ask),
MeetingPolicy::AlwaysRecord,
"{url} follows the Teams app's choice"
);
assert!(names_call(&Url::parse(url).unwrap()), "{url} names a call");
}
}
#[test]
fn requires_stability_and_offers_once_per_session() {
let candidate = candidate_from_context(&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet",
))
.unwrap();
let mut watch = Watch::new();
assert!(watch.run(Some(&candidate), &[], 2_000).is_empty());
assert_eq!(offered(&watch.run(Some(&candidate), &[], 500)), [2_000]);
assert!(watch.run(Some(&candidate), &[], 30_000).is_empty());
// A minute off the microphone ends the call.
assert!(watch.run(None, &[], 59_000).is_empty());
assert!(watch.run(Some(&candidate), &[], 2_500).is_empty());
watch.run(None, &[], 61_000);
assert!(watch.run(Some(&candidate), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&candidate), &[], 500).len(), 1);
}
#[test]
fn a_brief_microphone_release_does_not_re_offer_the_same_call() {
let candidate = candidate(MeetingSource::Zoom, "us.zoom.xos", "Zoom meeting");
let mut watch = Watch::new();
assert!(watch.run(Some(&candidate), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&candidate), &[], 500).len(), 1);
// Dismissed or ignored: twenty seconds off the microphone keeps it snoozed.
assert!(watch.run(None, &[], 20_000).is_empty());
assert!(watch.run(Some(&candidate), &[], 5_000).is_empty());
}
#[test]
fn an_open_offer_ends_with_its_call_or_a_replacement_detection() {
let slack = slack_huddle();
let zoom = zoom();
let mut watch = Watch::new();
assert!(watch.stable(&slack, &[]).is_some());
let first = watch.detector.open_offer().unwrap();
assert!(watch.run(Some(&slack), &[], 30_000).is_empty());
assert_eq!(watch.detector.open_offer(), Some(first));
assert!(watch.stable(&zoom, &[]).is_some());
let next = watch.detector.open_offer().unwrap();
assert_ne!(next, first);
watch.run(None, &[], 61_000);
assert_eq!(watch.detector.open_offer(), None);
}
#[test]
fn a_timed_out_offer_is_offered_again_when_the_call_retakes_the_microphone() {
// Google Meet's pre-join screen holds the microphone; nobody answers
// the offer before it times out (#1267).
let meet = candidate_from_context(&context(
"Arc",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync",
))
.unwrap();
let mut watch = Watch::new();
assert!(watch.stable(&meet, &[]).is_some());
assert!(watch.run(Some(&meet), &[], 30_000).is_empty());
let first = watch.detector.open_offer().unwrap();
watch.detector.offer_timed_out(first);
assert!(watch.run(Some(&meet), &[], 10_000).is_empty());
// Joining releases and retakes the microphone: offered again.
assert!(watch.run(None, &[], 500).is_empty());
assert_eq!(
watch.stable(&meet, &[]),
Some(Detection::Offer(meet.clone()))
);
// Even a late timeout for the first offer (identical call key)
// cannot shorten this offer's snooze.
assert_ne!(watch.detector.open_offer(), Some(first));
watch.detector.offer_timed_out(first);
assert!(watch.run(None, &[], 500).is_empty());
assert!(watch.run(Some(&meet), &[], 10_000).is_empty());
}
#[test]
fn named_meetings_keep_their_title_and_app_calls_use_the_app() {
let meet = candidate_from_context(&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet",
))
.unwrap();
let start = meet.recording_start();
assert_eq!(start.title.as_deref(), Some("Design sync"));
assert_eq!(start.app.as_deref(), Some("Google Meet"));
let zoom = candidate(MeetingSource::Zoom, "us.zoom.xos", "Zoom meeting").recording_start();
assert_eq!(zoom.title, None);
assert_eq!(zoom.app.as_deref(), Some("Zoom"));
let manual = MeetingCandidate::manual().recording_start();
assert_eq!((manual.title, manual.app), (None, None));
}
#[test]
fn call_end_is_reported_once_per_sustained_absence() {
let base = Instant::now();
let at = |ms: u64| base + Duration::from_millis(ms);
let mut presence = CallPresence::default();
assert!(!presence.observe_at(true, at(0)));
// A slow loop (900 ms an iteration) still ends the call after 3 s.
assert!(!presence.observe_at(false, at(900)));
assert!(!presence.observe_at(false, at(1_800)));
assert!(!presence.observe_at(false, at(2_700)));
assert!(presence.observe_at(false, at(3_900)));
assert!(!presence.observe_at(false, at(4_800)));
// Rejoining and leaving again asks again.
assert!(!presence.observe_at(true, at(5_000)));
assert!(!presence.observe_at(false, at(5_500)));
assert!(!presence.observe_at(false, at(8_000)));
assert!(presence.observe_at(false, at(8_500)));
}
#[test]
fn an_ended_call_stops_after_the_grace_unless_it_comes_back_or_is_kept() {
let base = Instant::now();
let at = |ms: u64| base + Duration::from_millis(ms);
let grace = AUTO_STOP_GRACE.as_millis() as u64;
let mut stop = AutoStop::default();
assert!(!stop.due_at(at(0)), "nothing is armed");
// The call ended: it stops once the grace runs out, once.
let mut presence = CallPresence::default();
assert!(!presence.observe_at(false, at(0)));
assert!(presence.observe_at(false, at(3_000)));
stop.arm_at(at(3_000));
assert!(!stop.due_at(at(3_000 + grace - 500)));
assert!(stop.due_at(at(3_000 + grace)));
assert!(!stop.due_at(at(3_000 + grace + 500)));
// The call app is back during the grace: nothing stops.
stop.arm_at(at(0));
assert!(!presence.observe_at(true, at(10_000)));
assert!(stop.disarm());
assert!(!stop.due_at(at(grace + 1_000)));
// Keep recording: this absence never stops it; leaving again re-arms.
stop.arm_at(at(0));
assert!(stop.disarm());
assert!(!stop.due_at(at(grace * 2)));
assert!(!presence.observe_at(false, at(20_000)));
assert!(!presence.observe_at(false, at(21_000)));
assert!(presence.observe_at(false, at(23_000)));
stop.arm_at(at(23_000));
assert!(stop.due_at(at(23_000 + grace)));
}
#[test]
fn classifies_helper_and_host_microphone_applications() {
let brave_helper = ActiveMicrophoneApplication {
pid: 2352,
bundle_id: "com.brave.Browser.helper".into(),
name: "Brave Browser Helper".into(),
};
let meet_context = context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync - Brave",
);
let meet = candidate_from_microphone(&brave_helper, &meet_context).unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.title, "Design sync");
assert!(meet.named);
assert_eq!(meet.application.as_deref(), Some("com.brave.Browser"));
let slack_helper = ActiveMicrophoneApplication {
pid: 31807,
bundle_id: "com.tinyspeck.slackmacgap.helper".into(),
name: "Slack Helper".into(),
};
let huddle = candidate_from_microphone(&slack_helper, &ContextSnapshot::default()).unwrap();
assert_eq!(huddle.source, MeetingSource::SlackHuddle);
assert_eq!(
huddle.application.as_deref(),
Some("com.tinyspeck.slackmacgap")
);
let zoom_helper = ActiveMicrophoneApplication {
pid: 9010,
bundle_id: "us.zoom.CptHost".into(),
name: "CptHost".into(),
};
let zoom = candidate_from_microphone(&zoom_helper, &ContextSnapshot::default()).unwrap();
assert_eq!(zoom.source, MeetingSource::Zoom);
assert_eq!(zoom.application.as_deref(), Some("us.zoom.xos"));
let safari_gpu = ActiveMicrophoneApplication {
pid: 7100,
bundle_id: "com.apple.WebKit.GPU".into(),
name: "Safari Graphics and Media".into(),
};
let browser = candidate_from_microphone(&safari_gpu, &ContextSnapshot::default()).unwrap();
assert_eq!(browser.source, MeetingSource::BrowserCall);
assert_eq!(browser.application.as_deref(), Some("com.apple.Safari"));
}
#[test]
fn cleans_google_meet_titles_and_ignores_raw_meet_codes() {
let code_only = candidate_from_context(&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - abc-defg-hij - Brave",
))
.unwrap();
assert_eq!(code_only.source, MeetingSource::GoogleMeet);
assert_eq!(code_only.title, "Google Meet");
assert!(!code_only.named);
assert_eq!(code_only.recording_start().title, None);
let named_prefix = candidate_from_context(&context(
"Google Chrome",
Some("https://meet.google.com/abc-defg-hij"),
"Meet – Weekly Architecture - Google Chrome",
))
.unwrap();
assert_eq!(named_prefix.title, "Weekly Architecture");
assert!(named_prefix.named);
let helium = candidate_from_context(&context(
"Helium",
Some("https://meet.google.com/abc-defg-hij"),
"Meet – Weekly Architecture - Helium",
))
.unwrap();
assert_eq!(helium.title, "Weekly Architecture");
assert!(helium.named);
let aside = candidate_from_context(&context(
"Aside",
Some("https://meet.google.com/abc-defg-hij"),
"Meet – Weekly Architecture - Aside",
))
.unwrap();
assert_eq!(aside.title, "Weekly Architecture");
assert!(aside.named);
}
#[test]
fn extracts_a_meet_code_from_a_meet_prefixed_address() {
let plain = Url::parse("https://meet.google.com/abc-defg-hij?authuser=0").unwrap();
assert_eq!(
google_meet_url_code(&plain).as_deref(),
Some("abc-defg-hij")
);
// Arc opens a Calendar link as `/meet/<code>?authuser=…`; the prefix
// is not the call's code.
let prefixed = Url::parse("https://meet.google.com/meet/abc-defg-hij?authuser=0").unwrap();
assert_eq!(
google_meet_url_code(&prefixed).as_deref(),
Some("abc-defg-hij")
);
// Uppercase still names the same code, and a fragment is ignored.
let uppercase = Url::parse("https://meet.google.com/meet/ABC-DEFG-HIJ#fragment").unwrap();
assert_eq!(
google_meet_url_code(&uppercase).as_deref(),
Some("abc-defg-hij")
);
// A nickname path without a code-shaped segment is its own code, as
// before; only a path with no segment at all names none.
for (address, code) in [
("https://meet.google.com/team-sync", Some("team-sync")),
("https://meet.google.com/lookup", Some("lookup")),
("https://meet.google.com/", None),
] {
assert_eq!(
google_meet_url_code(&Url::parse(address).unwrap()).as_deref(),
code,
"{address}"
);
}
}
#[test]
fn switching_tabs_during_a_snoozed_browser_call_does_not_re_offer() {
let brave = ActiveMicrophoneApplication {
pid: 1592,
bundle_id: "com.brave.Browser".into(),
name: "Brave Browser".into(),
};
let meet_context = context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet",
);
let meet = candidate_from_microphone(&brave, &meet_context).unwrap();
let fallback = candidate_from_microphone(&brave, &ContextSnapshot::default()).unwrap();
let mut watch = Watch::new();
assert!(watch.run(Some(&meet), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&meet), &[], 500).len(), 1);
assert!(watch.run(Some(&fallback), &[], 5_000).is_empty());
// A page that isn't a call, while the Meet call goes on, is still it.
let other = candidate_from_microphone(
&brave,
&context("Brave Browser", Some("https://mail.google.com/"), "Inbox"),
)
.unwrap();
assert!(watch.run(Some(&other), &[], 25_000).is_empty());
}
#[test]
fn a_windows_chrome_on_a_meet_page_is_a_meet_call() {
let chrome = ActiveMicrophoneApplication {
pid: 1234,
bundle_id: "chrome.exe".into(),
name: "chrome".into(),
};
let call = candidate_from_microphone(
&chrome,
&context(
"Google Chrome",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync - Google Chrome",
),
)
.unwrap();
assert_eq!(call.source, MeetingSource::GoogleMeet);
assert_eq!(call.site.as_deref(), Some("meet.google.com"));
assert_eq!(call.recording_start().app.as_deref(), Some("Google Meet"));
}
#[test]
fn a_windows_edge_on_a_meet_title_without_its_address_is_a_meet_call() {
let edge = ActiveMicrophoneApplication {
pid: 1234,
bundle_id: "msedge.exe".into(),
name: "msedge".into(),
};
let title = normalize_window_title(
"Meet - abc-defg-hij and 2 more pages - Personal - Microsoft\u{200b} Edge",
);
let call =
candidate_from_microphone(&edge, &context("Microsoft Edge", None, &title)).unwrap();
assert_eq!(call.source, MeetingSource::GoogleMeet);
assert_eq!(call.site.as_deref(), Some("meet.google.com"));
assert_eq!(call.call.as_deref(), Some("abc-defg-hij"));
}
#[test]
fn windows_window_titles_lose_the_browser_profile_and_other_tabs() {
let edge = normalize_window_title(
"Meet - abc-defg-hij and 2 more pages - Personal - Microsoft\u{200b} Edge",
);
assert_eq!(edge, "Meet - abc-defg-hij");
assert_eq!(
google_meet_title_code(&edge).as_deref(),
Some("abc-defg-hij")
);
assert!(is_google_meet_call_title(&edge));
let chrome = normalize_window_title("Meet - abc-defg-hij - Google Chrome");
assert_eq!(chrome, "Meet - abc-defg-hij");
assert_eq!(
google_meet_title_code(&chrome).as_deref(),
Some("abc-defg-hij")
);
let chrome_profile =
normalize_window_title("Meet - abc-defg-hij - Google Chrome - Personal");
assert_eq!(chrome_profile, "Meet - abc-defg-hij");
assert_eq!(
google_meet_title_code(&chrome_profile).as_deref(),
Some("abc-defg-hij")
);
// A named meeting keeps its name, with or without a profile segment,
// and without the count of the window's other tabs.
let named = normalize_window_title(
"Meet - Design sync and 2 more pages - Personal - Microsoft\u{200b} Edge",
);
assert_eq!(named, "Meet - Design sync");
assert_eq!(clean_google_meet_title(&named), "Design sync");
assert_eq!(google_meet_title_code(&named), None);
let single_profile = normalize_window_title("Meet - Design sync - Microsoft Edge");
assert_eq!(single_profile, "Meet - Design sync");
assert_eq!(clean_google_meet_title(&single_profile), "Design sync");
}
#[test]
fn a_call_s_address_is_chosen_before_another_window_s_page() {
let mut pages = BrowserPages::default();
pages.push_title(Some("Inbox - Google Chrome".into()));
pages.push_title(Some("Meet - abc-defg-hij - Google Chrome".into()));
pages.push_url(Some("https://mail.google.com/mail/u/0/".into()));
pages.push_url(Some("https://meet.google.com/abc-defg-hij".into()));
let (url, title) = call_page(&pages);
assert_eq!(
url.as_ref().map(Url::as_str),
Some("https://meet.google.com/abc-defg-hij")
);
assert_eq!(
title.as_deref(),
Some("Meet - abc-defg-hij - Google Chrome")
);
}
#[test]
fn a_call_s_title_drops_another_window_s_address() {
let mut pages = BrowserPages::default();
pages.push_title(Some("Meet - Design sync - Google Chrome".into()));
pages.push_url(Some("https://mail.google.com/".into()));
let (url, title) = call_page(&pages);
assert_eq!(url, None);
assert_eq!(title.as_deref(), Some("Meet - Design sync - Google Chrome"));
}
#[test]
fn a_browser_with_no_call_page_keeps_its_first_page() {
let mut pages = BrowserPages::default();
pages.push_title(Some("Docs".into()));
pages.push_url(Some("https://docs.example.com/".into()));
let (url, title) = call_page(&pages);
assert_eq!(
url.as_ref().map(Url::as_str),
Some("https://docs.example.com/")
);
assert_eq!(title.as_deref(), Some("Docs"));
assert_eq!(call_page(&BrowserPages::default()), (None, None));
}
fn meet_in_brave(url: &str, title: &str) -> MeetingCandidate {
candidate_from_microphone(&brave(), &context("Brave Browser", Some(url), title)).unwrap()
}
#[test]
fn a_different_meet_call_in_the_same_browser_is_not_snoozed_by_the_first() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let again = meet_in_brave(
"https://meet.google.com/abc-defg-hij?authuser=0",
"Meet - Design sync",
);
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
assert_eq!(first.call.as_deref(), Some("abc-defg-hij"));
assert_eq!(second.call.as_deref(), Some("xyz-wxyz-xyz"));
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
// The browser's tabs are still read, since its page can show
// another call.
assert!(!watch.detector.is_application_snoozed("com.brave.Browser"));
// The same call under another address is still snoozed.
assert!(watch.run(Some(&again), &[], 5_000).is_empty());
// A different call a few seconds later is offered (#1113).
assert!(watch.run(None, &[], 2_000).is_empty());
assert_eq!(watch.stable(&second, &[]), Some(Detection::Offer(second)));
// A code-only tab title names the call too; a named one doesn't.
assert_eq!(
google_meet_title_code("Meet - abc-defg-hij - Arc").as_deref(),
Some("abc-defg-hij")
);
assert_eq!(google_meet_title_code("Meet - Design sync"), None);
}
#[test]
fn meet_prefixed_addresses_are_snoozed_by_their_own_codes() {
let first = meet_in_brave(
"https://meet.google.com/meet/abc-defg-hij?authuser=0",
"Meet - Design sync",
);
let again = meet_in_brave(
"https://meet.google.com/meet/abc-defg-hij?authuser=1",
"Meet - Design sync",
);
let second = meet_in_brave(
"https://meet.google.com/meet/xyz-wxyz-xyz?authuser=0",
"Meet - Standup",
);
assert_eq!(first.call.as_deref(), Some("abc-defg-hij"));
assert_eq!(second.call.as_deref(), Some("xyz-wxyz-xyz"));
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
assert!(!watch.detector.is_application_snoozed("com.brave.Browser"));
// The same call under another account is still snoozed.
assert!(watch.run(Some(&again), &[], 5_000).is_empty());
// The first call ended without a stop by hand, so only its different
// code lets the next one through after a short gap (#1594).
assert!(watch.run(None, &[], 2_000).is_empty());
assert_eq!(watch.stable(&second, &[]), Some(Detection::Offer(second)));
}
#[test]
fn a_call_switched_to_while_the_browser_stays_on_the_microphone_is_not_snoozed() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
// The recording of the first call ends after its page named the
// second while the browser kept the microphone the whole time: no
// absence at all, and the second call is still offered as a new one
// two seconds after the first's offer.
assert_eq!(
offered(&watch.run(Some(&second), &[], 2_500)),
vec![4_500],
"the browser's new call was snoozed by the old one"
);
}
#[test]
fn a_call_switched_to_before_the_stop_is_detected_while_the_page_still_names_the_ended_one() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
// The first call's recording stopped after its page named the second
// while the browser never left the microphone; every read since still
// names the ended call, and the call the page named before the stop is
// the one Hex starts.
watch.switch_to(first.call.clone(), second.clone());
let detected = watch
.stable(&first, &[])
.unwrap_or_else(|| panic!("the switched call was snoozed as the ended one"));
let Detection::Offer(candidate) = detected else {
panic!("expected an offer, got {detected:?}");
};
assert_eq!(candidate.call.as_deref(), second.call.as_deref());
assert_eq!(candidate.call.as_deref(), Some("xyz-wxyz-xyz"));
}
#[test]
fn a_call_switched_to_before_the_stop_is_detected_from_a_page_that_names_no_call() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let fallback = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
// The browser's next reads name no call at all (its active tab is no
// call's page), and the call it named before the stop still starts.
let found = watch.run(Some(&fallback), &[], 2_500);
assert_eq!(found.len(), 1, "{found:?}");
let Detection::Offer(candidate) = &found[0].1 else {
panic!("expected an offer, got {:?}", found[0].1);
};
assert_eq!(candidate.call.as_deref(), second.call.as_deref());
assert_eq!(candidate.call.as_deref(), Some("xyz-wxyz-xyz"));
}
#[test]
fn forgetting_a_switched_call_snoozes_the_ended_calls_page_again() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
watch.detector.forget_switched_call();
// Kept recording, or the page came back to the recorded call: the
// ended call's page is snoozed again, even though the browser never
// left the microphone.
assert!(watch.run(Some(&first), &[], 25_000).is_empty());
}
#[test]
fn a_read_naming_a_third_call_clears_the_switched_call_and_offers_it() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let third = meet_in_brave("https://meet.google.com/qqq-qqqq-qqq", "Meet - Retro");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
// The page moves on from the ended call to a third one: the live read
// wins, the remembered call goes, and the third is offered.
let detected = watch
.stable(&third, &[])
.unwrap_or_else(|| panic!("the third call was not offered"));
let Detection::Offer(candidate) = detected else {
panic!("expected an offer, got {detected:?}");
};
assert_eq!(candidate.call.as_deref(), third.call.as_deref());
assert!(watch.detector.switched_call().is_none());
}
#[test]
fn the_snooze_ending_clears_the_switched_call() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
// The page names nothing and the browser leaves the microphone: the
// snooze ends, and the remembered call goes with it.
watch.leave();
assert!(watch.detector.switched_call().is_none());
}
#[test]
fn a_switched_call_is_forgotten_after_two_minutes_without_a_detection() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
watch.skip(SWITCHED_CALL_MEMORY.as_millis() as u64 + STEP_MS);
// The page still names the ended call, but the call it named before
// the stop is not started two minutes later.
assert!(watch.run(Some(&first), &[], 25_000).is_empty());
assert!(watch.detector.switched_call().is_none());
}
#[test]
fn a_recording_started_for_a_switched_call_keeps_the_ended_code() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let mut watch = Watch::new();
assert_eq!(
watch.stable(&first, &[]),
Some(Detection::Offer(first.clone()))
);
watch.switch_to(first.call.clone(), second.clone());
// The page keeps naming the ended call: detection starts the call it
// named and hands the ended code to the recording.
let detected = watch
.stable(&first, &[])
.unwrap_or_else(|| panic!("the switched call was snoozed as the ended one"));
let Detection::Offer(candidate) = detected else {
panic!("expected an offer, got {detected:?}");
};
assert_eq!(candidate.call.as_deref(), second.call.as_deref());
let ended = watch.detector.take_switched_from();
assert_eq!(ended.as_deref(), first.call.as_deref());
// The recording starts for the second call with the ended code kept:
// 40 s of reads naming it never end the recording, and a third call
// still does.
let mut switching = Switching::with_kept(second.call.clone(), ended);
assert!(
switching.run(Some("abc-defg-hij"), 40_000).is_empty(),
"the ended call's page ended the new recording"
);
assert_eq!(switching.run(Some("qqq-qqqq-qqq"), 4_500), vec![44_000]);
assert!(switching.switch.changed());
}
/// A `CallSwitch` fed on a fake monotonic clock, like the watcher feeds
/// it from its page reads.
struct Switching {
switch: CallSwitch,
base: Instant,
elapsed_ms: u64,
}
impl Switching {
fn new(recorded: Option<&str>) -> Self {
Self {
switch: CallSwitch::new(recorded.map(str::to_string)),
base: Instant::now(),
elapsed_ms: 0,
}
}
/// A recording of `recorded` that treats `kept` as already kept, as
/// one detected in place of that call's stopped one does.
fn with_kept(recorded: Option<String>, kept: Option<String>) -> Self {
Self {
switch: CallSwitch::with_kept(recorded, kept),
base: Instant::now(),
elapsed_ms: 0,
}
}
/// Observes `current` every `STEP_MS` for `ms`; the times it fired.
fn run(&mut self, current: Option<&str>, ms: u64) -> Vec<u64> {
let mut fired = Vec::new();
let end = self.elapsed_ms + ms;
while self.elapsed_ms < end {
let now = self.base + Duration::from_millis(self.elapsed_ms);
if self.switch.observe_at(current, now) {
fired.push(self.elapsed_ms);
}
self.elapsed_ms += STEP_MS;
}
fired
}
}
#[test]
fn a_call_switch_fires_once_after_another_code_holds_for_a_few_seconds() {
let mut switching = Switching::new(Some("abc-defg-hij"));
// The recorded call's own page, and a tab that names no call (or no
// page at all), never end a recording.
assert!(switching.run(Some("abc-defg-hij"), 6_000).is_empty());
assert!(switching.run(None, 6_000).is_empty());
// A different call's page, long enough: one ending.
assert_eq!(switching.run(Some("xyz-wxyz-xyz"), 4_500), vec![16_000]);
// Asked once: the same page keeps showing and asks nothing again.
assert!(switching.run(Some("xyz-wxyz-xyz"), 10_000).is_empty());
// Back on the recorded call, the count starts over: a later switch to
// that page ends once more, and once only.
assert!(switching.run(Some("abc-defg-hij"), 1_000).is_empty());
assert_eq!(switching.run(Some("xyz-wxyz-xyz"), 9_000), vec![31_500]);
}
#[test]
fn a_brief_glance_at_another_call_does_not_end_a_recording() {
let mut switching = Switching::new(Some("abc-defg-hij"));
assert!(switching.run(Some("xyz-wxyz-xyz"), 3_500).is_empty());
// The recorded call's page returns before the four seconds are up.
assert!(switching.run(Some("abc-defg-hij"), 1_000).is_empty());
assert!(switching.run(Some("xyz-wxyz-xyz"), 3_500).is_empty());
// Another call, but never four seconds of one.
assert!(switching.run(Some("qqq-qqqq-qqq"), 3_500).is_empty());
// Back on the recorded call nothing is counting any more.
assert!(switching.run(Some("abc-defg-hij"), 500).is_empty());
assert!(!switching.switch.changed());
}
#[test]
fn keeping_a_different_call_never_ends_the_recording_for_that_code() {
let mut switching = Switching::new(Some("abc-defg-hij"));
assert_eq!(switching.run(Some("xyz-wxyz-xyz"), 4_500), vec![4_000]);
assert!(switching.switch.changed());
switching.switch.keep();
assert!(!switching.switch.changed());
// Keeping again with nothing counting never forgets the code.
switching.switch.keep();
// The kept page stays for twenty seconds and never asks again.
assert!(switching.run(Some("xyz-wxyz-xyz"), 20_000).is_empty());
// A third call is still a switch.
assert_eq!(switching.run(Some("qqq-qqqq-qqq"), 4_500), vec![28_500]);
}
#[test]
fn a_call_switch_without_a_recorded_code_never_fires() {
let mut switching = Switching::new(None);
assert!(switching.run(Some("xyz-wxyz-xyz"), 20_000).is_empty());
assert!(switching.run(None, 5_000).is_empty());
assert!(!switching.switch.changed());
assert_eq!(switching.switch.recorded(), None);
}
#[test]
fn an_automatic_call_ended_stop_offers_the_call_again_when_it_returns() {
let zoom = zoom();
let always = [choice("us.zoom.xos", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
// Always record starts the recording on its own.
assert_eq!(
watch.stable(&zoom, &always),
Some(Detection::Record(zoom.clone()))
);
// The call app drops off the microphone, long enough to be a call
// ended (a mute or an audio restart that outlasts it). The watcher
// stops the recording after its grace and tells the detector the
// recording ended.
let away = (CALL_ENDED_AFTER + AUTO_STOP_GRACE).as_millis() as u64;
assert!(watch.run(None, &always, away).is_empty());
let stopped_at = watch.base + Duration::from_millis(watch.elapsed_ms);
watch.detector.recording_stopped_at(stopped_at);
// Back on the microphone: a new call, recorded again, rather than the
// snoozed one returning for the rest of the call.
assert_eq!(
watch.stable(&zoom, &always),
Some(Detection::Record(zoom.clone()))
);
}
#[test]
fn a_hand_stopped_call_still_on_the_microphone_stays_snoozed() {
let zoom = zoom();
let always = [choice("us.zoom.xos", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert_eq!(
watch.stable(&zoom, &always),
Some(Detection::Record(zoom.clone()))
);
// As a running recording's present poll does, then the hand stop: the
// app is still on the microphone.
let now = watch.base + Duration::from_millis(watch.elapsed_ms);
watch.detector.keep_snooze_alive_at(now);
watch.detector.recording_stopped_at(now);
// The app never leaves, so this is not a new call: no second
// recording while it holds the microphone.
assert!(watch.run(Some(&zoom), &always, 120_000).is_empty());
}
#[test]
fn a_stopped_call_s_app_is_snoozed_only_until_it_leaves_for_a_few_seconds() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert!(watch.stable(&slack, &[]).is_some());
watch
.detector
.recording_stopped_at(watch.base + Duration::from_millis(watch.elapsed_ms));
// A moment off the microphone: still the same call.
assert!(watch.run(None, &[], 2_000).is_empty());
assert!(watch.run(Some(&slack), &[], 5_000).is_empty());
// Off for 3 s: the next call is offered.
assert!(watch.run(None, &[], 3_500).is_empty());
assert_eq!(
watch.stable(&slack, &[]),
Some(Detection::Offer(slack.clone()))
);
// Not stopped by hand: the minute still applies.
assert!(watch.run(None, &[], 20_000).is_empty());
assert!(watch.run(Some(&slack), &[], 5_000).is_empty());
// The absence is seen while the stopped recording transcribes.
let mut watch = Watch::new();
assert!(watch.stable(&slack, &[]).is_some());
watch
.detector
.recording_stopped_at(watch.base + Duration::from_millis(watch.elapsed_ms));
for _ in 0..8 {
let now = watch.base + Duration::from_millis(watch.elapsed_ms);
watch.detector.observe_call_app_at(false, now);
watch.elapsed_ms += STEP_MS;
}
assert_eq!(watch.stable(&slack, &[]), Some(Detection::Offer(slack)));
}
#[test]
fn vivaldi_on_the_microphone_is_a_browser_call() {
// A Teams web app in Vivaldi runs in Vivaldi's own process (#1105).
let vivaldi = ActiveMicrophoneApplication {
pid: 4242,
bundle_id: "vivaldi-bin".into(),
name: "vivaldi-bin".into(),
};
let call = candidate_from_microphone(&vivaldi, &ContextSnapshot::default()).unwrap();
assert_eq!(call.source, MeetingSource::BrowserCall);
assert_eq!(call.application.as_deref(), Some("com.vivaldi.Vivaldi"));
assert_eq!(call.application_name.as_deref(), Some("Vivaldi"));
assert_eq!(call.offer_title(), "Vivaldi call detected");
assert_eq!(call.required_stability(), BROWSER_STABLE_FOR);
}
#[test]
fn helium_on_the_microphone_is_a_named_browser_call() {
// Helium's sandboxed audio service holds the microphone and reports
// its helper bundle; the main app is `net.imput.helium`.
let helium_helper = ActiveMicrophoneApplication {
pid: 6120,
bundle_id: "net.imput.helium.helper".into(),
name: "Helium Helper".into(),
};
assert!(helium_helper.is_browser());
let meet = candidate_from_microphone(
&helium_helper,
&context(
"Helium",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync",
),
)
.unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.application.as_deref(), Some("net.imput.helium"));
assert_eq!(meet.application_name.as_deref(), Some("Helium"));
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
// A page on no known call site is still a browser call, named by the
// browser.
let other = candidate_from_microphone(&helium_helper, &ContextSnapshot::default()).unwrap();
assert_eq!(other.source, MeetingSource::BrowserCall);
assert_eq!(other.offer_title(), "Helium call detected");
}
#[test]
fn ardia_on_the_microphone_is_a_named_browser_call() {
// Ardia's Chromium audio service holds the microphone and reports
// `com.ardia.browser.helper`; the main app is `com.ardia.browser`.
let ardia_helper = ActiveMicrophoneApplication {
pid: 6130,
bundle_id: "com.ardia.browser.helper".into(),
name: "Ardia Helper".into(),
};
assert!(ardia_helper.is_browser());
let meet = candidate_from_microphone(
&ardia_helper,
&context(
"Ardia",
Some("https://meet.google.com/abc-defg-hij"),
"Meet – Design sync - Ardia",
),
)
.unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.application.as_deref(), Some("com.ardia.browser"));
assert_eq!(meet.application_name.as_deref(), Some("Ardia"));
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
assert_eq!(meet.title, "Design sync");
// A page on no known call site is still a browser call, named by the
// browser.
let other = candidate_from_microphone(&ardia_helper, &ContextSnapshot::default()).unwrap();
assert_eq!(other.source, MeetingSource::BrowserCall);
assert_eq!(other.offer_title(), "Ardia call detected");
}
#[test]
fn zen_on_the_microphone_is_a_named_browser_call() {
// Zen is Firefox-based, and its media process is its own
// `app.zen-browser.plugincontainer`, never `org.mozilla.*`.
let zen_helper = ActiveMicrophoneApplication {
pid: 6140,
bundle_id: "app.zen-browser.plugincontainer".into(),
name: "plugin-container".into(),
};
assert!(zen_helper.is_browser());
let meet = candidate_from_microphone(
&zen_helper,
&context(
"Zen",
Some("https://meet.google.com/abc-defg-hij"),
"Meet – Design sync — Zen Browser",
),
)
.unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.application.as_deref(), Some("app.zen-browser.zen"));
assert_eq!(meet.application_name.as_deref(), Some("Zen"));
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
assert_eq!(meet.title, "Design sync");
// A page on no known call site is still a browser call, named by the
// browser.
let other = candidate_from_microphone(&zen_helper, &ContextSnapshot::default()).unwrap();
assert_eq!(other.source, MeetingSource::BrowserCall);
assert_eq!(other.offer_title(), "Zen call detected");
// On Linux the microphone's process names Zen by its binary: `zen` for
// the Flatpak and the tarball launcher, `zen-bin` for the tarball's own
// executable, content processes included. No site is read there.
for binary in ["zen", "zen-bin"] {
let zen = ActiveMicrophoneApplication {
pid: 6150,
bundle_id: binary.into(),
name: "Zen".into(),
};
assert!(zen.is_browser(), "{binary}");
let call = candidate_from_microphone(&zen, &ContextSnapshot::default()).unwrap();
assert_eq!(call.source, MeetingSource::BrowserCall, "{binary}");
assert_eq!(call.application.as_deref(), Some("app.zen-browser.zen"));
assert_eq!(call.application_name.as_deref(), Some("Zen"));
assert_eq!(call.offer_title(), "Zen call detected");
}
}
#[test]
fn zen_matches_its_own_name_not_a_name_that_contains_it() {
// `zen` as a substring would also match `Citizen`, so Zen matches its
// whole name like Arc. The shared case-sensitive name check above
// still accepts `Zendesk` for `Zen`; it runs for every browser and is
// unchanged here.
let citizen = context("Citizen", None, "");
assert!(!context_matches_browser(
"app.zen-browser.zen",
"Zen",
&citizen
));
assert!(!is_browser_application("citizen"));
assert!(!is_browser_application("zendesk"));
}
fn brave() -> ActiveMicrophoneApplication {
ActiveMicrophoneApplication {
pid: 49686,
bundle_id: "com.brave.Browser".into(),
name: "Brave Browser".into(),
}
}
fn arc() -> ActiveMicrophoneApplication {
ActiveMicrophoneApplication {
pid: 1592,
bundle_id: "company.thebrowser.Browser".into(),
name: "Arc".into(),
}
}
#[test]
fn offer_titles_name_the_service_site_or_browser_without_repeating_call() {
let problems = context(
"Brave Browser",
Some("https://www.example.com/board"),
"Problems",
);
let site = candidate_from_microphone(&brave(), &problems).unwrap();
assert_eq!(site.source, MeetingSource::BrowserCall);
assert_eq!(site.offer_title(), "example.com call detected");
assert_eq!(site.recording_start().app.as_deref(), Some("example.com"));
let unknown = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
assert_eq!(unknown.offer_title(), "Brave Browser call detected");
let meet = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - abc-defg-hij - Brave",
),
)
.unwrap();
assert_eq!(meet.offer_title(), "Google Meet call detected");
let banshee = candidate_from_microphone(
&brave(),
&context("Brave Browser", Some("https://banshee.app/room-1"), "Room"),
)
.unwrap();
assert_eq!(banshee.offer_title(), "Banshee call detected");
assert_eq!(banshee.recording_start().app.as_deref(), Some("Banshee"));
assert_eq!(
candidate(MeetingSource::SlackHuddle, "slack", "Slack huddle").offer_title(),
"Slack huddle detected"
);
}
fn choice(key: &str, policy: MeetingPolicy) -> MeetingProviderChoice {
MeetingProviderChoice {
key: key.into(),
label: key.into(),
policy,
}
}
#[test]
fn choices_apply_to_the_site_when_known_and_the_app_otherwise() {
let problems = context("Brave Browser", Some("https://example.com/"), "Problems");
let site = candidate_from_microphone(&brave(), &problems).unwrap();
let provider = site.provider().unwrap();
assert_eq!(provider.key, "example.com");
assert_eq!(
site.policy(
&[provider.choose(MeetingPolicy::NeverOffer)],
MeetingPolicy::Ask
),
MeetingPolicy::NeverOffer
);
assert!(site.can_always_record());
let unknown = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
let app = unknown.provider().unwrap();
assert_eq!(
(app.key.as_str(), app.label.as_str()),
("com.brave.Browser", "Brave Browser")
);
assert!(!unknown.can_always_record());
let never_site = [choice("example.com", MeetingPolicy::NeverOffer)];
assert_eq!(
unknown.policy(&never_site, MeetingPolicy::Ask),
MeetingPolicy::Ask
);
// A browser's choice covers its sites unless the site has its own.
let never_brave = choice("com.brave.Browser", MeetingPolicy::NeverOffer);
assert_eq!(
site.policy(std::slice::from_ref(&never_brave), MeetingPolicy::Ask),
MeetingPolicy::NeverOffer
);
assert_eq!(
site.policy(
&[
never_brave.clone(),
choice("example.com", MeetingPolicy::AlwaysRecord)
],
MeetingPolicy::Ask
),
MeetingPolicy::AlwaysRecord
);
// A site set back to Ask asks, whatever its browser's choice.
assert_eq!(
site.policy(
&[never_brave, choice("example.com", MeetingPolicy::Ask)],
MeetingPolicy::Ask
),
MeetingPolicy::Ask
);
// Known services carry the name Settings lists them under.
let meet = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet",
),
)
.unwrap();
let provider = meet.provider().unwrap();
assert_eq!(
(provider.key.as_str(), provider.label.as_str()),
("meet.google.com", "Google Meet")
);
let slack = slack_huddle();
assert_eq!(slack.provider().unwrap().label, "Slack huddles");
let banshee = candidate_from_microphone(
&brave(),
&context("Brave Browser", Some("https://banshee.app/room-1"), "Room"),
)
.unwrap();
let provider = banshee.provider().unwrap();
assert_eq!(
(provider.key.as_str(), provider.label.as_str()),
("banshee.app", "Banshee")
);
assert!(banshee.can_always_record());
assert!(slack.can_always_record());
}
fn zoom() -> MeetingCandidate {
// Zoom's audio host process holds the microphone; the call is Zoom.
let helper = ActiveMicrophoneApplication {
pid: 9010,
bundle_id: "us.zoom.CptHost".into(),
name: "CptHost".into(),
};
candidate_from_microphone(&helper, &ContextSnapshot::default()).unwrap()
}
fn slack_huddle() -> MeetingCandidate {
let helper = ActiveMicrophoneApplication {
pid: 31807,
bundle_id: "com.tinyspeck.slackmacgap.helper".into(),
name: "Slack Helper".into(),
};
candidate_from_microphone(&helper, &ContextSnapshot::default()).unwrap()
}
#[test]
fn always_record_starts_after_the_same_stability_time_as_an_offer() {
let slack = slack_huddle();
let always = [choice(
"com.tinyspeck.slackmacgap",
MeetingPolicy::AlwaysRecord,
)];
let mut watch = Watch::new();
assert!(watch.run(Some(&slack), &always, 2_000).is_empty());
assert_eq!(
watch.run(Some(&slack), &always, 500),
[(2_000, Detection::Record(slack.clone()))]
);
// Stopped mid-call: the call stays snoozed while it uses the microphone.
assert!(watch.run(Some(&slack), &always, 10_000).is_empty());
// Browser pages still need about 20 s on the microphone.
let problems = candidate_from_microphone(
&brave(),
&context("Brave Browser", Some("https://example.com/"), "Problems"),
)
.unwrap();
let always = [choice("example.com", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert!(watch.run(Some(&problems), &always, 6_000).is_empty());
assert!(watch.run(None, &always, 5_000).is_empty());
assert!(watch.run(Some(&problems), &always, 20_000).is_empty());
assert_eq!(
watch.run(Some(&problems), &always, 500),
[(31_000, Detection::Record(problems))]
);
}
/// The default answers calls with none of their own; a call's own answer
/// still wins, and a browser page Hex can't name is offered rather than
/// recorded, which would record every page.
#[test]
fn the_default_answers_calls_without_their_own() {
let slack = slack_huddle();
let mut watch = Watch::new();
watch.default = MeetingPolicy::AlwaysRecord;
assert_eq!(
watch.run(Some(&slack), &[], 2_500).pop().unwrap().1,
Detection::Record(slack.clone())
);
let mut watch = Watch::new();
watch.default = MeetingPolicy::AlwaysRecord;
let ask = [choice("com.tinyspeck.slackmacgap", MeetingPolicy::Ask)];
assert_eq!(
watch.run(Some(&slack), &ask, 2_500).pop().unwrap().1,
Detection::Offer(slack.clone())
);
let unknown = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
let mut watch = Watch::new();
watch.default = MeetingPolicy::AlwaysRecord;
assert_eq!(
watch.run(Some(&unknown), &[], 31_000).pop().unwrap().1,
Detection::Offer(unknown)
);
let mut watch = Watch::new();
watch.default = MeetingPolicy::NeverOffer;
assert!(watch.run(Some(&slack), &[], 8_000).is_empty());
let always = [choice(
"com.tinyspeck.slackmacgap",
MeetingPolicy::AlwaysRecord,
)];
let mut watch = Watch::new();
watch.default = MeetingPolicy::NeverOffer;
assert_eq!(
watch.run(Some(&slack), &always, 2_500).pop().unwrap().1,
Detection::Record(slack)
);
}
#[test]
fn never_offer_and_snoozed_calls_never_start() {
let slack = slack_huddle();
let never = [choice(
"com.tinyspeck.slackmacgap",
MeetingPolicy::NeverOffer,
)];
let mut watch = Watch::new();
assert!(watch.run(Some(&slack), &never, 8_000).is_empty());
// Offered and dismissed, then switched to Always record mid-call.
let mut watch = Watch::new();
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(
watch.run(Some(&slack), &[], 500),
[(2_000, Detection::Offer(slack.clone()))]
);
let always = [choice(
"com.tinyspeck.slackmacgap",
MeetingPolicy::AlwaysRecord,
)];
assert!(watch.run(None, &always, 20_000).is_empty());
assert!(watch.run(Some(&slack), &always, 8_000).is_empty());
// Don't offer, then Always record, while the call stays on the
// microphone well past the snooze reset: still nothing.
let never = [choice(
"com.tinyspeck.slackmacgap",
MeetingPolicy::NeverOffer,
)];
assert!(watch.run(Some(&slack), &never, 120_000).is_empty());
assert!(watch.run(Some(&slack), &always, 8_000).is_empty());
// The next call, after a minute off the microphone, records.
watch.run(None, &always, 61_000);
assert!(watch.run(Some(&slack), &always, 2_000).is_empty());
assert_eq!(
watch.run(Some(&slack), &always, 500).pop().unwrap().1,
Detection::Record(slack)
);
}
#[test]
fn a_browser_voice_note_is_not_offered_but_a_sustained_browser_call_is() {
let problems = context("Brave Browser", Some("https://example.com/"), "Problems");
let candidate = candidate_from_microphone(&brave(), &problems).unwrap();
let mut watch = Watch::new();
// A six-second voice note, then the mic closes for a few seconds.
assert!(watch.run(Some(&candidate), &[], 6_000).is_empty());
assert!(watch.run(None, &[], 5_000).is_empty());
assert!(watch.run(Some(&candidate), &[], 20_000).is_empty());
assert_eq!(watch.run(Some(&candidate), &[], 500).len(), 1);
}
#[test]
fn a_missed_microphone_check_does_not_restart_the_wait_for_an_offer() {
let app = ActiveMicrophoneApplication {
pid: 7,
bundle_id: "net.whatsapp.WhatsApp".into(),
name: "WhatsApp".into(),
};
let call = candidate_from_microphone(&app, &ContextSnapshot::default()).unwrap();
// One check misses the call 10 s in: still offered at 20 s.
let mut watch = Watch::new();
assert!(watch.run(Some(&call), &[], 10_000).is_empty());
assert!(watch.run(None, &[], 500).is_empty());
assert!(watch.run(Some(&call), &[], 9_500).is_empty());
assert_eq!(offered(&watch.run(Some(&call), &[], 500)), [20_000]);
// On a slow loop, two missed checks in a row are still a dropout.
let mut watch = Watch::new();
assert!(watch.run_every(Some(&call), &[], 9_900, 900).is_empty());
assert!(watch.run_every(None, &[], 1_800, 900).is_empty());
assert!(watch.run_every(Some(&call), &[], 9_000, 900).is_empty());
assert_eq!(
offered(&watch.run_every(Some(&call), &[], 900, 900)),
[20_700]
);
// A longer release is the call (or voice note) ending: start over.
let mut watch = Watch::new();
assert!(watch.run(Some(&call), &[], 10_000).is_empty());
assert!(watch.run(None, &[], 3_000).is_empty());
assert!(watch.run(Some(&call), &[], 20_000).is_empty());
assert_eq!(offered(&watch.run(Some(&call), &[], 500)), [33_000]);
}
#[test]
fn a_slow_loop_still_offers_an_unknown_browser_call_after_about_20_seconds() {
let candidate = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
assert_eq!(candidate.source, MeetingSource::BrowserCall);
// The loop sleeps 500 ms but its browser inspection takes 400 ms more.
let mut watch = Watch::new();
assert!(
watch
.run_every(Some(&candidate), &[], 20_700, 900)
.is_empty()
);
assert_eq!(
offered(&watch.run_every(Some(&candidate), &[], 900, 900)),
[20_700]
);
// The same call seen at the nominal pace offers at the same time.
let mut watch = Watch::new();
assert!(watch.run(Some(&candidate), &[], 20_000).is_empty());
assert_eq!(offered(&watch.run(Some(&candidate), &[], 500)), [20_000]);
}
#[test]
fn a_slow_loop_still_ends_a_snooze_after_a_minute() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert!(watch.run_every(Some(&slack), &[], 2_700, 900).is_empty());
assert_eq!(watch.run_every(Some(&slack), &[], 900, 900).len(), 1);
assert!(watch.run_every(None, &[], 54_000, 900).is_empty());
assert!(watch.run_every(Some(&slack), &[], 2_700, 900).is_empty());
watch.run_every(None, &[], 63_000, 900);
assert!(watch.run_every(Some(&slack), &[], 2_700, 900).is_empty());
assert_eq!(watch.run_every(Some(&slack), &[], 900, 900).len(), 1);
}
#[test]
fn a_stalled_loop_does_not_count_the_gap_as_time_on_the_microphone() {
let candidate = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
let mut watch = Watch::new();
assert!(watch.run(Some(&candidate), &[], 1_000).is_empty());
// The Mac slept; the call is on the microphone again afterwards.
watch.skip(60_000);
assert!(watch.run(Some(&candidate), &[], 20_000).is_empty());
assert_eq!(watch.run(Some(&candidate), &[], 500).len(), 1);
}
#[test]
fn banshee_is_offered_after_about_two_seconds() {
let banshee = candidate_from_microphone(&brave(), &banshee_page()).unwrap();
assert_eq!(banshee.source, MeetingSource::Banshee);
assert_eq!(banshee.site.as_deref(), Some("banshee.app"));
let mut watch = Watch::new();
assert!(watch.run(Some(&banshee), &[], 2_000).is_empty());
assert_eq!(offered(&watch.run(Some(&banshee), &[], 500)), [2_000]);
// A slow loop does not stretch it.
let mut watch = Watch::new();
assert!(watch.run_every(Some(&banshee), &[], 2_700, 900).is_empty());
assert_eq!(
offered(&watch.run_every(Some(&banshee), &[], 900, 900)),
[2_700]
);
}
#[test]
fn finding_the_page_keeps_the_run_toward_the_known_threshold() {
let unknown = candidate_from_microphone(&brave(), &ContextSnapshot::default()).unwrap();
let banshee = candidate_from_microphone(&brave(), &banshee_page()).unwrap();
// The page is found 1 s in: offered 2 s after the browser took the
// microphone, not 2 s after the page was found.
let mut watch = Watch::new();
assert!(watch.run(Some(&unknown), &[], 1_000).is_empty());
assert!(watch.run(Some(&banshee), &[], 1_000).is_empty());
assert_eq!(offered(&watch.run(Some(&banshee), &[], 500)), [2_000]);
// The time held while unresolved is never more than the browser held
// the microphone, and a different browser starts over.
let chrome = ActiveMicrophoneApplication {
pid: 400,
bundle_id: "com.google.Chrome.helper".into(),
name: "Google Chrome Helper".into(),
};
let chrome_unknown =
candidate_from_microphone(&chrome, &ContextSnapshot::default()).unwrap();
let mut watch = Watch::new();
assert!(watch.run(Some(&chrome_unknown), &[], 1_500).is_empty());
assert!(watch.run(Some(&banshee), &[], 2_000).is_empty());
assert_eq!(offered(&watch.run(Some(&banshee), &[], 500)), [3_500]);
// An unresolved page that is never found still needs 20 s.
let mut watch = Watch::new();
assert!(watch.run(Some(&unknown), &[], 20_000).is_empty());
assert_eq!(offered(&watch.run(Some(&unknown), &[], 500)), [20_000]);
}
#[test]
fn a_voice_note_on_another_site_is_not_a_banshee_call() {
// The browser's active tab is the page using the microphone; a
// Banshee tab open in the background is never read, so it can neither
// shorten the threshold nor trigger Always record.
let problems = candidate_from_microphone(
&brave(),
&context("Brave Browser", Some("https://example.com/"), "Problems"),
)
.unwrap();
let choices = [
choice("banshee.app", MeetingPolicy::AlwaysRecord),
choice("example.com", MeetingPolicy::NeverOffer),
];
let mut watch = Watch::new();
assert!(watch.run(Some(&problems), &choices, 25_000).is_empty());
// Without a choice for that site it is the unknown-page 20 s, an offer.
let choices = [choice("banshee.app", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert!(watch.run(Some(&problems), &choices, 2_500).is_empty());
assert_eq!(watch.run(Some(&problems), &choices, 20_000).len(), 1);
}
#[test]
fn a_gap_with_the_app_absent_counts_as_absence() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&slack), &[], 500).len(), 1);
// The Mac slept, or Meetings was off, for a while; the call is over.
watch.skip(120_000);
assert!(watch.run(None, &[], 500).is_empty());
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&slack), &[], 500).len(), 1);
// Still on the microphone after the gap: still the same call.
watch.skip(120_000);
assert!(watch.run(Some(&slack), &[], 10_000).is_empty());
}
#[test]
fn a_recorded_call_that_left_the_microphone_is_not_snoozed_for_another_minute() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&slack), &[], 500).len(), 1);
// Recorded for an hour; the app left the microphone after 5 minutes,
// so the first pass after Stop already ends the snooze.
watch.skip(3_600_000);
assert!(watch.run(None, &[], 500).is_empty());
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&slack), &[], 500).len(), 1);
// While the recording sees the app on the microphone, the snooze
// counts from the last such moment.
let mut watch = Watch::new();
watch.run(Some(&slack), &[], 2_500);
watch.skip(97_500);
watch
.detector
.keep_snooze_alive_at(watch.base + Duration::from_millis(watch.elapsed_ms));
assert!(watch.run(None, &[], 59_500).is_empty());
assert!(watch.run(Some(&slack), &[], 5_000).is_empty());
assert!(watch.run(None, &[], 61_000).is_empty());
assert!(watch.run(Some(&slack), &[], 2_000).is_empty());
assert_eq!(watch.run(Some(&slack), &[], 500).len(), 1);
}
fn banshee_page() -> ContextSnapshot {
context(
"Brave Browser",
Some("https://banshee.app/standup"),
"Standup",
)
}
#[test]
fn the_site_is_resolved_while_hex_is_in_front() {
let t0 = Instant::now();
let mut memory = BrowserPageMemory::default();
let hex = context("Hex", None, "Hex");
// Nothing seen yet: the site is unknown.
let page = memory.recall(&brave(), hex.clone(), t0);
assert_eq!(
candidate_from_microphone(&brave(), &page).unwrap().source,
MeetingSource::BrowserCall
);
// Brave was in front on Banshee, then Hex came forward.
memory.recall(&brave(), banshee_page(), t0);
let page = memory.recall(&brave(), hex, t0 + Duration::from_secs(30));
let candidate = candidate_from_microphone(&brave(), &page).unwrap();
assert_eq!(candidate.source, MeetingSource::Banshee);
assert_eq!(candidate.site.as_deref(), Some("banshee.app"));
}
#[test]
fn a_page_seen_minutes_ago_no_longer_names_the_call() {
let t0 = Instant::now();
let mut memory = BrowserPageMemory::default();
let hex = context("Hex", None, "Hex");
memory.recall(&brave(), banshee_page(), t0);
let page = memory.recall(&brave(), hex.clone(), t0 + Duration::from_secs(61));
assert_eq!(page, hex);
assert!(
candidate_from_microphone(&brave(), &page)
.unwrap()
.site
.is_none()
);
}
#[test]
fn a_remembered_page_belongs_to_its_browser_and_its_call() {
let t0 = Instant::now();
let mut memory = BrowserPageMemory::default();
let hex = context("Hex", None, "Hex");
memory.recall(&brave(), banshee_page(), t0);
let chrome = ActiveMicrophoneApplication {
pid: 400,
bundle_id: "com.google.Chrome.helper".into(),
name: "Google Chrome Helper".into(),
};
assert_eq!(memory.recall(&chrome, hex.clone(), t0), hex);
// The microphone is released: the next call starts fresh.
memory.recall(&brave(), banshee_page(), t0);
memory.forget();
assert_eq!(memory.recall(&brave(), hex.clone(), t0), hex);
// A fresh page replaces the remembered one.
memory.recall(&brave(), banshee_page(), t0);
let other = context("Brave Browser", Some("https://example.com/"), "Example");
assert_eq!(memory.recall(&brave(), other.clone(), t0), other);
assert_eq!(memory.recall(&brave(), hex, t0), other);
}
#[test]
fn a_call_answered_from_its_calendar_reminder_is_not_offered_again() {
let zoom = candidate(MeetingSource::Zoom, "us.zoom.xos", "Zoom meeting");
let zoom = MeetingCandidate {
application: Some("us.zoom.xos".into()),
..zoom
};
let zoom_keys = crate::calendar::CallProvider::Zoom
.detection_keys()
.iter()
.map(|key| key.to_string())
.collect::<Vec<_>>();
let mut watch = Watch::new();
watch
.detector
.snooze_calendar_call(zoom_keys.clone(), 1_000);
watch.wall_ms = 10;
assert_eq!(watch.stable(&zoom, &[]), None);
// Rejoining the same meeting later is still covered.
watch.wall_ms = 20;
watch.leave();
watch.wall_ms = 30;
assert_eq!(watch.stable(&zoom, &[]), None);
// Always record doesn't restart a call the user already stopped.
watch.wall_ms = 40;
watch.leave();
let always = [choice("us.zoom.xos", MeetingPolicy::AlwaysRecord)];
watch.wall_ms = 50;
assert_eq!(watch.stable(&zoom, &always), None);
// Another service's call is offered as usual.
let slack = slack_huddle();
watch.wall_ms = 60;
watch.leave();
watch.wall_ms = 70;
assert_eq!(
watch.stable(&slack, &[]),
Some(Detection::Offer(slack.clone()))
);
// Once the meeting ends, its service's calls are offered again.
watch.wall_ms = 2_000;
watch.leave();
assert_eq!(
watch.stable(&zoom, &[]),
Some(Detection::Offer(zoom.clone()))
);
// A Meet link covers the site in any browser.
let meet = candidate_from_microphone(
&brave(),
&context(
"Brave Browser",
Some("https://meet.google.com/abc-defg-hij"),
"Design sync - Google Meet",
),
)
.unwrap();
let mut watch = Watch::new();
watch
.detector
.snooze_calendar_call(vec!["meet.google.com".into()], 1_000);
watch.wall_ms = 10;
assert_eq!(watch.stable(&meet, &[]), None);
}
#[test]
fn a_google_meet_call_in_arc_records_after_about_two_seconds() {
let arc_helper = ActiveMicrophoneApplication {
pid: 5120,
bundle_id: "company.thebrowser.browser.helper".into(),
name: "Browser Helper".into(),
};
assert!(arc_helper.is_browser());
let meet = candidate_from_microphone(
&arc_helper,
&context(
"Arc",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync",
),
)
.unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(
meet.application.as_deref(),
Some("company.thebrowser.Browser")
);
assert_eq!(meet.application_name.as_deref(), Some("Arc"));
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
let always = [choice("meet.google.com", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert!(watch.run(Some(&meet), &always, 2_000).is_empty());
assert_eq!(
watch.run(Some(&meet), &always, 500),
[(2_000, Detection::Record(meet))]
);
}
#[test]
fn a_google_meet_call_in_aside_records_after_about_two_seconds() {
// Aside's Chromium audio service holds the microphone and reports
// `at.studio.AsideBrowser.helper`; the main app is
// `at.studio.AsideBrowser`.
let aside_helper = ActiveMicrophoneApplication {
pid: 5320,
bundle_id: "at.studio.AsideBrowser.helper".into(),
name: "Aside Helper".into(),
};
assert!(aside_helper.is_browser());
let meet = candidate_from_microphone(
&aside_helper,
&context(
"Aside",
Some("https://meet.google.com/abc-defg-hij"),
"Meet - Design sync - Aside",
),
)
.unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.application.as_deref(), Some("at.studio.AsideBrowser"));
assert_eq!(meet.application_name.as_deref(), Some("Aside"));
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
let always = [choice("meet.google.com", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert!(watch.run(Some(&meet), &always, 2_000).is_empty());
assert_eq!(
watch.run(Some(&meet), &always, 500),
[(2_000, Detection::Record(meet))]
);
}
#[test]
fn a_google_meet_call_in_arc_without_its_address_is_known_by_its_title() {
let arc_helper = ActiveMicrophoneApplication {
pid: 5120,
bundle_id: "company.thebrowser.browser.helper".into(),
name: "Browser Helper".into(),
};
// Automation off and Arc behind another app: no page address, only
// the window's title.
let page = context("Arc", None, "Meet - Design sync");
let meet = candidate_from_microphone(&arc_helper, &page).unwrap();
assert_eq!(meet.source, MeetingSource::GoogleMeet);
assert_eq!(meet.title, "Design sync");
assert_eq!(meet.site.as_deref(), Some("meet.google.com"));
let always = [choice("meet.google.com", MeetingPolicy::AlwaysRecord)];
let mut watch = Watch::new();
assert!(watch.run(Some(&meet), &always, 2_000).is_empty());
assert_eq!(
watch.run(Some(&meet), &always, 500),
[(2_000, Detection::Record(meet))]
);
let page = context("Arc", None, "Meet – abc-defg-hij");
let code = candidate_from_microphone(&arc_helper, &page).unwrap();
assert_eq!(code.source, MeetingSource::GoogleMeet);
assert!(!code.named);
// The landing page and other pages stay unnamed browser calls.
for title in ["Google Meet", "Meet", "Problems"] {
let page = context("Arc", None, title);
let other = candidate_from_microphone(&arc_helper, &page).unwrap();
assert_eq!(other.source, MeetingSource::BrowserCall, "{title}");
assert!(other.site.is_none());
}
}
#[test]
fn a_reminder_without_a_call_link_covers_only_the_next_call() {
let slack = slack_huddle();
let mut watch = Watch::new();
watch.detector.snooze_calendar_call(Vec::new(), 1_000);
watch.wall_ms = 10;
assert_eq!(watch.stable(&slack, &[]), None);
watch.wall_ms = 20;
watch.leave();
watch.wall_ms = 30;
assert_eq!(watch.stable(&slack, &[]), Some(Detection::Offer(slack)));
}
#[test]
fn late_offer_timeout_cannot_overwrite_a_stopped_call_snooze() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert_eq!(
watch.stable(&slack, &[]),
Some(Detection::Offer(slack.clone()))
);
let offer_id = watch.detector.open_offer().unwrap();
let now = watch.now();
watch.detector.recording_stopped_at(now);
assert_eq!(watch.detector.open_offer(), None);
assert_eq!(watch.detector.take_withdrawn_offers(), vec![offer_id]);
// A stale timeout for that offer must not replace Stopped's 3 s grace
// with TimedOut's immediate reset on absence.
watch.detector.offer_timed_out(offer_id);
assert!(watch.detector.take_withdrawn_offers().is_empty());
assert!(watch.run(None, &[], 2_000).is_empty());
assert!(watch.run(Some(&slack), &[], 5_000).is_empty());
// Still ends after CALL_ENDED_AFTER (3 s).
assert!(watch.run(None, &[], 3_500).is_empty());
assert_eq!(watch.stable(&slack, &[]), Some(Detection::Offer(slack)));
}
#[test]
fn replacing_an_open_offer_records_withdrawal_even_at_the_same_instant() {
let slack = slack_huddle();
let zoom = zoom();
let mut watch = Watch::new();
assert_eq!(watch.stable(&slack, &[]), Some(Detection::Offer(slack)));
let first = watch.detector.open_offer().unwrap();
assert!(watch.detector.take_withdrawn_offers().is_empty());
// Minting two OfferIds at the exact same Instant produces distinct IDs.
let now = watch.now();
assert_ne!(OfferId::at(now), OfferId::at(now));
assert_eq!(watch.stable(&zoom, &[]), Some(Detection::Offer(zoom)));
let second = watch.detector.open_offer().unwrap();
assert_ne!(first, second);
assert_eq!(watch.detector.take_withdrawn_offers(), vec![first]);
}
#[test]
fn switched_from_cannot_be_orphaned_across_new_switches_or_detections() {
let first = meet_in_brave("https://meet.google.com/abc-defg-hij", "Meet - Design sync");
let second = meet_in_brave("https://meet.google.com/xyz-wxyz-xyz", "Meet - Standup");
let zoom = zoom();
let mut watch = Watch::new();
assert!(watch.stable(&first, &[]).is_some());
watch.switch_to(first.call.clone(), second.clone());
assert!(watch.stable(&first, &[]).is_some());
// Starting another call switch replaces the unclaimed HandedOff code.
watch.switch_to(second.call.clone(), first.clone());
assert_eq!(watch.detector.take_switched_from(), None);
// Reaching stability without taking switched_from, then detecting an
// unrelated call, drops the stale HandedOff code rather than leaking it.
assert!(watch.stable(&second, &[]).is_some());
assert!(watch.stable(&zoom, &[]).is_some());
assert_eq!(watch.detector.take_switched_from(), None);
}
#[test]
fn withdraw_offer_retires_only_the_panel_while_keeping_the_snooze() {
let slack = slack_huddle();
let mut watch = Watch::new();
assert_eq!(
watch.stable(&slack, &[]),
Some(Detection::Offer(slack.clone()))
);
let id = watch.detector.open_offer().unwrap();
assert_eq!(watch.detector.withdraw_offer(), Some(id));
assert_eq!(watch.detector.open_offer(), None);
assert_eq!(watch.detector.take_withdrawn_offers(), vec![id]);
assert_eq!(watch.detector.withdraw_offer(), None);
// Call stays snoozed for the full minute after its panel closes.
assert!(watch.run(None, &[], 30_000).is_empty());
assert!(watch.run(Some(&slack), &[], 5_000).is_empty());
}
}