using PlotDirector.Companion.Protocol.Manuscript; using PlotLine.Models; using PlotLine.Services.Manuscript; internal static class ChapterStructuralContentTests { public static void Run() { void Check(bool value,string label){if(!value)throw new Exception(label);Console.WriteLine("PASS "+label);} void Refused(Action work,string label){try{work();}catch(ArgumentException){Console.WriteLine("PASS "+label);return;}throw new Exception("Unsafe structural evidence accepted: "+label);} const string prefix=""; const string suffix=""; var xml=prefix+"ChapterLetter text"+suffix; var texts=new[]{"Chapter\r","Letter text\r","\r"}; var geometry=new[]{new ChapterParagraph{NativeId="00000001",Length=texts[0].Length,WordLength=texts[0].Length,Outline=1},new ChapterParagraph{NativeId="00000002",Length=texts[1].Length,WordLength=texts[1].Length+1,Outline=10,Anchors=[new(){Offset=0,ShapeId=17,ShapeType=13}]},new ChapterParagraph{NativeId="00000003",Length=1,WordLength=1,Outline=10}}; var frames=ChapterWordPositions.Encode(geometry);var decoded=ChapterWordPositions.Decode(frames); var rows=ChapterFingerprint.ReadXml(xml,string.Concat(texts),null,true,decoded); Check(rows[1].Text==texts[1]&&rows[1].Length==texts[1].Length&&rows[1].WordLength==texts[1].Length+1,"Floating picture anchor retains separate native Word span without adding a text unit"); Check(rows.Select(p=>p.NativeId).SequenceEqual(geometry.Select(p=>p.NativeId))&&rows[2].Text=="\r","Native paragraph identities and empty paragraphs survive structural framing"); var identity=ChapterFingerprint.Identity(rows);var hash=ChapterWordPositions.Hash(rows);var wordEnd=500+rows.Sum(ChapterWordPositions.Span); ChapterWordPositions.Validate(rows,500,wordEnd,hash); Check(ChapterFingerprint.Content(rows.Select(p=>p.Text!))==ChapterFingerprint.Content(texts),"Picture position evidence leaves exact manuscript content hashes unchanged"); var legacy=rows.Select(p=>new ChapterParagraph{NativeId=p.NativeId,Length=p.Length,Outline=p.Outline}).ToArray(); Check(ChapterFingerprint.Identity(legacy)==identity,"Existing text/identity checkpoints retain the same v1 identity digest"); Refused(()=>ChapterWordPositions.Validate(rows,500,wordEnd,""),"Physical Word spans cannot omit their geometry digest"); Refused(()=>ChapterWordPositions.Validate(rows,500,wordEnd-1,hash),"Text offsets cannot be substituted for native chapter end positions"); var tampered=ChapterWordPositions.Decode(frames);tampered[1].Anchors[0].Offset=1; Refused(()=>ChapterWordPositions.Validate(tampered,500,wordEnd,hash),"Changing a native anchor position invalidates the geometry digest"); var bad=ChapterWordPositions.Decode(frames);bad[1].Anchors=[]; Refused(()=>ChapterWordPositions.Hash(bad),"A span/text mismatch needs exact picture evidence, not padding"); bad=ChapterWordPositions.Decode(frames);bad[1].Anchors[0].ShapeType=6; Refused(()=>ChapterWordPositions.Hash(bad),"Grouped objects are not silently certified as floating pictures"); bad=ChapterWordPositions.Decode(frames);bad[1].Anchors[0].Offset=bad[1].WordLength; Refused(()=>ChapterWordPositions.Hash(bad),"Out-of-paragraph anchors are rejected"); bad=ChapterWordPositions.Decode(frames);bad[1].WordLength++;bad[1].Anchors=[bad[1].Anchors[0],new(){Offset=0,ShapeId=18,ShapeType=13}]; Refused(()=>ChapterWordPositions.Hash(bad),"Duplicate native anchor positions cannot account for extra Word positions"); var occurrences=new List();var start=0; foreach(var row in rows){occurrences.Add(new(){NativeId=row.NativeId,Text=row.Text!,Outline=row.Outline,Ordinal=occurrences.Count,Start=start,End=start+row.WordLength});start+=row.WordLength;} var state=ParagraphManuscriptEngine.Bootstrap(12,7,Guid.NewGuid(),occurrences,[new(){ChapterId=10,SceneId=22,Start=2,End=3,Title="Imported scene"}]);state.Revision=3; var proof=new ChapterObservation{RequestId=Guid.NewGuid(),SessionId=Guid.NewGuid(),InstanceId=Guid.NewGuid(),ManuscriptId=state.ManuscriptId,BaseRevision=3,ChapterId=10,Start=500,End=wordEnd,ContentHash=ChapterFingerprint.Content(texts),IdentityHash=identity,PositionHash=hash,Paragraphs=rows}; var result=ChapterCheckpointEngine.Validate(state,proof,DateTime.UtcNow); Check(result.Positions[1].End-result.Positions[1].Start==rows[1].WordLength&&result.Positions[2].Start==result.Positions[1].End&&result.Positions[^1].End==wordEnd,"Checkpoint positions use measured Word spans through and after a zero-text anchor"); Check(result.Positions[1].SceneId==22&&result.Checkpoint.PositionHash==hash,"Structural acceptance preserves the imported scene identity and persists geometry evidence"); var before=state.Paragraphs.Select(p=>(p.Occurrence.Id,p.Occurrence.Text,p.SceneId,p.ChapterId)).ToArray(); state.Paragraphs[2].Story=false;state.Paragraphs[2].SceneId=null; var backMatter=ChapterCheckpointEngine.Validate(state,proof,DateTime.UtcNow); Check(backMatter.Positions[2].SceneId==null&&state.Paragraphs.Take(2).Select(p=>(p.Occurrence.Id,p.Occurrence.Text,p.SceneId,p.ChapterId)).SequenceEqual(before.Take(2)),"Imported non-story back matter is not silently assigned to the final scene"); var plainXml=xml.Replace("",""); Check(ChapterFingerprint.ReadXml(plainXml,string.Concat(texts)).All(p=>p.WordLength==0),"Ordinary text-only chapters retain the existing identity and position path"); var inlineXml=xml.Replace("",""); var inlineText="Letter \u0001text\r"; var inlineRows=ChapterWordPositions.Decode(frames);inlineRows[1].Length=inlineRows[1].WordLength=inlineText.Length;inlineRows[1].Anchors=[]; Check(ChapterFingerprint.ReadXml(inlineXml,texts[0]+inlineText+texts[2],null,true,inlineRows)[1].Text==inlineText,"Inline QR/signature image retains its native object character as text, not as a floating gap"); var mixedXml=xml.Replace("Letter text","Letter text"); var mixedRows=ChapterWordPositions.Decode(frames);mixedRows[1].Length=inlineText.Length;mixedRows[1].WordLength=inlineText.Length+1; Check(ChapterFingerprint.ReadXml(mixedXml,texts[0]+inlineText+texts[2],null,true,mixedRows)[1].WordLength==inlineText.Length+1,"Mixed inline QR and floating signature preserve both actual text units and zero-text Word positions"); var emptyPictureRows=ChapterWordPositions.Decode(frames);emptyPictureRows[1].Length=1;emptyPictureRows[1].WordLength=2; var emptyPictureXml=xml.Replace("Letter text",""); Check(ChapterFingerprint.ReadXml(emptyPictureXml,texts[0]+"\r"+texts[2],null,true,emptyPictureRows)[1].Text=="\r","A picture anchored to an otherwise empty paragraph preserves the paragraph terminator and native anchor position"); var section=prefix+"Text"+suffix; var sectionText="Text\f\r";var sectionIds="00000001"+sectionText.Length.ToString("X8"); Check(ChapterFingerprint.ReadXml(section,sectionText,sectionIds,true).Single().Text==sectionText,"Section break text comes from exact native paragraph evidence rather than XML projection"); Refused(()=>ChapterFingerprint.ReadXml(section,sectionText,sectionIds),"Section breaks cannot bypass native text certification"); var visualHeading=xml.Replace("","Author letter"); var headingText="Author letter\r";var visualRows=ChapterWordPositions.Decode(frames);visualRows[2].Length=visualRows[2].WordLength=headingText.Length; Check(ChapterFingerprint.ReadXml(visualHeading,texts[0]+texts[1]+headingText,null,true,visualRows)[2].Outline==10,"A visually formatted non-Heading-1 letter does not manufacture a chapter boundary"); Refused(()=>ChapterFingerprint.ReadXml(xml.Replace("",""),string.Concat(texts),null,true,geometry),"Text boxes remain explicitly unsupported even with native picture metadata"); Refused(()=>ChapterFingerprint.ReadXml(xml.Replace("Letter text","Letter text"),string.Concat(texts),null,true,geometry),"Fields do not acquire picture-anchor authority"); } }