using Microsoft.AspNetCore.SignalR;using Microsoft.Extensions.Logging.Abstractions; using PlotLine.Data;using PlotLine.Hubs;using PlotLine.Models;using PlotLine.Services;using PlotLine.Services.Context;using PlotLine.ViewModels; internal static class CharacterWorkspaceTests { public static async Task Run() { void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS main Character workspace: "+message);} var profile=new CharacterDetailViewModel{Character=new(){DefaultDescription="Author biography. Additional detail."},CharacterAnalysis=new(){Summary="Author biography.\n\tAdditional\u00a0detail."}}; Check(profile.OverviewAnalysisSummary==null,"whitespace-equivalent analysis is omitted without changing stored sources"); Check(profile.CharacterAnalysis.Summary!.Contains('\n')&&profile.Character.DefaultDescription.Contains(" "),"display deduplication leaves source values intact"); profile.CharacterAnalysis=new(){Summary="Author biography. Additional detail. A distinct recorded insight."}; Check(profile.OverviewAnalysisSummary==profile.CharacterAnalysis.Summary,"overlapping but distinct analysis is retained in full"); profile.CharacterAnalysis=new(){Summary=null};Check(profile.OverviewAnalysisSummary==null,"biography-only has no empty analysis card"); profile.Character.DefaultDescription=null;profile.CharacterAnalysis=new(){Summary="Recorded analysis only."};Check(profile.OverviewAnalysisSummary=="Recorded analysis only.","analysis-only retains the recorded narrative"); profile.CharacterAnalysis=new(){Summary=" \t\n"};Check(profile.OverviewAnalysisSummary==null,"neither narrative produces no analysis card"); profile.Character.DefaultDescription="Author biography.";profile.CharacterAnalysis=new(){Summary="Author Biography."};Check(profile.OverviewAnalysisSummary!=null,"comparison preserves meaningful spelling/case/punctuation differences"); var role="Owner";int records=0,galleries=0,changes=0,aliases=0;bool protectedEvent=false;var version=new string('A',64); var character=new Character{CharacterID=5,ProjectID=7,CharacterName="Canonical identity",CharacterImportance=6,AgeAtSeriesStart=25}; var definitions=new RelationshipMapData{Books=[new(){BookID=12,SortOrder=1}],Chapters=[new(){ChapterID=10,BookID=12,SortOrder=1}],Scenes=[new(){SceneID=90,ChapterID=10,SceneNumber=1},new(){SceneID=22,ChapterID=10,SceneNumber=2},new(){SceneID=2,ChapterID=10,SceneNumber=3}],Relationships=[new(){CharacterRelationshipID=9,ProjectID=7,CharacterAID=5,CharacterBID=6,RelationshipTypeName="Parent",RelationshipCategoryName="Family",IsInitialRelationship=true,InitialRelationshipStateName="Before"},new(){CharacterRelationshipID=11,ProjectID=7,CharacterAID=5,CharacterBID=7,RelationshipTypeName="Friend",RelationshipCategoryName="Friendship",StartSceneID=2}],RelationshipEvents=[new(){RelationshipEventID=1,CharacterRelationshipID=9,SceneID=22,RelationshipStateName="Now"},new(){RelationshipEventID=2,CharacterRelationshipID=9,SceneID=2,RelationshipStateName="Later",Description="Future notes"}]}; var reader=NativeWorkflowTests.Proxy((name,a)=>name switch{ "Summary"=>Task.FromResult((new CharacterWorkspaceCounts{Relationships=2,Appearances=70},(IReadOnlyList)Array.Empty())), "Records"=>Read((CharacterWorkspaceQuery)a![2]!),"Gallery"=>Gallery(),"Relationships"=>Task.FromResult((2,definitions)), "Frame"=>Task.FromResult((new CharacterRelationshipFrame{Relationship=definitions.Relationships[0],Version=version,EventCount=3,OtherPairRecords=1},a![3] is int eid?new CharacterWorkspaceEvent{Record=new(){RelationshipEventID=eid,SceneID=22,CharacterRelationshipID=9},ProtectedSource=protectedEvent}:null)), "Change"=>Changed(),_=>throw new Exception(name)}); Task<(int,IReadOnlyList)> Read(CharacterWorkspaceQuery q){records++;return Task.FromResult((1,(IReadOnlyList)new[]{new CharacterWorkspaceEntry{ID=1,SceneID=q.SceneID??22,Heading="Recorded source",BookTitle="Book",Kind=q.Tab}}));} Task<(int,IReadOnlyList)> Gallery(){galleries++;return Task.FromResult((0,(IReadOnlyList)Array.Empty()));}Task Changed(){changes++;return Task.CompletedTask;} var repo=NativeWorkflowTests.Proxy((n,a)=>n switch{"GetCharacterAsync"=>Task.FromResult((int)a![0]! ==99?null:character),"GetReferenceCharacterAsync"=>Task.FromResult(character),"ListAliasesAsync"=>Alias(),"GetLookupsAsync"=>Task.FromResult(new CharacterLookupData()),_=>throw new Exception("Unexpected eager read: "+n)}); Task> Alias(){aliases++;return Task.FromResult>(Enumerable.Range(1,75).Select(i=>new CharacterAlias{CharacterID=5,Alias="Alias "+i}).ToArray());} var client=NativeWorkflowTests.Proxy((_,_)=>Task.CompletedTask);var clients=NativeWorkflowTests.Proxy((_,_)=>client);var hub=NativeWorkflowTests.Proxy>((_,_)=>clients); var workspace=new CharacterWorkspace(repo,NativeWorkflowTests.Proxy((n,_)=>n=="GetForUserAsync"?Task.FromResult(new(){ProjectID=7,ProjectName="Author project",AccessRole=role}):throw new Exception("Unscoped project read")),NativeWorkflowTests.Proxy((_,_)=>Task.FromResult>([])),NativeWorkflowTests.Proxy((_,_)=>4),NativeWorkflowTests.Proxy((_,_)=>Task.FromResult(null)),reader,NativeWorkflowTests.Proxy((_,_)=>throw new Exception("Unexpected graph/history read")),null!,new ContextWorkspaceNotifications(hub,NullLogger.Instance),NativeWorkflowTests.Proxy((_,_)=>Task.CompletedTask),NativeWorkflowTests.Proxy((_,_)=>throw new Exception("Legacy full-detail service called"))); var header=await workspace.Header(5,null,CancellationToken.None);Check(records==0&&galleries==0&&aliases==0&&header.Character.CharacterName=="Canonical identity","shell loads only identity, scoped project, counts/age candidates and persisted enrichment status"); var overview=await workspace.Page(5,new(){Tab="overview"},CancellationToken.None);Check(records==1&&galleries==1&&aliases==1&&overview.Profile.Aliases.Count==50&&overview.Entries.Count<=3,"Overview is bounded; it does not load full character/project histories"); var page=await workspace.Page(5,new(){Tab="relationships",SceneID=22},CancellationToken.None);Check(page.Relationships.Single(r=>r.Relationship.CharacterRelationshipID==9).CurrentStateName=="Now"&&!page.Relationships[0].Events.Any(e=>e.SceneID==2),"selected-point relationship state and displayed history exclude future events using canonical ordering");Check(page.Relationships.Single(r=>r.Relationship.CharacterRelationshipID==11).CurrentStateName=="Not established / ended at this point","later definitions stay identifiable as author records without pretending to exist at the selected point"); Check(CharacterWorkspace.Classification(10)=="Major"&&CharacterWorkspace.Classification(6)=="Supporting"&&CharacterWorkspace.Classification(2)=="Minor"&&CharacterWorkspace.Classification(null)=="Unclassified"&&character.CharacterImportance==6,"classification labels preserve explicit values and do not reclassify existing records"); foreach(var q in new[]{new CharacterWorkspaceQuery{Tab="unknown"},new(){Tab="evolution",Page=0},new(){Tab="knowledge",Search=new string('x',121)}}){try{await workspace.Page(5,q,CancellationToken.None);throw new Exception("Invalid query accepted");}catch(ArgumentException){Check(true,"closed tabs and bounded query criteria reject invalid requests");}} var op=await workspace.Operation(5,9,null,"archive",CancellationToken.None);Check(op.Record.EventCount==3&&op.Record.OtherPairRecords==1,"removal preview exposes retained dependencies and separate pair records");op.Confirmed=true;op.Version=new string('B',64);try{await workspace.Change(op,CancellationToken.None);throw new Exception("Stale accepted");}catch(InvalidOperationException){Check(changes==0,"stale form is rejected before canonical mutation");} op.Version=version;op.Confirmed=false;try{await workspace.Change(op,CancellationToken.None);throw new Exception("Unconfirmed removal");}catch(ArgumentException){Check(changes==0,"destructive definition removal requires explicit confirmation");} protectedEvent=true;var evt=await workspace.Operation(5,9,1,"event-remove",CancellationToken.None);evt.Confirmed=true;try{await workspace.Change(evt,CancellationToken.None);throw new Exception("Protected source accepted");}catch(ArgumentException){Check(changes==0,"source/review-linked event cannot be removed or orphaned by focused operations");} role="Viewer";try{await workspace.Operation(5,9,null,"definition",CancellationToken.None);throw new Exception("Viewer edit accepted");}catch(UnauthorizedAccessException){Check(changes==0,"Viewer cannot open or execute an editing operation");} var viewer=await workspace.Header(5,null,CancellationToken.None);Check(!viewer.WorkspaceCanEdit,"Viewer header stays read-only");try{await workspace.Header(99,null,CancellationToken.None);throw new Exception("Unknown accepted");}catch(UnauthorizedAccessException){Check(true,"unavailable character rejected before reading histories");} } }