summaryrefslogtreecommitdiff
path: root/main_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'main_test.go')
-rw-r--r--main_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go
index 1838cdb..7b82cf2 100644
--- a/main_test.go
+++ b/main_test.go
@@ -249,3 +249,67 @@ func TestSessionLogFormat(t *testing.T) {
t.Fatalf("translation missing:\n%s", body)
}
}
+
+// feed sends the utterance character by character, as the streaming recogniser does,
+// then closes it with an endpoint. Returns every frame that reached the screen.
+func feed(t *testing.T, p *presenter, b *broker, u string) []string {
+ t.Helper()
+ ch, _, unsub := b.subscribe()
+ defer unsub()
+ r := []rune(u)
+ var frames []string
+ for i := 1; i <= len(r); i++ {
+ p.update(string(r[:i]), time.Now().UnixMilli(), false)
+ for {
+ select {
+ case s := <-ch:
+ frames = append(frames, s.Text)
+ default:
+ goto cont
+ }
+ }
+ cont:
+ }
+ p.update(u, time.Now().UnixMilli(), true)
+ for {
+ select {
+ case s := <-ch:
+ frames = append(frames, s.Text)
+ default:
+ return frames
+ }
+ }
+}
+
+func TestSentenceSettlesWholeWhenYouPause(t *testing.T) {
+ b := newBroker()
+ p := &presenter{max: 9, preview: 3, settle: 20, b: b}
+ frames := feed(t, p, b, "我觉得今年我的中文会说得好一些。")
+ last := frames[len(frames)-1]
+ if last != "我觉得今年我的中文会说得好一些" {
+ t.Fatalf("last frame = %q, want the whole sentence", last)
+ }
+ // ...and it is what Enter would log.
+ if got := p.sentence(); got != "我觉得今年我的中文会说得好一些。" {
+ t.Fatalf("sentence() = %q", got)
+ }
+}
+
+func TestLongSentenceStillEndsOnAFragment(t *testing.T) {
+ b := newBroker()
+ p := &presenter{max: 9, preview: 3, settle: 12, b: b}
+ u := "今天我想跟大家讲一讲我自己学习中文的一些经历"
+ frames := feed(t, p, b, u)
+ if last := frames[len(frames)-1]; last == u {
+ t.Fatalf("a %d character sentence should not settle at -settle 12", len([]rune(u)))
+ }
+}
+
+func TestSettleOffKeepsTakahashiFragments(t *testing.T) {
+ b := newBroker()
+ p := &presenter{max: 9, preview: 3, settle: 0, b: b}
+ frames := feed(t, p, b, "我觉得今年我的中文会说得好一些。")
+ if last := frames[len(frames)-1]; last != "会说得好一些" {
+ t.Fatalf("last frame = %q, want the final fragment", last)
+ }
+}