Fix weather, yes/no, and news integrations
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Jibo.Cloud.Application.Abstractions;
|
||||
using Jibo.Cloud.Infrastructure.News;
|
||||
@@ -43,13 +44,65 @@ public sealed class ProviderCachingTests
|
||||
Assert.NotNull(second);
|
||||
Assert.Equal(1, handler.GetCallCount("/geo/1.0/direct"));
|
||||
Assert.Equal(1, handler.GetCallCount("/data/2.5/weather"));
|
||||
Assert.Equal(0, handler.GetCallCount("/data/2.5/forecast"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OpenWeatherReportProvider_EnrichesCurrentHiLoFromForecast_WhenCurrentBandIsFlat()
|
||||
{
|
||||
var utcStart = DateTimeOffset.UtcNow.UtcDateTime.Date;
|
||||
var forecastWindowStart = new DateTimeOffset(utcStart, TimeSpan.Zero).ToUnixTimeSeconds();
|
||||
var forecastWindowMid = new DateTimeOffset(utcStart.AddHours(3), TimeSpan.Zero).ToUnixTimeSeconds();
|
||||
var forecastWindowLate = new DateTimeOffset(utcStart.AddHours(6), TimeSpan.Zero).ToUnixTimeSeconds();
|
||||
|
||||
var handler = new CountingHttpMessageHandler(message =>
|
||||
{
|
||||
var path = message.RequestUri?.AbsolutePath ?? string.Empty;
|
||||
return path switch
|
||||
{
|
||||
"/geo/1.0/direct" => JsonResponse(
|
||||
"""[{"name":"Lone Jack","country":"US","lat":38.8708,"lon":-94.1733}]"""),
|
||||
"/data/2.5/weather" => JsonResponse(
|
||||
"""{"name":"Lone Jack","weather":[{"main":"Clouds","description":"overcast clouds"}],"main":{"temp":77.0,"temp_max":77.0,"temp_min":77.0}}"""),
|
||||
"/data/2.5/forecast" => JsonResponse(
|
||||
$"{{\"city\":{{\"timezone\":0}},\"list\":[{{\"dt\":{forecastWindowStart},\"main\":{{\"temp\":76.0,\"temp_max\":81.0,\"temp_min\":70.0}}}},{{\"dt\":{forecastWindowMid},\"main\":{{\"temp\":80.0,\"temp_max\":84.0,\"temp_min\":69.0}}}},{{\"dt\":{forecastWindowLate},\"main\":{{\"temp\":78.0,\"temp_max\":79.0,\"temp_min\":67.0}}}}]}}"),
|
||||
_ => new HttpResponseMessage(HttpStatusCode.NotFound)
|
||||
};
|
||||
});
|
||||
var provider = new OpenWeatherReportProvider(
|
||||
new HttpClient(handler),
|
||||
new OpenWeatherOptions
|
||||
{
|
||||
ApiKey = "test-key",
|
||||
CurrentCacheTtlSeconds = 300,
|
||||
ForecastCacheTtlSeconds = 300,
|
||||
GeocodeCacheTtlSeconds = 300,
|
||||
FailureCacheTtlSeconds = 30
|
||||
},
|
||||
NullLogger<OpenWeatherReportProvider>.Instance);
|
||||
|
||||
var report = await provider.GetReportAsync(new WeatherReportRequest("Lone Jack,US", null, null, false, false, 0));
|
||||
|
||||
Assert.NotNull(report);
|
||||
Assert.Equal(77, report!.Temperature);
|
||||
Assert.Equal(84, report.HighTemperature);
|
||||
Assert.Equal(67, report.LowTemperature);
|
||||
Assert.Equal(1, handler.GetCallCount("/data/2.5/weather"));
|
||||
Assert.Equal(1, handler.GetCallCount("/data/2.5/forecast"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NewsApiBriefingProvider_ReusesCachedHeadlinesForIdenticalRequests()
|
||||
{
|
||||
var missingUserAgentRequestCount = 0;
|
||||
var handler = new CountingHttpMessageHandler(message =>
|
||||
{
|
||||
if (!message.Headers.TryGetValues("User-Agent", out var userAgents) ||
|
||||
!userAgents.Any())
|
||||
{
|
||||
missingUserAgentRequestCount += 1;
|
||||
}
|
||||
|
||||
var path = message.RequestUri?.AbsolutePath ?? string.Empty;
|
||||
return path switch
|
||||
{
|
||||
@@ -75,6 +128,7 @@ public sealed class ProviderCachingTests
|
||||
Assert.NotNull(first);
|
||||
Assert.NotNull(second);
|
||||
Assert.Equal(1, handler.GetCallCount("/v2/top-headlines"));
|
||||
Assert.Equal(0, missingUserAgentRequestCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -1588,7 +1588,7 @@ public sealed class JiboInteractionServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildDecisionAsync_ForecastWithoutDate_WithProvider_DefaultsToTomorrow()
|
||||
public async Task BuildDecisionAsync_ForecastWithoutDate_WithProvider_ReturnsFiveDaySummary()
|
||||
{
|
||||
var provider = new CapturingWeatherReportProvider
|
||||
{
|
||||
@@ -1599,14 +1599,21 @@ public sealed class JiboInteractionServiceTests
|
||||
var decision = await service.BuildDecisionAsync(new TurnContext
|
||||
{
|
||||
RawTranscript = "what's the forecast",
|
||||
NormalizedTranscript = "what's the forecast"
|
||||
NormalizedTranscript = "what's the forecast",
|
||||
Attributes = new Dictionary<string, object?>
|
||||
{
|
||||
["context"] = """{"runtime":{"location":{"iso":"2026-04-20T08:00:00-05:00"}}}"""
|
||||
}
|
||||
});
|
||||
|
||||
Assert.Equal("weather", decision.IntentName);
|
||||
Assert.Null(provider.LastRequest?.LocationQuery);
|
||||
Assert.True(provider.LastRequest?.IsTomorrow);
|
||||
Assert.Equal(1, provider.LastRequest?.ForecastDayOffset);
|
||||
Assert.Equal("Tomorrow in Kansas City, U.S., expect clear sky with a high near 79 degrees Fahrenheit and a low around 63 degrees Fahrenheit.", decision.ReplyText);
|
||||
Assert.False(provider.LastRequest?.IsTomorrow);
|
||||
Assert.Equal(5, provider.LastRequest?.ForecastDayOffset);
|
||||
Assert.Equal(5, provider.Requests.Count);
|
||||
Assert.Contains("next five-day forecast", decision.ReplyText, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("Tuesday: clear sky, high 79, low 63.", decision.ReplyText, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.Contains("Saturday: clear sky, high 79, low 63.", decision.ReplyText, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -1697,7 +1704,7 @@ public sealed class JiboInteractionServiceTests
|
||||
|
||||
[Theory]
|
||||
[InlineData("how is the weather", null, 0, false)]
|
||||
[InlineData("what's the forecast", null, 1, true)]
|
||||
[InlineData("what's the forecast", null, 5, false)]
|
||||
[InlineData("forecast for new york city", "New York City", 1, true)]
|
||||
[InlineData("what's today's forecast", null, 0, false)]
|
||||
[InlineData("what's the weather in chicago", "Chicago", 0, false)]
|
||||
@@ -1729,6 +1736,11 @@ public sealed class JiboInteractionServiceTests
|
||||
Assert.Equal(expectedIsTomorrow, provider.LastRequest.IsTomorrow);
|
||||
Assert.Equal("chitchat-skill", decision.SkillName);
|
||||
Assert.Equal(true, decision.SkillPayload?["weather_view_enabled"]);
|
||||
|
||||
if (string.Equals(transcript, "what's the forecast", StringComparison.Ordinal))
|
||||
{
|
||||
Assert.Equal(5, provider.Requests.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -2151,6 +2163,48 @@ public sealed class JiboInteractionServiceTests
|
||||
Assert.Equal("No.", decision.ReplyText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildDecisionAsync_SurprisesDateOfferPrompt_WithNoisyAffirmation_MapsToSurpriseIntent()
|
||||
{
|
||||
var service = CreateService();
|
||||
|
||||
var decision = await service.BuildDecisionAsync(new TurnContext
|
||||
{
|
||||
RawTranscript = "- Thank you. - Yes.",
|
||||
NormalizedTranscript = "- Thank you. - Yes.",
|
||||
Attributes = new Dictionary<string, object?>
|
||||
{
|
||||
["listenRules"] = (string[])["surprises-date/offer_date_fact", "globals/gui_nav", "globals/global_commands_launch"],
|
||||
["listenAsrHints"] = (string[])["$YESNO"],
|
||||
["context"] = """{"runtime":{"location":{"iso":"2026-04-20T08:00:00-05:00"}}}"""
|
||||
}
|
||||
});
|
||||
|
||||
Assert.Equal("proactive_offer_pizza_fact", decision.IntentName);
|
||||
Assert.Equal("Do you want to hear a fun pizza fact?", decision.ReplyText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildDecisionAsync_WordOfDayOfferPrompt_WithNoisyAffirmation_MapsToWordOfDayLaunch()
|
||||
{
|
||||
var service = CreateService();
|
||||
|
||||
var decision = await service.BuildDecisionAsync(new TurnContext
|
||||
{
|
||||
RawTranscript = "- Me. - Yes.",
|
||||
NormalizedTranscript = "- Me. - Yes.",
|
||||
Attributes = new Dictionary<string, object?>
|
||||
{
|
||||
["listenRules"] = (string[])["word-of-the-day/surprise", "globals/gui_nav", "globals/global_commands_launch"],
|
||||
["listenAsrHints"] = (string[])["$YESNO"]
|
||||
}
|
||||
});
|
||||
|
||||
Assert.Equal("word_of_the_day", decision.IntentName);
|
||||
Assert.Equal("Starting word of the day.", decision.ReplyText);
|
||||
Assert.Equal("@be/word-of-the-day", decision.SkillName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildDecisionAsync_SettingsDownloadPrompt_MapsShortDenialToNoIntent()
|
||||
{
|
||||
@@ -2171,7 +2225,7 @@ public sealed class JiboInteractionServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildDecisionAsync_SurprisesDateOfferPrompt_MapsShortAffirmationToYesIntent()
|
||||
public async Task BuildDecisionAsync_SurprisesDateOfferPrompt_MapsShortAffirmationToSurpriseFlow()
|
||||
{
|
||||
var service = CreateService();
|
||||
|
||||
@@ -2186,8 +2240,8 @@ public sealed class JiboInteractionServiceTests
|
||||
}
|
||||
});
|
||||
|
||||
Assert.Equal("yes", decision.IntentName);
|
||||
Assert.Equal("Yes.", decision.ReplyText);
|
||||
Assert.Equal("proactive_offer_pizza_fact", decision.IntentName);
|
||||
Assert.Equal("Do you want to hear a fun pizza fact?", decision.ReplyText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -1932,14 +1932,53 @@ public sealed class JiboWebSocketServiceTests
|
||||
Assert.Equal(3, replies.Count);
|
||||
|
||||
using var listenPayload = JsonDocument.Parse(replies[0].Text!);
|
||||
Assert.Equal("yes", listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("intent").GetString());
|
||||
Assert.Equal("proactive_offer_pizza_fact", listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("intent").GetString());
|
||||
var rules = listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("rules");
|
||||
Assert.Single(rules.EnumerateArray());
|
||||
Assert.Equal("surprises-date/offer_date_fact", rules[0].GetString());
|
||||
Assert.Equal("surprises-date/offer_date_fact", listenPayload.RootElement.GetProperty("data").GetProperty("match").GetProperty("rule").GetString());
|
||||
var selectedRule = rules[0].GetString();
|
||||
Assert.True(
|
||||
string.Equals(selectedRule, "surprises-date/offer_date_fact", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(selectedRule, "shared/yes_no", StringComparison.OrdinalIgnoreCase));
|
||||
Assert.Equal(selectedRule, listenPayload.RootElement.GetProperty("data").GetProperty("match").GetProperty("rule").GetString());
|
||||
Assert.Empty(listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("entities").EnumerateObject());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ClientAsr_WordOfDayOfferPrompt_MapsYesToWordOfDayLaunch()
|
||||
{
|
||||
await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-wod-offer-yesno-token",
|
||||
Text = """{"type":"LISTEN","transID":"trans-wod-offer-yes","data":{"rules":["word-of-the-day/surprise","globals/gui_nav","globals/mim_repeat","globals/global_commands_launch"],"asr":{"hints":["$YESNO"]}}}"""
|
||||
});
|
||||
|
||||
var replies = await _service.HandleMessageAsync(new WebSocketMessageEnvelope
|
||||
{
|
||||
HostName = "neo-hub.jibo.com",
|
||||
Path = "/listen",
|
||||
Kind = "neo-hub-listen",
|
||||
Token = "hub-wod-offer-yesno-token",
|
||||
Text = """{"type":"CLIENT_ASR","transID":"trans-wod-offer-yes","data":{"text":"Yes!"}}"""
|
||||
});
|
||||
|
||||
Assert.True(replies.Count >= 3);
|
||||
|
||||
using var listenPayload = JsonDocument.Parse(replies[0].Text!);
|
||||
var rules = listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("rules");
|
||||
Assert.Single(rules.EnumerateArray());
|
||||
var selectedRule = rules[0].GetString();
|
||||
Assert.True(
|
||||
string.Equals(selectedRule, "word-of-the-day/surprise", StringComparison.OrdinalIgnoreCase) ||
|
||||
string.Equals(selectedRule, "word-of-the-day/menu", StringComparison.OrdinalIgnoreCase));
|
||||
Assert.Equal(selectedRule, listenPayload.RootElement.GetProperty("data").GetProperty("match").GetProperty("rule").GetString());
|
||||
Assert.Equal(
|
||||
"word-of-the-day",
|
||||
listenPayload.RootElement.GetProperty("data").GetProperty("nlu").GetProperty("entities").GetProperty("domain").GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResponsePlanMapper_EscapesSpeechWithoutEncodingApostrophes()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user