PlotDirector/PlotLine.Tests/ContextSavedDataTests.cs
2026-11-30 23:20:12 +00:00

66 lines
3.6 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging.Abstractions;
using PlotLine.Hubs;
using PlotLine.Services;
using System.Text.Json;
internal static class ContextSavedDataTests
{
public static async Task Run()
{
var packets = new List<string>();
var client = NativeWorkflowTests.Proxy<IClientProxy>((_, args) =>
{
if ((string)args![0]! != "ContextInvalidated") throw new Exception("Saved data must not change manuscript position.");
packets.Add(JsonSerializer.Serialize(((object?[])args[1]!)[0]));
return Task.CompletedTask;
});
var clients = NativeWorkflowTests.Proxy<IHubClients>((_, args) =>
{
if ((string)args![0]! != NativeCompanionPresenceHub.UserGroup(7)) throw new Exception("Wrong account group.");
return client;
});
var hub = NativeWorkflowTests.Proxy<IHubContext<NativeCompanionPresenceHub>>((_, _) => clients);
var accessor = new HttpContextAccessor();
var notices = new ContextWorkspaceNotifications(hub, NullLogger<ContextWorkspaceNotifications>.Instance, accessor);
var filter = new ContextReferenceInvalidationFilter(notices);
foreach (var outcome in new[] { "preview", "saved", "badrequest", "invalid", "response400", "exception" })
{
accessor.HttpContext = new DefaultHttpContext();
var action = new ActionContext(accessor.HttpContext, new RouteData(), new ActionDescriptor());
var filters = new List<IFilterMetadata>();
var controller = new object();
var executing = new ActionExecutingContext(action, filters, new Dictionary<string, object?>(), controller);
var before = packets.Count;
await filter.OnActionExecutionAsync(executing, async () =>
{
if (outcome != "preview")
{
await notices.Invalidate(7, bookId:12, projectId:3, kind:"maps", entityId:44, entityType:"Map");
await notices.Invalidate(7, bookId:12, projectId:3, kind:"maps", entityId:44, entityType:"Map");
if (packets.Count != before) throw new Exception("Hint escaped before action completed.");
}
if (outcome == "invalid") action.ModelState.AddModelError("Name", "Invalid");
if (outcome == "response400") accessor.HttpContext.Response.StatusCode = 400;
return new ActionExecutedContext(action, filters, controller)
{
Result = outcome == "badrequest" ? new BadRequestResult() : new JsonResult(new { saved = true }),
Exception = outcome == "exception" ? new InvalidOperationException("Synthetic failed save") : null
};
});
if (packets.Count - before != (outcome == "saved" ? 1 : 0)) throw new Exception("Save/failed/preview notification boundary: " + outcome);
}
using var json = JsonDocument.Parse(packets.Single());
var hint = json.RootElement;
if (hint.GetProperty("projectId").GetInt32()!=3 || hint.GetProperty("bookId").GetInt32()!=12 ||
hint.GetProperty("kind").GetString()!="maps" || hint.GetProperty("entityId").GetInt32()!=44 ||
hint.EnumerateObject().Count()!=7) throw new Exception("Hints must contain scoped identifiers only.");
accessor.HttpContext = null;
}
}