Skip to content
Myr

tea_test.go

This file

Path

tea_test.go

846 lines · 17.0 KiB · Go

Last changed by

fix: skip capability queries when input is disabled (#1801)draw me an elephantbefore adoption8b10e22

History of this file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–6why, one step back
1package tea
23import (4 "bytes"5 "context"6 "errors"

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 7why, one step back
7 "fmt"

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 8–9why, one step back
8 "io"
9 "strings"

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

line 10why, one step back
10 "sync"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 11–13why, one step back
11 "sync/atomic"
12 "testing"13 "time"

fix: skip capability queries when input is disabled (#1801)draw me an elephantbefore adoption8b10e22

lines 14–15why, one step back
14
15 "github.com/charmbracelet/x/ansi"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 16–17why, one step back
16)
17

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 18–21why, one step back
18type ctxImplodeMsg struct {
19 cancel context.CancelFunc20}21

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 22–23why, one step back
22type incrementMsg struct{}
23

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 24–25why, one step back
24type panicMsg struct{}
25

Merge pull request #848 from wolfmagnate/fix-nested-cmd-orderAyman Bagabasbefore adoptionffa0502

lines 26–29why, one step back
26func panicCmd() Msg {
27 panic("testing goroutine panic behavior")28}29

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 30–34why, one step back
30type testModel struct {
31 executed atomic.Value32 counter atomic.Value33}34

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 35why, one step back
35func (m *testModel) Init() Cmd {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 36–39why, one step back
36 return nil
37}3839func (m *testModel) Update(msg Msg) (Model, Cmd) {

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 40–44why, one step back
40 switch msg := msg.(type) {
41 case ctxImplodeMsg:42 msg.cancel()43 time.Sleep(100 * time.Millisecond)44

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 45–52why, one step back
45 case incrementMsg:
46 i := m.counter.Load()47 if i == nil {48 m.counter.Store(1)49 } else {50 m.counter.Store(i.(int) + 1)51 }52

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 53–57why, one step back
53 case KeyPressMsg:
54 switch msg.String() {55 case "q", "ctrl+c":56 return m, Quit57 }

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 58–60why, one step back
58
59 case panicMsg:60 panic("testing panic behavior")

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 61–65why, one step back
61 }
6263 return m, nil64}65

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 66why, one step back
66func (m *testModel) View() View {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 67why, one step back
67 m.executed.Store(true)

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 68why, one step back
68 return NewView("success")

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 69–71why, one step back
69}
7071func TestTeaModel(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 72why, one step back
72 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 73–76why, one step back
73 var buf bytes.Buffer
74 var in bytes.Buffer75 in.Write([]byte("q"))76

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 77why, one step back
77 ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 78–79why, one step back
78 defer cancel()
79

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 80–84why, one step back
80 p := NewProgram(&testModel{},
81 WithContext(ctx),82 WithInput(&in),83 WithOutput(&buf),84 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 85–94why, one step back
85 if _, err := p.Run(); err != nil {
86 t.Fatal(err)87 }8889 if buf.Len() == 0 {90 t.Fatal("no output")91 }92}9394func TestTeaQuit(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 95why, one step back
95 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 96–99why, one step back
96 var buf bytes.Buffer
97 var in bytes.Buffer9899 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 100–103why, one step back
100 p := NewProgram(m,
101 WithInput(&in),102 WithOutput(&buf),103 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 104–118why, one step back
104 go func() {
105 for {106 time.Sleep(time.Millisecond)107 if m.executed.Load() != nil {108 p.Quit()109 return110 }111 }112 }()113114 if _, err := p.Run(); err != nil {115 t.Fatal(err)116 }117}118

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

line 119why, one step back
119func TestTeaWaitQuit(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 120why, one step back
120 t.Parallel()

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 121–128why, one step back
121 var buf bytes.Buffer
122 var in bytes.Buffer123124 progStarted := make(chan struct{})125 waitStarted := make(chan struct{})126 errChan := make(chan error, 1)127128 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 129–132why, one step back
129 p := NewProgram(m,
130 WithInput(&in),131 WithOutput(&buf),132 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 133–156why, one step back
133
134 go func() {135 _, err := p.Run()136 errChan <- err137 }()138139 go func() {140 for {141 time.Sleep(time.Millisecond)142 if m.executed.Load() != nil {143 close(progStarted)144145 <-waitStarted146 time.Sleep(50 * time.Millisecond)147 p.Quit()148149 return150 }151 }152 }()153154 <-progStarted155156 var wg sync.WaitGroup

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 157why, one step back
157 for range 5 {

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 158–173why, one step back
158 wg.Add(1)
159 go func() {160 p.Wait()161 wg.Done()162 }()163 }164 close(waitStarted)165 wg.Wait()166167 err := <-errChan168 if err != nil {169 t.Fatalf("Expected nil, got %v", err)170 }171}172173func TestTeaWaitKill(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 174why, one step back
174 t.Parallel()

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 175–182why, one step back
175 var buf bytes.Buffer
176 var in bytes.Buffer177178 progStarted := make(chan struct{})179 waitStarted := make(chan struct{})180 errChan := make(chan error, 1)181182 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 183–186why, one step back
183 p := NewProgram(m,
184 WithInput(&in),185 WithOutput(&buf),186 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 187–210why, one step back
187
188 go func() {189 _, err := p.Run()190 errChan <- err191 }()192193 go func() {194 for {195 time.Sleep(time.Millisecond)196 if m.executed.Load() != nil {197 close(progStarted)198199 <-waitStarted200 time.Sleep(50 * time.Millisecond)201 p.Kill()202203 return204 }205 }206 }()207208 <-progStarted209210 var wg sync.WaitGroup

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 211why, one step back
211 for range 5 {

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 212–226why, one step back
212 wg.Add(1)
213 go func() {214 p.Wait()215 wg.Done()216 }()217 }218 close(waitStarted)219 wg.Wait()220221 err := <-errChan222 if !errors.Is(err, ErrProgramKilled) {223 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)224 }225}226

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 227why, one step back
227func TestTeaWithFilter(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

lines 228–233why, one step back
228 for _, preventCount := range []uint32{0, 1, 2} {
229 t.Run(fmt.Sprintf("prevent_%d", preventCount), func(t *testing.T) {230 t.Parallel()231 testTeaWithFilter(t, preventCount)232 })233 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 234–244why, one step back
234}
235236func testTeaWithFilter(t *testing.T, preventCount uint32) {237 var buf bytes.Buffer238 var in bytes.Buffer239240 m := &testModel{}241 shutdowns := uint32(0)242 p := NewProgram(m,243 WithInput(&in),244 WithOutput(&buf),

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 245–247why, one step back
245 )
246 p.filter = func(_ Model, msg Msg) Msg {247 if _, ok := msg.(QuitMsg); !ok {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 248why, one step back
248 return msg

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 249–255why, one step back
249 }
250 if shutdowns < preventCount {251 atomic.AddUint32(&shutdowns, 1)252 return nil253 }254 return msg255 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 256–263why, one step back
256
257 go func() {258 for atomic.LoadUint32(&shutdowns) <= preventCount {259 time.Sleep(time.Millisecond)260 p.Quit()261 }262 }()263

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 264why, one step back
264 if _, err := p.Run(); err != nil {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 265–272why, one step back
265 t.Fatal(err)
266 }267 if shutdowns != preventCount {268 t.Errorf("Expected %d prevented shutdowns, got %d", preventCount, shutdowns)269 }270}271272func TestTeaKill(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 273why, one step back
273 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 274–277why, one step back
274 var buf bytes.Buffer
275 var in bytes.Buffer276277 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 278–281why, one step back
278 p := NewProgram(m,
279 WithInput(&in),280 WithOutput(&buf),281 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 282–291why, one step back
282 go func() {
283 for {284 time.Sleep(time.Millisecond)285 if m.executed.Load() != nil {286 p.Kill()287 return288 }289 }290 }()291

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 292–294why, one step back
292 _, err := p.Run()
293294 if !errors.Is(err, ErrProgramKilled) {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 295–296why, one step back
295 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)
296 }

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 297–302why, one step back
297
298 if errors.Is(err, context.Canceled) {299 // The end user should not know about the program's internal context state.300 // The program should only report external context cancellation as a context error.301 t.Fatalf("Internal context cancellation was reported as context error!")302 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 303–305why, one step back
303}
304305func TestTeaContext(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 306why, one step back
306 t.Parallel()

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 307why, one step back
307 ctx, cancel := context.WithCancel(t.Context())

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 308–311why, one step back
308 var buf bytes.Buffer
309 var in bytes.Buffer310311 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 312–316why, one step back
312 p := NewProgram(m,
313 WithContext(ctx),314 WithInput(&in),315 WithOutput(&buf),316 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 317–326why, one step back
317 go func() {
318 for {319 time.Sleep(time.Millisecond)320 if m.executed.Load() != nil {321 cancel()322 return323 }324 }325 }()326

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 327–339why, one step back
327 _, err := p.Run()
328329 if !errors.Is(err, ErrProgramKilled) {330 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)331 }332333 if !errors.Is(err, context.Canceled) {334 // The end user should know that their passed in context caused the kill.335 t.Fatalf("Expected %v, got %v", context.Canceled, err)336 }337}338339func TestTeaContextImplodeDeadlock(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 340why, one step back
340 t.Parallel()

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 341why, one step back
341 ctx, cancel := context.WithCancel(t.Context())

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 342–345why, one step back
342 var buf bytes.Buffer
343 var in bytes.Buffer344345 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 346–350why, one step back
346 p := NewProgram(m,
347 WithContext(ctx),348 WithInput(&in),349 WithOutput(&buf),350 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 351–360why, one step back
351 go func() {
352 for {353 time.Sleep(time.Millisecond)354 if m.executed.Load() != nil {355 p.Send(ctxImplodeMsg{cancel: cancel})356 return357 }358 }359 }()360

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 361–365why, one step back
361 if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {
362 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)363 }364}365

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

line 366why, one step back
366func TestTeaContextBatchDeadlock(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 367why, one step back
367 t.Parallel()

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 368why, one step back
368 ctx, cancel := context.WithCancel(t.Context())

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 369–377why, one step back
369 var buf bytes.Buffer
370 var in bytes.Buffer371372 inc := func() Msg {373 cancel()374 return incrementMsg{}375 }376377 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 378–382why, one step back
378 p := NewProgram(m,
379 WithContext(ctx),380 WithInput(&in),381 WithOutput(&buf),382 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 383–401why, one step back
383 go func() {
384 for {385 time.Sleep(time.Millisecond)386 if m.executed.Load() != nil {387 batch := make(BatchMsg, 100)388 for i := range batch {389 batch[i] = inc390 }391 p.Send(batch)392 return393 }394 }395 }()396397 if _, err := p.Run(); !errors.Is(err, ErrProgramKilled) {398 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)399 }400}401

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 402why, one step back
402func TestTeaBatchMsg(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 403why, one step back
403 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 404–411why, one step back
404 var buf bytes.Buffer
405 var in bytes.Buffer406407 inc := func() Msg {408 return incrementMsg{}409 }410411 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 412–415why, one step back
412 p := NewProgram(m,
413 WithInput(&in),414 WithOutput(&buf),415 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 416–438why, one step back
416 go func() {
417 p.Send(BatchMsg{inc, inc})418419 for {420 time.Sleep(time.Millisecond)421 i := m.counter.Load()422 if i != nil && i.(int) >= 2 {423 p.Quit()424 return425 }426 }427 }()428429 if _, err := p.Run(); err != nil {430 t.Fatal(err)431 }432433 if m.counter.Load() != 2 {434 t.Fatalf("counter should be 2, got %d", m.counter.Load())435 }436}437438func TestTeaSequenceMsg(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 439why, one step back
439 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 440–447why, one step back
440 var buf bytes.Buffer
441 var in bytes.Buffer442443 inc := func() Msg {444 return incrementMsg{}445 }446447 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 448–451why, one step back
448 p := NewProgram(m,
449 WithInput(&in),450 WithOutput(&buf),451 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 452–463why, one step back
452 go p.Send(sequenceMsg{inc, inc, Quit})
453454 if _, err := p.Run(); err != nil {455 t.Fatal(err)456 }457458 if m.counter.Load() != 2 {459 t.Fatalf("counter should be 2, got %d", m.counter.Load())460 }461}462463func TestTeaSequenceMsgWithBatchMsg(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 464why, one step back
464 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 465–475why, one step back
465 var buf bytes.Buffer
466 var in bytes.Buffer467468 inc := func() Msg {469 return incrementMsg{}470 }471 batch := func() Msg {472 return BatchMsg{inc, inc}473 }474475 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 476–479why, one step back
476 p := NewProgram(m,
477 WithInput(&in),478 WithOutput(&buf),479 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 480–490why, one step back
480 go p.Send(sequenceMsg{batch, inc, Quit})
481482 if _, err := p.Run(); err != nil {483 t.Fatal(err)484 }485486 if m.counter.Load() != 3 {487 t.Fatalf("counter should be 3, got %d", m.counter.Load())488 }489}490

fix: handle nested SequenceMsg in event loop and use sync.WaitGroup f… (#1463)Ayman Bagabasbefore adoptionf5da8d0

line 491why, one step back
491func TestTeaNestedSequenceMsg(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 492why, one step back
492 t.Parallel()

fix: handle nested SequenceMsg in event loop and use sync.WaitGroup f… (#1463)Ayman Bagabasbefore adoptionf5da8d0

lines 493–500why, one step back
493 var buf bytes.Buffer
494 var in bytes.Buffer495496 inc := func() Msg {497 return incrementMsg{}498 }499500 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 501–504why, one step back
501 p := NewProgram(m,
502 WithInput(&in),503 WithOutput(&buf),504 )

Merge pull request #848 from wolfmagnate/fix-nested-cmd-orderAyman Bagabasbefore adoptionffa0502

line 505why, one step back
505 go p.Send(sequenceMsg{inc, Sequence(inc, inc, Batch(inc, inc)), Quit})

fix: handle nested SequenceMsg in event loop and use sync.WaitGroup f… (#1463)Ayman Bagabasbefore adoptionf5da8d0

lines 506–510why, one step back
506
507 if _, err := p.Run(); err != nil {508 t.Fatal(err)509 }510

Merge pull request #848 from wolfmagnate/fix-nested-cmd-orderAyman Bagabasbefore adoptionffa0502

lines 511–512why, one step back
511 if m.counter.Load() != 5 {
512 t.Fatalf("counter should be 5, got %d", m.counter.Load())

fix: handle nested SequenceMsg in event loop and use sync.WaitGroup f… (#1463)Ayman Bagabasbefore adoptionf5da8d0

lines 513–515why, one step back
513 }
514}515

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 516why, one step back
516func TestTeaSend(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 517why, one step back
517 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 518–521why, one step back
518 var buf bytes.Buffer
519 var in bytes.Buffer520521 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 522–525why, one step back
522 p := NewProgram(m,
523 WithInput(&in),524 WithOutput(&buf),525 )

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 526–538why, one step back
526
527 // sending before the program is started is a blocking operation528 go p.Send(Quit())529530 if _, err := p.Run(); err != nil {531 t.Fatal(err)532 }533534 // sending a message after program has quit is a no-op535 p.Send(Quit())536}537538func TestTeaNoRun(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 539why, one step back
539 t.Parallel()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 540–543why, one step back
540 var buf bytes.Buffer
541 var in bytes.Buffer542543 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 544–547why, one step back
544 NewProgram(m,
545 WithInput(&in),546 WithOutput(&buf),547 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 548–550why, one step back
548}
549550func TestTeaPanic(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 551why, one step back
551 t.Parallel()

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 552–555why, one step back
552 var buf bytes.Buffer
553 var in bytes.Buffer554555 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 556–559why, one step back
556 p := NewProgram(m,
557 WithInput(&in),558 WithOutput(&buf),559 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 560–581why, one step back
560 go func() {
561 for {562 time.Sleep(time.Millisecond)563 if m.executed.Load() != nil {564 p.Send(panicMsg{})565 return566 }567 }568 }()569570 _, err := p.Run()571572 if !errors.Is(err, ErrProgramPanic) {573 t.Fatalf("Expected %v, got %v", ErrProgramPanic, err)574 }575576 if !errors.Is(err, ErrProgramKilled) {577 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)578 }579}580581func TestTeaGoroutinePanic(t *testing.T) {

fix: prevent data race with cursedRenderer.onMouse (#1691)Liam Stanleybefore adoptionc60f0c5

line 582why, one step back
582 t.Parallel()

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 583–586why, one step back
583 var buf bytes.Buffer
584 var in bytes.Buffer585586 m := &testModel{}

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 587–590why, one step back
587 p := NewProgram(m,
588 WithInput(&in),589 WithOutput(&buf),590 )

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 591–595why, one step back
591 go func() {
592 for {593 time.Sleep(time.Millisecond)594 if m.executed.Load() != nil {595 batch := make(BatchMsg, 10)

Merge pull request #848 from wolfmagnate/fix-nested-cmd-orderAyman Bagabasbefore adoptionffa0502

lines 596–598why, one step back
596 for i := 0; i < len(batch); i += 2 {
597 batch[i] = Sequence(panicCmd)598 batch[i+1] = Batch(panicCmd)

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

lines 599–613why, one step back
599 }
600 p.Send(batch)601 return602 }603 }604 }()605606 _, err := p.Run()607608 if !errors.Is(err, ErrProgramPanic) {609 t.Fatalf("Expected %v, got %v", ErrProgramPanic, err)610 }611612 if !errors.Is(err, ErrProgramKilled) {613 t.Fatalf("Expected %v, got %v", ErrProgramKilled, err)

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 614–671why, one step back
614 }
615}616617type benchModel struct {618 t testing.TB619}620621func (m benchModel) Init() Cmd {622 return nil623}624625func (m benchModel) Update(msg Msg) (Model, Cmd) {626 switch msg := msg.(type) {627 case KeyPressMsg:628 switch msg.String() {629 case "q", "ctrl+c":630 return m, Quit631 }632 }633634 return m, nil635}636637func (m benchModel) View() View {638 view := strings.Join([]string{639 " \x1b[38;5;63m╭─────────────────────────╮\x1b[m",640 " \x1b[38;5;63m│\x1b[m\x1b[25X\x1b[28G\x1b[38;5;63m│\x1b[m",641 " \x1b[38;5;63m│\x1b[m \x1b[38;5;231mHello There!\x1b[m \x1b[38;5;63m│\x1b[m",642 " \x1b[38;5;63m│\x1b[m\x1b[25X\x1b[28G\x1b[38;5;63m│\x1b[m",643 " \x1b[38;5;63m╰─────────────────────────╯\x1b[m",644 }, "\n")645646 return NewView(view)647}648649func BenchmarkTeaRun(b *testing.B) {650 for i := 0; i < b.N; i++ {651 var buf bytes.Buffer652653 m := benchModel{b}654 r, w := io.Pipe()655 p := NewProgram(m,656 WithInput(r),657 WithOutput(&buf),658 )659660 go func() {661 for _, input := range "abcdefghijklmnopq" {662 time.Sleep(10 * time.Millisecond)663 w.Write([]byte(string(input)))664 }665 }()666667 if _, err := p.Run(); err != nil {668 b.Fatalf("Run failed: %v", err)669 }670671 _ = r.CloseWithError(io.EOF)

fix: skip capability queries when input is disabled (#1801)draw me an elephantbefore adoption8b10e22

lines 672–824why, one step back
672 }
673}674675// quitModel quits as soon as it starts.676type quitModel struct{}677678func (quitModel) Init() Cmd { return Quit }679func (quitModel) Update(Msg) (Model, Cmd) { return quitModel{}, nil }680func (quitModel) View() View { return NewView("") }681682// TestTeaNoCapabilityQueryWithoutInput is a regression test for683// https://github.com/charmbracelet/gum/issues/1118: programs without input684// can't read the terminal's responses to capability queries, so the replies685// leak into the shell after the program exits. Don't send the queries when686// input is disabled.687func TestTeaNoCapabilityQueryWithoutInput(t *testing.T) {688 t.Parallel()689690 query := ansi.RequestModeSynchronizedOutput + ansi.RequestModeUnicodeCore691 env := []string{"TERM=xterm-256color", "TERM_PROGRAM=ghostty"}692693 for _, tt := range []struct {694 name string695 input io.Reader696 want bool697 }{698 {name: "input disabled", input: nil, want: false},699 {name: "input enabled", input: bytes.NewReader(nil), want: true},700 } {701 t.Run(tt.name, func(t *testing.T) {702 t.Parallel()703704 var buf bytes.Buffer705 p := NewProgram(quitModel{},706 WithInput(tt.input),707 WithOutput(&buf),708 WithEnvironment(env),709 )710 if _, err := p.Run(); err != nil {711 t.Fatal(err)712 }713714 // The queued output may not have been flushed if the program715 // exited before the first tick, so inspect it too. Reading716 // p.outputBuf here is deliberate: it checks the buffered bytes717 // that would have been flushed, without depending on flush718 // timing (and it ties the test to the renderer's buffer, which719 // is an intentional trade-off).720 got := buf.String() + p.outputBuf.String()721 if strings.Contains(got, query) != tt.want {722 t.Fatalf("capability query present = %v, want %v; output: %q", !tt.want, tt.want, got)723 }724 })725 }726}727728// terminalQueryCmds lists every command that sends a terminal query whose729// reply must be read from the input reader.730var terminalQueryCmds = []Cmd{731 RequestBackgroundColor,732 RequestForegroundColor,733 RequestCursorColor,734 ReadClipboard,735 ReadPrimaryClipboard,736 RequestTerminalVersion,737 RequestCapability("TN"),738 RequestCursorPosition,739}740741// queryModel issues every terminal query that needs a reply on the input742// reader, then quits once each one has been handled.743type queryModel struct {744 handled int745}746747func (queryModel) Init() Cmd {748 return Batch(terminalQueryCmds...)749}750751func (m queryModel) Update(msg Msg) (Model, Cmd) {752 switch msg.(type) {753 case backgroundColorMsg, foregroundColorMsg, cursorColorMsg,754 readClipboardMsg, readPrimaryClipboardMsg, terminalVersion,755 requestCapabilityMsg, requestCursorPosMsg:756 // Each query is handled by an intermediate reader that turns the757 // terminal's reply into a message, so Update only sees the request758 // messages because the event loop passes them through after the759 // intermediate reads. If that pass-through ever changes, this test760 // will hang instead of quitting.761 m.handled++762 if m.handled == len(terminalQueryCmds) {763 return m, Quit764 }765 }766 return m, nil767}768769func (queryModel) View() View { return NewView("") }770771// TestTeaNoTerminalQueriesWithoutInput is a regression test for772// https://github.com/charmbracelet/gum/issues/1118: programs without input773// can't read the terminal's responses, so query replies leak into the shell774// after the program exits. No input-dependent query should be sent when input775// is disabled.776func TestTeaNoTerminalQueriesWithoutInput(t *testing.T) {777 t.Parallel()778779 queries := []string{780 ansi.RequestSystemClipboard,781 ansi.RequestPrimaryClipboard,782 ansi.RequestBackgroundColor,783 ansi.RequestForegroundColor,784 ansi.RequestCursorColor,785 ansi.RequestNameVersion,786 ansi.RequestTermcap("TN"),787 ansi.RequestCursorPositionReport,788 }789 env := []string{"TERM=xterm-256color", "TERM_PROGRAM=ghostty"}790791 for _, tt := range []struct {792 name string793 input io.Reader794 want bool795 }{796 {name: "input disabled", input: nil, want: false},797 {name: "input enabled", input: bytes.NewReader(nil), want: true},798 } {799 t.Run(tt.name, func(t *testing.T) {800 t.Parallel()801802 var buf bytes.Buffer803 p := NewProgram(queryModel{},804 WithInput(tt.input),805 WithOutput(&buf),806 WithEnvironment(env),807 )808 if _, err := p.Run(); err != nil {809 t.Fatal(err)810 }811812 // The queued output may not have been flushed if the program813 // exited before the first tick, so inspect it too. Reading814 // p.outputBuf here is deliberate: it checks the buffered bytes815 // that would have been flushed, without depending on flush816 // timing (and it ties the test to the renderer's buffer, which817 // is an intentional trade-off).818 got := buf.String() + p.outputBuf.String()819 for _, query := range queries {820 if strings.Contains(got, query) != tt.want {821 t.Errorf("query %q present = %v, want %v; output: %q", query, !tt.want, tt.want, got)822 }823 }824 })

chore: merge pull request #1376 from desertwitch/stacked-patchesAyman Bagabasbefore adoption915f65d

line 825why, one step back
825 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 826why, one step back
826}

fix: don't panic in ProgressBarState.String() for out-of-range values (#1748)Ankitbefore adoption930e18c

lines 827–846why, one step back
827
828// TestProgressBarStateStringOutOfRange is a regression test for829// https://github.com/charmbracelet/bubbletea/issues/1711: String panicked830// with an index-out-of-range error for any ProgressBarState outside the831// [ProgressBarNone, ProgressBarWarning] range. Since State is an exported832// field on ProgressBar, callers can assign such a value without going833// through NewProgressBar.834func TestProgressBarStateStringOutOfRange(t *testing.T) {835 for _, s := range []ProgressBarState{-1, 5, 100} {836 if got, want := s.String(), "Unknown"; got != want {837 t.Errorf("ProgressBarState(%d).String() = %q, want %q", int(s), got, want)838 }839 }840841 for s := ProgressBarNone; s <= ProgressBarWarning; s++ {842 if got := s.String(); got == "Unknown" {843 t.Errorf("ProgressBarState(%d).String() = %q, want a known name", int(s), got)844 }845 }846}