{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Test.Tweet where

import           Airship
import           Airship.Resource.Static

import           Control.Monad.Trans.State

import qualified Data.ByteString.Lazy as BSL

import           Disorder.Core.IO

import           Network.HTTP.Types
import           Network.Wai
import qualified Network.Wai.Test as WT

import           P

import           System.IO

import           Test.QuickCheck
import           Test.QuickCheck.Instances ()

import           Tweeter

makeRequest :: Application -> Request -> IO WT.SResponse
makeRequest a r = do
  x <- BSL.fromStrict <$> requestBody r
  WT.runSession (WT.srequest $ WT.SRequest r x) a

app :: RoutingSpec (StateT TweetState IO) () -> TweetState -> Application
app routes' s = do
  resourceToWaiT
    defaultAirshipConfig
    (const $ flip evalStateT s)
    routes'
    errors

prop_endpoint_create :: Property
prop_endpoint_create = testIO $ do
  db <- init
  let req = defaultRequest {
          requestMethod = methodGet
        , pathInfo = ["tweets"]
        , requestHeaders = [(hContentType, "application/json")]}

  static <- staticResource Cache "assets"

  let s = TweetState Nothing
  r <- makeRequest (app (routes db static) s) req
  pure $ WT.simpleStatus r === status200

return []
tests :: IO Bool
tests = $forAllProperties $ quickCheckWithResult (stdArgs {maxSuccess = 2})
