Add weekly weather cards and improve news API fallback
This commit is contained in:
@@ -149,6 +149,103 @@ public sealed class ProviderCachingTests
|
||||
Assert.Equal(1, handler.GetCallCount("/v2/everything"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NewsApiBriefingProvider_ContinuesFallbackChain_WhenCategoryReturnsHttpError()
|
||||
{
|
||||
var handler = new CountingHttpMessageHandler(message =>
|
||||
{
|
||||
var path = message.RequestUri?.AbsolutePath ?? string.Empty;
|
||||
if (!string.Equals(path, "/v2/top-headlines", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
var query = message.RequestUri?.Query ?? string.Empty;
|
||||
if (query.Contains("category=sports", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.BadRequest)
|
||||
{
|
||||
Content = new StringContent(
|
||||
"""{"status":"error","code":"parameterInvalid","message":"Category not supported for this key."}""",
|
||||
Encoding.UTF8,
|
||||
"application/json")
|
||||
};
|
||||
}
|
||||
|
||||
return JsonResponse(
|
||||
"""{"status":"ok","articles":[{"title":"General robotics update","description":"Top story","source":{"name":"AP News"},"url":"https://example.com/general"}]}""");
|
||||
});
|
||||
var provider = new NewsApiBriefingProvider(
|
||||
new HttpClient(handler),
|
||||
new NewsApiOptions
|
||||
{
|
||||
ApiKey = "test-key",
|
||||
CacheTtlSeconds = 300,
|
||||
FailureCacheTtlSeconds = 30
|
||||
},
|
||||
NullLogger<NewsApiBriefingProvider>.Instance);
|
||||
|
||||
var result = await provider.GetBriefingAsync(new NewsBriefingRequest(["sports"], 3));
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Single(result!.Headlines);
|
||||
Assert.Equal("General robotics update", result.Headlines[0].Title);
|
||||
Assert.Equal("success", result.ProviderStatus);
|
||||
Assert.Equal(2, handler.GetCallCount("/v2/top-headlines"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NewsApiBriefingProvider_PropagatesApiErrorCodeAndMessage_WhenAllEndpointsFail()
|
||||
{
|
||||
var handler = new CountingHttpMessageHandler(message =>
|
||||
{
|
||||
var path = message.RequestUri?.AbsolutePath ?? string.Empty;
|
||||
if (string.Equals(path, "/v2/top-headlines", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.BadRequest)
|
||||
{
|
||||
Content = new StringContent(
|
||||
"""{"status":"error","code":"parameterInvalid","message":"Category 'general' is not available for this account."}""",
|
||||
Encoding.UTF8,
|
||||
"application/json")
|
||||
};
|
||||
}
|
||||
|
||||
if (string.Equals(path, "/v2/everything", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.BadRequest)
|
||||
{
|
||||
Content = new StringContent(
|
||||
"""{"status":"error","code":"parametersMissing","message":"Missing required search query."}""",
|
||||
Encoding.UTF8,
|
||||
"application/json")
|
||||
};
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||
});
|
||||
var provider = new NewsApiBriefingProvider(
|
||||
new HttpClient(handler),
|
||||
new NewsApiOptions
|
||||
{
|
||||
ApiKey = "test-key",
|
||||
DefaultCategories = ["general"],
|
||||
CacheTtlSeconds = 300,
|
||||
FailureCacheTtlSeconds = 30
|
||||
},
|
||||
NullLogger<NewsApiBriefingProvider>.Instance);
|
||||
|
||||
var result = await provider.GetBriefingAsync(new NewsBriefingRequest([], 3));
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Empty(result!.Headlines);
|
||||
Assert.Equal("http_error", result.ProviderStatus);
|
||||
Assert.Equal("parameterInvalid", result.ProviderErrorCode);
|
||||
Assert.Equal("Category 'general' is not available for this account.", result.ProviderMessage);
|
||||
Assert.Equal((int)HttpStatusCode.BadRequest, result.ProviderHttpStatusCode);
|
||||
Assert.Contains("/v2/top-headlines", result.ProviderEndpoint, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static HttpResponseMessage JsonResponse(string body)
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.OK)
|
||||
|
||||
Reference in New Issue
Block a user