feat(import): search all providers in parallel with a license filter
The import page searches Openverse and the Internet Archive at the same time and lists each source's results as they arrive, with a Quelle column. A multiselect dropdown picks the licenses: Openverse filters on its server, the Internet Archive after the search. Provider texts are translated, and the unused bearer token support is gone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c3c698aa93
commit
8d1790d164
9 files changed
+268
-101
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Radiosender.Models;
|
||||
using Radiosender.Services.Providers;
|
||||
|
||||
namespace Radiosender.Tests;
|
||||
@@ -30,6 +31,30 @@ public class MusicProviderTests
|
||||
private static HttpResponseMessage Json(string json, string contentType = "application/json") =>
|
||||
new(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, contentType) };
|
||||
|
||||
[Fact]
|
||||
public async Task InternetArchive_KeepsOnlyTheChosenLicenses()
|
||||
{
|
||||
var provider = new InternetArchiveProvider(Http(uri =>
|
||||
Json(uri.AbsolutePath == "/advancedsearch.php" ? ArchiveSearchJson : ArchiveMetadataJson)));
|
||||
|
||||
var publicDomain = await provider.SearchAsync("jazz", new HashSet<string> { "pdm" });
|
||||
var none = await provider.SearchAsync("jazz", new HashSet<string>());
|
||||
|
||||
Assert.Equal(3, publicDomain.Count);
|
||||
Assert.All(publicDomain, track => Assert.Equal("Public Domain Mark 1.0", track.License));
|
||||
Assert.Empty(none);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("CC BY-NC-SA 4.0", "by-nc-sa")]
|
||||
[InlineData("CC BY 3.0", "by")]
|
||||
[InlineData("CC0 1.0", "cc0")]
|
||||
[InlineData("Public Domain Mark 1.0", "pdm")]
|
||||
[InlineData("CC SAMPLING+ 1.0", null)]
|
||||
[InlineData("", null)]
|
||||
public void LicenseType_ReadsTheCodeFromTheLicenseName(string license, string? code) =>
|
||||
Assert.Equal(code, LicenseType.CodeOf(license));
|
||||
|
||||
[Fact]
|
||||
public async Task InternetArchive_TurnsTheMp3FilesOfEachItemIntoTracks()
|
||||
{
|
||||
|
||||
@@ -91,6 +91,33 @@ public sealed class OpenverseTests : IDisposable
|
||||
Assert.Equal("Openverse", Assert.Single(controller.Providers).Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Controller_SearchesAllProvidersInParallelAndYieldsEachWhenDone()
|
||||
{
|
||||
var slowGate = new TaskCompletionSource<IReadOnlyList<MusicTrack>>();
|
||||
var slow = new FakeProvider("Slow", (_, _) => slowGate.Task);
|
||||
var failing = new FakeProvider("Failing", (_, _) => throw new MusicProviderException("kaputt"));
|
||||
IReadOnlySet<string>? seenLicenses = null;
|
||||
var fast = new FakeProvider("Fast", (_, licenses) => { seenLicenses = licenses; return Task.FromResult<IReadOnlyList<MusicTrack>>([Track()]); });
|
||||
var controller = new OnlineImportController([slow, failing, fast], NewClient((_, _) => Respond(HttpStatusCode.OK)), new SongRepository(Path.Combine(_directory, "test.db")));
|
||||
var licenses = new HashSet<string> { "by" };
|
||||
|
||||
List<ProviderResult> results = [];
|
||||
await foreach (var result in controller.SearchAllAsync("jazz", licenses))
|
||||
{
|
||||
results.Add(result);
|
||||
if (results.Count == 2)
|
||||
slowGate.SetResult([Track() with { Id = "slow" }]); // slow only finishes after the others were yielded
|
||||
}
|
||||
|
||||
Assert.Equal(["Failing", "Fast", "Slow"], results.Select(r => r.Provider.Name).Order());
|
||||
Assert.Equal("Slow", results[2].Provider.Name);
|
||||
Assert.Equal("kaputt", Assert.Single(results, r => r.Error is not null).Error!.Message);
|
||||
Assert.Empty(results.Single(r => r.Provider == failing).Tracks);
|
||||
Assert.Single(results.Single(r => r.Provider == fast).Tracks);
|
||||
Assert.Same(licenses, seenLicenses);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Search_ParsesResults()
|
||||
{
|
||||
@@ -119,6 +146,23 @@ public sealed class OpenverseTests : IDisposable
|
||||
Assert.Equal("CC0 1.0", tracks[1].License);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Search_SendsTheChosenLicenses()
|
||||
{
|
||||
var requested = new List<Uri>();
|
||||
var client = NewProvider((request, _) =>
|
||||
{
|
||||
requested.Add(request.RequestUri!);
|
||||
return Respond(HttpStatusCode.OK, new StringContent(SearchJson, Encoding.UTF8, "application/json"));
|
||||
});
|
||||
|
||||
await client.SearchAsync("jazz", new HashSet<string> { "cc0", "by" });
|
||||
var none = await client.SearchAsync("jazz", new HashSet<string>());
|
||||
|
||||
Assert.Equal("https://api.openverse.org/v1/audio/?q=jazz&page_size=20&license=by%2Ccc0", Assert.Single(requested).AbsoluteUri);
|
||||
Assert.Empty(none);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Search_RateLimited_ThrowsReadableError()
|
||||
{
|
||||
@@ -229,6 +273,14 @@ public sealed class OpenverseTests : IDisposable
|
||||
Assert.Empty(songs.GetAll());
|
||||
}
|
||||
|
||||
private sealed class FakeProvider(string name, Func<string, IReadOnlySet<string>?, Task<IReadOnlyList<MusicTrack>>> search) : IMusicProvider
|
||||
{
|
||||
public string Name => name;
|
||||
|
||||
public Task<IReadOnlyList<MusicTrack>> SearchAsync(string query, IReadOnlySet<string>? licenses = null, CancellationToken cancellationToken = default) =>
|
||||
search(query, licenses);
|
||||
}
|
||||
|
||||
private sealed class FakeHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handle) : HttpMessageHandler
|
||||
{
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) =>
|
||||
|
||||
Reference in new issue
Block a user