package app

import (
	"strings"
	"testing"
)

// scrollDiff must clamp to [0, diffMaxScroll] against the same geometry the modal renders to,
// so PgUp/PgDn (and g/G) past either end land exactly on the edge — no dead presses unwinding
// an overshoot. Backs the fullscreen viewer for every tool preview (diff / command / plain).
func TestDiffScrollClampsToBounds(t *testing.T) {
	m := inSessionModel(120, 30)
	var b strings.Builder
	b.WriteString("--- a/f\n+++ b/f\n@@ -0,0 +1,40 @@\n")
	for i := 0; i < 40; i++ {
		b.WriteString("+a new line\n")
	}
	// currentDiff() falls back to the last "tool" transcript entry when no approval is pending.
	m.routerMessages[m.selectedID] = []RouterEntry{{Role: "tool", Content: b.String()}}

	max := m.diffMaxScroll()
	if max <= 0 {
		t.Fatalf("fixture diff must exceed the modal body height to test scrolling; max=%d", max)
	}

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

// A diff that fits the modal body never scrolls.
func TestDiffScrollNoOpWhenFits(t *testing.T) {
	m := inSessionModel(120, 30)
	m.routerMessages[m.selectedID] = []RouterEntry{
		{Role: "tool", Content: "--- a/f\n+++ b/f\n@@ -1,1 +1,1 @@\n-x\n+y\n"},
	}
	m.scrollDiff(5)
	if m.diffScrollOffset != 0 {
		t.Fatalf("short diff should not scroll, got %d", m.diffScrollOffset)
	}
}
