From 6f9aaf04904a5b1d455fae358d3f88cc1e5384ee Mon Sep 17 00:00:00 2001 From: wuhei Date: Thu, 19 Nov 2026 20:47:32 +0100 Subject: tests: brokers now start behind the intro gate Publishing is gated on the intro being dismissed, so a plain newBroker() is silent and the colour-alternation test hung. Use a started broker in tests, and cover the gate itself (nothing published before /start, first slide after it carries start). --- main_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) (limited to 'main_test.go') diff --git a/main_test.go b/main_test.go index 7b82cf2..674b3d3 100644 --- a/main_test.go +++ b/main_test.go @@ -7,6 +7,14 @@ import ( "time" ) +// newStartedBroker returns a broker past the intro page: nothing is published before +// Enter, which the unit tests do not care about. +func newStartedBroker() *broker { + b := newBroker() + b.started = true + return b +} + // collect returns the phrases published by one run of presenter.update. func collect(t *testing.T, p *presenter, b *broker, u string, endpoint bool) []string { t.Helper() @@ -25,7 +33,7 @@ func collect(t *testing.T, p *presenter, b *broker, u string, endpoint bool) []s } func TestShortSentenceIsNotCutIntoAStub(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} // 10 chars with a full stop: used to end up on screen as "了". @@ -37,7 +45,7 @@ func TestShortSentenceIsNotCutIntoAStub(t *testing.T) { } func TestLongRunIsCutButNeverLeavesAStub(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} u := "今天我想跟大家讲一讲我自己学习中文的一些经历" @@ -59,7 +67,7 @@ func TestLongRunIsCutButNeverLeavesAStub(t *testing.T) { } func TestPunctuationBreaksSlides(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} got := collect(t, p, b, "第一,第二,第三。", true) want := []string{"第一", "第二", "第三"} @@ -74,7 +82,7 @@ func TestPunctuationBreaksSlides(t *testing.T) { } func TestEnglishLineIsStickyAcrossPhrases(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} collect(t, p, b, "第一", false) @@ -102,7 +110,7 @@ func TestEnglishLineIsStickyAcrossPhrases(t *testing.T) { } func TestColourAlternatesPerSentence(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} first := []Slide{} @@ -135,7 +143,7 @@ next: } func TestCutBlanksTheScreen(t *testing.T) { - b := newBroker() + b := newStartedBroker() b.publish("你好", time.Now().UnixMilli()) ch, _, unsub := b.subscribe() defer unsub() @@ -174,7 +182,7 @@ func TestContextWindowPrefersClauseBoundary(t *testing.T) { } func TestKeepTurnsSentenceGreenUntilTheNextOne(t *testing.T) { - b := newBroker() + b := newStartedBroker() ch, _, unsub := b.subscribe() defer unsub() b.publish("我自学中文已经五年了", time.Now().UnixMilli()) @@ -199,7 +207,7 @@ func TestKeepTurnsSentenceGreenUntilTheNextOne(t *testing.T) { } func TestPresentersentencePrefersTheOneBeingSpoken(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, b: b} if got := p.sentence(); got != "" { t.Fatalf("empty presenter returned %q", got) @@ -282,7 +290,7 @@ func feed(t *testing.T, p *presenter, b *broker, u string) []string { } func TestSentenceSettlesWholeWhenYouPause(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, settle: 20, b: b} frames := feed(t, p, b, "我觉得今年我的中文会说得好一些。") last := frames[len(frames)-1] @@ -296,7 +304,7 @@ func TestSentenceSettlesWholeWhenYouPause(t *testing.T) { } func TestLongSentenceStillEndsOnAFragment(t *testing.T) { - b := newBroker() + b := newStartedBroker() p := &presenter{max: 9, preview: 3, settle: 12, b: b} u := "今天我想跟大家讲一讲我自己学习中文的一些经历" frames := feed(t, p, b, u) @@ -306,10 +314,34 @@ func TestLongSentenceStillEndsOnAFragment(t *testing.T) { } func TestSettleOffKeepsTakahashiFragments(t *testing.T) { - b := newBroker() + b := newStartedBroker() 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) } } + +func TestNothingIsPublishedBeforeStart(t *testing.T) { + b := newBroker() // intro still up + ch, _, unsub := b.subscribe() + defer unsub() + b.publish("台下的噪音", time.Now().UnixMilli()) + b.publishEN("noise from the room") + b.keepNow() + b.cutNow() + select { + case s := <-ch: + t.Fatalf("published before the intro was dismissed: %+v", s) + default: + } + b.start() + s := <-ch + if !s.Start || s.Text != "" { + t.Fatalf("start broadcast = %+v, want an empty start=true message", s) + } + b.publish("现在开始", time.Now().UnixMilli()) + if got := <-ch; got.Text != "现在开始" || !got.Start { + t.Fatalf("first slide after start = %+v", got) + } +} -- cgit v1.2.3