import Foundation
import Testing

@testable import Forji

@MainActor
struct NavigationStateTests {
    @Test func navigateToNotificationsSetsCorrectValues() {
        let state = NavigationState.shared
        state.consume() // reset

        state.navigateToNotifications(serverURL: "https://forgejo.example.com", username: "alice")

        #expect(state.pendingNavigation != nil)
        #expect(state.pendingNavigation?.serverURL == "https://forgejo.example.com")
        #expect(state.pendingNavigation?.username == "alice")
        #expect(state.pendingNavigation?.targetTab == 3)
    }

    @Test func consumeClearsPendingNavigation() {
        let state = NavigationState.shared

        state.navigateToNotifications(serverURL: "https://forgejo.example.com", username: "alice")
        #expect(state.pendingNavigation != nil)

        state.consume()
        #expect(state.pendingNavigation == nil)
    }

    @Test func navigateIncrementsRefreshTrigger() {
        let state = NavigationState.shared
        let before = state.notificationsRefreshTrigger

        state.navigateToNotifications(serverURL: "https://forgejo.example.com", username: "alice")
        #expect(state.notificationsRefreshTrigger == before + 1)

        state.navigateToNotifications(serverURL: "https://forgejo.example.com", username: "alice")
        #expect(state.notificationsRefreshTrigger == before + 2)
    }

    @Test func consumeDoesNotResetRefreshTrigger() {
        let state = NavigationState.shared
        state.navigateToNotifications(serverURL: "https://forgejo.example.com", username: "alice")
        let triggerAfterNav = state.notificationsRefreshTrigger

        state.consume()
        #expect(state.notificationsRefreshTrigger == triggerAfterNav)
    }
}
