<commit_before>package org.mauritania.photosync.starter

import java.io.File
import java.net.InetSocketAddress

import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
import org.mauritania.photosync.TestHelper
import org.specs2.mutable.Specification


class StarterSpec extends Specification {

  "The starter" should {

    "throw exception in case of bad arguments" in {
      Starter.main(Array("--bad-argument")) must throwA[IllegalArgumentException]
    }

    "works correctly under normal conditions" in {

      val tmp = TestHelper.createTmpDir("photosync-tmp-starter")

      val expectedDownloadedFile = new File(tmp, new File("100OLYMP", "OR.ORF").getPath)

      val server = HttpServer.create(new InetSocketAddress(8085), 0)
      server.createContext("/", new RootHandler())
      server.setExecutor(null)
      server.start()

      Starter.main(
        Array(
          "--server-name", "localhost",
          "--server-port", "8085",
          "--output-directory", tmp.getAbsolutePath
        )
      )

      server.stop(0)

      expectedDownloadedFile.exists() must beTrue

      done
    }

  }

}

class RootHandler extends HttpHandler {

  val rootResponse = """
...
</style>
<script type="text/javascript">
wlansd = new Array();
wlansd[0]="/DCIM/,100OLYMP,0,0,0,0";
...
"""
  val folderResponse = """
...
</style>
<script type="text/javascript">
wlansd = new Array();
wlansd[0]="/DCIM/100OLYMP/,OR.ORF,15441739,0,18229,43541";
...
                     """
  val fileContentResponse = "=== PHOTO SAMPLE ==="

  def handle(t: HttpExchange) {
    t.getRequestURI.toString match {<commit_msg>Use withTmpDir in StarterSpec tests too<commit_after>package org.mauritania.photosync.starter

import java.io.File
import java.net.InetSocketAddress

import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
import org.mauritania.photosync.olympus.sync.TempDir
import org.specs2.mutable.Specification


class StarterSpec extends Specification with TempDir {

  "The starter" should {

    "throw exception in case of bad arguments" in {
      Starter.main(Array("--bad-argument")) must throwA[IllegalArgumentException]
    }

    "works correctly under normal conditions" in {
      withTmpDir { tmp =>
        val expectedDownloadedFile = new File(tmp, new File("100OLYMP", "OR.ORF").getPath)

        val server = HttpServer.create(new InetSocketAddress(8085), 0)
        server.createContext("/", new RootHandler())
        server.setExecutor(null)
        server.start()

        Starter.main(
          Array(
            "--server-name", "localhost",
            "--server-port", "8085",
            "--output-directory", tmp.getAbsolutePath
          )
        )

        server.stop(0)

        expectedDownloadedFile.exists() must beTrue
      }
    }

  }

}

class RootHandler extends HttpHandler {

  val rootResponse = """
...
</style>
<script type="text/javascript">
wlansd = new Array();
wlansd[0]="/DCIM/,100OLYMP,0,0,0,0";
...
"""
  val folderResponse = """
...
</style>
<script type="text/javascript">
wlansd = new Array();
wlansd[0]="/DCIM/100OLYMP/,OR.ORF,15441739,0,18229,43541";
...
                     """
  val fileContentResponse = "=== PHOTO SAMPLE ==="

  def handle(t: HttpExchange) {
    t.getRequestURI.toString match {
{'fraction_non_alphanumeric': 0.13669501822600244, 'fraction_numerical': 0.027339003645200487, 'mean_word_length': 3.0554187192118225, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5527368', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1022, 'n_words': 209}