Files
pipelines/internal/tui/model_test.go
T

102 lines
3.2 KiB
Go

package tui
import (
"context"
"net/http/httptest"
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"git.a-wan.net/alan/pipelines/internal/server"
)
func TestTKeyPingRoundTrip(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv := httptest.NewServer(server.Handler(ctx))
defer srv.Close()
m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect")
connected := m.connect()()
if _, ok := connected.(connectedMsg); !ok {
t.Fatalf("connect: %#v", connected)
}
defer connected.(connectedMsg).conn.CloseNow()
updated, read := m.Update(connected)
m = updated.(Model)
if m.status != strings.TrimPrefix(srv.URL, "http://") {
t.Fatalf("connected status = %q, want server host and port", m.status)
}
for i := 0; i < 2; i++ {
updated, send := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'t'}})
m = updated.(Model)
if _, ok := send().(sentMsg); !ok {
t.Fatal("ping command failed")
}
response := read()
if _, ok := response.(socketMsg); !ok {
t.Fatalf("read: %#v", response)
}
updated, read = m.Update(response)
m = updated.(Model)
}
if text := strings.Join(m.lines, "\n"); !strings.Contains(text, "pong #1:") || !strings.Contains(text, "pong #2:") {
t.Fatalf("missing responses: %s", text)
}
_, quit := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
if _, ok := quit().(tea.QuitMsg); !ok {
t.Fatal("q did not quit")
}
}
func TestPingBeforeConnection(t *testing.T) {
m := New(context.Background(), "ws://localhost/connect")
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'t'}})
if cmd != nil || !strings.Contains(strings.Join(updated.(Model).lines, "\n"), "not connected") {
t.Fatal("expected disconnected message without sending")
}
}
func TestLayoutResizes(t *testing.T) {
for _, size := range []tea.WindowSizeMsg{
{Width: 80, Height: 24},
{Width: 40, Height: 8},
{Width: 12, Height: 4},
{Width: 1, Height: 1},
} {
m := New(context.Background(), "ws://localhost/connect")
m.status = "127.0.0.1:8080"
for i := 0; i < 20; i++ {
m.appendLine(strings.Repeat("x", 100))
}
updated, _ := m.Update(size)
view := ansi.Strip(updated.View())
lines := strings.Split(view, "\n")
if len(lines) != size.Height {
t.Fatalf("%+v: height = %d, view = %q", size, len(lines), view)
}
for _, line := range lines {
if lipgloss.Width(line) > size.Width {
t.Fatalf("%+v: line too wide: %q", size, line)
}
}
if size.Width >= 40 {
if !strings.HasPrefix(lines[0], "Pipelines ") || !strings.HasSuffix(lines[0], "127.0.0.1:8080") || lipgloss.Width(lines[0]) != size.Width {
t.Fatalf("invalid header: %q", lines[0])
}
if !strings.HasPrefix(lines[len(lines)-1], ": commands") || !strings.Contains(lines[len(lines)-1], "● 20 NEW") || !strings.HasSuffix(lines[len(lines)-1], ":logs ") {
t.Fatalf("invalid footer: %q", lines[len(lines)-1])
}
if !strings.Contains(lines[len(lines)-2], "─") {
t.Fatal("missing footer separator")
}
if !strings.Contains(lines[1], "─") || lipgloss.Width(lines[1]) != size.Width {
t.Fatal("missing full-width header separator")
}
}
}
}