74 lines
2.9 KiB
C#
74 lines
2.9 KiB
C#
using Wiki.Application.Encyclopedia;
|
|
using Wiki.Core.Model;
|
|
|
|
namespace Wiki.Application.Tests;
|
|
|
|
public class EncyclopediaStateTests
|
|
{
|
|
private static readonly DateTimeOffset At = new(2026, 10, 24, 3, 0, 0, TimeSpan.Zero);
|
|
|
|
[Fact]
|
|
public void EncyclopediaStateJson_RoundTrip()
|
|
{
|
|
var state = new EncyclopediaState(
|
|
new Dictionary<string, SubjectAttempt>
|
|
{
|
|
["heating"] = new("abc", AttemptOutcome.Written, At),
|
|
["sauna"] = new("def", AttemptOutcome.Rejected, At),
|
|
},
|
|
new PlanAttempt("zzz", At));
|
|
|
|
var read = EncyclopediaStateJson.Read(EncyclopediaStateJson.Write(state));
|
|
|
|
Assert.Equal(state.Subjects, read.Subjects);
|
|
Assert.Equal(state.FailedPlan, read.FailedPlan);
|
|
}
|
|
|
|
[Fact]
|
|
public void EncyclopediaStateJson_Malformed_ReadsEmpty()
|
|
{
|
|
var read = EncyclopediaStateJson.Read("{{{ nope");
|
|
Assert.Empty(read.Subjects);
|
|
Assert.Null(read.FailedPlan);
|
|
}
|
|
|
|
[Fact]
|
|
public void EncyclopediaStateJson_NullEntry_Dropped()
|
|
{
|
|
var read = EncyclopediaStateJson.Read("""{"subjects":{"a":null,"b":{"fingerprint":"f","outcome":"written","at":"2026-10-24T03:00:00+00:00"}},"failedPlan":null}""");
|
|
Assert.Equal(["b"], read.Subjects.Keys);
|
|
}
|
|
|
|
[Fact]
|
|
public void Empty_DoesNotShareMutableState()
|
|
{
|
|
((Dictionary<string, SubjectAttempt>)EncyclopediaState.Empty.Subjects)["x"] = new("f", AttemptOutcome.Written, At);
|
|
Assert.Empty(EncyclopediaState.Empty.Subjects);
|
|
}
|
|
|
|
[Fact]
|
|
public void SubjectFingerprint_ChangesWithSourceHashOrTitle()
|
|
{
|
|
var planned = new PlannedArticle("heating", "Heizung", "Heating", ArticleCategory.System, ["a", "b"], []);
|
|
var notes = new[] { PlanArticlesUseCaseTests.Note("a", hash: "1"), PlanArticlesUseCaseTests.Note("b", hash: "2") };
|
|
|
|
var baseline = EncyclopediaState.SubjectFingerprint(planned, notes);
|
|
|
|
Assert.Equal(baseline, EncyclopediaState.SubjectFingerprint(planned, notes));
|
|
Assert.Matches("^[0-9a-f]{64}$", baseline);
|
|
var changedHash = new[] { notes[0], PlanArticlesUseCaseTests.Note("b", hash: "3") };
|
|
Assert.NotEqual(baseline, EncyclopediaState.SubjectFingerprint(planned, changedHash));
|
|
Assert.NotEqual(baseline, EncyclopediaState.SubjectFingerprint(planned with { TitleDe = "Heizungsanlage" }, notes));
|
|
}
|
|
|
|
[Fact]
|
|
public void NoteSetFingerprint_IgnoresOrder_ChangesWithSlugs()
|
|
{
|
|
var ab = EncyclopediaState.NoteSetFingerprint([PlanArticlesUseCaseTests.Note("a"), PlanArticlesUseCaseTests.Note("b")]);
|
|
var ba = EncyclopediaState.NoteSetFingerprint([PlanArticlesUseCaseTests.Note("b"), PlanArticlesUseCaseTests.Note("a")]);
|
|
var abc = EncyclopediaState.NoteSetFingerprint([PlanArticlesUseCaseTests.Note("a"), PlanArticlesUseCaseTests.Note("b"), PlanArticlesUseCaseTests.Note("c")]);
|
|
Assert.Equal(ab, ba);
|
|
Assert.NotEqual(ab, abc);
|
|
}
|
|
}
|