Skip to content

Commit

Permalink
Merge pull request #120 from dillonkearns/fix-static-http-generate-fi…
Browse files Browse the repository at this point in the history
…les-hanging

Fix static http generate files hanging
  • Loading branch information
dillonkearns authored Jun 16, 2020
2 parents 32f1936 + 4962bf6 commit 1605580
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 46 deletions.
34 changes: 21 additions & 13 deletions src/Pages/Internal/Platform/StaticResponses.elm
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ nextStep config siteMetadata mode secrets allRawResponses errors (StaticResponse
let
usableRawResponses : Dict String String
usableRawResponses =
Dict.Extra.filterMap
(\key value ->
value
|> Result.map Just
|> Result.withDefault Nothing
)
rawResponses
rawResponses
|> Dict.Extra.filterMap
(\key value ->
value
|> Result.map Just
|> Result.withDefault Nothing
)

hasPermanentError =
usableRawResponses
Expand All @@ -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
Expand Down
109 changes: 76 additions & 33 deletions tests/StaticHttpRequestsTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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"
}
]
]
]
]

Expand Down Expand Up @@ -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.equal
(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
)

[ _ ] ->
Expand All @@ -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
Expand Down

0 comments on commit 1605580

Please sign in to comment.