diff options
| author | wuhei <wuhei@users.noreply.github.com> | 2026-11-19 19:29:21 +0100 |
|---|---|---|
| committer | wuhei <wuhei@users.noreply.github.com> | 2026-11-19 19:29:21 +0100 |
| commit | 78455c67406808fc1da9f8655708738f10e30d23 (patch) | |
| tree | 9576aa52b20c40037e39e0c504bf5e4755963806 /main_test.go | |
| parent | b457d19d9b2c634570300d0f73087e18b4ec4a4a (diff) | |
| download | lilchinese-78455c67406808fc1da9f8655708738f10e30d23.tar.gz | |
settle short sentences on screen when the speaker pauses
A long sentence flipped through fragments and stopped on the last one: '我觉得今年
我的中文会说得好一些。' ended up on screen as '会说得好一些'. The frame that stays
up is also the frame that gets recorded, translated and logged, so it has to be the
whole sentence.
- -settle (default 20): at an endpoint, if the finished sentence is that short, it is
published whole and replaces the trailing fragment. Fragments still flip while you
speak, so the Takahashi rhythm is unchanged; -settle 0 restores it exactly.
- the fragment is no longer published for a sentence that is about to settle (one
look, not two), so there is no flash of a frame that is immediately replaced.
- tests: settles whole, long sentences still fragment, -settle 0 keeps fragments
Diffstat (limited to 'main_test.go')
| -rw-r--r-- | main_test.go | 64 |
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) + } +} |
