fix(storage): initialize a workspace under the lock and refuse a missing database

This commit is contained in:
yyc12345 committed 2026-10-04 16:11:51 +08:00
1 parent 78e5faa3c4
commit 647232c447
6 files changed
+141 -6

No files matched your search

@@ -41,6 +41,42 @@ public class WorkspaceSessionTests {
() => WorkspaceSession.Open(directory.FullPath));
}
[TestMethod]
public void OpenWithoutDatabaseThrowsDatabaseMissing() {
using var directory = new TempDirectory();
WorkspaceInitializer.Initialize(directory.FullPath);
File.Delete(WorkspacePaths.Database(directory.FullPath));
Assert.ThrowsExactly<WorkspaceDatabaseMissingException>(
() => WorkspaceSession.Open(directory.FullPath));
}
[TestMethod]
public void InitializeHoldsLockAndSeedsPreset() {
using var directory = new TempDirectory();
using WorkspaceSession session = WorkspaceSession.Initialize(directory.FullPath);
// The lock is held for the whole session, so a second initialize cannot interleave.
Assert.ThrowsExactly<WorkspaceLockException>(() => WorkspaceSession.Initialize(directory.FullPath));
// The preset columns and tags were written under the same lock.
Assert.AreEqual(3, session.Repository.GetColumns().Count);
Assert.AreEqual(8, session.Repository.GetTags().Count);
}
[TestMethod]
public void InitializeExistingWorkspaceThrowsAndReleasesLock() {
using var directory = new TempDirectory();
WorkspaceInitializer.Initialize(directory.FullPath);
Assert.ThrowsExactly<InvalidOperationException>(() => WorkspaceSession.Initialize(directory.FullPath));
// The failed initialize released the lock, so the workspace can still be opened.
using WorkspaceSession session = WorkspaceSession.Open(directory.FullPath);
Assert.AreEqual(directory.FullPath, session.FolderPath);
}
[TestMethod]
public void RepositoryIsUsableThroughSession() {
using var directory = new TempDirectory();