package app

import (
	"strings"
	"testing"

	"github.com/charmbracelet/x/ansi"
)

// editorLines splits the buffer on newlines into one display row each, in order.
func TestEditorLinesSplitsOnNewline(t *testing.T) {
	m := NewModel(nil)
	m.theme = NewTheme(SoftBlue)
	m.inputBuffer = "one\ntwo\nthree"
	m.inputCursor = len(m.inputBuffer)

	lines := m.editorLines(80)
	if len(lines) != 3 {
		t.Fatalf("want 3 lines, got %d", len(lines))
	}
	for i, want := range []string{"one", "two", "three"} {
		if got := strings.TrimRight(ansi.Strip(lines[i]), " ▏"); got != want {
			t.Errorf("line %d = %q, want %q", i, got, want)
		}
	}
}

// A buffer taller than maxInputLines shows only the last maxInputLines rows (cursor tail).
func TestEditorLinesCapsHeight(t *testing.T) {
	m := NewModel(nil)
	m.theme = NewTheme(SoftBlue)
	m.inputBuffer = strings.Repeat("x\n", maxInputLines+4) + "last"
	m.inputCursor = len(m.inputBuffer)

	if got := len(m.editorLines(80)); got != maxInputLines {
		t.Fatalf("want capped at %d lines, got %d", maxInputLines, got)
	}
}
