fix(app): a portable copy in a OneDrive folder keeps its index outside it

BRIEF §6.2 forbids a SQLite index in a synchronised folder. AppEnvironment.Detect now resolves the data folder
through Core.Windows' AppPaths and OneDriveFolders: a portable copy whose .\data would lie under a OneDrive sync
root (a OneDrive folder, or a Desktop or Documents folder backed up into one) keeps its data in
%LOCALAPPDATA%\Titano and stays portable otherwise; AppEnvironment.SyncedFolder names the OneDrive folder so the
user can be told. The pure choice lives in Core/IO/DataLocation (tested on Linux); reading the OneDrive accounts
key no longer fails start-up when it cannot be read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:02:33 +00:00
1 parent 8eaaef5085
commit 815782a708
12 files changed
+279 -53

No files matched your search

@@ -0,0 +1,34 @@
using System.IO;
using Titano.App.Infrastructure;
using Titano.Core.Windows.Shell;
using Titano.Testing;
namespace Titano.App.Tests.Infrastructure;
/// <summary>BRIEF §6.2: the client's data folder, which is never inside a folder OneDrive synchronises.</summary>
public sealed class AppEnvironmentTests(TestContext context)
{
[Test]
public void APortableCopyInOneDriveKeepsItsDataInTheUserFolderAndSaysSo()
{
var oneDrive = context.CreateTempDirectory("onedrive");
var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Desktop", "Titano")).FullName;
File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), "");
var local = context.CreateTempDirectory("local");
var environment = AppEnvironment.From(AppPaths.Resolve(app, local, [oneDrive]));
Assert.Equal(Path.Combine(local, "Titano", "titano.db"), environment.DatabasePath);
Assert.True(environment.IsPortable, "still a portable copy: updates fetch the portable zip, no sign-in entry");
Assert.Equal<string?>(oneDrive, environment.SyncedFolder);
Assert.True(Directory.Exists(environment.LogDirectory));
Assert.False(Directory.Exists(Path.Combine(app, "data")), "nothing is written into the synchronised folder");
// Elsewhere (a USB stick) a portable copy keeps its data beside it.
var stick = context.CreateTempDirectory("stick");
File.WriteAllText(Path.Combine(stick, PortableMode.MarkerFileName), "");
var beside = AppEnvironment.From(AppPaths.Resolve(stick, local, [oneDrive]));
Assert.Equal(Path.Combine(stick, "data"), beside.DataDirectory);
Assert.Null(beside.SyncedFolder);
}
}
@@ -0,0 +1,68 @@
using Titano.Core.IO;
using Titano.Testing;
namespace Titano.Core.Tests.Foundation;
/// <summary>BRIEF §6.2: where the client keeps its index, and never inside a folder OneDrive synchronises.</summary>
public sealed class DataLocationTests(TestContext context)
{
[Test]
public void AnInstalledCopyUsesThePerUserFolderAndAPortableOneItsOwn()
{
var app = context.CreateTempDirectory("app");
var local = context.CreateTempDirectory("local");
var installed = DataLocation.Choose(app, portable: false, local, []);
Assert.Equal(new DataLocation(Path.Combine(local, "Titano"), false, null, null), installed);
var portable = DataLocation.Choose(app, portable: true, local, []);
Assert.Equal(new DataLocation(Path.Combine(app, "data"), true, null, null), portable);
}
[Test]
public void APortableCopyInASynchronisedFolderKeepsItsDataInThePerUserFolder()
{
// A portable copy unpacked on a Desktop that OneDrive's folder backup moved into the sync root.
var oneDrive = context.CreateTempDirectory("onedrive");
var app = Path.Combine(oneDrive, "Desktop", "Titano");
var local = context.CreateTempDirectory("local");
var location = DataLocation.Choose(app, portable: true, local, [Path.Combine(context.CreateTempDirectory("other"), "OneDrive"), oneDrive]);
Assert.Equal(Path.Combine(local, "Titano"), location.Root);
Assert.True(location.IsPortable, "still a portable copy (updates, no sign-in entry)");
Assert.Equal<string?>(oneDrive, location.PortableSyncRoot);
Assert.Null(location.SyncedFolder);
// A sync root that only shares the start of the name is another folder.
var beside = DataLocation.Choose(oneDrive + "Backup", portable: true, local, [oneDrive]);
Assert.Equal(new DataLocation(Path.Combine(oneDrive + "Backup", "data"), true, null, null), beside);
}
[Test]
public void APerUserFolderInsideASyncRootIsReported()
{
var oneDrive = context.CreateTempDirectory("onedrive");
var local = Path.Combine(oneDrive, "AppData", "Local");
var installed = DataLocation.Choose(context.CreateTempDirectory("app"), portable: false, local, [oneDrive]);
Assert.Equal<string?>(oneDrive, installed.SyncedFolder);
var portable = DataLocation.Choose(Path.Combine(oneDrive, "Titano"), portable: true, local, [oneDrive]);
Assert.Equal<(string, string?, string?)>((Path.Combine(local, "Titano"), oneDrive, oneDrive), (portable.Root, portable.SyncedFolder, portable.PortableSyncRoot));
}
[Test]
public void SyncRootMatchingIsSegmentWiseAndIgnoresCase()
{
var root = Path.Combine(context.CreateTempDirectory(), "OneDrive");
Assert.True(DataLocation.IsUnder(root, root));
Assert.True(DataLocation.IsUnder(Path.Combine(root, "Pictures"), root + Path.DirectorySeparatorChar));
Assert.True(DataLocation.IsUnder(Path.Combine(root, "x").ToUpperInvariant(), root));
Assert.True(DataLocation.IsUnder(@"C:\Users\ada\OneDrive\Desktop\Titano\data", @"c:\users\ada\onedrive"), "Windows paths on any platform");
Assert.False(DataLocation.IsUnder(root + "Backup", root));
Assert.False(DataLocation.IsUnder(root, string.Empty));
Assert.Null(DataLocation.SyncRootOf(root + "Backup", [root]));
Assert.Equal<string?>(root, DataLocation.SyncRootOf(Path.Combine(root, "Documents"), ["", root]));
}
}
@@ -17,6 +17,7 @@ public sealed class AppPathsTests(TestContext context)
Assert.Equal(Path.Combine(app, "data"), paths.DataRoot);
Assert.Equal(Path.Combine(app, "data", "titano.db"), paths.DatabasePath);
Assert.False(paths.IsInSyncedFolder);
Assert.Null(paths.PortableSyncRoot);
paths.CreateDirectories();
Assert.True(Directory.Exists(paths.LogsDirectory));
@@ -37,13 +38,31 @@ public sealed class AppPathsTests(TestContext context)
}
[Test]
public void DataInsideOneDriveIsReportedAndRefused()
public void APortableCopyInOneDriveKeepsItsDataInLocalAppData()
{
var oneDrive = context.CreateTempDirectory("onedrive");
var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Tools", "Titano")).FullName;
File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), "");
var local = context.CreateTempDirectory("local");
var paths = AppPaths.Resolve(app, context.CreateTempDirectory("local"), [oneDrive]);
var paths = AppPaths.Resolve(app, local, [oneDrive]);
Assert.True(paths.IsPortable);
Assert.Equal(Path.Combine(local, "Titano"), paths.DataRoot);
Assert.Equal<string?>(oneDrive, paths.PortableSyncRoot);
Assert.False(paths.IsInSyncedFolder);
paths.CreateDirectories();
Assert.True(Directory.Exists(paths.LogsDirectory));
Assert.False(Directory.Exists(Path.Combine(app, "data")), "nothing is written into the synchronised folder");
}
[Test]
public void DataInsideOneDriveIsReportedAndRefused()
{
// Local application data inside a sync root: nowhere is safe, so nothing is created.
var oneDrive = context.CreateTempDirectory("onedrive");
var local = Directory.CreateDirectory(Path.Combine(oneDrive, "AppData", "Local")).FullName;
var paths = AppPaths.Resolve(context.CreateTempDirectory("app"), local, [oneDrive]);
Assert.True(paths.IsInSyncedFolder);
Assert.Equal<string?>(oneDrive, paths.SyncedFolder);
Assert.Throws<InvalidOperationException>(paths.CreateDirectories);
@@ -54,10 +73,7 @@ public sealed class AppPathsTests(TestContext context)
public void SyncRootMatchingIsSegmentWise()
{
var root = Path.Combine(context.CreateTempDirectory(), "OneDrive");
Assert.True(OneDriveFolders.IsUnder(root, root));
Assert.True(OneDriveFolders.IsUnder(Path.Combine(root, "Pictures"), root + Path.DirectorySeparatorChar));
Assert.True(OneDriveFolders.IsUnder(Path.Combine(root, "x").ToUpperInvariant(), root));
Assert.False(OneDriveFolders.IsUnder(root + "Backup", root));
Assert.Equal<string?>(root, OneDriveFolders.Containing(Path.Combine(root, "Pictures"), [root]));
Assert.Null(OneDriveFolders.Containing(root + "Backup", [root]));
}
@@ -68,8 +84,10 @@ public sealed class AppPathsTests(TestContext context)
var app = context.CreateTempDirectory("app");
File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), "");
var paths = AppPaths.Resolve(app, KnownFolders.LocalAppData);
var roots = OneDriveFolders.SyncRoots();
Assert.True(paths.IsPortable);
Assert.Equal(OneDriveFolders.Containing(paths.DataRoot, OneDriveFolders.SyncRoots()), paths.SyncedFolder);
Assert.True(OneDriveFolders.SyncRoots().All(Path.IsPathFullyQualified));
Assert.Equal(OneDriveFolders.Containing(Path.Combine(app, PortableMode.DataFolderName), roots), paths.PortableSyncRoot);
Assert.Equal(OneDriveFolders.Containing(paths.DataRoot, roots), paths.SyncedFolder);
Assert.True(roots.All(Path.IsPathFullyQualified));
}
}
@@ -0,0 +1,39 @@
using Titano.Core.Windows.Shell;
using Titano.Testing;
namespace Titano.Core.Windows.Tests.Shell;
/// <summary>The OneDrive folders of the user as Windows reports them. Serial: a test sets a process environment variable.</summary>
[Serial]
public sealed class OneDriveFoldersTests(TestContext context)
{
private const string Variable = "OneDriveCommercial";
[Test]
[WindowsOnly]
public void APortableCopyInTheUsersOneDriveKeepsItsDataInLocalAppData()
{
var oneDrive = context.CreateTempDirectory("onedrive");
var previous = Environment.GetEnvironmentVariable(Variable);
Environment.SetEnvironmentVariable(Variable, oneDrive);
try
{
Assert.Contains(OneDriveFolders.SyncRoots(), root => string.Equals(root, Path.TrimEndingDirectorySeparator(oneDrive), StringComparison.OrdinalIgnoreCase));
// A Desktop that OneDrive's folder backup moved into the sync root, with a portable copy unpacked on it.
var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Desktop", "Titano")).FullName;
File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), "");
var local = context.CreateTempDirectory("local");
var paths = AppPaths.Resolve(app, local);
Assert.True(paths.IsPortable);
Assert.Equal(Path.Combine(local, AppPaths.ProductFolderName), paths.DataRoot);
Assert.NotNull(paths.PortableSyncRoot);
Assert.False(paths.IsInSyncedFolder);
}
finally
{
Environment.SetEnvironmentVariable(Variable, previous);
}
}
}