package com.github.sormuras.bach.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.sormuras.bach.Bach;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class GitHubTests {

  @Test
  void simplicissimus() {
    var baos = new ByteArrayOutputStream();
    var log = new PrintStream(baos, true, StandardCharsets.UTF_8);
    var bach = new Bach(log, Bach::newHttpClient);
    var hub = new GitHub(bach, "sormuras", "simplicissimus");
    assertTrue(hub.findLatestCommitHash().isPresent());

    var module = "com.github.sormuras.simplicissimus";
    assertEquals(Optional.empty(), hub.findReleasedModule(module, "0"));
    assertEquals(Optional.empty(), hub.findReleasedModule(module, "1"));
    assertEquals(Optional.empty(), hub.findReleasedModule(module, "1.3.1"));
    assertEquals(
        "https://github.com/sormuras/simplicissimus/releases/download/1.4.1/com.github.sormuras.simplicissimus@1.4.1.jar",
        hub.findReleasedModule(module, "1.4.1").orElseThrow());

    var latest = hub.findLatestReleaseTag().orElseThrow();
    assertEquals(
        "https://github.com/sormuras/simplicissimus/releases/download/"
            + latest
            + "/com.github.sormuras.simplicissimus@"
            + latest
            + ".jar",
        hub.findReleasedModule(module, latest).orElseThrow());
  }

  @ParameterizedTest
  @ValueSource(strings = {"early-access", "1-ea+2", "1-ea+1", "0"})
  void sawdust(String version) {
    var baos = new ByteArrayOutputStream();
    var log = new PrintStream(baos, true, StandardCharsets.UTF_8);
    var bach = new Bach(log, Bach::newHttpClient);
    var hub = new GitHub(bach, "sormuras", "sawdust");
    assertTrue(hub.findReleasedModule("com.github.sormuras.sawdust", version).isPresent());
    assertTrue(hub.findReleasedModule("com.github.sormuras.sawdust.api", version).isPresent());
    assertTrue(hub.findReleasedModule("com.github.sormuras.sawdust.core", version).isPresent());
  }
}

@@ -1177,18 +1177,217 @@
 load/1.4
-.1
+/com.github.sormuras.simplicissimus@1.4.jar%22,%0A        hub.findReleasedModule(module, %221.4%22).orElseThrow());%0A    assertEquals(%0A        %22https://github.com/sormuras/simplicissimus/releases/download/1.4.6
 /com.git
@@ -1418,17 +1418,17 @@
 mus@1.4.
-1
+6
 .jar%22,%0A 
@@ -1470,17 +1470,216 @@
 e, %221.4.
-1
+6%22).orElseThrow());%0A    assertEquals(%0A        %22https://github.com/sormuras/simplicissimus/releases/download/1.5/com.github.sormuras.simplicissimus@1.5.jar%22,%0A        hub.findReleasedModule(module, %221.5
 %22).orEls

Include more released simplicissimus modules in GitHub-related tests