super-voice-daemon/src/output/preview_tests.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | use super::*; |
3 | | |
4 | | #[test] |
5 | 1 | fn test_preprocess_text() { |
6 | | // Basic functionality |
7 | 1 | assert_eq!(preprocess_text("hello world", true), "Hello world"); |
8 | 1 | assert_eq!(preprocess_text("hello world", false), "Hello world."); |
9 | 1 | assert_eq!(preprocess_text("", true), ""); |
10 | | |
11 | 1 | assert_eq!(preprocess_text("...hello world", true), "Hello world"); |
12 | 1 | assert_eq!(preprocess_text(" ... hello world ", true), "Hello world"); |
13 | 1 | assert_eq!( |
14 | 1 | preprocess_text(" multiple spaces ", true), |
15 | 1 | "Multiple spaces" |
16 | | ); |
17 | 1 | } |
18 | | |
19 | | #[test] |
20 | 1 | fn preprocess_text_strips_unsafe_control_and_format_chars() { |
21 | | // Non-whitespace C0/C1 controls (ESC, BEL, NUL, backspace) are removed. |
22 | 1 | assert_eq!(preprocess_text("he\u{1b}[31mllo\u{07}", true), "He[31mllo"); |
23 | 1 | assert_eq!(preprocess_text("a\u{00}b\u{08}c", true), "Abc"); |
24 | | // Bidi overrides and zero-width chars are removed. |
25 | 1 | assert_eq!(preprocess_text("ab\u{202e}cd", true), "Abcd"); |
26 | 1 | assert_eq!(preprocess_text("wor\u{200b}d", true), "Word"); |
27 | | // Real whitespace (newline/tab) is preserved as a word separator, not glued. |
28 | 1 | assert_eq!( |
29 | 1 | preprocess_text("hello\nworld\tfoo", true), |
30 | 1 | "Hello world foo" |
31 | | ); |
32 | 1 | } |
33 | | |
34 | | #[test] |
35 | 1 | fn test_find_common_prefix() { |
36 | 1 | assert_eq!(find_common_prefix("hello world", "hello there"), 6); |
37 | 1 | assert_eq!(find_common_prefix("abc", "def"), 0); |
38 | 1 | assert_eq!(find_common_prefix("same text", "same text"), 9); |
39 | 1 | } |
40 | | |
41 | | #[test] |
42 | 1 | fn normalize_text_keeps_the_case_it_was_given() { |
43 | 1 | assert_eq!(normalize_text(" ... hello World "), "hello World"); |
44 | 1 | assert_eq!(normalize_text("he\u{1b}llo"), "hello"); |
45 | 1 | assert_eq!(normalize_text(" "), ""); |
46 | 1 | } |
47 | | |
48 | | #[test] |
49 | 1 | fn capitalize_first_uppercases_only_the_first_char() { |
50 | 1 | assert_eq!(capitalize_first(""), ""); |
51 | 1 | assert_eq!(capitalize_first("hello World"), "Hello World"); |
52 | | // ASCII only, as the transcript always was. |
53 | 1 | assert_eq!(capitalize_first("élan"), "élan"); |
54 | 1 | } |
55 | | |
56 | | // --------------------------------------------------------------------------- |
57 | | // Window stitching |
58 | | // --------------------------------------------------------------------------- |
59 | | |
60 | | /// The shared text is the end of the transcript and the start of the window. |
61 | | #[test] |
62 | 1 | fn find_overlap_locates_the_shared_text_at_the_seam() { |
63 | 1 | let session = "hello my name is jorge"; |
64 | 1 | let preview = "name is jorge and i like"; |
65 | 1 | let o = find_overlap(session, preview).expect("the windows share text"); |
66 | 1 | assert_eq!(&session[..o.session_end], "hello my name is jorge"); |
67 | 1 | assert_eq!(&preview[o.preview_end..], " and i like"); |
68 | 1 | } |
69 | | |
70 | | /// A window's first word is capitalized by the model as if it opened a |
71 | | /// sentence; that must not hide the overlap. |
72 | | #[test] |
73 | 1 | fn find_overlap_ignores_case() { |
74 | 1 | let session = "hello my name is Jorge"; |
75 | 1 | let preview = "Name is Jorge and I like"; |
76 | 1 | let o = find_overlap(session, preview).expect("case must not matter"); |
77 | 1 | assert_eq!(&preview[o.preview_end..], " and I like"); |
78 | 1 | } |
79 | | |
80 | | /// A run that ends well before the transcript's end is a coincidence, not the |
81 | | /// seam: stitching on it would cut the transcript there. |
82 | | #[test] |
83 | 1 | fn find_overlap_rejects_a_run_far_from_the_seam() { |
84 | 1 | assert_eq!( |
85 | 1 | find_overlap( |
86 | 1 | "the cat sat on the mat and then it slept for a very long time in the warm afternoon sun", |
87 | 1 | "the cat sat purring" |
88 | 1 | ), |
89 | 1 | None |
90 | | ); |
91 | 1 | } |
92 | | |
93 | | /// A small model rewords both ends of the shared stretch. "I'm trying |
94 | | /// whisper. It's more breathing timing" and "And a whisper, a small breathing |
95 | | /// tiny model" share "breathing" thirty characters short of the transcript's |
96 | | /// end and thirty into the window, and that is still the seam. |
97 | | #[test] |
98 | 1 | fn find_overlap_survives_a_reworded_seam() { |
99 | 1 | let session = "I'm trying whisper. It's more breathing timing"; |
100 | 1 | let preview = "And a whisper, a small breathing tiny model"; |
101 | 1 | let o = find_overlap(session, preview).expect("breathing is shared"); |
102 | | // The run is "breathing" plus whatever its neighbours happen to share |
103 | | // ("timing" and "tiny" both start with "ti"); where exactly it ends is |
104 | | // immaterial as long as the transcript keeps its text up to there and the |
105 | | // window supplies the rest. |
106 | 1 | assert!( |
107 | 1 | session[..o.session_end].contains("more breathing"), |
108 | | "run ends at {:?}", |
109 | | &session[..o.session_end] |
110 | | ); |
111 | 1 | assert!( |
112 | 1 | preview[o.preview_end..].ends_with("ny model"), |
113 | | "window continues with {:?}", |
114 | | &preview[o.preview_end..] |
115 | | ); |
116 | 1 | } |
117 | | |
118 | | /// A whole word exactly at the seam is the seam, however short. |
119 | | #[test] |
120 | 1 | fn find_overlap_accepts_a_whole_word_exactly_at_the_seam() { |
121 | 1 | let o = find_overlap("i went to the", "the store").expect("the seam word"); |
122 | 1 | assert_eq!(o.session_end, 13); |
123 | 1 | assert_eq!(o.preview_end, 3); |
124 | | // The transcript's closing period does not move the seam. |
125 | 1 | let o = find_overlap("saying to text.", "Text right now").expect("the seam word"); |
126 | 1 | assert_eq!(o.session_end, 14); |
127 | 1 | assert_eq!(o.preview_end, 4); |
128 | 1 | } |
129 | | |
130 | | /// Short runs that do not sit at the seam are still coincidences. |
131 | | #[test] |
132 | 1 | fn find_overlap_rejects_a_short_run_away_from_the_seam() { |
133 | | // "like" ends the transcript but the window does not start with it. |
134 | 1 | assert_eq!(find_overlap("and i like", "we like it"), None); |
135 | | // Two characters are never enough, even at the seam. |
136 | 1 | assert_eq!(find_overlap("i went to", "to be"), None); |
137 | 1 | } |
138 | | |
139 | | /// Two equally long runs: "hello " at the window's start and " world" deeper |
140 | | /// in. The window's head is the shared audio, so the first wins — picking the |
141 | | /// second would splice nothing on and freeze the preview. |
142 | | #[test] |
143 | 1 | fn find_overlap_prefers_the_run_at_the_windows_start() { |
144 | 1 | let o = find_overlap("hello world", "hello there world").expect("runs exist"); |
145 | 1 | assert_eq!(o.session_end, 6); |
146 | 1 | assert_eq!(o.preview_end, 6); |
147 | 1 | } |
148 | | |
149 | | /// The offsets are byte offsets: slicing with them must not split a multibyte |
150 | | /// char. |
151 | | #[test] |
152 | 1 | fn find_overlap_returns_byte_offsets_safe_for_slicing() { |
153 | 1 | let session = "café au lait"; |
154 | 1 | let preview = "au lait please"; |
155 | 1 | let o = find_overlap(session, preview).expect("shared text"); |
156 | 1 | assert_eq!(&session[..o.session_end], "café au lait"); |
157 | 1 | assert_eq!(&preview[o.preview_end..], " please"); |
158 | 1 | } |
159 | | |
160 | | #[test] |
161 | 1 | fn merge_starts_the_transcript_from_the_first_window() { |
162 | 1 | assert_eq!(merge_window_preview("", "hello"), "hello"); |
163 | 1 | } |
164 | | |
165 | | /// Early in a take every window is a re-read of the whole take so far. |
166 | | #[test] |
167 | 1 | fn merge_extends_when_the_window_is_a_re_read_of_the_whole_take() { |
168 | 1 | assert_eq!( |
169 | 1 | merge_window_preview("hello my name", "hello my name is jorge"), |
170 | 1 | "hello my name is jorge" |
171 | | ); |
172 | 1 | } |
173 | | |
174 | | /// The model gave the first window lowercase and the second a capital. Same |
175 | | /// speech; the second extends the first. This used to type "After After". |
176 | | #[test] |
177 | 1 | fn merge_extends_when_the_re_read_differs_only_in_case() { |
178 | 1 | assert_eq!( |
179 | 1 | merge_window_preview("after", "After doing the first review"), |
180 | 1 | "After doing the first review" |
181 | | ); |
182 | 1 | } |
183 | | |
184 | | /// A window that ends at a pause gets a period the next re-read does not |
185 | | /// repeat mid-sentence. |
186 | | #[test] |
187 | 1 | fn merge_extends_past_the_windows_closing_punctuation() { |
188 | 1 | assert_eq!( |
189 | 1 | merge_window_preview("after doing.", "After doing the first review"), |
190 | 1 | "After doing the first review" |
191 | | ); |
192 | 1 | } |
193 | | |
194 | | #[test] |
195 | 1 | fn merge_keeps_the_transcript_when_a_shorter_re_read_differs_only_in_case() { |
196 | 1 | assert_eq!( |
197 | 1 | merge_window_preview("After doing the first review", "after."), |
198 | 1 | "After doing the first review" |
199 | | ); |
200 | 1 | } |
201 | | |
202 | | #[test] |
203 | 1 | fn merge_keeps_the_transcript_when_the_window_is_a_shorter_re_read() { |
204 | 1 | assert_eq!( |
205 | 1 | merge_window_preview("hello my name is jorge", "hello my name"), |
206 | 1 | "hello my name is jorge" |
207 | | ); |
208 | 1 | } |
209 | | |
210 | | #[test] |
211 | 1 | fn merge_continues_from_the_shared_text() { |
212 | 1 | assert_eq!( |
213 | 1 | merge_window_preview("hello my name is jorge", "name is jorge and i like"), |
214 | 1 | "hello my name is jorge and i like" |
215 | | ); |
216 | 1 | } |
217 | | |
218 | | /// The previous window ended mid-word. The new window heard the whole word, |
219 | | /// and its reading replaces the fragment. |
220 | | #[test] |
221 | 1 | fn merge_takes_the_windows_reading_of_a_word_cut_at_the_seam() { |
222 | 1 | assert_eq!( |
223 | 1 | merge_window_preview("hello my name is jor", "my name is jorge and i"), |
224 | 1 | "hello my name is jorge and i" |
225 | | ); |
226 | 1 | } |
227 | | |
228 | | /// A window that ends inside the shared text has nothing new; the transcript |
229 | | /// must not lose what it already had. |
230 | | #[test] |
231 | 1 | fn merge_never_shrinks_the_transcript() { |
232 | 1 | assert_eq!( |
233 | 1 | merge_window_preview("hello my name is jorge", "name is jorge"), |
234 | 1 | "hello my name is jorge" |
235 | | ); |
236 | 1 | assert_eq!( |
237 | 1 | merge_window_preview("my name is jorge washington", "name is jorge"), |
238 | 1 | "my name is jorge washington" |
239 | | ); |
240 | 1 | } |
241 | | |
242 | | /// What the whisper take produced as "…to text. text right now". |
243 | | #[test] |
244 | 1 | fn merge_stitches_a_whole_word_at_the_seam() { |
245 | 1 | assert_eq!( |
246 | 1 | merge_window_preview("what I'm saying to text.", "Text right now"), |
247 | 1 | "what I'm saying to text right now" |
248 | | ); |
249 | 1 | } |
250 | | |
251 | | #[test] |
252 | 1 | fn merge_continues_from_a_reworded_seam() { |
253 | 1 | assert_eq!( |
254 | 1 | merge_window_preview( |
255 | 1 | "I'm trying whisper. It's more breathing timing", |
256 | 1 | "And a whisper, a small breathing tiny model" |
257 | 1 | ), |
258 | 1 | "I'm trying whisper. It's more breathing tiny model" |
259 | | ); |
260 | 1 | } |
261 | | |
262 | | /// No shared text at all: append rather than stall. The stitched preview may |
263 | | /// repeat a word; a preview that stops growing looks like recognition died. |
264 | | #[test] |
265 | 1 | fn merge_appends_when_nothing_is_shared() { |
266 | 1 | assert_eq!( |
267 | 1 | merge_window_preview("hello there", "how are you"), |
268 | 1 | "hello there how are you" |
269 | | ); |
270 | 1 | } |