using System.Text.Json; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Mvc; using PlotLine.Controllers; using PlotLine.Models; using PlotLine.Services; internal static class CharacterQuickAttributesTests { public static async Task Run() { void Check(bool value,string label){if(!value)throw new Exception(label);Console.WriteLine("PASS character quick attributes: "+label);} var definitions=CharacterQuickAttributes.Definitions([new(){CharacterSexValueID=17,SexName="Recorded custom value"}]); Check(definitions.Single(g=>g.Id=="sex").Values.Any(v=>v.Value=="17"&&v.Label=="Recorded custom value"),"custom catalogue values remain available without binary assumptions"); Check(CharacterTypes.Values.Select(v=>v.Value).Distinct().Count()==6 && !CharacterTypes.IsValid("human"),"six stable type codes reject invented classifications"); Check(new Character().CharacterTypeCode=="unclassified", "new/legacy projections remain Unclassified without inference"); Check(CharacterTypes.Definition("animal").Icon=="attribute-paw", "animal associations reuse the shared paw icon"); var animal=new Character{CharacterID=5,CharacterTypeCode="animal",Species="Fictional species",CharacterImportance=10}; Check(JsonSerializer.Serialize(CharacterQuickAttributes.Snapshot(animal)).Contains("\"type\":\"animal\"") && animal.Species=="Fictional species" && animal.CharacterImportance==10,"type snapshots leave species and importance independent"); var snapshot=JsonSerializer.Serialize(CharacterQuickAttributes.Snapshot(new(){CharacterID=5,IsDeceased=null,Sex="Legacy custom sex",DefaultDescription="Private complete biography"})); Check(!snapshot.Contains("Private complete biography")&&snapshot.Contains("legacy:Legacy custom sex")&&snapshot.Contains("\"life\":\"\""),"live snapshots preserve unknown/legacy states and omit biographies"); var user=NativeWorkflowTests.Proxy((name,a)=>name=="get_UserId"?23:throw new Exception(name)); var protection=new EphemeralDataProtectionProvider(); var controller=new CharacterAttributesController(null!,null!,null!,user,null!,null!,protection); var receipt=new CharacterAttributesController.UndoReceipt(24,7,"importance",[],DateTime.UtcNow.AddMinutes(10)); string Protect(CharacterAttributesController.UndoReceipt r)=>protection.CreateProtector("CharacterQuickAttributes.Undo.v1").Protect(JsonSerializer.Serialize(r)); Check(await controller.Undo(new(Protect(receipt))) is BadRequestObjectResult,"Undo receipt cannot be used by another author"); Check(await controller.Undo(new(Protect(receipt with{User=23,Expires=DateTime.UtcNow.AddSeconds(-1)}))) is BadRequestObjectResult,"expired Undo is rejected before persistence"); Check(await controller.Undo(new(Protect(receipt)+"tampered")) is BadRequestObjectResult,"modified Undo receipt is rejected before persistence"); Check(await controller.Assign(new(7,"type",[new(5,"PERSON",DateTime.UtcNow.ToString("O"))])) is BadRequestObjectResult,"type codes are canonical and case-sensitive at the endpoint"); Check(await controller.Assign(new(7,"biography",[new(5,"rewrite",DateTime.UtcNow.ToString("O"))])) is BadRequestObjectResult,"precision/free-text fields cannot enter the quick-assignment endpoint"); Check(await controller.Assign(new(7,"life",[new(5,"1",DateTime.UtcNow.ToString("O")),new(5,"0",DateTime.UtcNow.ToString("O"))])) is BadRequestObjectResult,"duplicate IDs cannot widen or repeat a bulk assignment"); } }