module Handler.Utils.RatingSpec where

import TestImport
import Handler.Utils.Rating
import ModelSpec ()
import Model.RatingSpec ()

import Data.Time.Clock.System (systemEpochDay)
import qualified Data.ByteString.Lazy as LBS

import qualified Data.Text as Text

import Utils (assertM)


spec :: Spec
spec = describe "Rating file parsing/pretty-printing" $ do
  it "roundtrips" . property $ \(_ :: SubmissionId, subId) (mRating -> rating) ->
    parseRating' (formatRating subId rating) === Just (ratingValues rating)
  it "has idempotent formatting" . property $ \(_ :: SubmissionId, subId) (mRating -> rating) ->
    fmap (\r' -> formatRating subId $ rating { ratingValues = r' }) (parseRating' $ formatRating subId rating) === Just (formatRating subId rating)
  where
    parseRating' :: LBS.ByteString -> Maybe Rating'
    parseRating' = parseRating . flip (File "bewertung.txt") time . Just . LBS.toStrict

    time = UTCTime systemEpochDay 0
    mRating rating = rating { ratingValues = mRating' $ ratingValues rating }
    mRating' rating' = rating' { ratingTime = Just time -- There is no field for ratingTime so we just always expect file modification time
                               , ratingComment = assertM (not . Text.null) $ Text.strip <$> ratingComment rating'
                               }
