Files
golang-cli/keypress/keypress_test.go
T
pim 9e0a98ed07 feat: Validate + keypress subpackage; RFC-style design.md (v1.2.0)
Validate(root): optional startup/test check for tree authoring faults —
>1 slot child per node, empty word, duplicate sibling words, dead-end
node — traversing circular slots without looping (#3).

keypress subpackage: WaitForKey(ctx, cancel) cancels a context on any
keystroke for watch-style streaming commands, with per-GOOS cbreak
(linux TCGETS/TCSETS, BSD TIOCGETA/TIOCSETA) and a non-tty/unsupported
fallback that just waits on ctx. Lifts the last OpenBSD-specific bit out
of evpnc/maglevc's watch.go (#6).

docs: replace PROPOSAL.md with an RFC-2119 design.md (FR/NFR for the
library). Example now dogfoods Validate (a unit test) and keypress (a
bounded `watch` command).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-05 22:11:13 +02:00

32 lines
818 B
Go

// SPDX-FileCopyrightText: (C) Copyright 2026 Pim van Pelt <pim@ipng.ch>
// 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")
}
}