darkwing/server/services/browser_profile_services/config/
voices.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Configuration for text-to-speech voices and speech synthesis
//!
//! This module handles configuration of available text-to-speech voices based
//! on the platform (Windows, macOS, Linux) and locale settings. It provides
//! structs and implementations for managing voice settings and capabilities.

use crate::{
  database::browser_profile::Platform,
  server::{
    dtos::{
      browser_profile_dto::BrowserProfileFullData, start_dto::StartRequest,
    },
    error::Error,
  },
};
use serde::{Deserialize, Serialize};

use super::{
  consts::{MACOS_VOICES, WINDOWS_VOICES},
  screen::Screen,
  FromStartRequest, Navigator,
};

/// Configuration for a single text-to-speech voice
///
/// Contains the language and name of a voice that can be used for speech
/// synthesis.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Voice {
  #[serde(rename = "lang")]
  pub(super) language: String,
  pub(super) name: String,
}

/// Collection of available text-to-speech voices
///
/// Wraps a vector of Voice structs representing all available voices for the
/// current platform and locale configuration.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Voices(pub Vec<Voice>);

/// Creates voice configuration from browser profile and start request data
///
/// Filters available voices based on:
/// - Platform (Windows, macOS, Linux)
/// - Navigator locale
/// - Special handling for en-US and en locales
impl FromStartRequest<Voices> for Voices {
  fn from_start_request(
    bp: &BrowserProfileFullData,
    _request: &StartRequest,
    navigator: &Navigator,
    _screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    let locale = navigator.locale.clone();

    let voices = match bp.platform {
      Platform::Windows => WINDOWS_VOICES
        .iter()
        .copied()
        .filter(|voice| {
          voice.0.to_lowercase() == locale.clone().to_lowercase() // TODO(@araratbakaryan): differs from macos impl
            || voice.0.to_lowercase() == "en-US".to_string().to_lowercase()
        })
        .collect(),
      Platform::Macos => MACOS_VOICES
        .iter()
        .copied()
        .filter(|voice| {
          voice.0.to_lowercase() != locale.clone().to_lowercase()  // TODO(@araratbakaryan): differs from windows impl
            || voice.0.to_lowercase() == "en-US".to_string().to_lowercase()
            || voice.0.to_lowercase() == "en".to_string().to_lowercase()
        })
        .collect(),
      Platform::Linux => Vec::new(),
    };

    let voices = voices
      .into_iter()
      .map(|voice| Voice {
        language: voice.0.to_string(),
        name: voice.1.to_string(),
      })
      .collect();

    Ok(Self(voices))
  }
}

/// Configuration for speech synthesis capabilities
///
/// Controls whether speech synthesis is enabled and manages the collection
/// of available voices.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpeechVoices {
  pub(super) enabled: bool,
  pub(super) voices: Voices,
}

/// Creates speech synthesis configuration from browser profile and start
/// request data
///
/// Currently always sets enabled to false while preserving the voice
/// configuration.
impl FromStartRequest<SpeechVoices> for SpeechVoices {
  fn from_start_request(
    bp: &BrowserProfileFullData,
    request: &StartRequest,
    navigator: &Navigator,
    screen: &Screen,
    token: &str,
  ) -> Result<Self, Error> {
    Ok(SpeechVoices {
      enabled: false,
      voices: Voices::from_start_request(
        bp, request, navigator, screen, token,
      )?,
    })
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::server::services::browser_profile_services::config::consts::LINUX_MOCK_PROFILE;

  fn create_base_profile() -> BrowserProfileFullData {
    LINUX_MOCK_PROFILE.clone()
  }

  fn create_navigator(locale: &str) -> Navigator {
    Navigator {
      locale: locale.to_string(),
      ..Navigator::default()
    }
  }

  #[test]
  fn test_windows_voices() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Windows;
    let request = StartRequest::get_mock();
    let navigator = create_navigator("en-US");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    // Windows should include voices matching the locale and en-US
    let voices = voices.0;
    assert!(!voices.is_empty());
    assert!(voices.iter().any(|v| v.language == "en-US"));
    Ok(())
  }

  #[test]
  fn test_windows_voices_non_english() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Windows;
    let request = StartRequest::get_mock();
    let navigator = create_navigator("en-CA");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    let voices = voices.0;
    assert!(!voices.is_empty());
    // Should include both en-CA and en-US voices
    assert!(voices.iter().any(|v| v.language == "en-CA"));
    assert!(voices.iter().any(|v| v.language == "en-US"));
    Ok(())
  }

  #[test]
  fn test_macos_voices() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Macos;
    let request = StartRequest::get_mock();
    let navigator = create_navigator("en-US");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    let voices = voices.0;
    assert!(!voices.is_empty());
    assert!(voices.iter().any(|v| v.language == "en-US"));
    Ok(())
  }

  #[test]
  fn test_macos_voices_non_english() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Macos;
    let request = StartRequest::get_mock();
    let navigator = create_navigator("fr-FR");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    let voices = voices.0;
    assert!(!voices.is_empty());
    // macOS implementation differs from Windows - it includes voices that
    // don't match the locale (except en-US)
    assert!(voices.iter().any(|v| v.language == "en-US"));
    Ok(())
  }

  #[test]
  fn test_linux_voices() -> Result<(), Error> {
    let profile = create_base_profile(); // Already Linux platform
    let request = StartRequest::get_mock();
    let navigator = create_navigator("en-US");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    // Linux should have no voices
    assert!(voices.0.is_empty());
    Ok(())
  }

  #[test]
  fn test_voice_structure() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform = Platform::Windows;
    let request = StartRequest::get_mock();
    let navigator = create_navigator("en-US");
    let screen = Screen::default();
    let token = String::new();

    let voices = Voices::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    // Check that voices have both language and name fields populated
    for voice in voices.0 {
      assert!(!voice.language.is_empty());
      assert!(!voice.name.is_empty());
    }
    Ok(())
  }
}