using System.Collections.Generic;
using System.Linq;
using AcDream.Core.Journal;
namespace AcDream.Core.Tests.Journal;
///
/// Campaign QJ slice QJ1: retail's journal file
/// (gmJournalUI::LoadPages @0x00496AC0 /
/// SavePages @0x00497270).
///
public sealed class JournalFileTests
{
[Fact]
public void APageRoundTripsThroughTheFile()
{
var page = new JournalPage(
Label: "Aerlinthe",
Title: "Recall ring",
Notes: "Talk to the archmage first.",
TimerDays: 1,
TimerHours: 2,
TimerMinutes: 3,
LocationX: 12.5f,
LocationY: -8.25f,
HasLocation: true,
RunningTimerSeconds: 90d);
JournalReadResult result = JournalFile.Read(JournalFile.Write([page]));
Assert.Null(result.Error);
Assert.Equal(page, Assert.Single(result.Pages));
}
[Fact]
public void SeveralPagesKeepTheirFileOrder()
{
// is written but page order IS file order — a reader that
// trusted the number would reshuffle a hand-edited file.
List pages =
[
new(Label: "one"), new(Label: "two"), new(Label: "three"),
];
JournalReadResult result = JournalFile.Read(JournalFile.Write(pages));
Assert.Equal(
new[] { "one", "two", "three" },
result.Pages.Select(p => p.Label).ToArray());
}
[Fact]
public void AFileThatDoesNotOpenWithAPageMarkerIsRefused()
{
// Retail's own strictness, and its own message. Accepting this would
// scatter the first page's text into no page at all.
JournalReadResult result = JournalFile.Read(" orphaned\n\n");
Assert.Equal(JournalFile.MalformedFileMessage, result.Error);
Assert.Empty(result.Pages);
}
[Fact]
public void AnEmptyFileIsACharacterWhoHasWrittenNothing()
{
// Absence is not corruption: refusing here would show an error to
// every character opening the journal for the first time.
JournalReadResult result = JournalFile.Read(string.Empty);
Assert.Null(result.Error);
Assert.Empty(result.Pages);
}
[Fact]
public void AnUnknownTagIsSkippedRatherThanRefused()
{
// A newer client's tag must not make this one reject the file.
JournalReadResult result = JournalFile.Read(
"\n kept\n whatever\n");
Assert.Null(result.Error);
Assert.Equal("kept", Assert.Single(result.Pages).Title);
}
[Fact]
public void TheWrittenFormIsRetailsTagOrder()
{
string text = JournalFile.Write([new JournalPage(Label: "L", Title: "T")]);
string[] tags = text
.Split('\n', System.StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Length >= 6 ? line[..6] : line)
.ToArray();
Assert.Equal(
new[]
{
"", "", "", "", "",
"", "", "", "