module Handler.Utils.ZipSpec where

import TestImport

import Handler.Utils.Zip

import Data.Conduit
import qualified Data.Conduit.List as Conduit

import Data.List (dropWhileEnd)

import ModelSpec ()

import System.FilePath
import Data.Time

spec :: Spec
spec = describe "Zip file handling" $ do
  it "has compatible encoding/decoding to/from zip files" . property $ do
    zipFiles <- listOf $ scale (`div` 2) arbitrary
    return . property $ do
      zipFiles' <- runConduit $ Conduit.sourceList zipFiles =$= produceZip def =$= void consumeZip =$= Conduit.consume
      forM_ (zipFiles `zip` zipFiles') $ \(file, file') -> do
        let acceptableFilenameChanges
              = makeValid . dropWhile isPathSeparator . bool (dropWhileEnd isPathSeparator) addTrailingPathSeparator (isNothing $ fileContent file) . normalise . makeValid
            acceptableTimeDifference t1 t2 = abs (diffUTCTime t1 t2) <= 2
        (shouldBe `on` acceptableFilenameChanges) (fileTitle file') (fileTitle file)
        (fileModified file', fileModified file) `shouldSatisfy` uncurry acceptableTimeDifference
        (fileContent file') `shouldBe` (fileContent file)
