From 4962bf6520c092f2096b60507f0476214d998d7d Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 15 Jun 2020 19:42:04 -0700 Subject: [PATCH] Make pending requests check check if the cache contains requests. Fixes #107. --- .../Internal/Platform/StaticResponses.elm | 20 +++- tests/StaticHttpRequestsTests.elm | 109 ++++++++++++------ 2 files changed, 90 insertions(+), 39 deletions(-) diff --git a/src/Pages/Internal/Platform/StaticResponses.elm b/src/Pages/Internal/Platform/StaticResponses.elm index f9a38030..3a86f293 100644 --- a/src/Pages/Internal/Platform/StaticResponses.elm +++ b/src/Pages/Internal/Platform/StaticResponses.elm @@ -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 diff --git a/tests/StaticHttpRequestsTests.elm b/tests/StaticHttpRequestsTests.elm index fea2e5be..21adf078 100644 --- a/tests/StaticHttpRequestsTests.elm +++ b/tests/StaticHttpRequestsTests.elm @@ -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