Remove duplicate function, add some unit tests for BackendTask resolution.

This commit is contained in:
Dillon Kearns 2023-01-12 10:28:29 -08:00
parent 82e34921db
commit 52e0a4fab8
2 changed files with 194 additions and 21 deletions

View File

@ -65,7 +65,7 @@ cacheRequestResolution request rawResponses =
case request of
Request urlList lookupFn ->
if List.isEmpty urlList then
cacheRequestResolutionHelp rawResponses (lookupFn Nothing rawResponses)
cacheRequestResolution (lookupFn Nothing rawResponses) rawResponses
else
Incomplete urlList (Request [] lookupFn)
@ -78,23 +78,3 @@ type Status error value
= Incomplete (List Pages.StaticHttp.Request.Request) (RawRequest error value)
| HasPermanentError Error
| Complete (Result error value)
cacheRequestResolutionHelp :
RequestsAndPending
-> RawRequest error value
-> Status error value
cacheRequestResolutionHelp rawResponses request =
case request of
Request urlList lookupFn ->
if urlList |> List.isEmpty then
cacheRequestResolutionHelp
rawResponses
(lookupFn Nothing rawResponses)
else
Incomplete urlList
(Request [] lookupFn)
ApiRoute value ->
Complete value

View File

@ -0,0 +1,193 @@
module StaticResponsesTests exposing (all)
import BackendTask exposing (BackendTask)
import BackendTask.Http
import BuildError exposing (BuildError)
import Exception exposing (Throwable)
import Expect
import Json.Decode as Decode
import Json.Encode as Encode
import Pages.Internal.Platform.StaticResponses as StaticResponses exposing (NextStep(..))
import Pages.Internal.StaticHttpBody exposing (Body(..))
import Pages.StaticHttp.Request as Request exposing (Request)
import RequestsAndPending exposing (ResponseBody)
import Test exposing (Test, describe, test)
all : Test
all =
describe "StaticResponses"
[ test "simple get" <|
\() ->
BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
(Decode.field "stargazers_count" Decode.int)
|> BackendTask.throw
|> expectRequestChain 123
[ [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
, Encode.object
[ ( "stargazers_count", Encode.int 123 )
]
)
]
]
, test "andThen" <|
\() ->
BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
(Decode.field "stargazers_count" Decode.int)
|> BackendTask.andThen
(\elmPagesStars ->
BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-graphql"
(Decode.field "stargazers_count" Decode.int)
|> BackendTask.map (\graphqlStars -> elmPagesStars + graphqlStars)
)
|> BackendTask.throw
|> expectRequestChain 579
[ [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
, Encode.object
[ ( "stargazers_count", Encode.int 123 )
]
)
]
, [ ( get "https://api.github.com/repos/dillonkearns/elm-graphql"
, Encode.object
[ ( "stargazers_count", Encode.int 456 )
]
)
]
]
]
get : String -> Request
get url =
{ url = url
, method = "GET"
, headers = []
, body = EmptyBody
, cacheOptions = Nothing
}
expectRequestChain :
a
-> List (List ( Request, Encode.Value ))
-> BackendTask Throwable a
-> Expect.Expectation
expectRequestChain expectedValue expectedChain request =
expectRequestChainHelp expectedValue
(expectedChain |> List.map (List.map Tuple.first))
(expectedChain
|> List.map
(List.map
(Tuple.mapFirst
(withInternalHeader
(RequestsAndPending.JsonBody Encode.null)
)
)
)
)
[]
{ allRawResponses = Encode.object []
, staticResponses = request
, errors = []
}
expectRequestChainHelp :
a
-> List (List Request)
-> List (List ( Request, Encode.Value ))
-> List (List Request)
->
{ staticResponses : BackendTask Throwable a
, errors : List BuildError
, allRawResponses : Encode.Value
}
-> Expect.Expectation
expectRequestChainHelp expectedValue fullExpectedChain expectedChain chainSoFar values =
case
StaticResponses.nextStep values
of
Finish actualFinalValue ->
Expect.all
[ \() ->
chainSoFar
|> List.reverse
|> List.map (List.map .url)
|> Expect.equal (fullExpectedChain |> List.map (List.map .url))
, \() ->
actualFinalValue
|> Expect.equal expectedValue
]
()
FinishedWithErrors errors ->
("Expected no errors, got FinishedWithErrors: \n"
++ BuildError.errorsToString errors
)
|> Expect.fail
Continue requests rawRequest ->
let
latestActualChainReversed : List (List Request)
latestActualChainReversed =
requests :: chainSoFar
in
case expectedChain of
first :: rest ->
let
thing : Encode.Value
thing =
first
|> List.map
(\( request, response ) ->
( Request.hash request
, Encode.object
[ ( "response"
, Encode.object
[ ( "body", response )
, ( "bodyKind", Encode.string "json" )
]
)
]
)
)
|> Encode.object
in
expectRequestChainHelp expectedValue
fullExpectedChain
rest
(requests :: chainSoFar)
{ allRawResponses = thing
, staticResponses = rawRequest
, errors = []
}
_ ->
-- TODO give error because it's not complete but should be?
latestActualChainReversed
|> List.reverse
|> List.map (List.map .url)
|> Expect.equal (fullExpectedChain |> List.map (List.map .url))
withInternalHeader : ResponseBody -> { a | headers : List ( String, String ) } -> { a | headers : List ( String, String ) }
withInternalHeader res req =
{ req
| headers =
( "elm-pages-internal"
, case res of
RequestsAndPending.JsonBody _ ->
"ExpectJson"
RequestsAndPending.BytesBody _ ->
"ExpectBytes"
RequestsAndPending.StringBody _ ->
"ExpectString"
RequestsAndPending.WhateverBody ->
"ExpectWhatever"
)
:: req.headers
}