Make pending requests check check if the cache contains requests. Fixes #107.

This commit is contained in:
Dillon Kearns 2020-06-15 19:42:04 -07:00
parent 77d5af5a1c
commit 4962bf6520
2 changed files with 90 additions and 39 deletions

View File

@ -431,16 +431,24 @@ nextStep config siteMetadata mode secrets allRawResponses errors (StaticResponse
StaticHttpRequest.resolveUrls
ApplicationType.Cli
request
(rawResponses |> Dict.map (\key value -> value |> Result.withDefault ""))
(rawResponses
|> Dict.map (\key value -> value |> Result.withDefault "")
|> Dict.union (allRawResponses |> Dict.Extra.filterMap (\_ value -> value))
)
fetchedAllKnownUrls =
(knownUrlsToFetch
|> List.map Secrets.maskedLookup
|> List.map HashRequest.hash
(rawResponses
|> Dict.keys
|> Set.fromList
|> Set.size
|> Set.union (allRawResponses |> Dict.keys |> Set.fromList)
)
== (rawResponses |> Dict.keys |> List.length)
|> Set.diff
(knownUrlsToFetch
|> List.map Secrets.maskedLookup
|> List.map HashRequest.hash
|> Set.fromList
)
|> Set.isEmpty
in
if hasPermanentHttpError || hasPermanentError || (allUrlsKnown && fetchedAllKnownUrls) then
False

View File

@ -25,7 +25,7 @@ import Secrets
import SimulatedEffect.Cmd
import SimulatedEffect.Http as Http
import SimulatedEffect.Ports
import Test exposing (Test, describe, only, test)
import Test exposing (Test, describe, only, skip, test)
import Test.Http
@ -699,6 +699,7 @@ Found an unhandled HTML tag in markdown doc."""
"https://api.github.com/repos/dillonkearns/elm-pages"
"""{ "stargazer_count": 86 }"""
|> expectSuccessNew
[]
[ \success ->
success.filesToGenerate
|> Expect.equal
@ -707,6 +708,57 @@ Found an unhandled HTML tag in markdown doc."""
}
]
]
, test "it sends success port when no HTTP requests are needed because they're all cached" <|
\() ->
startLowLevel
(StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages-starter")
(starDecoder
|> Decode.map
(\starCount ->
[ Ok
{ path = [ "test.txt" ]
, content = "Star count: " ++ String.fromInt starCount
}
]
)
)
)
(Ok ())
[ ( { url = "https://api.github.com/repos/dillonkearns/elm-pages"
, method = "GET"
, headers = []
, body = StaticHttpBody.EmptyBody
}
, """{"stargazer_count":86}"""
)
, ( { url = "https://api.github.com/repos/dillonkearns/elm-pages-starter"
, method = "GET"
, headers = []
, body = StaticHttpBody.EmptyBody
}
, """{"stargazer_count":23}"""
)
]
[ ( []
, StaticHttp.get (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages") starDecoder
)
]
|> expectSuccessNew
[ ( ""
, [ ( get "https://api.github.com/repos/dillonkearns/elm-pages"
, """{"stargazer_count":86}"""
)
]
)
]
[ \success ->
success.filesToGenerate
|> Expect.equal
[ { path = [ "test.txt" ]
, content = "Star count: 23"
}
]
]
]
]
@ -977,28 +1029,39 @@ starDecoder =
expectSuccess : List ( String, List ( Request.Request, String ) ) -> ProgramTest model msg effect -> Expect.Expectation
expectSuccess expectedRequests previous =
expectSuccessNew expectedRequests [] previous
expectSuccessNew : List ( String, List ( Request.Request, String ) ) -> List (ToJsPayload.ToJsSuccessPayload PathKey -> Expect.Expectation) -> ProgramTest model msg effect -> Expect.Expectation
expectSuccessNew expectedRequests expectations previous =
previous
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder ToJsPayload.toJsCodec)
(\value ->
case value of
[ ToJsPayload.Success portPayload ] ->
portPayload.pages
|> Expect.equalDicts
(expectedRequests
|> List.map
(\( url, requests ) ->
( url
, requests
(ToJsPayload.Success portPayload) :: rest ->
portPayload
|> Expect.all
((\subject ->
subject.pages
|> Expect.equalDicts
(expectedRequests
|> List.map
(\( request, response ) ->
( Request.hash request, response )
(\( url, requests ) ->
( url
, requests
|> List.map
(\( request, response ) ->
( Request.hash request, response )
)
|> Dict.fromList
)
)
|> Dict.fromList
)
)
|> Dict.fromList
)
:: expectations
)
[ _ ] ->
@ -1009,26 +1072,6 @@ expectSuccess expectedRequests previous =
)
expectSuccessNew : List (ToJsPayload.ToJsSuccessPayload PathKey -> Expect.Expectation) -> ProgramTest model msg effect -> Expect.Expectation
expectSuccessNew expectations previous =
previous
|> ProgramTest.expectOutgoingPortValues
"toJsPort"
(Codec.decoder ToJsPayload.toJsCodec)
(\value ->
case value of
[ ToJsPayload.Success portPayload ] ->
portPayload
|> Expect.all expectations
[ _ ] ->
Expect.fail "Expected success port."
_ ->
Expect.fail ("Expected ports to be called once, but instead there were " ++ String.fromInt (List.length value) ++ " calls.")
)
expectError : List String -> ProgramTest model msg effect -> Expect.Expectation
expectError expectedErrors previous =
previous