using Explorer.Application; using Explorer.Storage.Sqlite; using Microsoft.Extensions.Logging.Abstractions; namespace Explorer.Storage.Tests; public class SqliteDatabaseSessionTests { [Fact] public async Task Can_browse_and_edit_a_standalone_database() { var dir = Path.Combine(Path.GetTempPath(), "ew-sql", Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); var path = Path.Combine(dir, "sample.db"); var indexPath = Path.Combine(dir, "index.db"); await using var writer = new SqliteIndexStore(indexPath, NullLogger.Instance); await writer.OpenAsync(); await writer.CloseAsync(); await using var session = new SqliteDatabaseSession(indexPath); await session.OpenAsync(path, preferWrite: true); Assert.True(session.CanWrite); await session.ExecuteAsync("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT);"); await session.InsertRowAsync("notes", new Dictionary { ["body"] = "hello" }); var page = await session.ReadTableAsync("notes", 0, 50); Assert.Contains("body", page.Columns); Assert.Single(page.Rows); var rowId = Convert.ToInt64(page.Rows[0][0]); await session.UpdateCellAsync("notes", rowId, "body", "world"); var query = await session.ExecuteAsync("SELECT body FROM notes"); Assert.Equal("world", query.Rows[0][0]?.ToString()); await session.DeleteRowAsync("notes", rowId); Assert.Equal(0, (await session.ReadTableAsync("notes", 0, 10)).TotalRows); } [Fact] public async Task Index_opens_read_only_while_writer_holds_lock() { var dir = Path.Combine(Path.GetTempPath(), "ew-sql", Guid.NewGuid().ToString("N")); Directory.CreateDirectory(dir); var path = Path.Combine(dir, "index.db"); await using var writer = new SqliteIndexStore(path, NullLogger.Instance); await writer.OpenAsync(); await using var session = new SqliteDatabaseSession(path); await session.OpenAsync(path, preferWrite: true); Assert.False(session.CanWrite); Assert.Contains("Read-only", session.ModeLabel, StringComparison.OrdinalIgnoreCase); var tables = await session.ListTablesAsync(); Assert.Contains("sources", tables); } }