acdream/tests/AcDream.App.Tests/UI/JournalPersistenceTests.cs
Erik c5cc8ae5fc feat(journal): QJ3/QJ4/QJ5 — both remaining tabs are live
The Journal panel now has all three tabs working: contracts from the server,
and a per-character notebook with its searchable index.

Two ported details that a reimplementation would get wrong in a way nobody
notices until they lose work:

Every navigation button commits the current page FIRST. Retail's
ListenToElementMessage @0x004968D0 calls SaveThisPage on the way out of all
five of them, which is why paging away never eats what you just typed. And the
file is written when the notes page is HIDDEN, not only at exit — a crash then
costs at most the page in front of you.

The search is CASE-SENSITIVE across label, title and notes: retail compares
with wcsstr and lowercases neither side. Making it insensitive would be
friendlier and would be a divergence, so it is ported as-is with a test naming
the reason. The double-click window is a full SECOND (m_LastClickTime + 1.0,
@0x00493158) rather than the 500 ms the item-interaction path uses, and firing
it clears the tracker so a third click does not re-open.

Two unlabelled buttons on the notes page turned out to be prev/next: retail
switches on (idElement - 0x10000565), which names them without a caption. The
running-timer readout is authored at the same x as the three day/hour/minute
boxes, so the strip is one or the other — that overlap is the data form of
ShowEditableTimer versus ShowRunningTimer, not a layout bug.

DeltaTimeToString moved out of the contract code into AcDream.Core.Ui. It is
ClientUISystem's, not gmContractsUI's — the journal timer and the contract
repeat countdown both call it, and it only lived under Quests because that was
its first caller. A bridge class to reach it across features would have been
the wrong answer to the same observation.

The journal file lives in the client's data directory rather than beside the
executable, for the same reason the chat log does. Register QJ-1.

Campaign QJ slices 3, 4 and 5 of 5 — code-complete, connected gate owed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 15:42:05 +02:00

180 lines
5.6 KiB
C#

using System;
using System.IO;
using System.Linq;
using AcDream.App.UI;
using AcDream.Core.Journal;
using AcDream.Runtime.Gameplay;
namespace AcDream.App.Tests.UI;
/// <summary>
/// Campaign QJ slice QJ5: the per-character journal file.
/// </summary>
public sealed class JournalPersistenceTests : IDisposable
{
private static readonly DateTime Now = new(2026, 8, 21, 12, 0, 0, DateTimeKind.Utc);
private readonly string _directory =
Path.Combine(Path.GetTempPath(), "acdream-journal-" + Guid.NewGuid().ToString("N"));
public void Dispose()
{
try { Directory.Delete(_directory, recursive: true); }
catch (IOException) { /* the test already said what it needed to */ }
}
private (RuntimeJournalState Journal, JournalPersistence File, List<string> Reports) New()
{
var journal = new RuntimeJournalState();
var reports = new List<string>();
return (journal, new JournalPersistence(journal, _directory, reports.Add), reports);
}
[Fact]
public void APageWrittenInOneSessionIsThereInTheNext()
{
// The whole point of the feature.
(RuntimeJournalState first, JournalPersistence firstFile, _) = New();
firstFile.Load("Acdream");
first.NewPage();
first.UpdateCurrent("label", "A title", "Some notes.");
Assert.True(firstFile.Save(Now));
first.Dispose();
(RuntimeJournalState second, JournalPersistence secondFile, _) = New();
secondFile.Load("Acdream");
JournalPage page = Assert.Single(second.View.Pages);
Assert.Equal("A title", page.Title);
Assert.Equal("Some notes.", page.Notes);
second.Dispose();
}
[Fact]
public void EachCharacterGetsItsOwnFile()
{
// Sharing one file would show a character another's notes.
(RuntimeJournalState journal, JournalPersistence file, _) = New();
file.Load("Acdream");
journal.NewPage();
journal.UpdateCurrent("a", "Acdream's page", string.Empty);
file.Save(Now);
file.Load("Someone Else");
Assert.Empty(journal.View.Pages);
journal.Dispose();
}
[Fact]
public void ACharacterWithNoFileLoadsAnEmptyJournalWithoutComplaining()
{
// First-time use is the ordinary case, not an error.
(RuntimeJournalState journal, JournalPersistence file, List<string> reports) = New();
file.Load("Newcomer");
Assert.Empty(journal.View.Pages);
Assert.Empty(reports);
journal.Dispose();
}
[Fact]
public void AMalformedFileReportsAndLeavesTheJournalEmpty()
{
// Half-reading a notebook loses pages, and the next save would then
// write that loss back over the original.
(RuntimeJournalState journal, JournalPersistence file, List<string> reports) = New();
Directory.CreateDirectory(_directory);
File.WriteAllText(
Path.Combine(_directory, JournalFile.FileNameFor("acdream", "Acdream")),
"<TITL> no page marker first\n");
file.Load("Acdream");
Assert.Equal(JournalFile.MalformedFileMessage, Assert.Single(reports));
Assert.Empty(journal.View.Pages);
journal.Dispose();
}
[Fact]
public void SavingIsSkippedWhenNothingChanged()
{
// Retail's save fires on every hide; rewriting an unchanged file on
// every tab switch is pure disk churn.
(RuntimeJournalState journal, JournalPersistence file, _) = New();
file.Load("Acdream");
journal.NewPage();
Assert.True(file.Save(Now));
Assert.False(file.Save(Now));
journal.Dispose();
}
[Fact]
public void SavingBeforeAnyCharacterIsLoadedIsANoOp()
{
// The panel can be disposed before a character ever entered the world.
(RuntimeJournalState journal, JournalPersistence file, _) = New();
Assert.False(file.Save(Now));
Assert.False(Directory.Exists(_directory));
journal.Dispose();
}
[Fact]
public void ARunningTimerPersistsItsREMAININGTime()
{
// Saving the value it started at would resurrect the full duration on
// every reload.
(RuntimeJournalState journal, JournalPersistence file, _) = New();
file.Load("Acdream");
journal.NewPage();
journal.SetTimer(0, 1, 0);
journal.StartTimer(Now);
file.Save(Now.AddMinutes(30));
journal.Dispose();
(RuntimeJournalState reloaded, JournalPersistence reloadedFile, _) = New();
reloadedFile.Load("Acdream");
Assert.Equal(1800d, Assert.Single(reloaded.View.Pages).RunningTimerSeconds);
reloaded.Dispose();
}
[Fact]
public void CloseSavesAndForgetsTheCharacter()
{
(RuntimeJournalState journal, JournalPersistence file, _) = New();
file.Load("Acdream");
journal.NewPage();
file.Close(Now);
Assert.Null(file.CurrentPath);
Assert.False(file.Save(Now));
journal.Dispose();
}
[Fact]
public void AnUnwritableDirectoryReportsRatherThanThrowing()
{
// Losing a note is bad; taking the client down while the player is
// writing one is worse.
var journal = new RuntimeJournalState();
var reports = new List<string>();
string blocked = Path.Combine(_directory, "blocked");
Directory.CreateDirectory(_directory);
File.WriteAllText(blocked, "not a directory");
var file = new JournalPersistence(journal, blocked, reports.Add);
file.Load("Acdream");
journal.NewPage();
Assert.False(file.Save(Now));
Assert.NotEmpty(reports);
journal.Dispose();
}
}