59 lines
9.9 KiB
C#
59 lines
9.9 KiB
C#
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services.Context;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
internal static class ContextFamilyTests
|
|
{
|
|
internal static (ContextFamilyStructure Structure,SceneEditViewModel Scene) Fixture(bool large=false,int sceneID=22,bool many=false)
|
|
{
|
|
var people=Enumerable.Range(1,80).Concat(Enumerable.Range(100,large?120:0)).Select(id=>new Character{CharacterID=id,ProjectID=7,CharacterName=id switch{5=>"Scene viewpoint",15=>"Scene sibling",22=>"Other family participant",30=>"An individual",_=>"Recorded person "+id},BirthDate=id is 7 or 8?new(2000+(id-7)*2,1,1):null,IsArchived=id==6,IsDeceased=id==6?true:null}).ToList();
|
|
var links=new List<CharacterRelationship>();int sequence=0;
|
|
void Add(int a,int b,string type,bool reciprocal=false,string category="Family")=>links.Add(new(){CharacterRelationshipID=++sequence,ProjectID=7,CharacterAID=a,CharacterBID=b,RelationshipTypeName=type,RelationshipCategoryName=category,IsReciprocal=reciprocal,IsInitialRelationship=true});
|
|
Add(1,2,"Spouse",true,"Romantic / Sexual");Add(1,3,"Parent");Add(2,3,"Parent");Add(3,4,"Spouse",true,"Romantic / Sexual");Add(3,5,"Parent");Add(4,5,"Parent");Add(3,15,"Parent");Add(5,6,"Spouse",true,"Romantic / Sexual");Add(5,10,"Former spouse",true,"Romantic / Sexual");Add(5,7,"Biological Parent");Add(6,7,"Biological Parent");Add(5,8,"Parent");Add(6,8,"Parent");Add(5,9,"Parent");Add(10,9,"Parent");Add(7,9,"Half-sibling",true);Add(11,7,"Adoptive Parent");Add(12,7,"Step Parent");Add(7,16,"Parent");Add(16,17,"Parent");
|
|
Add(21,22,"Parent");Add(21,23,"Spouse",true,"Romantic / Sexual");Add(24,21,"Parent");Add(24,25,"Parent");
|
|
Add(40,41,"Parent");Add(41,40,"Parent");Add(50,51,"Parent",true);Add(60,61,"Guardian");Add(5,22,"Friend",true,"Friendship");Add(5,30,"Partner",true,"Professional / Social");
|
|
if(large)for(int id=100;id<220;id++)Add(id==100?17:100+(id-101)/2,id,"Parent");
|
|
var scene=new SceneEditViewModel{SceneID=sceneID,ChapterID=10,SceneTitle="Family exploration fixture",POVCharacterID=sceneID==23?22:5,SceneCharacters=[..new[]{5,15,22,30,40,41}.Select(id=>new SceneCharacter{CharacterID=id,PresenceTypeName="Present"}),new(){CharacterID=25,PresenceTypeName="Present",RoleInSceneTypeName="Mentioned Only"}]};
|
|
if(many)scene.SceneCharacters=[..scene.SceneCharacters,..new[]{31,32,33,34,35,36,37,38,39,42,43,44,45,46}.Select(id=>new SceneCharacter{CharacterID=id,PresenceTypeName="Present"})];
|
|
return(new(people,links),scene);
|
|
}
|
|
public static void Run()
|
|
{
|
|
void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS Context family: "+message);}
|
|
var(data,scene)=Fixture(true);var topology=new ContextFamilyTopology(data.Links);var index=ContextFamilyExploration.Index(data,scene);
|
|
Check(index.Groups.First().IsPov&&index.DefaultKey==topology.Key(5),"POV family sorts first; scene co-occurrence does not group unrelated families");
|
|
Check(index.Groups.Single(g=>g.SceneMembers.Contains(5)).SceneMembers.Contains(15)&&topology.Key(5)!=topology.Key(22),"siblings and common recorded ancestry share one index entry; unrelated families remain separate");
|
|
Check(!index.Groups.Any(g=>g.SceneMembers.Contains(25))&&index.Groups.Single(g=>g.SceneMembers.Contains(30)).SceneMembers.Count==1,"mentioned-only is not physical; a person without genealogy remains an individual despite professional partnership");
|
|
Check(topology.Key(40)!=topology.Key(41)&&topology.Key(50)!=topology.Key(51)&&topology.Key(60)!=topology.Key(61)&&index.Warnings.Any(),"cyclic, reciprocal-parent and ambiguous guardianship records cannot merge families or establish ancestry");
|
|
var initial=topology.Visible(5,[]);Check(initial.People.Contains(15)&&!initial.People.Contains(1)&&!initial.People.Contains(100)&&!initial.People.Contains(22),"initial family is compact, includes recorded siblings, and excludes distant generations and other families");
|
|
var expand=topology.Visible(5,[3,7]);Check(expand.People.Contains(1)&&expand.People.Contains(2)&&expand.People.Contains(11)&&expand.People.Contains(12)&&expand.People.Contains(16),"node expansion reveals only recorded ancestors, adoption, step-parentage and descendants");
|
|
Check(topology.Visible(5,[]).People.SequenceEqual(initial.People)&&expand.Branches.Any(b=>b.ID==3&&b.Expanded),"collapse returns the compact tree and expansion state is explicit");
|
|
Check(data.Links.Single(r=>r.CharacterAID==11&&r.CharacterBID==7).RelationshipTypeName=="Adoptive Parent"&&data.Links.Single(r=>r.CharacterAID==6&&r.CharacterBID==7).RelationshipTypeName=="Biological Parent","adoptive and biological parentage retain separate canonical semantics");
|
|
Check(!data.Links.Any(r=>r.CharacterAID==11&&r.CharacterBID==12)&&topology.Visible(30,[]).People.SequenceEqual([30]),"no second parent or parental partnership is invented for an individual");
|
|
Check(data.Links.Any(r=>r.CharacterAID==5&&r.CharacterBID==9)&&data.Links.Any(r=>r.CharacterAID==10&&r.CharacterBID==9)&&!data.Links.Any(r=>r.CharacterAID==6&&r.CharacterBID==9),"multiple unions and half-siblings retain exact recorded parentage");
|
|
var changed=new ContextFamilyStructure(data.Seeds,data.Links.Append(new(){CharacterRelationshipID=500,CharacterAID=8,CharacterBID=22,RelationshipTypeName="Spouse",RelationshipCategoryName="Romantic / Sexual"}).ToList());Check(ContextFamilyExploration.Index(changed,scene).Groups.Count==index.Groups.Count-1,"a canonical connection correction recomputes family membership");
|
|
Check(ContextFamilyExploration.Index(data,Fixture(false,23).Scene).Groups.First().SceneMembers.Contains(22),"a new scene defaults to its new POV family independently of knowledge");
|
|
scene.POVCharacterID=25;Check(ContextFamilyExploration.Index(data,scene).Groups.First().SceneMembers.Contains(25),"POV is included independently of physical classification");
|
|
try{topology.Visible(5,[22]);throw new Exception("foreign branch accepted");}catch(ArgumentException){Check(true,"branch expansion cannot import a different family");}
|
|
var watch=System.Diagnostics.Stopwatch.StartNew();var broad=new ContextFamilyTopology(Enumerable.Range(2,4096).Select(id=>new CharacterRelationship{CharacterRelationshipID=id,CharacterAID=1,CharacterBID=id,RelationshipTypeName="Parent",RelationshipCategoryName="Family"}).ToList());var bounded=broad.Visible(1,[]);Check(bounded.Truncated&&bounded.People.Length==128&&bounded.Links.Length<=256,"a 4,097-person family remains bounded without sorting the focus neighbourhood per person");
|
|
var deep=new ContextFamilyTopology(Enumerable.Range(1,4096).Select(id=>new CharacterRelationship{CharacterRelationshipID=id,CharacterAID=id,CharacterBID=id+1,RelationshipTypeName="Parent",RelationshipCategoryName="Family"}).ToList());Check(deep.Key(4097)==1&&deep.Visible(1,[]).People.Length==2,"deep canonical ancestry uses iterative traversal and starts with only adjacent generations");Console.WriteLine($"Family 4096-edge broad/deep metadata qualification: {watch.ElapsedMilliseconds}ms");
|
|
RunAccess().GetAwaiter().GetResult();
|
|
|
|
}
|
|
static async Task RunAccess()
|
|
{
|
|
void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS Context family access: "+message);}
|
|
var(data,scene)=Fixture();var structures=0;var trees=0;int[] requested=[];var role="Viewer";
|
|
var reader=NativeWorkflowTests.Proxy<IContextFamilyReader>((name,args)=>{if(name=="Structure"){structures++;requested=((IReadOnlyList<int>)args![2]!).ToArray();return Task.FromResult(data);}trees++;var people=(IReadOnlyList<int>)args![2]!;var links=(IReadOnlyList<int>)args[3]!;return Task.FromResult(new RelationshipMapData{Books=[new(){BookID=12}],Chapters=[new(){ChapterID=10,BookID=12}],Scenes=[new(){SceneID=22,ChapterID=10}],Characters=data.Seeds.Where(c=>people.Contains(c.CharacterID)).ToList(),Relationships=data.Links.Where(r=>links.Contains(r.CharacterRelationshipID)).ToList()});});
|
|
var characters=NativeWorkflowTests.Proxy<ICharacterRepository>((_,a)=>Task.FromResult<Character?>(new(){CharacterID=(int)a![0]!,ProjectID=(int)a[0]! ==99?8:7}));var user=NativeWorkflowTests.Proxy<ICurrentUserService>((_,_)=>4);
|
|
var lenses=new ContextRelationshipLenses(NativeWorkflowTests.Proxy<IContextRelationshipReader>((_,_)=>throw new Exception("Story reader should not be used")),characters,NativeWorkflowTests.Proxy<IWordCompanionRepository>((_,_)=>Task.FromResult<WordCompanionBookStructureResponse?>(new(){BookId=12,Chapters=[new(){ChapterId=10,Scenes=[new(){SceneId=22}]}]})),NativeWorkflowTests.Proxy<IBookRepository>((_,_)=>Task.FromResult<Book?>(new(){BookID=12,ProjectID=7})),NativeWorkflowTests.Proxy<IProjectRepository>((_,_)=>Task.FromResult<Project?>(new(){ProjectID=7,AccessRole=role})),user,NativeWorkflowTests.Proxy<IStoryContextService>((_,_)=>Task.FromResult<StoryContextViewModel?>(new(){Position=new(7,12,10,22),Scene=scene})));
|
|
var service=new ContextFamilyExploration(lenses,reader,characters,user);await service.Index(12,22,CancellationToken.None);Check(requested.Contains(5)&&requested.Contains(15)&&!requested.Contains(25),"authorised index receives explicit physical seeds plus POV, not mentioned-only identities");
|
|
var tree=await service.Tree(12,22,5,[],CancellationToken.None);Check(!tree.Graph.CanEdit&&tree.Graph.Nodes.Count<=128&&tree.Graph.Nodes.All(n=>n.ID!=22),"viewer receives a bounded selected-family tree without unrelated physical participants");
|
|
var before=structures;try{await service.Tree(12,22,99,[],CancellationToken.None);throw new Exception("Foreign focus accepted");}catch(UnauthorizedAccessException){Check(before==structures,"foreign-project focus is rejected before reading family metadata");}
|
|
try{await service.Tree(12,999,5,[],CancellationToken.None);throw new Exception("Foreign scene accepted");}catch(UnauthorizedAccessException){Check(before==structures,"foreign-book scene is rejected before reading family metadata");}
|
|
var beforeTrees=trees;try{await service.Tree(12,22,5,[22],CancellationToken.None);throw new Exception("Foreign branch accepted");}catch(ArgumentException){Check(beforeTrees==trees,"branch IDs from another component cannot request a tree snapshot");}
|
|
}
|
|
|
|
}
|