elm-pages-v3-beta/tests/StaticHttpRequestsTests.elm

297 lines
12 KiB
Elm
Raw Normal View History

module StaticHttpRequestsTests exposing (all)
2019-10-22 08:00:15 +03:00
import Codec
import Dict exposing (Dict)
2019-10-22 06:21:52 +03:00
import Expect
import Html
import Json.Decode as Decode
import Pages.ContentCache as ContentCache
import Pages.Document as Document
import Pages.ImagePath as ImagePath
import Pages.Internal.Platform.Cli as Main exposing (..)
import Pages.Manifest as Manifest
import Pages.PagePath as PagePath
import ProgramTest exposing (ProgramTest)
2019-10-24 18:26:41 +03:00
import Secrets exposing (Secrets)
import SimulatedEffect.Cmd
import SimulatedEffect.Http
2019-10-22 06:21:52 +03:00
import SimulatedEffect.Ports
import StaticHttp
import Test exposing (Test, describe, only, test)
all : Test
all =
describe "Static Http Requests"
[ test "initial requests are sent out" <|
\() ->
start
[ ( []
2019-10-24 18:26:41 +03:00
, { url = \_ -> Ok "https://api.github.com/repos/dillonkearns/elm-pages", decoder = Decode.succeed () }
)
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }"""
2019-10-22 08:00:15 +03:00
|> ProgramTest.expectOutgoingPortValues
2019-10-22 06:21:52 +03:00
"toJsPort"
2019-10-22 08:00:15 +03:00
(Codec.decoder Main.toJsCodec)
(Expect.equal
[ Main.Success
{ pages =
Dict.fromList
[ ( "/"
, Dict.fromList
[ ( "https://api.github.com/repos/dillonkearns/elm-pages"
, """{ "stargazer_count": 86 }"""
)
2019-10-22 08:00:15 +03:00
]
)
]
2019-10-22 09:02:31 +03:00
, manifest = manifest
2019-10-22 08:00:15 +03:00
}
]
)
, test "port is sent out once all requests are finished" <|
\() ->
start
[ ( [ "elm-pages" ]
2019-10-24 18:26:41 +03:00
, { url = \_ -> Ok "https://api.github.com/repos/dillonkearns/elm-pages", decoder = Decode.succeed () }
)
, ( [ "elm-pages-starter" ]
2019-10-24 18:26:41 +03:00
, { url = \_ -> Ok "https://api.github.com/repos/dillonkearns/elm-pages-starter", decoder = Decode.succeed () }
)
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }"""
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages-starter"
"""{ "stargazer_count": 22 }"""
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder Main.toJsCodec)
(Expect.equal
[ Main.Success
{ pages =
Dict.fromList
[ ( "/elm-pages"
, Dict.fromList
[ ( "https://api.github.com/repos/dillonkearns/elm-pages"
, """{ "stargazer_count": 86 }"""
)
]
)
, ( "/elm-pages-starter"
, Dict.fromList
[ ( "https://api.github.com/repos/dillonkearns/elm-pages-starter"
, """{ "stargazer_count": 22 }"""
)
]
)
]
, manifest = manifest
}
]
)
, test "an error is sent out for decoder failures" <|
\() ->
start
2019-10-24 18:26:41 +03:00
[ ( [ "elm-pages" ], { url = \_ -> Ok "https://api.github.com/repos/dillonkearns/elm-pages", decoder = Decode.fail "The user should get this message from the CLI." } )
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }"""
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder Main.toJsCodec)
(Expect.equal
2019-10-24 16:11:56 +03:00
[ Errors
(Dict.fromList
[ ( "/elm-pages"
, "Problem with the given value:\n\n{\n \"stargazer_count\": 86\n }\n\nThe user should get this message from the CLI."
)
]
)
]
)
2019-10-24 18:26:41 +03:00
, test "uses real secrets to perform request and masked secrets to store and lookup response" <|
\() ->
start
[ ( []
, { url =
\secrets ->
secrets
|> Secrets.get "API_KEY"
|> Result.map
(\apiKey ->
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=" ++ apiKey
)
, decoder = Decode.succeed ()
}
)
]
|> ProgramTest.simulateHttpOk
"GET"
"https://api.github.com/repos/dillonkearns/elm-pages?apiKey=ABCD1234"
"""{ "stargazer_count": 86 }"""
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder Main.toJsCodec)
(Expect.equal
[ Main.Success
{ pages =
Dict.fromList
[ ( "/"
, Dict.fromList
[ ( "https://api.github.com/repos/dillonkearns/elm-pages?apiKey=<API_KEY>"
, """{ "stargazer_count": 86 }"""
)
]
)
]
, manifest = manifest
}
]
)
]
2019-10-24 18:26:41 +03:00
start : List ( List String, { url : Secrets -> Result String String, decoder : Decode.Decoder () } ) -> ProgramTest Main.Model Main.Msg (Main.Effect PathKey)
start pages =
let
document =
Document.fromList
[ Document.parser
{ extension = "md"
, metadata = Decode.succeed ()
, body = \_ -> Ok ()
}
]
content =
pages
|> List.map
(\( path, _ ) ->
( path, { extension = "md", frontMatter = "null", body = Just "" } )
)
contentCache =
ContentCache.init document content
siteMetadata =
contentCache
|> Result.map
(\cache -> cache |> ContentCache.extractMetadata PathKey)
|> Result.mapError
(\error ->
error
|> Dict.toList
|> List.map (\( path, errorString ) -> errorString)
)
config =
{ toJsPort = toJsPort
, manifest = manifest
, view =
\allFrontmatter page ->
let
thing =
pages
|> Dict.fromList
|> Dict.get
(page.path
|> PagePath.toString
|> String.dropLeft 1
|> String.split "/"
|> List.filter (\pathPart -> pathPart /= "")
)
in
case thing of
Just { url, decoder } ->
2019-10-24 18:26:41 +03:00
StaticHttp.jsonRequestWithSecrets url
decoder
|> StaticHttp.map
(\staticData -> { view = \model viewForPage -> { title = "Title", body = Html.text "" }, head = [] })
Nothing ->
Debug.todo "Couldn't find page"
}
in
ProgramTest.createDocument
{ init = Main.init identity contentCache siteMetadata config identity
, update = Main.update siteMetadata config
, view = \_ -> { title = "", body = [ Html.text "" ] }
}
|> ProgramTest.withSimulatedEffects simulateEffects
2019-10-24 18:26:41 +03:00
|> ProgramTest.start (flags """{"secrets":
{"API_KEY": "ABCD1234"}
}""")
flags : String -> Decode.Value
flags jsonString =
case Decode.decodeString Decode.value jsonString of
Ok value ->
value
Err _ ->
Debug.todo "Invalid JSON value."
2019-10-22 09:02:31 +03:00
simulateEffects : Main.Effect PathKey -> ProgramTest.SimulatedEffect Main.Msg
simulateEffects effect =
case effect of
NoEffect ->
SimulatedEffect.Cmd.none
SendJsData value ->
2019-10-22 08:00:15 +03:00
SimulatedEffect.Ports.send "toJsPort" (value |> Codec.encoder Main.toJsCodec)
-- toJsPort value |> Cmd.map never
Batch list ->
list
|> List.map simulateEffects
|> SimulatedEffect.Cmd.batch
2019-10-24 18:26:41 +03:00
FetchHttp realSecrets urlWithSecrets ->
SimulatedEffect.Http.get
2019-10-24 17:06:50 +03:00
{ url = urlWithSecrets realSecrets |> Result.withDefault "TODO" -- TODO handle error
, expect =
SimulatedEffect.Http.expectString
(\response ->
2019-10-24 17:06:50 +03:00
GotStaticHttpResponse
{ url = Secrets.useFakeSecrets urlWithSecrets
, response = response
}
)
}
toJsPort foo =
Cmd.none
type PathKey
= PathKey
manifest : Manifest.Config PathKey
manifest =
{ backgroundColor = Nothing
, categories = []
, displayMode = Manifest.Standalone
, orientation = Manifest.Portrait
, description = "elm-pages - A statically typed site generator."
, iarcRatingId = Nothing
, name = "elm-pages docs"
, themeColor = Nothing
2019-10-22 09:02:31 +03:00
, startUrl = PagePath.external ""
, shortName = Just "elm-pages"
2019-10-22 09:02:31 +03:00
, sourceIcon = ImagePath.external ""
}