-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathstream_dispatch_unit.cpp
More file actions
1591 lines (1390 loc) · 61.4 KB
/
Copy pathstream_dispatch_unit.cpp
File metadata and controls
1591 lines (1390 loc) · 61.4 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// stream_dispatch_unit.cpp - dispatcher-level streaming entry-point tests.
//
// These exercise the parts of the streaming surface that do not depend
// on a loaded model: state machine rejections, update zero-init,
// nullable-update handling, transcribe_run rejection while ACTIVE,
// reset behavior from each state, and the safe-sentinel returns of the
// streaming accessors when session is NULL or in IDLE.
//
// Capability-gated paths (NOT_IMPLEMENTED for non-streaming families,
// TRANSLATE rejection, language validation) require a real model and
// are covered by stream_capability_unit.cpp.
#include "transcribe-arch.h"
#include "transcribe-model.h"
#include "transcribe-session.h"
#include "transcribe.h"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
namespace {
int g_failures = 0;
int g_begin_calls = 0;
const char * g_feed_texts[8] = {};
int g_n_feed_texts = 0;
int g_feed_text_i = 0;
const char * g_finalize_text = "";
const char * g_token_texts[8] = {};
int g_n_token_rows = 0;
int g_forced_n_committed_tokens = -1;
int g_forced_n_committed_words = -1;
int g_forced_n_committed_segments = -1;
int64_t g_feed_audio_committed_us = 0;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
++g_failures; \
} \
} while (0)
void reset_sequence_globals() {
for (int i = 0; i < 8; ++i) {
g_feed_texts[i] = nullptr;
g_token_texts[i] = nullptr;
}
g_n_feed_texts = 0;
g_feed_text_i = 0;
g_finalize_text = "";
g_n_token_rows = 0;
g_forced_n_committed_tokens = -1;
g_forced_n_committed_words = -1;
g_forced_n_committed_segments = -1;
g_feed_audio_committed_us = 0;
}
void test_accessors_on_null_ctx() {
CHECK(transcribe_stream_get_state(nullptr) == TRANSCRIBE_STREAM_IDLE);
CHECK(transcribe_stream_revision(nullptr) == 0);
CHECK(transcribe_stream_n_committed_segments(nullptr) == 0);
CHECK(transcribe_stream_n_committed_words(nullptr) == 0);
CHECK(transcribe_stream_n_committed_tokens(nullptr) == 0);
CHECK(transcribe_stream_last_status(nullptr) == TRANSCRIBE_OK);
}
void test_accessors_on_idle_ctx() {
transcribe_session session;
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_IDLE);
CHECK(transcribe_stream_revision(&session) == 0);
CHECK(transcribe_stream_n_committed_segments(&session) == 0);
CHECK(transcribe_stream_n_committed_words(&session) == 0);
CHECK(transcribe_stream_n_committed_tokens(&session) == 0);
CHECK(transcribe_stream_last_status(&session) == TRANSCRIBE_OK);
}
void test_default_params() {
transcribe_stream_params p;
transcribe_stream_params_init(&p);
CHECK(p.family == nullptr);
CHECK(p.struct_size == sizeof(transcribe_stream_params));
}
void test_begin_null_args() {
// session == NULL is still rejected.
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
CHECK(transcribe_stream_begin(nullptr, &rp, &sp) == TRANSCRIBE_ERR_INVALID_ARG);
// NULL run/stream params now mean "all defaults", so they are no
// longer rejected as INVALID_ARG. With this model-less context the
// call proceeds past parameter validation to the model/capability
// gate (NOT_IMPLEMENTED); the point under test is that NULL params
// is accepted, not the downstream gate's exact code.
transcribe_session session;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) != TRANSCRIBE_ERR_INVALID_ARG);
CHECK(transcribe_stream_begin(&session, &rp, nullptr) != TRANSCRIBE_ERR_INVALID_ARG);
// None of these should have moved the context off IDLE.
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_IDLE);
}
void test_begin_rejected_when_active() {
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
CHECK(transcribe_stream_begin(&session, &rp, &sp) == TRANSCRIBE_ERR_INVALID_ARG);
// Still ACTIVE — rejection must not clear lifecycle.
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_ACTIVE);
}
void test_begin_no_model_returns_not_implemented() {
// session with no model still reaches the model/arch check.
transcribe_session session;
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
CHECK(transcribe_stream_begin(&session, &rp, &sp) == TRANSCRIBE_ERR_NOT_IMPLEMENTED);
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_IDLE);
}
transcribe_status fake_stream_begin(transcribe_session * session,
const transcribe_run_params * run_params,
const transcribe_stream_params * stream_params) {
(void) session;
(void) run_params;
(void) stream_params;
++g_begin_calls;
return TRANSCRIBE_OK;
}
transcribe_status fake_stream_feed(transcribe_session * session,
const float * pcm,
int n_samples,
transcribe_stream_update * update) {
(void) session;
(void) pcm;
(void) n_samples;
(void) update;
return TRANSCRIBE_OK;
}
transcribe_status fake_stream_finalize(transcribe_session * session, transcribe_stream_update * update) {
(void) session;
(void) update;
return TRANSCRIBE_OK;
}
transcribe_status fake_sequence_stream_begin(transcribe_session * session,
const transcribe_run_params * run_params,
const transcribe_stream_params * stream_params) {
(void) session;
(void) run_params;
(void) stream_params;
g_feed_text_i = 0;
return TRANSCRIBE_OK;
}
transcribe_status fake_sequence_stream_feed(transcribe_session * session,
const float * pcm,
int n_samples,
transcribe_stream_update * update) {
(void) pcm;
(void) n_samples;
(void) update;
if (g_feed_text_i < g_n_feed_texts) {
session->full_text = g_feed_texts[g_feed_text_i++];
session->has_result = true;
if (g_n_token_rows > 0) {
session->tokens.clear();
for (int i = 0; i < g_n_token_rows; ++i) {
transcribe_session::TokenEntry tok;
tok.text = g_token_texts[i] != nullptr ? g_token_texts[i] : "";
session->tokens.push_back(tok);
}
}
}
session->stream_audio_committed_us = g_feed_audio_committed_us;
if (g_forced_n_committed_tokens >= 0) {
session->n_committed_tokens = g_forced_n_committed_tokens;
}
if (g_forced_n_committed_words >= 0) {
session->n_committed_words = g_forced_n_committed_words;
}
if (g_forced_n_committed_segments >= 0) {
session->n_committed_segments = g_forced_n_committed_segments;
}
if (update != nullptr) {
update->audio_committed_ms = g_feed_audio_committed_us / 1000;
}
return TRANSCRIBE_OK;
}
transcribe_status fake_sequence_stream_finalize(transcribe_session * session, transcribe_stream_update * update) {
(void) update;
session->full_text = g_finalize_text != nullptr ? g_finalize_text : "";
session->has_result = true;
return TRANSCRIBE_OK;
}
bool fake_accepts_no_ext(const transcribe_model * model, transcribe_ext_slot slot, uint32_t kind) {
(void) model;
(void) slot;
(void) kind;
return false;
}
int g_validate_calls = 0;
// stream_validate that always rejects, modeling a family preflight that
// found a bad extension value. Must run BEFORE the dispatcher clears the
// snapshot; must NOT lead to stream_begin being called.
transcribe_status fake_stream_validate_reject(const transcribe_session * session,
const transcribe_run_params * run_params,
const transcribe_stream_params * stream_params) {
(void) session;
(void) run_params;
(void) stream_params;
++g_validate_calls;
return TRANSCRIBE_ERR_INVALID_ARG;
}
void test_begin_rejects_unknown_ext_kind_before_hook() {
const transcribe::Arch arch = {
"fake-stream",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
nullptr, // stream_validate
fake_stream_begin,
fake_stream_feed,
fake_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
transcribe_model model;
model.arch = &arch;
model.caps.supports_streaming = true;
model.caps.max_timestamp_kind = TRANSCRIBE_TIMESTAMPS_NONE;
transcribe_session session;
session.model = &model;
session.has_result = true;
session.full_text = "previous result";
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
const transcribe_ext ext = { sizeof(transcribe_ext), 0x58585858u };
sp.family = &ext;
g_begin_calls = 0;
CHECK(transcribe_stream_begin(&session, &rp, &sp) == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(g_begin_calls == 0);
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_IDLE);
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
}
void test_begin_rejects_tiny_ext_before_hook() {
const transcribe::Arch arch = {
"fake-stream",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
nullptr, // stream_validate
fake_stream_begin,
fake_stream_feed,
fake_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
transcribe_model model;
model.arch = &arch;
model.caps.supports_streaming = true;
model.caps.max_timestamp_kind = TRANSCRIBE_TIMESTAMPS_NONE;
transcribe_session session;
session.model = &model;
session.has_result = true;
session.full_text = "previous result";
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
transcribe_ext ext = { 0, 0 };
sp.family = &ext;
g_begin_calls = 0;
CHECK(transcribe_stream_begin(&session, &rp, &sp) == TRANSCRIBE_ERR_BAD_STRUCT_SIZE);
CHECK(g_begin_calls == 0);
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_IDLE);
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
}
// A family preflight (stream_validate) that rejects must behave like the
// other pre-hook failures: the previous result snapshot is preserved, the
// lifecycle stays put (no FAILED transition), and stream_begin is never
// called. This pins the dispatcher contract that lets a family validate
// extension values before the snapshot is cleared (see transcribe-arch.h
// stream_validate). Without it, a caller-side config typo would destroy
// the prior utterance's transcript.
void test_begin_family_preflight_reject_preserves_snapshot() {
const transcribe::Arch arch = {
"fake-stream",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
fake_stream_validate_reject, // stream_validate rejects
fake_stream_begin,
fake_stream_feed,
fake_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
transcribe_model model;
model.arch = &arch;
model.caps.supports_streaming = true;
model.caps.max_timestamp_kind = TRANSCRIBE_TIMESTAMPS_NONE;
// Seed a session that previously FAILED, carrying a non-OK
// last_status and a partial snapshot. Using a non-OK seed (rather
// than the default OK) is deliberate: it proves the dispatcher
// leaves last_status UNTOUCHED on a preflight reject, not that it
// forces OK. A test that started from OK could not tell the two
// apart.
transcribe_session session;
session.model = &model;
session.has_result = true;
session.full_text = "previous result";
session.stream_state = TRANSCRIBE_STREAM_FAILED; // a prior failed stream
session.stream_last_status = TRANSCRIBE_ERR_BACKEND; // its failing status
transcribe_run_params rp;
transcribe_run_params_init(&rp);
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
g_validate_calls = 0;
g_begin_calls = 0;
CHECK(transcribe_stream_begin(&session, &rp, &sp) == TRANSCRIBE_ERR_INVALID_ARG);
// Preflight ran, begin did not.
CHECK(g_validate_calls == 1);
CHECK(g_begin_calls == 0);
// Snapshot preserved, lifecycle untouched (NOT cleared, NOT moved).
CHECK(session.has_result);
CHECK(session.full_text == "previous result");
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_FAILED);
// last_status untouched — the prior non-OK value survives a
// pre-clear rejection unchanged (the dispatcher does not reset it
// to OK, nor overwrite it with the rejection's status).
CHECK(transcribe_stream_last_status(&session) == TRANSCRIBE_ERR_BACKEND);
}
void test_feed_rejects_idle() {
transcribe_session session;
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
upd.result_changed = true; // dirty sentinel — must be zeroed
upd.is_final = true;
upd.revision = 42;
upd.input_received_ms = 999;
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_ERR_INVALID_ARG);
// Dispatcher zero-inits update before the state check returns,
// preserving the caller's struct_size.
CHECK(upd.struct_size == sizeof(transcribe_stream_update));
CHECK(upd.result_changed == false);
CHECK(upd.is_final == false);
CHECK(upd.revision == 0);
CHECK(upd.input_received_ms == 0);
}
void test_feed_rejects_finished_and_failed() {
transcribe_session session;
float pcm = 0.0f;
session.stream_state = TRANSCRIBE_STREAM_FINISHED;
CHECK(transcribe_stream_feed(&session, &pcm, 1, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(session.stream_state == TRANSCRIBE_STREAM_FINISHED);
session.stream_state = TRANSCRIBE_STREAM_FAILED;
CHECK(transcribe_stream_feed(&session, &pcm, 1, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(session.stream_state == TRANSCRIBE_STREAM_FAILED);
}
void test_feed_rejects_null_ctx_and_bad_input() {
float pcm = 0.0f;
CHECK(transcribe_stream_feed(nullptr, &pcm, 1, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
// Negative n_samples
CHECK(transcribe_stream_feed(&session, &pcm, -1, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
// Zero n_samples — polling without audio goes through the
// accessors, not feed.
CHECK(transcribe_stream_feed(&session, &pcm, 0, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
// pcm NULL is rejected unconditionally, regardless of n_samples.
CHECK(transcribe_stream_feed(&session, nullptr, 16000, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(transcribe_stream_feed(&session, nullptr, 0, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
// The ACTIVE state survives every malformed-input rejection
// because the dispatcher returns before the family hook runs.
CHECK(transcribe_stream_get_state(&session) == TRANSCRIBE_STREAM_ACTIVE);
}
void test_feed_active_no_hook_returns_not_implemented() {
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
float pcm = 0.0f;
// No model → defensive NOT_IMPLEMENTED branch.
CHECK(transcribe_stream_feed(&session, &pcm, 1, nullptr) == TRANSCRIBE_ERR_NOT_IMPLEMENTED);
}
void test_finalize_rejects_non_active() {
transcribe_session session;
// IDLE
CHECK(transcribe_stream_finalize(&session, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
session.stream_state = TRANSCRIBE_STREAM_FINISHED;
CHECK(transcribe_stream_finalize(&session, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
session.stream_state = TRANSCRIBE_STREAM_FAILED;
CHECK(transcribe_stream_finalize(&session, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
CHECK(transcribe_stream_finalize(nullptr, nullptr) == TRANSCRIBE_ERR_INVALID_ARG);
}
void test_finalize_update_zeroinit() {
transcribe_session session;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
upd.result_changed = true;
upd.is_final = false; // will be cleared then forced true on success path
upd.revision = 99;
upd.audio_committed_ms = 7;
// No model + ACTIVE → returns NOT_IMPLEMENTED, but the dispatcher
// zero-inits update before returning so the caller sees a clean
// struct (is_final is only forced true after a hook call, which
// doesn't happen here — verify the early-return path leaves
// update fully zeroed and preserves the caller's struct_size).
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
CHECK(transcribe_stream_finalize(&session, &upd) == TRANSCRIBE_ERR_NOT_IMPLEMENTED);
CHECK(upd.struct_size == sizeof(transcribe_stream_update));
CHECK(upd.result_changed == false);
CHECK(upd.is_final == false);
CHECK(upd.revision == 0);
CHECK(upd.audio_committed_ms == 0);
}
void test_reset_from_each_state() {
// IDLE: no-op transition, must remain IDLE.
{
transcribe_session session;
transcribe_stream_reset(&session);
CHECK(session.stream_state == TRANSCRIBE_STREAM_IDLE);
}
// FINISHED: clears result + counters, returns to IDLE.
{
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_FINISHED;
session.has_result = true;
session.full_text = "stale";
session.stream_revision = 7;
session.n_committed_tokens = 5;
session.stream_last_status = TRANSCRIBE_ERR_OOM;
session.stream_audio_input_us = 12345;
transcribe_stream_reset(&session);
CHECK(session.stream_state == TRANSCRIBE_STREAM_IDLE);
CHECK(session.has_result == false);
CHECK(session.full_text.empty());
CHECK(session.stream_revision == 0);
CHECK(session.n_committed_tokens == 0);
CHECK(session.stream_last_status == TRANSCRIBE_OK);
CHECK(session.stream_audio_input_us == 0);
}
// FAILED: same as FINISHED — clear everything, back to IDLE.
{
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_FAILED;
session.stream_last_status = TRANSCRIBE_ERR_ABORTED;
session.was_aborted = true;
session.n_committed_segments = 3;
transcribe_stream_reset(&session);
CHECK(session.stream_state == TRANSCRIBE_STREAM_IDLE);
CHECK(session.stream_last_status == TRANSCRIBE_OK);
CHECK(session.was_aborted == false);
CHECK(session.n_committed_segments == 0);
}
// NULL session — no-op.
transcribe_stream_reset(nullptr);
}
void test_run_rejected_while_active() {
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
session.has_result = true;
session.full_text = "active stream result";
transcribe_run_params rp;
transcribe_run_params_init(&rp);
float pcm = 0.0f;
const transcribe_status st = transcribe_run(&session, &pcm, 1, &rp);
CHECK(st == TRANSCRIBE_ERR_INVALID_ARG);
// Rejection must NOT clear the active stream's result snapshot.
CHECK(session.has_result);
CHECK(session.full_text == "active stream result");
CHECK(session.stream_state == TRANSCRIBE_STREAM_ACTIVE);
}
void test_run_clears_stream_snapshot_from_finished() {
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_FINISHED;
session.stream_revision = 12;
session.n_committed_tokens = 4;
session.stream_last_status = TRANSCRIBE_ERR_BACKEND;
session.stream_audio_input_us = 55555;
session.has_result = true;
session.full_text = "old run text";
transcribe_run_params rp;
transcribe_run_params_init(&rp);
float pcm = 0.0f;
// No model → NOT_IMPLEMENTED, but clear_result + state reset must
// have already run by the time we get here.
const transcribe_status st = transcribe_run(&session, &pcm, 1, &rp);
CHECK(st == TRANSCRIBE_ERR_NOT_IMPLEMENTED);
CHECK(session.has_result == false);
CHECK(session.full_text.empty());
CHECK(session.stream_revision == 0);
CHECK(session.n_committed_tokens == 0);
CHECK(session.stream_last_status == TRANSCRIBE_OK);
CHECK(session.stream_audio_input_us == 0);
CHECK(session.stream_state == TRANSCRIBE_STREAM_IDLE);
}
void test_clear_result_preserves_lifecycle() {
// clear_result is called by run AND begin and explicitly preserves
// stream_state — only the streaming dispatcher transitions
// lifecycle. Verify directly so a future refactor doesn't silently
// tangle the two.
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_ACTIVE;
session.stream_revision = 9;
session.n_committed_words = 2;
session.has_result = true;
session.full_text = "snapshot";
session.clear_result();
CHECK(session.stream_state == TRANSCRIBE_STREAM_ACTIVE);
CHECK(session.has_result == false);
CHECK(session.full_text.empty());
CHECK(session.stream_revision == 0);
CHECK(session.n_committed_words == 0);
}
void test_last_status_survives_reads() {
transcribe_session session;
session.stream_state = TRANSCRIBE_STREAM_FAILED;
session.stream_last_status = TRANSCRIBE_ERR_ABORTED;
CHECK(transcribe_stream_last_status(&session) == TRANSCRIBE_ERR_ABORTED);
// Multiple reads do not consume or alter the value.
CHECK(transcribe_stream_last_status(&session) == TRANSCRIBE_ERR_ABORTED);
CHECK(session.stream_state == TRANSCRIBE_STREAM_FAILED);
}
const transcribe::Arch & sequence_arch() {
static const transcribe::Arch arch = {
"fake-sequence-stream",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
nullptr,
fake_sequence_stream_begin,
fake_sequence_stream_feed,
fake_sequence_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
return arch;
}
const transcribe::Arch & sequence_arch_named_parakeet() {
static const transcribe::Arch arch = {
"parakeet",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
nullptr,
fake_sequence_stream_begin,
fake_sequence_stream_feed,
fake_sequence_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
return arch;
}
const transcribe::Arch & sequence_arch_named_moonshine_streaming() {
static const transcribe::Arch arch = {
"moonshine_streaming",
nullptr,
nullptr,
nullptr,
nullptr, // run_batch
nullptr,
fake_sequence_stream_begin,
fake_sequence_stream_feed,
fake_sequence_stream_finalize,
nullptr,
fake_accepts_no_ext,
};
return arch;
}
void init_streaming_model(transcribe_model & model) {
reset_sequence_globals();
model.arch = &sequence_arch();
model.caps.supports_streaming = true;
model.caps.max_timestamp_kind = TRANSCRIBE_TIMESTAMPS_NONE;
}
void test_stream_text_on_finalize_policy() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "hello wor";
g_n_feed_texts = 1;
g_finalize_text = "hello world";
g_feed_audio_committed_us = 123000;
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
sp.commit_policy = TRANSCRIBE_STREAM_COMMIT_ON_FINALIZE;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.result_changed);
CHECK(!upd.committed_changed);
CHECK(upd.tentative_changed);
CHECK(upd.audio_committed_ms == 0);
CHECK(transcribe_stream_n_committed_tokens(&session) == 0);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hello wor") == 0);
CHECK(std::strcmp(text.committed_text, "") == 0);
CHECK(std::strcmp(text.tentative_text, "hello wor") == 0);
transcribe_stream_update fin;
transcribe_stream_update_init(&fin);
CHECK(transcribe_stream_finalize(&session, &fin) == TRANSCRIBE_OK);
CHECK(fin.is_final);
CHECK(fin.result_changed);
CHECK(fin.committed_changed);
CHECK(fin.tentative_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hello world") == 0);
CHECK(std::strcmp(text.committed_text, "hello world") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
}
void test_stream_auto_known_family_uses_family_stable_prefix() {
transcribe_model model;
init_streaming_model(model);
model.arch = &sequence_arch_named_parakeet();
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "family stable";
g_n_feed_texts = 1;
g_n_token_rows = 1;
g_token_texts[0] = "family stable";
g_forced_n_committed_tokens = 1;
CHECK(transcribe_stream_begin(&session, nullptr, nullptr) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "family stable") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
}
void test_stream_auto_unknown_family_falls_back_to_generic_stable_prefix() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "generic stable";
g_feed_texts[1] = "generic stable";
g_feed_texts[2] = "generic stable";
g_n_feed_texts = 3;
g_n_token_rows = 1;
g_token_texts[0] = "generic stable";
g_forced_n_committed_tokens = 1;
CHECK(transcribe_stream_begin(&session, nullptr, nullptr) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "") == 0);
CHECK(std::strcmp(text.tentative_text, "generic stable") == 0);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(!upd.committed_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "") == 0);
CHECK(std::strcmp(text.tentative_text, "generic stable") == 0);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.committed_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "generic stable") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
}
void test_stream_stable_prefix_known_family_uses_family_boundary() {
transcribe_model model;
init_streaming_model(model);
model.arch = &sequence_arch_named_moonshine_streaming();
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "token stable";
g_n_feed_texts = 1;
g_n_token_rows = 1;
g_token_texts[0] = "token stable";
g_forced_n_committed_tokens = 1;
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
sp.commit_policy = TRANSCRIBE_STREAM_COMMIT_STABLE_PREFIX;
sp.stable_prefix_agreement_n = 3;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.committed_changed);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "token stable") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
}
void test_stream_family_token_prefix_maps_trimmed_leading_space() {
transcribe_model model;
init_streaming_model(model);
model.arch = &sequence_arch_named_moonshine_streaming();
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "And";
g_n_feed_texts = 1;
g_n_token_rows = 1;
g_token_texts[0] = " And";
g_forced_n_committed_tokens = 1;
CHECK(transcribe_stream_begin(&session, nullptr, nullptr) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.committed_changed);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "And") == 0);
CHECK(std::strcmp(text.committed_text, "And") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
}
void test_stream_text_stable_prefix_does_not_rewrite_committed() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "hello wor";
g_feed_texts[1] = "hello world";
g_feed_texts[2] = "hullo world!";
g_n_feed_texts = 3;
g_finalize_text = "hullo world!!";
g_feed_audio_committed_us = 0;
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
sp.commit_policy = TRANSCRIBE_STREAM_COMMIT_STABLE_PREFIX;
sp.stable_prefix_agreement_n = 2;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "") == 0);
CHECK(std::strcmp(text.tentative_text, "hello wor") == 0);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.committed_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hello world") == 0);
CHECK(std::strcmp(text.committed_text, "hello wor") == 0);
CHECK(std::strcmp(text.tentative_text, "ld") == 0);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(!upd.committed_changed);
CHECK(upd.tentative_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hullo world!") == 0);
CHECK(std::strcmp(text.committed_text, "hello wor") == 0);
CHECK(std::strcmp(text.tentative_text, "ld!") == 0);
transcribe_stream_update fin;
transcribe_stream_update_init(&fin);
CHECK(transcribe_stream_finalize(&session, &fin) == TRANSCRIBE_OK);
CHECK(!fin.committed_changed);
CHECK(fin.tentative_changed);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hullo world!!") == 0);
CHECK(std::strcmp(text.committed_text, "hello wor") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
CHECK(text.full_text_bytes == std::strlen("hullo world!!"));
CHECK(text.raw_tentative_start_bytes == text.full_text_bytes);
}
void test_stream_text_clamps_raw_tentative_offset_after_shrink() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "hello wor";
g_feed_texts[1] = "hello world";
g_feed_texts[2] = "hi";
g_n_feed_texts = 3;
g_finalize_text = "hi";
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
sp.commit_policy = TRANSCRIBE_STREAM_COMMIT_STABLE_PREFIX;
sp.stable_prefix_agreement_n = 2;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.committed_text, "hello wor") == 0);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, "hi") == 0);
CHECK(std::strcmp(text.committed_text, "hello wor") == 0);
CHECK(std::strcmp(text.tentative_text, "") == 0);
CHECK(text.full_text_bytes == 2);
CHECK(text.raw_tentative_start_bytes == text.full_text_bytes);
}
void test_stream_committed_row_hints_can_exceed_current_rows() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
g_feed_texts[0] = "abc";
g_n_feed_texts = 1;
g_n_token_rows = 1;
g_token_texts[0] = "abc";
g_forced_n_committed_tokens = 7;
g_forced_n_committed_words = 3;
g_forced_n_committed_segments = 2;
CHECK(transcribe_stream_begin(&session, nullptr, nullptr) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(transcribe_n_tokens(&session) == 1);
CHECK(transcribe_stream_n_committed_tokens(&session) == 7);
CHECK(transcribe_stream_n_committed_words(&session) == 3);
CHECK(transcribe_stream_n_committed_segments(&session) == 2);
}
void test_stream_text_stable_prefix_floors_utf8_boundary() {
transcribe_model model;
init_streaming_model(model);
transcribe_session session;
session.model = &model;
static const char cafe_acute[] = "caf\303\251"; // cafe + U+00E9
static const char cafe_circ[] = "caf\303\252"; // cafe + U+00EA
g_feed_texts[0] = cafe_acute;
g_feed_texts[1] = cafe_circ;
g_n_feed_texts = 2;
g_finalize_text = cafe_circ;
g_feed_audio_committed_us = 0;
transcribe_stream_params sp;
transcribe_stream_params_init(&sp);
sp.commit_policy = TRANSCRIBE_STREAM_COMMIT_STABLE_PREFIX;
sp.stable_prefix_agreement_n = 2;
CHECK(transcribe_stream_begin(&session, nullptr, &sp) == TRANSCRIBE_OK);
float pcm = 0.0f;
transcribe_stream_update upd;
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
transcribe_stream_update_init(&upd);
CHECK(transcribe_stream_feed(&session, &pcm, 1, &upd) == TRANSCRIBE_OK);
CHECK(upd.committed_changed);
transcribe_stream_text text;
transcribe_stream_text_init(&text);
CHECK(transcribe_stream_get_text(&session, &text) == TRANSCRIBE_OK);
CHECK(std::strcmp(text.full_text, cafe_circ) == 0);
CHECK(std::strcmp(text.committed_text, "caf") == 0);
CHECK(text.committed_text_bytes == 3);
CHECK(std::strcmp(text.tentative_text, "\303\252") == 0);
CHECK(text.raw_tentative_start_bytes == 3);
}
void test_stream_text_stable_prefix_respects_agreement_n() {