package ui import ( "context" "testing" "time" "github.com/kamelion/ratatoskr-go/internal/chat" ) func TestCommandsMapToText(t *testing.T) { var got []string c := NewCommands(func(text string) { got = append(got, text) }) c.Start() c.Cancel() c.Skip() c.Retry(7) c.Status(7) c.Continue(3) c.Approve() c.SendText("просто текст") want := []string{"/start", "/cancel", "/skip", "/retry 7", "/status 7", "/continue 3", "создавай", "просто текст"} if len(got) != len(want) { t.Fatalf("got %d commands, want %d: %v", len(got), len(want), got) } for i := range want { if got[i] != want[i] { t.Errorf("command %d = %q, want %q", i, got[i], want[i]) } } } func TestNilWindowSubmitReachesHandler(t *testing.T) { w := NewNilWindow() incoming := make(chan chat.Incoming, 1) w.OnMessage(func(inc chat.Incoming) { incoming <- inc }) w.Submit("/start") select { case inc := <-incoming: if inc.UserID != UID { t.Errorf("UserID = %q, want %q", inc.UserID, UID) } if inc.Address != Address { t.Errorf("Address = %q, want %q", inc.Address, Address) } if inc.Msg.Text != "/start" { t.Errorf("Msg.Text = %q, want /start", inc.Msg.Text) } case <-time.After(2 * time.Second): t.Fatal("handler not called") } } func TestNilWindowRunStopsOnCancel(t *testing.T) { w := NewNilWindow() ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- w.Run(ctx) }() time.Sleep(50 * time.Millisecond) cancel() select { case <-done: case <-time.After(2 * time.Second): t.Fatal("Run did not stop on cancel") } } func TestNilWindowSendNoop(t *testing.T) { w := NewNilWindow() if err := w.Send(context.Background(), Address, chat.Message{Text: "hi"}); err != nil { t.Fatalf("Send: %v", err) } }