Repository: scala-steward-org/scala-steward
Pull request #2895 (2023-01-07 14:41:25)

Test RepoCacheAlg with an up-to-date cache


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/Context.scala b/modules/core/src/main/scala/org/scalasteward/core/application/Context.scala
index 5e397faa..7aaed54a 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/Context.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/Context.scala
@@ -54,6 +54,7 @@ import org.typelevel.log4cats.Logger
 import org.typelevel.log4cats.slf4j.Slf4jLogger
 
 final class Context[F[_]](implicit
+    val artifactMigrationsLoader: ArtifactMigrationsLoader[F],
     val buildToolDispatcher: BuildToolDispatcher[F],
     val coursierAlg: CoursierAlg[F],
     val dateTimeAlg: DateTimeAlg[F],
@@ -61,8 +62,8 @@ final class Context[F[_]](implicit
     val fileAlg: FileAlg[F],
     val filterAlg: FilterAlg[F],
     val gitAlg: GitAlg[F],
-    val httpJsonClient: HttpJsonClient[F],
     val hookExecutor: HookExecutor[F],
+    val httpJsonClient: HttpJsonClient[F],
     val logger: Logger[F],
     val mavenAlg: MavenAlg[F],
     val millAlg: MillAlg[F],
@@ -70,9 +71,9 @@ final class Context[F[_]](implicit
     val pruningAlg: PruningAlg[F],
     val pullRequestRepository: PullRequestRepository[F],
     val refreshErrorAlg: RefreshErrorAlg[F],
+    val repoCacheAlg: RepoCacheAlg[F],
     val repoConfigAlg: RepoConfigAlg[F],
     val sbtAlg: SbtAlg[F],
-    val artifactMigrationsLoader: ArtifactMigrationsLoader[F],
     val scalafixMigrationsFinder: ScalafixMigrationsFinder,
     val scalafixMigrationsLoader: ScalafixMigrationsLoader[F],
     val scalafmtAlg: ScalafmtAlg[F],


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/repocache/RepoCacheAlgTest.scala b/modules/core/src/test/scala/org/scalasteward/core/repocache/RepoCacheAlgTest.scala
new file mode 100644
index 00000000..58471296
--- /dev/null
+++ b/modules/core/src/test/scala/org/scalasteward/core/repocache/RepoCacheAlgTest.scala
@@ -0,0 +1,62 @@
+package org.scalasteward.core.repocache
+
+import io.circe.Encoder
+import io.circe.generic.semiauto
+import io.circe.syntax._
+import munit.CatsEffectSuite
+import org.http4s.HttpApp
+import org.http4s.circe._
+import org.http4s.dsl.Http4sDsl
+import org.http4s.syntax.all._
+import org.scalasteward.core.TestInstances.dummySha1
+import org.scalasteward.core.data.RepoData
+import org.scalasteward.core.git.Branch
+import org.scalasteward.core.mock.MockContext.context._
+import org.scalasteward.core.mock.{MockEff, MockState}
+import org.scalasteward.core.vcs.data.{Repo, RepoOut, UserOut}
+
+class RepoCacheAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
+  // prevent IntelliJ from removing the import of org.http4s.circe._
+  locally(encodeUri)
+
+  implicit val userOutEncoder: Encoder[UserOut] =
+    semiauto.deriveEncoder
+
+  implicit val repoOutEncoder: Encoder[RepoOut] =
+    semiauto.deriveEncoder
+
+  test("checkCache: up-to-date cache") {
+    val repo = Repo("typelevel", "cats-effect")
+    val repoOut = RepoOut(
+      "cats-effect",
+      UserOut("scala-steward"),
+      Some(
+        RepoOut(
+          "cats-effect",
+          UserOut("typelevel"),
+          None,
+          uri"https://github.com/typelevel/cats-effect.git",
+          Branch("main")
+        )
+      ),
+      uri"https://github.com/scala-steward/cats-effect.git",
+      Branch("main")
+    )
+    val repoCache = RepoCache(dummySha1, Nil, None, None)
+    val workspace = workspaceAlg.rootDir.unsafeRunSync()
+    val state = MockState.empty
+      .copy(clientResponses = HttpApp[MockEff] {
+        case POST -> Root / "repos" / "typelevel" / "cats-effect" / "forks" =>
+          Ok(repoOut.asJson.spaces2)
+        case GET -> Root / "repos" / "typelevel" / "cats-effect" / "branches" / "main" =>
+          Ok(s""" { "name": "main", "commit": { "sha": "${dummySha1.value}" } } """)
+        case _ => NotFound()
+      })
+      .addFiles(
+        workspace / "store/repo_cache/v1/github/typelevel/cats-effect/repo_cache.json" -> repoCache.asJson.spaces2
+      )
+    val obtained = state.flatMap(repoCacheAlg.checkCache(repo).runA)
+    val expected = (RepoData(repo, repoCache, repoConfigAlg.mergeWithGlobal(None)), repoOut)
+    assertIO(obtained, expected)
+  }
+}