/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.util

import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.HttpException
import retrofit2.Response

/**
 * Test doubles emulate Retrofit's error contract: a suspend API method throws
 * [HttpException] on a non-2xx and an [java.io.IOException] on a transport failure,
 * exactly what `safeApiCall` folds into `ApiResult.HttpError` / `NetworkError`.
 */

private val JSON = "application/json".toMediaTypeOrNull()

/** An [HttpException] carrying [code] — what a fake API throws to drive an HTTP-error path. */
fun httpError(code: Int): HttpException =
    HttpException(Response.error<Any>(code, "{}".toResponseBody(JSON)))

/** A successful bodyless [Response] (for `@DELETE`/moderation endpoints returning `Response<Unit>`). */
fun okUnit(): Response<Unit> = Response.success(Unit)

/** A non-2xx bodyless [Response] with [code]. */
fun errorUnit(code: Int): Response<Unit> = Response.error(code, "{}".toResponseBody(JSON))
