package app

import "testing"

// scrollModal must clamp the body offset of a tall fixed-content modal (help / stats) to
// [0, scrollableModalMax] so paging past either end lands exactly — the modal scrolls
// instead of clipping its bottom (incl. the close hint) off-screen.
func TestModalScrollClampsToBounds(t *testing.T) {
	m := inSessionModel(120, 24) // short enough that the help cheat-sheet overflows
	m.overlay = OverlayHelp

	body := m.activeModalBody()
	max := m.scrollableModalMax(len(body))
	if max <= 0 {
		t.Fatalf("help body (%d lines) must exceed the modal viewport (%d) to test scrolling", len(body), m.modalBodyRows())
	}

	m.scrollModal(10_000) // past the bottom
	if m.modalScroll != max {
		t.Fatalf("scroll past end clamped to %d, want max %d", m.modalScroll, max)
	}
	m.scrollModal(-10_000) // past the top
	if m.modalScroll != 0 {
		t.Fatalf("scroll past top clamped to %d, want 0", m.modalScroll)
	}
}

// A modal that fits the screen never scrolls.
func TestModalScrollNoOpWhenFits(t *testing.T) {
	m := inSessionModel(120, 60) // tall enough that help fits in full
	m.overlay = OverlayHelp
	if max := m.scrollableModalMax(len(m.activeModalBody())); max != 0 {
		t.Fatalf("help should fit at height 60, got max scroll %d", max)
	}
	m.scrollModal(5)
	if m.modalScroll != 0 {
		t.Fatalf("a modal that fits should not scroll, got %d", m.modalScroll)
	}
}
