From 0ea7d8c77ef719437d269c9e2d2b4f4d0f175158 Mon Sep 17 00:00:00 2001 From: cesnimda Date: Tue, 17 Nov 2026 14:41:01 +0100 Subject: [PATCH] fix(i18n): honour persisted Bokmal preferences in saved search alerts --- .../SavedJobSearchAlertTests.cs | 63 ++++++++++++++++--- .../SavedJobSearchAlertHostedService.cs | 7 +-- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/JobTrackerApi.Tests/SavedJobSearchAlertTests.cs b/JobTrackerApi.Tests/SavedJobSearchAlertTests.cs index 91b7ce26..1e891441 100644 --- a/JobTrackerApi.Tests/SavedJobSearchAlertTests.cs +++ b/JobTrackerApi.Tests/SavedJobSearchAlertTests.cs @@ -12,8 +12,20 @@ namespace JobTrackerApi.Tests; public sealed class SavedJobSearchAlertTests { - [Fact] - public async Task Worker_establishes_baseline_then_notifies_only_for_later_results() + [Theory] + [InlineData("nb-NO", true)] + [InlineData("nb", true)] + [InlineData("no", true)] + [InlineData(" NB-nO ", true)] + [InlineData(" nB ", true)] + [InlineData(" NO ", true)] + [InlineData("en", false)] + [InlineData(" EN ", false)] + [InlineData(null, false)] + [InlineData("", false)] + [InlineData("sv-SE", false)] + [InlineData("nn-NO", false)] + public async Task Worker_uses_owner_language_for_later_results(string? language, bool norwegian) { var clock = new MutableTimeProvider(new DateTimeOffset(2026, 8, 31, 8, 0, 0, TimeSpan.Zero)); var discovery = new FakeDiscovery(clock, "job-1"); @@ -39,6 +51,9 @@ public sealed class SavedJobSearchAlertTests await using (var scope = provider.CreateAsyncScope()) { var db = scope.ServiceProvider.GetRequiredService(); + db.Users.AddRange( + new ApplicationUser { Id = "owner-without-jobs", UiLanguage = language }, + new ApplicationUser { Id = "other-owner", UiLanguage = norwegian ? "en" : "nb-NO" }); db.SavedJobSearches.Add(new SavedJobSearch { OwnerUserId = "owner-without-jobs", @@ -70,13 +85,43 @@ public sealed class SavedJobSearchAlertTests discovery.Ids.Add("job-2"); Assert.Equal(new BackgroundWorkerRunResult(true, 1, 1, 0), await worker.RunOnceAsync(default)); - await using var verification = provider.CreateAsyncScope(); - var notification = Assert.Single(await verification.ServiceProvider.GetRequiredService() - .UserNotifications.IgnoreQueryFilters().AsNoTracking().ToListAsync()); - Assert.Equal("owner-without-jobs", notification.OwnerUserId); - Assert.Equal("saved_search_results", notification.Kind); - Assert.Equal("/discover", notification.LinkPath); - Assert.Contains("Backend Oslo", notification.Message); + await using (var verification = provider.CreateAsyncScope()) + { + var notification = Assert.Single(await verification.ServiceProvider.GetRequiredService() + .UserNotifications.IgnoreQueryFilters().AsNoTracking().ToListAsync()); + Assert.Equal("owner-without-jobs", notification.OwnerUserId); + Assert.Equal("saved_search_results", notification.Kind); + Assert.Equal("/discover", notification.LinkPath); + Assert.Equal(norwegian ? "Ny stilling funnet" : "New vacancy found", notification.Title); + Assert.Equal(norwegian + ? "Det lagrede søket «Backend Oslo» fant en ny stilling." + : "Your saved search ‘Backend Oslo’ found a new vacancy.", notification.Message); + } + + discovery.Ids.AddRange(["job-3", "job-4"]); + Assert.Equal(new BackgroundWorkerRunResult(true, 1, 1, 0), await worker.RunOnceAsync(default)); + await using (var verification = provider.CreateAsyncScope()) + Assert.Single(await verification.ServiceProvider.GetRequiredService() + .UserNotifications.IgnoreQueryFilters().ToListAsync()); + + clock.SetUtcNow(clock.GetUtcNow().AddHours(7)); + Assert.Equal(new BackgroundWorkerRunResult(true, 1, 1, 0), await worker.RunOnceAsync(default)); + clock.SetUtcNow(clock.GetUtcNow().AddHours(7)); + Assert.Equal(new BackgroundWorkerRunResult(true, 1, 1, 0), await worker.RunOnceAsync(default)); + await using (var verification = provider.CreateAsyncScope()) + { + var notifications = await verification.ServiceProvider.GetRequiredService() + .UserNotifications.IgnoreQueryFilters().OrderBy(item => item.CreatedAtUtc).ToListAsync(); + Assert.Equal(2, notifications.Count); + var notification = notifications[1]; + Assert.Equal("owner-without-jobs", notification.OwnerUserId); + Assert.Equal("saved_search_results", notification.Kind); + Assert.Equal("/discover", notification.LinkPath); + Assert.Equal(norwegian ? "2 nye stillinger funnet" : "2 new vacancies found", notification.Title); + Assert.Equal(norwegian + ? "Det lagrede søket «Backend Oslo» fant 2 nye stillinger." + : "Your saved search ‘Backend Oslo’ found 2 new vacancies.", notification.Message); + } } private sealed class FakeDiscovery(MutableTimeProvider clock, params string[] ids) : IJobDiscoverySearchService diff --git a/JobTrackerApi/Services/SavedJobSearchAlertHostedService.cs b/JobTrackerApi/Services/SavedJobSearchAlertHostedService.cs index 3c158c6f..76d6eb1a 100644 --- a/JobTrackerApi/Services/SavedJobSearchAlertHostedService.cs +++ b/JobTrackerApi/Services/SavedJobSearchAlertHostedService.cs @@ -44,10 +44,9 @@ public sealed class SavedJobSearchAlertHostedService( var runner = services.GetRequiredService(); var now = timeProvider.GetUtcNow(); var dueBefore = now - GetInterval(); - var useNorwegian = string.Equals( - await db.Users.AsNoTracking().Where(user => user.Id == db.CurrentUserId).Select(user => user.UiLanguage).FirstOrDefaultAsync(cancellationToken), - "nb", - StringComparison.OrdinalIgnoreCase); + var language = (await db.Users.AsNoTracking().Where(user => user.Id == db.CurrentUserId) + .Select(user => user.UiLanguage).FirstOrDefaultAsync(cancellationToken))?.Trim(); + var useNorwegian = language?.ToLowerInvariant() is "nb" or "nb-no" or "no"; var searches = await db.SavedJobSearches.Include(search => search.Results) .Where(search => search.IsActive) .OrderBy(search => search.Id)