import ForgejoKit
import Foundation
import Testing
@testable import Forji

struct SessionRestoreErrorTests {

    @Test func mapsTokenValidationAuthenticationErrors() {
        #expect(
            SessionRestoreError.fromTokenValidationError(AuthenticationError.invalidCredentials)
                == .tokenExpired
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(AuthenticationError.unknownError(statusCode: 403))
                == .tokenPermissionDenied
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(AuthenticationError.serverNotFound)
                == .serverNotFound
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(AuthenticationError.certificateError)
                == .certificateError
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(AuthenticationError.unknownError(statusCode: 502))
                == .serverUnavailable
        )
    }

    @Test func mapsTokenValidationServiceErrors() {
        #expect(
            SessionRestoreError.fromTokenValidationError(ServiceError.httpError(statusCode: 401))
                == .tokenExpired
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(ServiceError.httpError(statusCode: 403))
                == .tokenPermissionDenied
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(ServiceError.httpError(statusCode: 503))
                == .serverUnavailable
        )
        #expect(
            SessionRestoreError.fromTokenValidationError(ServiceError.invalidResponse)
                == .invalidServerResponse
        )
    }

    @Test func mapsTokenValidationNetworkErrors() {
        #expect(
            SessionRestoreError.fromTokenValidationError(URLError(.timedOut))
                == .networkUnavailable
        )
    }

    @Test func describesPermissionFailureWithoutCallingItExpired() {
        #expect(
            SessionRestoreError.tokenPermissionDenied.errorDescription?
                .contains("does not have permission") == true
        )
        #expect(
            SessionRestoreError.tokenPermissionDenied.errorDescription?
                .contains("expired") == false
        )
    }
}
