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(); var client = NativeWorkflowTests.Proxy((_, 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((_, args) => { if ((string)args![0]! != NativeCompanionPresenceHub.UserGroup(7)) throw new Exception("Wrong account group."); return client; }); var hub = NativeWorkflowTests.Proxy>((_, _) => clients); var accessor = new HttpContextAccessor(); var notices = new ContextWorkspaceNotifications(hub, NullLogger.Instance, accessor); var filter = new ContextReferenceInvalidationFilter(notices); foreach (var outcome in new[] { "preview", "saved", "badrequest", "invalid", "unrelated-validation", "response400", "exception" }) { accessor.HttpContext = new DefaultHttpContext(); var action = new ActionContext(accessor.HttpContext, new RouteData(), new ActionDescriptor()); var filters = new List(); var controller = new object(); var executing = new ActionExecutingContext(action, filters, new Dictionary(), 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 is "invalid" or "unrelated-validation") action.ModelState.AddModelError("Name", "Invalid"); if (outcome == "response400") accessor.HttpContext.Response.StatusCode = 400; return new ActionExecutedContext(action, filters, controller) { Result = outcome == "badrequest" ? new BadRequestResult() : outcome == "invalid" ? new ViewResult() : new JsonResult(new { saved = true }), Exception = outcome == "exception" ? new InvalidOperationException("Synthetic failed save") : null }; }); if (packets.Count - before != (outcome is "saved" or "unrelated-validation" ? 1 : 0)) throw new Exception("Save/failed/preview notification boundary: " + outcome); } using var json = JsonDocument.Parse(packets.First()); 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; } }