http = require "luajit-curl-helper.http"
index_title = "miti.sh · Catalin Constantin Mititiuc"

req = (url) ->
  request = http.init url
  st = request\perform!
  error request\lastError! if not st
  request

describe "test environment", ->
  it "can't connect to the internet", ->
    assert.has_error (-> req "http://example.org"), "Couldn't resolve host name"

describe "http://miti.sh", ->
  it "redirects to https", ->
    request = req "http://miti.sh"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://miti.sh/"

describe "https://webdevcat.me", ->
  it "permanently redirects to https://miti.sh", ->
    request = req "https://webdevcat.me"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://miti.sh/"

describe "https://webdevcat.me/git", ->
  it "permanently redirects to https://git.miti.sh/ccm/", ->
    request = req "https://webdevcat.me/git"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://git.miti.sh/ccm/"

describe "https://webdevcat.me/git/", ->
  it "permanently redirects to https://git.miti.sh", ->
    request = req "https://webdevcat.me/git"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://git.miti.sh/ccm/"

describe "https://webdevcat.me/git/pandoc", ->
  it "permanently redirects to https://git.miti.sh/ccm/pandoc", ->
    request = req "https://webdevcat.me/git/pandoc"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://git.miti.sh/ccm/pandoc"

describe "https://git.webdevcat.me", ->
  it "permanently redirects to https://git.miti.sh/", ->
    request = req "https://git.webdevcat.me"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://git.miti.sh/"

describe "https://webdevcat.me/apps/btroops", ->
  it "permanently redirects to https://apps.miti.sh/btroops", ->
    request = req "https://webdevcat.me/apps/btroops"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://apps.miti.sh/btroops"

describe "https://apps.webdevcat.me", ->
  it "permanently redirects to https://apps.miti.sh/", ->
    request = req "https://apps.webdevcat.me"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://apps.miti.sh/"

describe "https://apps.webdevcat.me/btroops", ->
  it "permanently redirects to https://apps.miti.sh/btroops", ->
    request = req "https://apps.webdevcat.me/btroops"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://apps.miti.sh/btroops"

describe "https://miti.sh", ->
  it "sends /index.html", ->
    request = req "https://miti.sh"
    assert.same request\statusCode!, 200
    assert.same request\statusMessage!, "OK"
    assert.same request\body!\match("<title>(.*)</title>"), index_title

describe "https://miti.sh/index", ->
  it "sends /index.html", ->
    request = req "https://miti.sh/index"
    assert.same request\statusCode!, 200
    assert.same request\statusMessage!, "OK"
    assert.same request\body!\match("<title>(.*)</title>"), index_title

describe "https://miti.sh/index.html", ->
  it "sends /index.html", ->
    request = req "https://miti.sh/index.html"
    assert.same request\statusCode!, 200
    assert.same request\statusMessage!, "OK"
    assert.same request\body!\match("<title>(.*)</title>"), index_title

describe "https://miti.sh/posts/", ->
  it "permanently redirects to http://miti.sh/posts", ->
    request = req "https://miti.sh/posts/"
    assert.same request\statusCode!, 301
    assert.same request\statusMessage!, "Moved Permanently"
    assert.same request\header!.Location, "https://miti.sh/posts"

describe "https://miti.sh/posts", ->
  it "sends /posts/index.html", ->
    with require "sitegen.path"
      assert .exists("html/posts/index.html"),
        "missing html/posts/index.html (try `make build file=blog.html`)"

    request = req "https://miti.sh/posts"
    assert.same request\statusCode!, 200
    assert.same request\statusMessage!, "OK"
    assert.same request\body!\match("<title>(.*)</title>"), "miti.sh · Posts"

describe "https://git.miti.sh", ->
  it "reverse-proxies request to a gitea unix socket", ->
    Path = require "sitegen.path"
    socket_fname = "unixstreamsrvr.moon"
    socket_dir = "/run/gitea"
    socket_owner = "nobody"
    basepath = Path.basepath debug.getinfo(1).short_src

    Path.exec "install", "-o", socket_owner, "-d", socket_dir
    cmd = "su -s /bin/bash -c 'moon %s' %s"
    server = io.popen cmd\format Path.join(basepath, socket_fname), socket_owner
    Path.exec "sleep", "0.1"
    result = Path.read_exec "find", socket_dir, "-type", "s", "-ls"
    assert.truthy result\match "nobody%s+nogroup.+" .. Path.join(socket_dir, "gitea.socket")

    req "https://git.miti.sh"

    reqheader = with server\read "*a"
      server\close!

    assert.truthy reqheader\match "Host: git.miti.sh"

describe "https://apps.miti.sh", ->
  it "doesn't find it", ->
    request = req "https://apps.miti.sh"
    assert.same request\statusCode!, 404
    assert.same request\statusMessage!, "Not Found"
