Files
james.maesandClaude Fable 5.2 1bf7c8132d
node / node (push) Successful in 13s
build / scripts (push) Successful in 13s
windows / windows (push) Successful in 1m42s
build / windows (push) Successful in 1m42s
fix(settings): keep stored preferences when the file is shared or unreadable (#47)
Settings changes are applied to the latest stored settings under a cross-process lock and renamed into place in one step, so two instances no longer erase each other's changes and a file that cannot be read is never replaced by defaults. Invalid content is repaired per property with a copy of the original kept aside. ADR-0006 records the file protocol.

Closes #46

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: James Maes <james@kof22.com>
2026-11-11 01:12:56 +00:00

101 lines
4.4 KiB
C#

/*
* Copyright (c) 2026 James Maes
* SPDX-License-Identifier: MIT
*/
using KofTwentyTwo.AppKit.Logging;
namespace MarkdownViewr.Windows.Core.Tests;
/// <summary>Tests of the app's identity and the services derived from it.</summary>
public class AppIdentityTests
{
/// <summary>Info: the id and display name are the app's.</summary>
[Fact]
public void Info_IdentifiesTheApp()
{
Assert.Equal("markdownviewr-windows", AppIdentity.Info.Id);
Assert.Equal("MarkdownViewr", AppIdentity.Info.DisplayName);
Assert.EndsWith("_DATA_DIR", AppIdentity.Info.DataDirectoryVariable, StringComparison.Ordinal);
Assert.NotNull(AppIdentity.Info.RepositoryUrl);
Assert.NotEmpty(AppIdentity.Info.Attributions);
Assert.DoesNotContain("Darin", AppIdentity.Info.Description, StringComparison.Ordinal);
Assert.Equal("https://github.com/darinkelkhoff/markdownViewr", AppIdentity.OriginalRepositoryUrl.AbsoluteUri);
Assert.Equal("https://kelkhoff.com/", AppIdentity.OriginalAuthorWebsite.AbsoluteUri);
Assert.DoesNotContain(AppIdentity.Info.Attributions, credit => credit.Name.Contains("Darin Kelkhoff", StringComparison.Ordinal));
Assert.Equal(AppIdentity.Info.RepositoryUrl!.AbsoluteUri.TrimEnd('/') + "/issues/new/choose", AppIdentity.IssuesUrl.AbsoluteUri);
Assert.Equal(AppIdentity.Info.RepositoryUrl.AbsoluteUri.TrimEnd('/') + "/discussions", AppIdentity.SupportUrl.AbsoluteUri);
}
/// <summary>Development channel metadata cannot reuse stable titles, settings variables or service identities.</summary>
[Theory]
[InlineData(null, "markdownviewr-windows", "MarkdownViewr")]
[InlineData("stable", "markdownviewr-windows", "MarkdownViewr")]
[InlineData("dev", "markdownviewr-windows-dev", "MarkdownViewr (dev)")]
[InlineData("unknown", "markdownviewr-windows", "MarkdownViewr")]
public void ForChannel_InstalledEdition_SelectsIsolatedIdentity(string? channel, string id, string displayName)
{
KofTwentyTwo.AppKit.AppInfo info = AppIdentity.ForChannel(channel);
Assert.Equal(id, info.Id);
Assert.Equal(displayName, info.DisplayName);
Assert.Equal(id.ToUpperInvariant().Replace('-', '_') + "_DATA_DIR", info.DataDirectoryVariable);
Assert.Equal(AppIdentity.Info.RepositoryUrl, info.RepositoryUrl);
Assert.NotEqual(AppIdentity.ForChannel("stable").DataDirectoryVariable, AppIdentity.ForChannel("dev").DataDirectoryVariable, StringComparer.Ordinal);
Assert.Equal("markdownviewr-windows", AppIdentity.Info.Id);
}
/// <summary>Paths and SettingsStore: settings live in the app's data folder.</summary>
[Fact]
public void SettingsStore_UsesTheAppsSettingsFile()
{
Assert.Equal(AppIdentity.Paths.SettingsFile, AppIdentity.SettingsStore.Path);
Assert.EndsWith("settings.json", AppIdentity.SettingsStore.Path, StringComparison.Ordinal);
}
/// <summary>CreateLog: writes daily files in the app's logs folder, named after the id.</summary>
[Fact]
public void CreateLog_WritesToTheAppsLogsFolder()
{
IActivityLog log = AppIdentity.CreateLog();
Assert.Equal(AppIdentity.Paths.LogsDirectory, log.LogDirectory);
Assert.StartsWith("markdownviewr-windows-", Path.GetFileName(log.CurrentLogFilePath), StringComparison.Ordinal);
}
}
/// <summary>Tests of the app's settings.</summary>
public class AppSettingsTests
{
/// <summary>Sanitize: invalid shell preferences fall back to their supported defaults.</summary>
[Fact]
public void Sanitize_RepairsShellSettings()
{
var settings = new AppSettings { Theme = "Purple" };
settings.Sanitize();
Assert.Equal("System", settings.Theme);
}
/// <summary>Round trip: the shell theme survives a save and a load.</summary>
[Fact]
public void SaveAndLoad_KeepsTheTheme()
{
using var temp = new TempDirectory();
var store = new AppSettingsStore(temp.File("settings.json"));
Assert.NotNull(store.Update(_ => new AppSettings { Theme = "Dark", DocumentTheme = "GitHub Light", Frontmatter = "Metadata", UserThemes = [Themes.ThemeCatalog.Find("GitHub Light").Json] }));
Assert.Equal("Dark", store.Load().Theme);
Assert.Equal("GitHub Light", store.Load().DocumentTheme);
Assert.Equal(Documents.FrontmatterMode.Metadata, store.Load().FrontmatterKind);
Assert.Equal(Themes.ThemeCatalog.Find("GitHub Light").Json, Assert.Single(store.Load().UserThemes));
}
}