using Titano.App.Settings;
using Titano.Testing;
namespace Titano.App.Tests.Settings;
///
/// Settings › Archive never shows a read of the archive's settings that overlapped an edit being written: on a slow share
/// that read can hold archive.json as it was before the edit, and would undo the edit on the rows and in this PC's
/// defaults while the archive keeps it.
///
public sealed class ArchiveReadGateTests
{
[Test]
public void AReadWithNoEditMeanwhileIsShown()
{
var gate = new ArchiveReadGate();
Assert.True(gate.IsCurrent(gate.BeginRead(), out var again));
Assert.False(again);
// An edit written and ended before the read began is in the file it reads.
gate.BeginWrite();
Assert.False(gate.EndWrite(), "no read overlapped it");
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
Assert.False(again);
}
[Test]
public void AReadThatAnEditOverlapsIsReadAgainWhenTheEditIsWritten()
{
// Settings is shown and reads archive.json; the user commits a template before the read answers.
var gate = new ArchiveReadGate();
var read = gate.BeginRead();
gate.BeginWrite();
Assert.False(gate.IsCurrent(read, out var again), "the read may hold the file before the edit");
Assert.False(again, "the edit is still being written: its end reads again");
Assert.True(gate.EndWrite(), "the edit's end reads the rows again");
// That read is shown, and asks nothing more of later edits.
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
gate.BeginWrite();
Assert.False(gate.EndWrite());
}
[Test]
public void AReadOverlappingAnEditAlreadyWrittenIsReadAgainAtOnce()
{
// The edit began and ended while the read was out.
var gate = new ArchiveReadGate();
var read = gate.BeginRead();
gate.BeginWrite();
Assert.False(gate.EndWrite(), "the read was not dropped yet");
Assert.False(gate.IsCurrent(read, out var again));
Assert.True(again, "nothing else reads again: the read does");
// A read that began while an edit was being written, which ended before the read answered.
gate.BeginWrite();
read = gate.BeginRead();
Assert.False(gate.EndWrite());
Assert.False(gate.IsCurrent(read, out again));
Assert.True(again);
Assert.True(gate.IsCurrent(gate.BeginRead(), out again));
}
[Test]
public void OnlyTheLastOfSeveralEditsReadsAgain()
{
// A proxy size and a thumbnail size picked one after the other, both overlapping a read.
var gate = new ArchiveReadGate();
var read = gate.BeginRead();
gate.BeginWrite();
gate.BeginWrite();
Assert.False(gate.IsCurrent(read, out var again));
Assert.False(again);
Assert.False(gate.EndWrite(), "another edit is still being written");
// A read begun meanwhile is dropped too, and left to the last edit.
read = gate.BeginRead();
Assert.False(gate.IsCurrent(read, out again));
Assert.False(again);
Assert.True(gate.EndWrite());
Assert.Throws(() => gate.EndWrite());
}
}