import Testing

@testable import NucleicApp

@Suite("Transcript scroll following")
struct TranscriptScrollFollowPolicyTests {
    @Test func continuousGeometryCollapsesIntoThreeThresholdRegions() {
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: -20) == .atBottom)
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: 24) == .atBottom)
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: 25) == .hysteresis)
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: 64) == .hysteresis)
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: 65) == .away)
        #expect(TranscriptScrollFollowPolicy.region(distanceToBottom: 10_000) == .away)
    }

    @Test func streamedContentGrowthCannotDisengageFollowing() {
        #expect(TranscriptScrollFollowPolicy.following(
            current: true,
            distanceToBottom: 240,
            isSettling: false,
            isUserScrolling: false,
            isJumpingToBottom: false))
    }

    @Test func deliberateScrollPastHysteresisDisengagesFollowing() {
        #expect(!TranscriptScrollFollowPolicy.following(
            current: true,
            distanceToBottom: 65,
            isSettling: false,
            isUserScrolling: true,
            isJumpingToBottom: false))
    }

    @Test func hysteresisBandRetainsCurrentState() {
        #expect(TranscriptScrollFollowPolicy.following(
            current: true,
            distanceToBottom: 40,
            isSettling: false,
            isUserScrolling: true,
            isJumpingToBottom: false))
        #expect(!TranscriptScrollFollowPolicy.following(
            current: false,
            distanceToBottom: 40,
            isSettling: false,
            isUserScrolling: true,
            isJumpingToBottom: false))
    }

    @Test func reachingBottomAlwaysReengagesFollowing() {
        #expect(TranscriptScrollFollowPolicy.following(
            current: false,
            distanceToBottom: 24,
            isSettling: false,
            isUserScrolling: true,
            isJumpingToBottom: false))
    }

    @Test func staleDecelerationCannotUndoExplicitJump() {
        #expect(TranscriptScrollFollowPolicy.following(
            current: true,
            distanceToBottom: 500,
            isSettling: false,
            isUserScrolling: true,
            isJumpingToBottom: true))
    }
}
