72 lines
4.2 KiB
C#
72 lines
4.2 KiB
C#
using System.Reflection;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
internal static class TimelineDropTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
var current = new SceneEditViewModel {
|
|
SceneID = 10, PrimaryLocationID = 1,
|
|
LocationOptions = [new SelectListItem("Old quay", "1"), new SelectListItem("Station", "2")],
|
|
CharacterOptions = [new SelectListItem("Traveller", "3")],
|
|
CharacterRoleOptions = [new SelectListItem("Participant", "4")],
|
|
PresenceTypeOptions = [new SelectListItem("Present", "5")]
|
|
};
|
|
var saves = 0;
|
|
SceneCharacter? savedCharacter = null;
|
|
var sceneService = Proxy<ISceneService>((name, args) => name switch {
|
|
"GetEditAsync" => Task.FromResult<SceneEditViewModel?>(current),
|
|
"SaveAsync" => SaveScene(),
|
|
_ => throw new NotSupportedException(name)
|
|
});
|
|
Task<int> SaveScene() { saves++; return Task.FromResult(10); }
|
|
var characters = Proxy<ICharacterRepository>((name, args) => {
|
|
if (name != "SaveSceneCharacterAsync") throw new NotSupportedException(name);
|
|
savedCharacter = (SceneCharacter)args![0]!;
|
|
return Task.FromResult(42);
|
|
});
|
|
var service = new TimelineService(null!, null!, null!, null!, null!, null!, sceneService,
|
|
null!, characters, null!, null!, null!, null!, null!, null!);
|
|
async Task Reject(Func<Task> operation) {
|
|
try { await operation(); }
|
|
catch (InvalidOperationException) { return; }
|
|
throw new Exception("Invalid drop was accepted.");
|
|
}
|
|
await Reject(() => service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 2, ExpectedLocationID = 1 }));
|
|
await Reject(() => service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 2, ExpectedLocationID = null, ConfirmReplacement = true }));
|
|
await Reject(() => service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 99, ExpectedLocationID = 1, ConfirmReplacement = true }));
|
|
await Reject(() => service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 1, ExpectedLocationID = 1, ConfirmReplacement = true }));
|
|
if (saves != 0) throw new Exception("Rejected location drop mutated the scene.");
|
|
await service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 2, ExpectedLocationID = 1, ConfirmReplacement = true });
|
|
if (current.PrimaryLocationID != 2 || saves != 1) throw new Exception("Explicit replacement was not saved.");
|
|
current.PrimaryLocationID = null;
|
|
await service.AddLocationByDropAsync(new() { SceneID = 10, LocationID = 1 });
|
|
if (current.PrimaryLocationID != 1 || saves != 2) throw new Exception("Empty scene assignment was not saved.");
|
|
await Reject(() => service.AddCharacterByDropAsync(new() { SceneID = 10, CharacterID = 99 }));
|
|
await Reject(() => service.AddCharacterByDropAsync(new() { SceneID = 10, CharacterID = 3, PresenceTypeID = 99 }));
|
|
current.SceneCharacters = [new() { CharacterID = 3 }];
|
|
await Reject(() => service.AddCharacterByDropAsync(new() { SceneID = 10, CharacterID = 3 }));
|
|
if (savedCharacter is not null) throw new Exception("Rejected character drop was saved.");
|
|
current.SceneCharacters = [];
|
|
await service.AddCharacterByDropAsync(new() { SceneID = 10, CharacterID = 3, RoleInSceneTypeID = 4, PresenceTypeID = 5, LocationID = 2 });
|
|
if (savedCharacter is not { SceneID: 10, CharacterID: 3, RoleInSceneTypeID: 4, PresenceTypeID: 5, LocationID: 2 })
|
|
throw new Exception("Character fields did not reach persistence.");
|
|
}
|
|
|
|
private static T Proxy<T>(Func<string, object?[]?, object?> handler) where T : class
|
|
{
|
|
var proxy = DispatchProxy.Create<T, DropProxy>();
|
|
((DropProxy)(object)proxy).Handler = handler;
|
|
return proxy;
|
|
}
|
|
public class DropProxy : DispatchProxy
|
|
{
|
|
public Func<string, object?[]?, object?> Handler = null!;
|
|
protected override object? Invoke(MethodInfo? method, object?[]? args) => Handler(method!.Name, args);
|
|
}
|
|
}
|