// SPDX-FileCopyrightText: (C) Copyright 2026 Pim van Pelt // SPDX-License-Identifier: Apache-2.0 package keypress import ( "context" "testing" "time" ) // TestWaitForKeyReturnsOnContextCancel checks WaitForKey unblocks when the // context ends, both on the non-tty path (cbreak fails → wait on ctx) and the // tty path (select returns on ctx.Done). Under `go test` stdin is normally not // a terminal, exercising the former. func TestWaitForKeyReturnsOnContextCancel(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() // already done done := make(chan struct{}) go func() { WaitForKey(ctx, func() {}) close(done) }() select { case <-done: case <-time.After(2 * time.Second): t.Fatal("WaitForKey did not return after the context was cancelled") } }