exh-0.2.0: test/Spec.hs
module Main where
import Conduit
import Control.Monad.Trans.Cont
import Control.Retry
import Data.Either
import Data.Maybe
import Network.HTTP.Client
import Test.Hspec
import Text.HTML.DOM
import Web.Exhentai.API.Auth
import Web.Exhentai.API.Gallery
import Web.Exhentai.API.MPV
import qualified Web.Exhentai.Parsing.Gallery as G
import Web.Exhentai.Parsing.Image
import qualified Web.Exhentai.Parsing.Search as S
import Web.Exhentai.Types
import Web.Exhentai.Types.CookieT
import Web.Exhentai.Utils
import Prelude hiding (readFile)
main :: IO ()
main = do
sanityCheck
runCookieT retryPolicyDefault $ do
auth $ Credential "VictorYu" "Victor142857"
vars <- fetchMpv $ Gallery 1815207 "5f4ceed6d7"
withSinkFile "1.png" $ \sinkF -> evalContT $ do
resp <- fetchImage $ head $ toRequests vars
let source = responseBody resp
runConduit $ source .| sinkF
{-
withSinkFile "2.zip" $ \sinkF -> evalContT $ do
resp <- streamOriginal "https://exhentai.org/archiver.php?gid=1800698&token=3d413c962e&or=447251--2c3a0356a675f5eb4c5a2afe08b9f15e385647bb"
let source = responseBody resp
runConduit $ source .| sinkF
-}
sanityCheck :: IO ()
sanityCheck = do
image <- readFile "test/Image.html"
galleryMpv <- readFile "test/Gallery-MPV.html"
galleryNonMpv <- readFile "test/Gallery-NonMPV.html"
galleryReplaced <- readFile "test/Gallery-Replaced.html"
search <- readFile "test/Search-Extended.html"
galleryWithAd <- readFile "test/Gallery-ADs.html"
hspec $ do
describe "Image.imageSrc" $ do
it "should return the image source link" $ do
(image ^?: imageSrc) `shouldSatisfy` isJust
describe "Image.nextPage" $ do
it "should return the link to the next page" $ do
(image ^?: nextPage) `shouldSatisfy` isJust
describe "Gallery.enTitle" $ do
it "should return the title of the gallery" $ do
(galleryMpv ^?: G.enTitle) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.enTitle) `shouldSatisfy` isJust
(galleryReplaced ^?: G.enTitle) `shouldSatisfy` isJust
describe "Gallery.jaTitle" $ do
it "should return the original title of the gallery" $ do
(galleryMpv ^?: G.jaTitle) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.jaTitle) `shouldSatisfy` isJust
(galleryReplaced ^?: G.jaTitle) `shouldSatisfy` isJust
describe "Gallery.category" $ do
it "should return the category of the gallery" $ do
(galleryMpv ^?: G.category) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.category) `shouldSatisfy` isJust
(galleryReplaced ^?: G.category) `shouldSatisfy` isJust
describe "Gallery.uploader" $ do
it "should return the uploader of the gallery" $ do
(galleryMpv ^?: G.uploader) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.uploader) `shouldSatisfy` isJust
(galleryReplaced ^?: G.uploader) `shouldSatisfy` isJust
describe "Gallery.ratingCount" $ do
it "should return the total rating count of the gallery" $ do
(galleryMpv ^?: G.ratingCount) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.ratingCount) `shouldSatisfy` isJust
(galleryReplaced ^?: G.ratingCount) `shouldSatisfy` isJust
describe "Gallery.averageRating" $ do
it "should return the average rating of the gallery" $ do
(galleryMpv ^?: G.averageRating) `shouldSatisfy` isJust
(galleryNonMpv ^?: G.averageRating) `shouldSatisfy` isJust
(galleryReplaced ^?: G.averageRating) `shouldSatisfy` isJust
describe "Gallery.newer" $ do
it "should return the link to the newer version of the library on replaced galleries and nothing otherwise" $ do
(galleryMpv ^?: G.newer) `shouldSatisfy` isNothing
(galleryNonMpv ^?: G.newer) `shouldSatisfy` isNothing
(galleryReplaced ^?: G.newer) `shouldSatisfy` isJust
describe "Gallery.parseGallery" $ do
it "should parse gallery just fine" $ do
parseGallery galleryMpv `shouldSatisfy` isRight
parseGallery galleryNonMpv `shouldSatisfy` isRight
parseGallery galleryReplaced `shouldSatisfy` isRight
parseGallery galleryWithAd `shouldSatisfy` isRight
describe "Search.pages" $ do
it "should return available page range" $ do
(search ^?: S.pages) `shouldSatisfy` isJust
describe "Search.galleryPreviewElement" $ do
it "should return gallery preview elements" $ do
(search ^?: S.galleryPreviewElement) `shouldSatisfy` isJust
describe "Search.galleryLink" $ do
it "should return galleries" $ do
(search ^?: S.galleryPreviewElement . S.galleryLink) `shouldSatisfy` isJust