-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathlive_audio_test.cpp
More file actions
355 lines (280 loc) · 11.1 KB
/
live_audio_test.cpp
File metadata and controls
355 lines (280 loc) · 11.1 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <gtest/gtest.h>
#include "mock_core.h"
#include "mock_object_factory.h"
#include "foundry_local_exception.h"
#include "openai/openai_live_audio_types.h"
#include "openai/openai_live_audio_session.h"
#include <nlohmann/json.hpp>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
using namespace foundry_local;
using namespace foundry_local::Testing;
// ---------------------------------------------------------------------------
// LiveAudioTranscriptionResponse parsing tests
// ---------------------------------------------------------------------------
TEST(LiveAudioTypesTest, FromJson_BasicResponse) {
nlohmann::json j = {
{"text", "hello world"},
{"is_final", true},
{"start_time", 0.5},
{"end_time", 1.5}};
auto resp = LiveAudioTranscriptionResponse::FromJson(j.dump());
EXPECT_EQ("hello world", resp.text);
EXPECT_TRUE(resp.is_final);
ASSERT_TRUE(resp.start_time.has_value());
EXPECT_DOUBLE_EQ(0.5, resp.start_time.value());
ASSERT_TRUE(resp.end_time.has_value());
EXPECT_DOUBLE_EQ(1.5, resp.end_time.value());
EXPECT_FALSE(resp.id.has_value());
}
TEST(LiveAudioTypesTest, FromJson_WithId) {
nlohmann::json j = {
{"text", "hello"},
{"is_final", true},
{"id", "result-abc-123"}};
auto resp = LiveAudioTranscriptionResponse::FromJson(j.dump());
ASSERT_TRUE(resp.id.has_value());
EXPECT_EQ("result-abc-123", resp.id.value());
}
TEST(LiveAudioTypesTest, FromJson_WithoutId) {
nlohmann::json j = {
{"text", "hello"},
{"is_final", true}};
auto resp = LiveAudioTranscriptionResponse::FromJson(j.dump());
EXPECT_FALSE(resp.id.has_value());
}
TEST(LiveAudioTypesTest, FromJson_CamelCaseFields) {
nlohmann::json j = {
{"text", "test"},
{"isFinal", false},
{"startTime", 1.0},
{"endTime", 2.0}};
auto resp = LiveAudioTranscriptionResponse::FromJson(j.dump());
EXPECT_EQ("test", resp.text);
EXPECT_FALSE(resp.is_final);
ASSERT_TRUE(resp.start_time.has_value());
EXPECT_DOUBLE_EQ(1.0, resp.start_time.value());
}
TEST(LiveAudioTypesTest, FromJson_WithContent) {
nlohmann::json j = {
{"text", "hello"},
{"is_final", true},
{"content", {{{"text", "hi"}, {"transcript", "hi there"}}}}};
auto resp = LiveAudioTranscriptionResponse::FromJson(j.dump());
ASSERT_EQ(1u, resp.content.size());
EXPECT_EQ("hi", resp.content[0].text);
EXPECT_EQ("hi there", resp.content[0].transcript);
}
TEST(LiveAudioTypesTest, FromJson_EmptyJson) {
auto resp = LiveAudioTranscriptionResponse::FromJson("{}");
EXPECT_TRUE(resp.text.empty());
EXPECT_FALSE(resp.is_final);
EXPECT_FALSE(resp.start_time.has_value());
EXPECT_FALSE(resp.end_time.has_value());
EXPECT_TRUE(resp.content.empty());
}
TEST(LiveAudioTypesTest, CoreErrorResponse_TryParse_Valid) {
nlohmann::json j = {
{"code", "RATE_LIMITED"},
{"message", "Too many requests"},
{"is_transient", true}};
auto result = CoreErrorResponse::TryParse(j.dump());
ASSERT_TRUE(result.has_value());
EXPECT_EQ("RATE_LIMITED", result->code);
EXPECT_EQ("Too many requests", result->message);
EXPECT_TRUE(result->is_transient);
}
TEST(LiveAudioTypesTest, CoreErrorResponse_TryParse_Invalid) {
auto result = CoreErrorResponse::TryParse("not json");
EXPECT_FALSE(result.has_value());
}
// ---------------------------------------------------------------------------
// LiveAudioTranscriptionSession tests
// ---------------------------------------------------------------------------
class LiveAudioSessionTest : public ::testing::Test {
protected:
MockCore core_;
NullLogger logger_;
void SetUpStartHandlers(const std::string& sessionHandle = "session-123") {
core_.OnCall("audio_stream_start", sessionHandle);
}
void SetUpPushHandler(const std::string& responseJson = "") {
core_.OnCall("audio_stream_push",
[responseJson](std::string_view, const std::string*, NativeCallbackFn, void*) {
return responseJson;
});
}
void SetUpStopHandler() {
core_.OnCall("audio_stream_stop", "");
}
void SetUpAllHandlers(const std::string& pushResponse = "") {
SetUpStartHandlers();
SetUpPushHandler(pushResponse);
SetUpStopHandler();
}
};
TEST_F(LiveAudioSessionTest, ConstructorDefaults) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
EXPECT_FALSE(session.IsStarted());
EXPECT_FALSE(session.IsStopped());
EXPECT_EQ(16000, session.Settings().sample_rate);
EXPECT_EQ(1, session.Settings().channels);
EXPECT_EQ(16, session.Settings().bits_per_sample);
}
TEST_F(LiveAudioSessionTest, SettingsCanBeModifiedBeforeStart) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Settings().sample_rate = 44100;
session.Settings().channels = 2;
session.Settings().language = "en";
EXPECT_EQ(44100, session.Settings().sample_rate);
EXPECT_EQ(2, session.Settings().channels);
EXPECT_EQ("en", session.Settings().language.value());
}
TEST_F(LiveAudioSessionTest, Start_Success) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
EXPECT_TRUE(session.IsStarted());
EXPECT_FALSE(session.IsStopped());
EXPECT_EQ(16000, session.ActiveSettings().sample_rate);
session.Stop();
EXPECT_TRUE(session.IsStopped());
}
TEST_F(LiveAudioSessionTest, Start_WithCustomSettings) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Settings().sample_rate = 44100;
session.Settings().language = "fr";
session.Start();
EXPECT_EQ(44100, session.ActiveSettings().sample_rate);
EXPECT_EQ("fr", session.ActiveSettings().language.value());
// Verify the request included our settings
auto lastArg = core_.GetLastDataArg("audio_stream_start");
auto parsed = nlohmann::json::parse(lastArg);
EXPECT_EQ("44100", parsed["Params"]["SampleRate"].get<std::string>());
EXPECT_EQ("fr", parsed["Params"]["Language"].get<std::string>());
session.Stop();
}
TEST_F(LiveAudioSessionTest, Start_Failure) {
core_.OnCallThrow("audio_stream_start", "Connection refused");
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
EXPECT_THROW(session.Start(), Exception);
EXPECT_FALSE(session.IsStarted());
}
TEST_F(LiveAudioSessionTest, Start_EmptyHandle) {
core_.OnCall("audio_stream_start", "");
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
EXPECT_THROW(session.Start(), Exception);
EXPECT_FALSE(session.IsStarted());
}
TEST_F(LiveAudioSessionTest, DoubleStartThrows) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
EXPECT_THROW(session.Start(), Exception);
session.Stop();
}
TEST_F(LiveAudioSessionTest, AppendBeforeStartThrows) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
std::vector<uint8_t> data = {0, 1, 2, 3};
EXPECT_THROW(session.Append(data.data(), data.size()), Exception);
}
TEST_F(LiveAudioSessionTest, AppendAfterStopThrows) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
session.Stop();
std::vector<uint8_t> data = {0, 1, 2, 3};
EXPECT_THROW(session.Append(data.data(), data.size()), Exception);
}
TEST_F(LiveAudioSessionTest, Start_InvalidCapacityThrows) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Settings().push_queue_capacity = 0;
EXPECT_THROW(session.Start(), Exception);
}
TEST_F(LiveAudioSessionTest, StopParseFinalResponse) {
SetUpStartHandlers();
SetUpPushHandler();
// audio_stream_stop returns a final transcription result
nlohmann::json finalResponse = {
{"text", "final result"},
{"is_final", true}};
core_.OnCall("audio_stream_stop", finalResponse.dump());
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
session.Stop();
// The final result should be retrievable from the result queue
LiveAudioTranscriptionResponse result;
auto status = session.TryGetNext(result, std::chrono::milliseconds(100));
EXPECT_EQ(TranscriptionStatus::Result, status);
EXPECT_EQ("final result", result.text);
EXPECT_TRUE(result.is_final);
}
TEST_F(LiveAudioSessionTest, AppendAndGetResult) {
nlohmann::json pushResponse = {
{"text", "hello"},
{"is_final", false}};
SetUpAllHandlers(pushResponse.dump());
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
// Append some data
std::vector<uint8_t> data(320, 0);
session.Append(data.data(), data.size());
// Try to get a result
LiveAudioTranscriptionResponse result;
auto status = session.TryGetNext(result, std::chrono::seconds(2));
if (status == TranscriptionStatus::Result) {
EXPECT_EQ("hello", result.text);
EXPECT_FALSE(result.is_final);
}
session.Stop();
}
TEST_F(LiveAudioSessionTest, StopSendsCommand) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
session.Stop();
EXPECT_EQ(1, core_.GetCallCount("audio_stream_stop"));
auto lastArg = core_.GetLastDataArg("audio_stream_stop");
auto parsed = nlohmann::json::parse(lastArg);
EXPECT_EQ("session-123", parsed["Params"]["SessionHandle"].get<std::string>());
}
TEST_F(LiveAudioSessionTest, StopWhenNotStartedIsNoop) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Stop(); // Should not throw
EXPECT_EQ(0, core_.GetCallCount("audio_stream_stop"));
}
TEST_F(LiveAudioSessionTest, DoubleStopIsNoop) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
session.Stop();
session.Stop(); // Should not throw or send a second command
EXPECT_EQ(1, core_.GetCallCount("audio_stream_stop"));
}
TEST_F(LiveAudioSessionTest, DestructorStopsSession) {
SetUpAllHandlers();
{
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
// Destructor should call Stop
}
EXPECT_EQ(1, core_.GetCallCount("audio_stream_stop"));
}
TEST_F(LiveAudioSessionTest, TryGetNextTimeout) {
SetUpAllHandlers();
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
session.Start();
LiveAudioTranscriptionResponse result;
auto status = session.TryGetNext(result, std::chrono::milliseconds(50));
EXPECT_EQ(TranscriptionStatus::Timeout, status);
session.Stop();
}
TEST_F(LiveAudioSessionTest, GetErrorMessage_NoError) {
LiveAudioTranscriptionSession session(&core_, "whisper-model", &logger_);
EXPECT_TRUE(session.GetErrorMessage().empty());
}