Unione delle correzioni delle piattaforme

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 01:43:47 +00:00
commit 9353a547c4
36 files changed
+1683 -152

No files matched your search

@@ -305,4 +305,43 @@ public sealed class DesktopCompositionTests
await DesktopTestBed.WaitUntilAsync(() => browser.LastError is not null, TimeSpan.FromSeconds(10), "il tentativo di avviare Edge");
Assert.IsFalse(browser.IsAvailable, "the fake msedge.exe does not exist: the browser is retried, nothing else breaks");
}
[TestMethod]
[Timeout(60_000, CooperativeCancellation = true)]
public async Task EveryEdgeFebeStartsIsTiedToFebeSoACrashLeavesNoEdgeHoldingThePort()
{
if (!OperatingSystem.IsLinux())
{
Assert.Inconclusive("Il finto msedge.exe di questa prova è uno script di shell.");
return;
}
// An «Edge» that starts and closes at once: every start must be tied to Febe before anything else happens, also
// the starts that fail and the ones after (pkg-6: an Edge left behind by a crash keeps the port 9222).
using var temp = new TempFolder("desktop-edge");
var edge = temp.Combine("Edge", "msedge.exe");
Directory.CreateDirectory(Path.GetDirectoryName(edge)!);
await File.WriteAllTextAsync(edge, "#!/bin/sh\nexit 3\n", TestContext.CancellationToken);
File.SetUnixFileMode(edge, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
var shell = new FakeDesktopShell { EdgePath = edge, TieSucceeds = false };
await using var bed = await DesktopTestBed.StartAsync(["--browser", "edge"], shell, temp, adjust: o => o with { EdgeDebugPort = FreePort() });
await DesktopTestBed.WaitUntilAsync(() => !shell.TiedToFebe.IsEmpty, TimeSpan.FromSeconds(20), "Edge legato alla vita di Febe");
var browser = (EdgeBrowser)bed.App.Browser!;
await DesktopTestBed.WaitUntilAsync(() => browser.LastError is not null, TimeSpan.FromSeconds(20), "il primo avvio di Edge, che si chiude subito");
Assert.IsFalse(browser.IsAvailable);
await bed.StopAsync();
// Windows refused: the log says what to do, in Italian.
var log = string.Concat(Directory.EnumerateFiles(Path.Combine(bed.Folders.DefaultDataFolder, "registro")).Select(File.ReadAllText));
StringAssert.Contains(log, "Windows non ha legato Edge a Febe");
}
private static int FreePort()
{
var listener = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
@@ -295,6 +295,170 @@ public sealed class DesktopLogicTests
Assert.IsNull(store.Pending);
}
[TestMethod]
public void AfterAMoveThisFebeHoldsTheNewFolderSoASecondLaunchDoesNotStartASecondFebe()
{
// pkg-1: the guard is named after the Data folder. After a move every launch reads the new folder: this Febe
// must hold its guard, or the next launch would start a second interface, browser and engine on it.
using var temp = new TempFolder("desktop-sposta");
var from = temp.Combine("vecchia");
var to = temp.Combine("nuova");
Directory.CreateDirectory(from);
File.WriteAllText(Path.Combine(from, "febe.sqlite"), "database");
var guards = new FakeInstanceGuards();
var store = new MemoryLocationStore { Pending = to };
using var started = guards.TryAcquire(from);
Assert.IsNotNull(started, "Program: the guard of the folder this Febe started on");
(string? Recorded, bool Written)? atClaim = null;
var result = DataFolderMove.ApplyPending(store, from, claimTarget: folder =>
{
atClaim = (store.Current, Directory.Exists(folder));
return guards.TryAcquire(folder);
});
Assert.AreEqual(DataFolderMoveOutcome.Moved, result.Outcome, result.Message);
Assert.AreEqual((null, false), atClaim, "the guard comes before the folder is recorded and before anything is written in it");
Assert.IsNotNull(result.TargetGuard);
Assert.AreEqual(Path.GetFullPath(to), store.Current);
// The next launch (Start menu, a start with the computer) reads the recorded folder and finds this Febe.
Assert.IsNull(guards.TryAcquire(store.Current!), "a second launch signals this Febe instead of starting another");
result.TargetGuard!.Dispose();
using var afterExit = guards.TryAcquire(store.Current!);
Assert.IsNotNull(afterExit, "after Esci the folder is free again");
}
[TestMethod]
public void AMoveIntoAFolderAnotherFebeHoldsDoesNotHappenAndAFailedMoveGivesTheGuardBack()
{
using var temp = new TempFolder("desktop-sposta");
var from = temp.Combine("vecchia");
Directory.CreateDirectory(from);
File.WriteAllText(Path.Combine(from, "febe.sqlite"), "database");
var guards = new FakeInstanceGuards();
// Another Febe (started with --dati) already holds the new folder: nothing is copied, the old folder stays.
var taken = temp.Combine("presa");
using (guards.TryAcquire(taken))
{
var store = new MemoryLocationStore { Pending = taken };
var refused = DataFolderMove.ApplyPending(store, from, claimTarget: guards.TryAcquire);
Assert.AreEqual(DataFolderMoveOutcome.Failed, refused.Outcome);
StringAssert.Contains(refused.Message, "un altro Febe la sta già usando");
Assert.IsNull(refused.TargetGuard);
Assert.IsNull(store.Pending);
Assert.IsNull(store.Current);
Assert.IsFalse(Directory.Exists(taken), "nothing written there");
Assert.IsTrue(File.Exists(Path.Combine(from, "febe.sqlite")));
}
// A copy that fails: Febe stays on the old folder and does not keep the guard of the new one.
var halfway = temp.Combine("a-meta");
var failing = new MemoryLocationStore { Pending = halfway };
var failed = DataFolderMove.ApplyPending(failing, from, new SyncProgress(_ => throw new IOException("disco pieno")), claimTarget: guards.TryAcquire);
Assert.AreEqual(DataFolderMoveOutcome.Failed, failed.Outcome);
Assert.IsNull(failed.TargetGuard);
using var free = guards.TryAcquire(halfway);
Assert.IsNotNull(free, "the guard of a move that did not happen is given back");
}
[TestMethod]
public void TheDataFolderLockLetsOneFebeOpenTheFolderWhateverItsSpelling()
{
using var temp = new TempFolder("desktop-blocco");
var data = temp.Combine("dati");
Assert.AreEqual(DataFolderLockOutcome.Acquired, DataFolderLock.TryAcquire(data, out var first, TimeSpan.Zero));
Assert.IsNotNull(first);
Assert.AreEqual(Path.Combine(data, DataFolderLock.FileName), first.Path);
Assert.IsTrue(File.Exists(first.Path));
// The same folder under another name (on Windows: a short 8.3 name, subst, a junction; here a symbolic link):
// the guards are named after the path and differ, the lock is the same file.
var alias = temp.Combine("alias");
Directory.CreateSymbolicLink(alias, data);
Assert.AreNotEqual(InstanceIdentity.For(data), InstanceIdentity.For(alias));
Assert.AreEqual(DataFolderLockOutcome.HeldByAnother, DataFolderLock.TryAcquire(alias, out var second, TimeSpan.Zero));
Assert.IsNull(second);
// Held for a moment only (an antivirus looking at the file): the start waits instead of giving up.
var release = Task.Run(async () =>
{
await Task.Delay(300, TestContext.CancellationToken);
first.Dispose();
}, TestContext.CancellationToken);
Assert.AreEqual(DataFolderLockOutcome.Acquired, DataFolderLock.TryAcquire(alias, out var later, TimeSpan.FromSeconds(10)));
release.Wait(TestContext.CancellationToken);
later!.Dispose();
// A Data folder where nothing can be written: the start-up checks explain it, the lock does not decide.
var file = temp.Combine("un-file");
File.WriteAllText(file, "x");
Assert.AreEqual(DataFolderLockOutcome.Unavailable, DataFolderLock.TryAcquire(file, out var none, TimeSpan.Zero));
Assert.IsNull(none);
}
[TestMethod]
public void AFebeStartedWithItsOwnDataFolderHasItsOwnStartWithTheComputer()
{
// pkg-5: «Febe.exe --dati D:\FebeSecondo» (a second account) must start that folder at the next login, and its
// switch must neither show nor remove the start of the usual Febe.
var exe = Path.Combine(Path.GetTempPath(), "Programmi", "Febe", "Febe.exe");
var second = Path.Combine(Path.GetTempPath(), "Febe Secondo");
Assert.AreEqual("Febe", Autostart.ValueNameFor(null));
Assert.AreEqual(InstanceIdentity.For(second), Autostart.ValueNameFor(second));
StringAssert.StartsWith(Autostart.ValueNameFor(second), "Febe-");
Assert.AreNotEqual(Autostart.ValueNameFor(second), Autostart.ValueNameFor(second + "2"));
var command = Autostart.Command(exe, second);
Assert.AreEqual($"\"{exe}\" --nascosto --dati \"{second}\"", command);
Assert.IsTrue(Autostart.PointsTo(command, exe, second));
Assert.IsTrue(Autostart.PointsTo(command, exe, second + Path.DirectorySeparatorChar), "the same folder");
Assert.IsFalse(Autostart.PointsTo(command, exe), "the usual Febe does not take it for its own");
Assert.IsFalse(Autostart.PointsTo(Autostart.Command(exe), exe, second), "nor the reverse: that command starts the usual folder");
Assert.IsFalse(Autostart.PointsTo(Autostart.Command(exe, second + "2"), exe, second), "another account");
Assert.IsFalse(Autostart.PointsTo($"\"{exe}\" --nascosto --dati", exe, second));
// Febe.exe reads the command back as written: hidden, on that Data folder.
var words = Autostart.Split(command)!;
Assert.AreEqual(exe, words[0]);
Assert.IsTrue(DesktopCommandLine.TryParse([.. words.Skip(1)], out var options, out var problem), problem);
Assert.IsTrue(options.StartHidden);
Assert.AreEqual(Path.GetFullPath(second), options.DataFolder);
// A folder at the root of a drive: the backslash before the closing quote is doubled, so it reads back whole.
CollectionAssert.AreEqual(new[] { exe, "--nascosto", "--dati", @"D:\" }, Autostart.Split(Autostart.Command(exe, @"D:\")));
CollectionAssert.AreEqual(new[] { "a", "b\"c", @"d\e" }, Autostart.Split("a \"b\\\"c\" d\\e"));
Assert.ThrowsExactly<ArgumentException>(() => Autostart.Command(exe, "C:\\a\"b"));
}
/// <summary>The single-instance guards of Windows (named mutexes), by Data folder, in memory.</summary>
private sealed class FakeInstanceGuards
{
private readonly HashSet<string> _held = [];
public IDisposable? TryAcquire(string dataFolder)
{
var name = InstanceIdentity.MutexName(dataFolder);
lock (_held)
{
return _held.Add(name) ? new Guard(this, name) : null;
}
}
private void Release(string name)
{
lock (_held)
{
_held.Remove(name);
}
}
private sealed class Guard(FakeInstanceGuards owner, string name) : IDisposable
{
public void Dispose() => owner.Release(name);
}
}
private sealed class SyncProgress(Action<DataFolderMoveProgress> report) : IProgress<DataFolderMoveProgress>
{
public void Report(DataFolderMoveProgress value) => report(value);
@@ -47,7 +47,12 @@ internal sealed class DesktopTestBed : IAsyncDisposable
public string Origin => $"{App.Address.Scheme}://{App.Address.Authority}";
public static async Task<DesktopTestBed> StartAsync(string[]? args = null, FakeDesktopShell? shell = null, TempFolder? temp = null, MemoryLocationStore? locations = null)
/// <param name="args">The command line of Febe.exe.</param>
/// <param name="shell">The fake Windows-only part.</param>
/// <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)
{
var ownsTemp = temp is null;
temp ??= new TempFolder("desktop");
@@ -58,6 +63,8 @@ internal sealed class DesktopTestBed : IAsyncDisposable
throw new ArgumentException(problem);
}
options = adjust?.Invoke(options) ?? options;
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) });
@@ -50,6 +50,18 @@ internal sealed class FakeDesktopShell : IDesktopShell
ProcessWindows.Enqueue("nascondi " + processId);
return Task.CompletedTask;
}
/// <summary>The processes the platform asked to tie to Febe's life (Edge), by id.</summary>
public ConcurrentQueue<int> TiedToFebe { get; } = new();
/// <summary>What «Windows» answers when a process is tied to Febe.</summary>
public bool TieSucceeds { get; set; } = true;
public bool EndWithFebe(System.Diagnostics.Process process)
{
TiedToFebe.Enqueue(process.Id);
return TieSucceeds;
}
}
/// <summary>Folder picker, explorer, default browser, notifications (the Windows alerts) and autostart, faked.</summary>