E4: la ricerca dei video fermi riprova chi era troppo lento e non trattiene la chiusura

«Avviala dalla selezione» mandava a una selezione che saltava il video; un file in due account si cercava solo nel
primo; un avvio di video già in coda sembrava una ricerca nuova; una lettura ferma bloccava la chiusura di Febe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 18:57:57 +01:00
1 parent 9277e0a18c
commit 6dcaf30db6
10 files changed
+293 -30

No files matched your search

@@ -1,11 +1,14 @@
using System.Net;
using System.Text.Json;
using Febe.App;
using Febe.Core.Abstractions;
using Febe.Core.Alerts;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Library;
using Febe.Core.Library.StillVideos;
using Febe.Core.Storage;
using Febe.Data;
using Febe.Desktop.Core;
using Febe.Tests.Desktop.Support;
using Febe.Tests.Support;
@@ -216,6 +219,85 @@ public sealed class DesktopCompositionTests
Assert.IsFalse((await again.JsonAsync(HttpMethod.Get, "/api/settings/general")).GetProperty("canChangeDataFolder").GetBoolean(), "with --dati the command line decides");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task ClosingFebeEndsTheSearchForStillVideos()
{
// «Cerca i video fermi» waiting for a long work (tappa E4): a stop ends it before the database closes.
var ct = TestContext.CancellationToken;
await using var bed = await DesktopTestBed.StartAsync();
var account = await bed.App.Database.Accounts.AddAsync(Make.Account("marta.demo", "100"), ct);
var video = (await bed.App.Database.Entries.AddAsync(
Make.Entry(account.Id, "1", Make.T0), [Make.Stored(Make.Video(), Make.Sha(1), "post/a.mp4")], ct)).Stored.Media[0];
var service = bed.App.Web!.Services.GetRequiredService<StillVideoService>();
Assert.IsTrue(bed.App.Web.Services.GetRequiredService<LongWorkGate>().TryEnter(LongWorkKind.Import, out var import, out _));
using (import)
{
var started = await bed.JsonAsync(HttpMethod.Post, "/api/still-videos/start", $$"""{"mediaIds":[{{video.Id}}]}""");
Assert.AreEqual((true, 1), (started.GetProperty("running").GetBoolean(), started.GetProperty("added").GetInt32()));
await DesktopTestBed.WaitUntilAsync(() => service.Progress.Waiting is not null, TimeSpan.FromSeconds(10), "la ricerca aspetta");
Assert.IsFalse(service.Job.IsCompleted);
await bed.StopAsync();
}
Assert.IsTrue(service.Job.IsCompletedSuccessfully, "ended with Febe, not after it");
Assert.IsFalse(service.Progress.Running);
await using var database = await FebeDatabase.OpenAsync(bed.Folders.DefaultDataFolder, ct);
Assert.IsNull(await database.VideoAnalyses.GetAsync(Make.Sha(1), ct), "nothing stored");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
[DataRow(false)]
[DataRow(true)]
public async Task ClosingFebeGivesUpTheVideoAtWorkAndNeverWaitsWithoutAnEnd(bool stuck)
{
// A read stuck on a library that does not answer (stuck) must not keep the engine and the browser from closing;
// one that ends is waited for, and stores nothing.
var ct = TestContext.CancellationToken;
var begun = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using var release = new ManualResetEventSlim();
var hooks = new FebeAppHooks
{
StillVideoAnalyser = (_, _, token) =>
{
begun.TrySetResult();
if (stuck)
{
release.Wait(TimeSpan.FromSeconds(40), CancellationToken.None);
}
else
{
token.WaitHandle.WaitOne(TimeSpan.FromSeconds(40));
Thread.Sleep(1000);
}
return new StillVideoResult(VideoAnalysisOutcome.Moving, null, null, 2, 0, false, 12, 1, 0);
},
};
await using var bed = await DesktopTestBed.StartAsync(hooks: hooks);
var account = await bed.App.Database.Accounts.AddAsync(Make.Account("marta.demo", "100"), ct);
byte[] bytes = [1, 2, 3, 4];
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes));
var video = (await bed.App.Database.Entries.AddAsync(
Make.Entry(account.Id, "1", Make.T0), [Make.Stored(Make.Video(), sha, "post/a.mp4")], ct)).Stored.Media[0];
var path = bed.App.Library!.FullPath(account, "post/a.mp4");
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllBytesAsync(path, bytes, ct);
var service = bed.App.Web!.Services.GetRequiredService<StillVideoService>();
await bed.JsonAsync(HttpMethod.Post, "/api/still-videos/start", $$"""{"mediaIds":[{{video.Id}}]}""");
await begun.Task.WaitAsync(TimeSpan.FromSeconds(20), ct);
await bed.StopAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(30), ct);
Assert.AreEqual(!stuck, service.Job.IsCompleted);
release.Set();
await service.Job.WaitAsync(TimeSpan.FromSeconds(10), ct);
await using var database = await FebeDatabase.OpenAsync(bed.Folders.DefaultDataFolder, ct);
Assert.IsNull(await database.VideoAnalyses.GetAsync(sha, ct), "nothing stored");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task TheAlertsOfTheEngineBecomeWindowsNotifications()
@@ -52,7 +52,8 @@ internal sealed class DesktopTestBed : IAsyncDisposable
/// <param name="temp">The folder of «AppData» and «Immagini»; null: a new one, deleted with the bed.</param>
/// <param name="locations">The registry values of the Data folder.</param>
/// <param name="adjust">Options the command line cannot give (the debugging port of Edge).</param>
public static async Task<DesktopTestBed> StartAsync(string[]? args = null, FakeDesktopShell? shell = null, TempFolder? temp = null, MemoryLocationStore? locations = null, Func<DesktopOptions, DesktopOptions>? adjust = null)
/// <param name="hooks">The fakes of the composition.</param>
public static async Task<DesktopTestBed> StartAsync(string[]? args = null, FakeDesktopShell? shell = null, TempFolder? temp = null, MemoryLocationStore? locations = null, Func<DesktopOptions, DesktopOptions>? adjust = null, FebeAppHooks? hooks = null)
{
var ownsTemp = temp is null;
temp ??= new TempFolder("desktop");
@@ -67,7 +68,7 @@ internal sealed class DesktopTestBed : IAsyncDisposable
var folders = new DesktopFolders(temp.Combine("AppData", "Local"), temp.Combine("Immagini"));
var platform = new DesktopPlatform(options, shell, locations, dataFolderFixed: options.DataFolder is not null);
var app = await FebeApp.StartAsync(options.AppOptions(options.DataFolder ?? folders.DefaultDataFolder, folders), platform, new FebeAppHooks { ShutdownTimeout = TimeSpan.FromSeconds(1) });
var app = await FebeApp.StartAsync(options.AppOptions(options.DataFolder ?? folders.DefaultDataFolder, folders), platform, (hooks ?? new FebeAppHooks()) with { ShutdownTimeout = TimeSpan.FromSeconds(1) });
return new DesktopTestBed(temp, ownsTemp, shell, locations, platform, app);
}