PlotDirector/PlotLine.Tests/ContextIntentPaletteTests.cs
2026-12-02 21:05:56 +00:00

62 lines
3.6 KiB
C#

using PlotLine.Services.Context;
using static PlotLine.Services.Context.ContextIntentPalette;
internal static class ContextIntentPaletteTests
{
public static void Run()
{
static void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
static void Reject(Action action)
{
try { action(); } catch (ArgumentException) { return; }
throw new Exception("Expected invalid palette query rejection.");
}
var scope = new IntentPaletteScope(1, 2, 3, null);
Row[] rows = [
new("character:5", "Aster", "character", false, true, true),
new("character:5", "Aster", "character", false, true, false),
new("candidate:9", "Aster", "character", true, false, true),
new("location:5", "Aster", "location", false, false, true)
];
AliasRow[] aliases = [new("character:5", " Captain "), new("character:5", "CAPTAIN"),
new("character:5", "Aster"), new("candidate:9", ""), new("asset:1", "Unrelated")];
SourceRow[] sources = [new("character:5", 8, "Recorded appearance"),
new("character:5", 8, "Recorded appearance"), new("character:5", 8, "Planned reference"),
new("asset:1", 8, "Unrelated")];
var frame = Compose(scope, rows, aliases, sources);
Check(frame.Scope == scope && frame.Entries.Count == 3 && !frame.HasMore,
"Chapter-only scope and identity deduplication are preserved.");
var character = frame.Entries[0];
Check(character.Key == "character:5" && character.InScope && character.PossiblyAmbiguous,
"Any ambiguous/source appearance survives deduplication.");
Check(character.Aliases.SequenceEqual(new[] { "Aster", "Captain" }) && character.Sources.Count == 2,
"Aliases and provenance deduplicate independently without unrelated attachments.");
Check(frame.Entries[1].IsCandidate && !frame.Entries[1].InScope && !frame.Entries[2].IsCandidate,
"Equal labels, proposed identities and cross-kind numeric IDs never merge.");
Check(frame.Entries[1].Sources.Count == 0 && frame.Entries[1].Aliases.Count == 1,
"No candidate evidence or aliases are inferred from equal canonical labels.");
aliases[0] = new("character:5", "Changed");
sources[0] = new("character:5", 9, "Changed");
Check(character.Aliases[1] == "Captain" && character.Sources.All(s => s.SceneID == 8),
"Frame attachments are stable snapshots.");
var many = Enumerable.Range(1, 201).Select(i => new Row($"asset:{i}", $"Asset {i}", "asset", false, false, false)).ToArray();
var page = Compose(scope, many, [], []);
Check(page.Entries.Count == PageSize && page.HasMore && page.Entries[^1].Key == "asset:200",
"Overflow is explicit and order is preserved; a page is never a complete match index.");
Check(!Compose(scope, many.Take(200).ToArray(), [], []).HasMore, "An exact page is complete.");
Validate(new(2, 3));
Validate(new(2, 3, 4, true, " Aster "));
Reject(() => Validate(new(0, 3)));
Reject(() => Validate(new(2, 0)));
Reject(() => Validate(new(2, 3, 0)));
Reject(() => Validate(new(2, 3, Search: new string('x', 201))));
Reject(() => Validate(new(2, 3, Search: null!)));
try
{
Compose(scope, [rows[0], rows[0] with { IsCandidate = true }], [], []);
throw new Exception("Conflicting identity metadata was accepted.");
}
catch (InvalidOperationException) { }
}
}