feat(m1): storage layer

- Shared SQLite plumbing: PRAGMAs (WAL/NORMAL/foreign_keys) and
  user_version migrations with typed SchemaVersionException
- Workspace folder: .ykanban initialization, empty schema with
  DDL-level constraints, preset column seeding (ResX-driven),
  exclusive lock file with holder diagnostics
- AppData folder: app.db workspace registry (injectable clock),
  app.json config store with kebab-case JSON names via explicit
  [JsonProperty]/[EnumMember], nested sections, corrupt-file backup
- ResX infrastructure under Assets/Locales (en + zh-Hans) with
  MSBuild strongly-typed public Resources class
- 54 unit tests: DDL constraints, migrations, PRAGMAs, lock
  exclusivity, config degradation, culture-aware presets
This commit is contained in:
yyc12345 committed 2026-10-01 18:17:47 +08:00
1 parent e934eb9629
commit 31fc58d9c7
25 files changed
+1737 -1

No files matched your search

@@ -0,0 +1,169 @@
using YKanBan.Storage.AppData;
using YKanBan.Tests.TestUtilities;
namespace YKanBan.Tests.Storage.AppData;
/// <summary>Tests for app.json load/save and its degradation rules.</summary>
[TestClass]
public class AppConfigStoreTests
{
[TestMethod]
public void MissingFileYieldsDefaults()
{
using var directory = new TempDirectory();
var store = new AppConfigStore(Path.Combine(directory.FullPath, "app.json"));
AppConfig config = store.Load();
Assert.AreEqual(AppConfig.CurrentFormatVersion, config.Version);
Assert.AreEqual("en", config.Language);
Assert.AreEqual(ThemeOption.FollowSystem, config.Theme);
Assert.AreEqual(CardSortOption.Id, config.Sort.Card);
Assert.AreEqual(SidebarSortOption.NameAsc, config.Sort.Sidebar);
Assert.AreEqual(ColumnWidthPreset.Standard, config.ColumnWidth.Preset);
Assert.AreEqual(320, config.ColumnWidth.CustomPixels);
Assert.IsTrue(config.Confirmations.DeleteCard);
Assert.IsTrue(config.Confirmations.DeleteColumn);
Assert.IsTrue(config.Confirmations.DeleteTag);
Assert.IsTrue(config.Confirmations.DiscardEdit);
}
[TestMethod]
public void CorruptJsonIsBackedUpAndDefaultsAreUsed()
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.FullPath, "app.json");
File.WriteAllText(path, "{{{ not json");
var store = new AppConfigStore(path);
AppConfig config = store.Load();
Assert.AreEqual("en", config.Language);
Assert.IsTrue(File.Exists(path + ".bak"));
Assert.AreEqual("{{{ not json", File.ReadAllText(path + ".bak"));
}
[TestMethod]
public void WrongShapeJsonIsBackedUpAndDefaultsAreUsed()
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.FullPath, "app.json");
File.WriteAllText(path, "[1, 2, 3]");
var store = new AppConfigStore(path);
AppConfig config = store.Load();
Assert.AreEqual(AppConfig.CurrentFormatVersion, config.Version);
Assert.IsTrue(File.Exists(path + ".bak"));
}
[TestMethod]
public void SaveAndLoadRoundTripsAllSettings()
{
using var directory = new TempDirectory();
var store = new AppConfigStore(Path.Combine(directory.FullPath, "app.json"));
store.Save(new AppConfig
{
Language = "zh-Hans",
Theme = ThemeOption.Dark,
Sort = new SortSettings
{
Card = CardSortOption.Title,
Sidebar = SidebarSortOption.RecentlyUpdated,
},
ColumnWidth = new ColumnWidthSettings
{
Preset = ColumnWidthPreset.Custom,
CustomPixels = 500,
},
Confirmations = new ConfirmationSettings
{
DeleteCard = false,
DeleteColumn = false,
DeleteTag = false,
DiscardEdit = false,
},
});
AppConfig loaded = store.Load();
Assert.AreEqual("zh-Hans", loaded.Language);
Assert.AreEqual(ThemeOption.Dark, loaded.Theme);
Assert.AreEqual(CardSortOption.Title, loaded.Sort.Card);
Assert.AreEqual(SidebarSortOption.RecentlyUpdated, loaded.Sort.Sidebar);
Assert.AreEqual(ColumnWidthPreset.Custom, loaded.ColumnWidth.Preset);
Assert.AreEqual(500, loaded.ColumnWidth.CustomPixels);
Assert.IsFalse(loaded.Confirmations.DeleteCard);
Assert.IsFalse(loaded.Confirmations.DeleteColumn);
Assert.IsFalse(loaded.Confirmations.DeleteTag);
Assert.IsFalse(loaded.Confirmations.DiscardEdit);
}
[TestMethod]
public void SaveWritesKebabCaseNamesAndVersionField()
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.FullPath, "app.json");
var store = new AppConfigStore(path);
store.Save(new AppConfig());
string json = File.ReadAllText(path);
// Keys and enum values are serialized in kebab-case; "version" is the migration hook.
Assert.IsTrue(json.Contains($"\"version\": {AppConfig.CurrentFormatVersion}"));
Assert.IsTrue(json.Contains("\"language\": \"en\""));
Assert.IsTrue(json.Contains("\"theme\": \"follow-system\""));
Assert.IsTrue(json.Contains("\"sort\":"));
Assert.IsTrue(json.Contains("\"card\": \"id\""));
Assert.IsTrue(json.Contains("\"sidebar\": \"name-asc\""));
Assert.IsTrue(json.Contains("\"column-width\":"));
Assert.IsTrue(json.Contains("\"preset\": \"standard\""));
Assert.IsTrue(json.Contains("\"custom-pixels\": 320"));
Assert.IsTrue(json.Contains("\"confirmations\":"));
Assert.IsTrue(json.Contains("\"delete-card\": true"));
Assert.IsTrue(json.Contains("\"delete-column\": true"));
Assert.IsTrue(json.Contains("\"delete-tag\": true"));
Assert.IsTrue(json.Contains("\"discard-edit\": true"));
}
[TestMethod]
public void MissingSectionsFallBackToTheirDefaults()
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.FullPath, "app.json");
File.WriteAllText(path, $$"""
{
"version": {{AppConfig.CurrentFormatVersion}},
"language": "zh-Hans"
}
""");
var store = new AppConfigStore(path);
AppConfig config = store.Load();
// The present field loads; absent sections each fall back to defaults.
Assert.AreEqual("zh-Hans", config.Language);
Assert.AreEqual(CardSortOption.Id, config.Sort.Card);
Assert.AreEqual(ColumnWidthPreset.Standard, config.ColumnWidth.Preset);
Assert.IsTrue(config.Confirmations.DeleteCard);
}
[TestMethod]
public void LoadPreservesUnknownFieldsTolerantly()
{
using var directory = new TempDirectory();
string path = Path.Combine(directory.FullPath, "app.json");
// A newer version writes an unknown field plus a known one.
File.WriteAllText(path, $$"""
{
"version": {{AppConfig.CurrentFormatVersion}},
"language": "zh-Hans",
"some-future-field": 42
}
""");
var store = new AppConfigStore(path);
AppConfig config = store.Load();
Assert.AreEqual("zh-Hans", config.Language);
}
}
@@ -0,0 +1,136 @@
using Microsoft.Data.Sqlite;
using YKanBan.Storage;
using YKanBan.Storage.AppData;
using YKanBan.Tests.TestUtilities;
namespace YKanBan.Tests.Storage.AppData;
/// <summary>Tests for the global app.db workspace registry.</summary>
[TestClass]
public class AppDatabaseTests
{
private static string DatabasePath(TempDirectory directory)
=> Path.Combine(directory.FullPath, "app.db");
[TestMethod]
public void UpsertInsertsRowWithTimestamps()
{
using var directory = new TempDirectory();
long now = 1000;
using var db = new AppDatabase(DatabasePath(directory), () => now);
db.UpsertWorkspace(@"C:\work\repo");
IReadOnlyList<WorkspaceRecord> records = db.GetWorkspaces();
Assert.AreEqual(1, records.Count);
Assert.AreEqual(@"C:\work\repo", records[0].Path);
Assert.AreEqual(1000L, records[0].AddedAt);
Assert.AreEqual(1000L, records[0].LastEditedAt);
}
[TestMethod]
public void RepeatedUpsertKeepsOriginalAddedAt()
{
using var directory = new TempDirectory();
long now = 1000;
using var db = new AppDatabase(DatabasePath(directory), () => now);
db.UpsertWorkspace(@"C:\work\repo");
now = 2000;
db.UpsertWorkspace(@"C:\work\repo");
WorkspaceRecord record = db.GetWorkspaces().Single();
Assert.AreEqual(1000L, record.AddedAt);
Assert.AreEqual(2000L, record.LastEditedAt);
}
[TestMethod]
public void TouchRefreshesLastEditedAt()
{
using var directory = new TempDirectory();
long now = 1000;
using var db = new AppDatabase(DatabasePath(directory), () => now);
db.UpsertWorkspace(@"C:\work\repo");
now = 2500;
db.TouchLastEdited(@"C:\work\repo");
WorkspaceRecord record = db.GetWorkspaces().Single();
Assert.AreEqual(1000L, record.AddedAt);
Assert.AreEqual(2500L, record.LastEditedAt);
}
[TestMethod]
public void TouchOnUnknownPathRegistersTheWorkspace()
{
using var directory = new TempDirectory();
long now = 3000;
using var db = new AppDatabase(DatabasePath(directory), () => now);
// Self-healing: touching an unregistered workspace still leaves a valid row.
db.TouchLastEdited(@"C:\work\other");
WorkspaceRecord record = db.GetWorkspaces().Single();
Assert.AreEqual(3000L, record.AddedAt);
Assert.AreEqual(3000L, record.LastEditedAt);
}
[TestMethod]
public void RemoveDeletesTheRow()
{
using var directory = new TempDirectory();
using var db = new AppDatabase(DatabasePath(directory));
db.UpsertWorkspace(@"C:\work\repo");
db.RemoveWorkspace(@"C:\work\repo");
Assert.AreEqual(0, db.GetWorkspaces().Count);
}
[TestMethod]
public void RowsPersistAcrossReopen()
{
using var directory = new TempDirectory();
string path = DatabasePath(directory);
using (var db = new AppDatabase(path, () => 1000))
{
db.UpsertWorkspace(@"C:\work\repo");
}
using var reopened = new AppDatabase(path, () => 9999);
WorkspaceRecord record = reopened.GetWorkspaces().Single();
Assert.AreEqual(@"C:\work\repo", record.Path);
Assert.AreEqual(1000L, record.AddedAt);
}
[TestMethod]
public void OpenMigratesToCurrentSchemaVersion()
{
using var directory = new TempDirectory();
string path = DatabasePath(directory);
using (var db = new AppDatabase(path))
{
}
using var raw = new SqliteConnection($"Data Source={path}");
raw.Open();
Assert.AreEqual(AppDatabase.CurrentSchemaVersion, SqliteDatabase.ReadUserVersion(raw));
}
[TestMethod]
public void OpenAppliesWalJournalMode()
{
using var directory = new TempDirectory();
string path = DatabasePath(directory);
using (var db = new AppDatabase(path))
{
}
// WAL is a persistent property of the database file: a plain new
// connection reports it without any PRAGMA replay.
using var raw = new SqliteConnection($"Data Source={path}");
raw.Open();
Assert.AreEqual("wal", SqliteTestHelper.ScalarString(raw, "PRAGMA journal_mode;"));
}
}