96 lines
7.8 KiB
C#
96 lines
7.8 KiB
C#
using System.Reflection;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
internal static class RelationshipWorkspaceTests
|
|
{
|
|
public static RelationshipMapData Data() => new()
|
|
{
|
|
Books = [new() { BookID = 12, ProjectID = 7, BookNumber = 1 }],
|
|
Chapters = [new() { ChapterID = 21, BookID = 12, ChapterNumber = 1 }],
|
|
Scenes = Enumerable.Range(1, 300).Select(i => new Scene { SceneID = i, ChapterID = 21, SceneNumber = i, SceneTitle = $"Scene {i}", SortOrder = i }).ToList(),
|
|
Characters = Enumerable.Range(1, 75).Select(i => new Character { CharacterID = i, ProjectID = 7, CharacterName = i == 1 ? "Élan Vale" : $"Cast {i:00}", CharacterImportance = i % 11 }).ToList(),
|
|
Relationships = Enumerable.Range(1, 74).Select(i => new CharacterRelationship { CharacterRelationshipID = i, ProjectID = 7, CharacterAID = 1, CharacterBID = i + 1, CharacterAName = "Élan Vale", CharacterBName = $"Cast {i + 1:00}", RelationshipTypeID = 1, RelationshipTypeName = "Friend", RelationshipCategoryName = "Friendship", IsInitialRelationship = true, InitialRelationshipStateID = 1, InitialRelationshipStateName = "Friendly", IsReciprocal = true }).ToList(),
|
|
RelationshipCategories = [new() { RelationshipCategoryID = 1, CategoryName = "Friendship" }],
|
|
RelationshipEvents = [new() { RelationshipEventID = 91, CharacterRelationshipID = 1, SceneID = 100, RelationshipStateID = 2, RelationshipStateName = "Distrustful" }, new() { RelationshipEventID = 92, CharacterRelationshipID = 1, SceneID = 200, RelationshipStateID = 1, RelationshipStateName = "Friendly" }]
|
|
};
|
|
public static CharacterLookupData Lookups() => new()
|
|
{
|
|
RelationshipTypes = [new() { RelationshipTypeID = 1, TypeName = "Friend", IsActive = true }],
|
|
RelationshipStates = [new() { RelationshipStateID = 1, StateName = "Friendly", IsActive = true }, new() { RelationshipStateID = 2, StateName = "Distrustful", IsActive = true }]
|
|
};
|
|
public static ICharacterRepository Repository(RelationshipMapData data) => Stub<ICharacterRepository>((m, _) => m switch
|
|
{
|
|
"GetRelationshipMapAsync" => Task.FromResult(data),
|
|
"ListCharactersAsync" => Task.FromResult(data.Characters),
|
|
"GetLookupsAsync" => Task.FromResult(Lookups()),
|
|
_ => throw new Exception(m)
|
|
});
|
|
public static RelationshipMapService Service(RelationshipMapData data) => new(Stub<IProjectRepository>((m, _) => m switch
|
|
{
|
|
"GetAsync" => Task.FromResult<Project?>(new() { ProjectID = 7, ProjectName = "Cast fixture" }),
|
|
_ => throw new Exception(m)
|
|
}), Repository(data));
|
|
public static async Task<RelationshipMapViewModel> Fixture() => (await Service(Data()).GetAsync(7, null, null, null, null, true))!;
|
|
public static void StateAndEditing() => Run().GetAwaiter().GetResult();
|
|
private static async Task Run()
|
|
{
|
|
var data = Data(); var service = Service(data);
|
|
var initial = (await service.GetAsync(7, null, null, null, null, true))!;
|
|
Check(initial.Cast.Count == 75 && initial.Cast.Select(x => x.CharacterID).Distinct().Count() == 75, "complete cast without duplication");
|
|
Check(initial.Cast.Select(x => x.CharacterImportance).SequenceEqual(initial.Cast.Select(x => x.CharacterImportance).OrderDescending()), "importance ordering");
|
|
Check(initial.SceneOptions.Count == 300 && initial.ChangeSceneIDs.Order().SequenceEqual(new[] { 100, 200 }), "long timeline and change markers");
|
|
Check(initial.Relationships.First().CurrentStateName == "Friendly", "initial state");
|
|
foreach (var (scene, expected) in new[] { (99,"Friendly"), (100,"Distrustful"), (199,"Distrustful"), (200,"Friendly"), (300,"Friendly") })
|
|
{
|
|
var point = (await service.GetAsync(7, null, null, scene, null, true))!;
|
|
Check(point.Relationships.Single(x => x.CharacterRelationshipID == 1).CurrentStateName == expected, $"state after scene {scene}");
|
|
Check(point.Links.Count == point.Relationships.Count && point.Links.All(l => point.Relationships.Any(r => r.CharacterRelationshipID == l.CharacterRelationshipID && r.CssClass == l.CssClass)), "canonical map/list lines");
|
|
}
|
|
var dated = new RelationshipMapData { Characters = data.Characters, Books = data.Books, Chapters = data.Chapters, Scenes = data.Scenes,
|
|
Relationships = [new() { CharacterRelationshipID = 800, CharacterAID = 3, CharacterBID = 4, StartSceneID = 150, RelationshipTypeName = "Friend" }] };
|
|
Check(!(await Service(dated).GetAsync(7, null, null, 149, null, true))!.Relationships.Any(), "future relationship hidden");
|
|
Check((await Service(dated).GetAsync(7, null, null, 150, null, true))!.Relationships.Count == 1, "dated relationship starts on its scene");
|
|
var focused = (await service.GetAsync(7, null, null, 100, 2, false))!;
|
|
Check(focused.Relationships.Count == 1 && focused.Nodes.Single(x => x.IsFocus).CharacterID == 2, "existing Focus Mode");
|
|
var saves = new List<object>();
|
|
var characters = Stub<ICharacterService>((m,a) => { saves.Add(a![0]!); return m switch { "SaveInitialRelationshipAsync" or "UpdateRelationshipEventAsync" or "AddRelationshipEventAsync" => Task.FromResult(1), _ => throw new Exception(m) }; });
|
|
var controller = new RelationshipMapController(service, Repository(data), characters);
|
|
var edit = new WorkspaceRelationshipEdit { ProjectID = 7, SourceID = 2, TargetID = 1, TypeID = 1, StateID = 2, Mutual = true };
|
|
Check(await controller.Save(edit) is OkResult, "initial edit");
|
|
Check(((InitialRelationshipEditViewModel)saves.Single()).CharacterRelationshipID == 1, "reverse reciprocal pair reuses canonical record");
|
|
saves.Clear(); edit.SceneID = 100;
|
|
Check(await controller.Save(edit) is OkResult, "scene edit");
|
|
Check(((RelationshipEventCreateViewModel)saves.Single()).RelationshipEventID == 91, "same-scene event updates rather than duplicates");
|
|
saves.Clear(); edit.SceneID = 101;
|
|
await controller.Save(edit);
|
|
Check(((RelationshipEventCreateViewModel)saves.Single()).CharacterRelationshipID == 1, "new scene change uses existing record");
|
|
saves.Clear(); edit.SourceID = 3; edit.TargetID = 4; edit.SceneID = null;
|
|
Check(await controller.Save(edit) is OkResult, "new initial relationship");
|
|
Check(((InitialRelationshipEditViewModel)saves.Single()).CharacterRelationshipID == 0, "new initial uses canonical creation");
|
|
saves.Clear(); edit.SceneID = 50;
|
|
Check(await controller.Save(edit) is OkResult, "new scene-specific relationship");
|
|
Check(((RelationshipEventCreateViewModel)saves.Single()).CharacterRelationshipID is null && ((RelationshipEventCreateViewModel)saves.Single()).IsReciprocal, "new scene relationship preserves mutual status");
|
|
saves.Clear(); edit.SourceID = 999;
|
|
Check(await controller.Save(edit) is BadRequestObjectResult && saves.Count == 0, "reject foreign character without writes");
|
|
edit.SourceID = 1; edit.TargetID = 1;
|
|
Check(await controller.Save(edit) is BadRequestObjectResult, "reject self relationships");
|
|
edit.SourceID = 2; edit.TargetID = 1; edit.SceneID = 999;
|
|
Check(await controller.Save(edit) is BadRequestObjectResult, "reject foreign scene");
|
|
}
|
|
private static void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
|
|
private static T Stub<T>(Func<string, object?[]?, object?> invoke) where T : class
|
|
{
|
|
var proxy = DispatchProxy.Create<T, Proxy>(); ((Proxy)(object)proxy).Handler = invoke; return proxy;
|
|
}
|
|
public class Proxy : DispatchProxy
|
|
{
|
|
public Func<string, object?[]?, object?> Handler = null!;
|
|
protected override object? Invoke(MethodInfo? method, object?[]? args) => Handler(method!.Name, args);
|
|
}
|
|
}
|