using System.Diagnostics; using PlotDirector.Companion.Client; using PlotDirector.Companion.Protocol.Manuscript; internal static class ChapterReopenTests { private static void Check(bool value,string name){if(!value)throw new Exception(name);Console.WriteLine("PASS "+name);} private static string Frames(IEnumerable rows)=>string.Concat(rows.Select(p=>p.NativeId+p.Length.ToString("X8")+p.Outline.ToString("X8"))); private static void Reject(Action action,string name){try{action();}catch(ArgumentException){Check(true,name);return;}throw new Exception(name);} public static void Run() { var chapters=new List();var id=1;var words=0; for(var chapter=0;chapter<48;chapter++) { var count=chapter==47?8457-47*176:176; chapters.Add(Enumerable.Range(0,count).Select(i=>{ var text=i==0?$"Chapter {chapter+1}\r":string.Join(" ",Enumerable.Repeat("synthetic",18+(i%5==0?1:0)))+"\r"; words+=text.Split(' ').Length; return new ChapterParagraph{NativeId=(id++).ToString("X8"),Text=text,Length=text.Length,Outline=i==0?1:10}; }).ToArray()); } var watch=Stopwatch.StartNew(); foreach(var rows in chapters) { var restored=ChapterFingerprint.ReadLiveText(string.Concat(rows.Select(p=>p.Text)),Frames(rows)); Check(ChapterFingerprint.Content(restored.Select(p=>p.Text!))==ChapterFingerprint.Content(rows.Select(p=>p.Text!))&&ChapterFingerprint.Identity(restored)==ChapterFingerprint.Identity(rows),"New-session live evidence retains existing v1 content and identities"); } Console.WriteLine($"PORTABLE live verification: 48 chapters, 8457 paragraphs, {words} words, {watch.Elapsed.TotalMilliseconds:F1} ms; excludes COM, network and UI."); var xmls=chapters.Select(rows=>""+string.Concat(rows.Select(p=>$"{(p.Outline==1?"":"")}{p.Text![..^1]}"))+"").ToArray(); watch.Restart();for(var i=0;i<48;i++){var rows=ChapterFingerprint.ReadXml(xmls[i],string.Concat(chapters[i].Select(p=>p.Text)));ChapterFingerprint.Content(rows.Select(p=>p.Text!));ChapterFingerprint.Identity(rows);} Console.WriteLine($"PORTABLE previous XML parse and fingerprints: {watch.Elapsed.TotalMilliseconds:F1} ms; excludes Word XML extraction and native identity enumeration."); var sample=chapters[0];var original=ChapterFingerprint.Content(sample.Select(p=>p.Text!)); foreach(var index in new[]{1,17,80}) { var edited=sample.Select(p=>p.Text!).ToArray();edited[index]="edited paragraph\r"; Check(ChapterFingerprint.Content(edited)!=original,"Saved/offline/unsaved text edits cannot produce unchanged content evidence"); } Check(ChapterFingerprint.Content(sample.Skip(1).Select(p=>p.Text!))!=original,"Deleted paragraph changes content evidence"); Check(ChapterFingerprint.Content(sample.Select(p=>p.Text!).Append("new scene\r"))!=original,"Inserted scene changes content evidence"); Check(ChapterFingerprint.Content(chapters[1].Select(p=>p.Text!))!=original,"Chapter rename/reorder is not filename-based identity"); var text=string.Concat(sample.Select(p=>p.Text));var frames=Frames(sample); Reject(()=>ChapterFingerprint.ReadLiveText(text,frames[..^1]),"Interrupted identity frames are refused"); Reject(()=>ChapterFingerprint.ReadLiveText(text,frames[..24]),"Missing paragraph evidence is refused"); Reject(()=>ChapterFingerprint.ReadLiveText(text,frames[..24]+frames[..24]+frames[48..]),"Duplicate native identities are refused"); Reject(()=>ChapterFingerprint.ReadLiveText(text,frames[..16]+"00000000"+frames[24..]),"Invalid native outline is refused"); Reject(()=>ChapterFingerprint.ReadLiveText(text+"x",frames),"Unaccounted native text is refused"); foreach(var editedChapters in new[]{new[]{7},new[]{0,19,47}}) { var changed=new List(); for(var c=0;c<48;c++) { var body=string.Concat(chapters[c].Select(p=>p.Text)); if(editedChapters.Contains(c))body=body.Replace("synthetic","different"); // equal UTF-16 lengths var evidence=ChapterFingerprint.ReadLiveText(body,Frames(chapters[c])); if(ChapterFingerprint.Content(evidence.Select(p=>p.Text!))!=ChapterFingerprint.Content(chapters[c].Select(p=>p.Text!)))changed.Add(c); } Check(changed.SequenceEqual(editedChapters),"Only genuinely edited chapters fail saved content fingerprints"); } var scheduler=new WordAccessScheduler();var steps=0;var pumps=0; Action? next=null;next=()=>{if(++steps<8457)scheduler.Post("chapter:fixture",next!);};scheduler.Post("chapter:fixture",next); watch.Restart();while(scheduler.Pump())pumps++; Check(steps==8457&&pumps==529,"Bounded chapter batching removes one timer tick per paragraph"); Console.WriteLine($"SCHEDULER MODEL: old 8457 ticks / 211425 ms minimum, new {pumps} ticks / {pumps*25} ms minimum; portable execution {watch.Elapsed.TotalMilliseconds:F1} ms. No COM latency simulated."); var selection=false;var continuation=false; scheduler.Post("chapter:first",()=>{scheduler.Post("selection",()=>selection=true);scheduler.Post("chapter:next",()=>continuation=true);}); scheduler.Pump();Check(!selection&&!continuation,"Chapter batching yields immediately to pending selection"); scheduler.Pump();Check(selection&&!continuation,"Selection executes before further chapter reads");scheduler.Clear(); scheduler.Post("chapter:first",()=>{scheduler.PrioritizeAuthor(TimeSpan.FromSeconds(1));scheduler.Post("chapter:next",()=>continuation=true);}); scheduler.Pump();Check(!scheduler.Pump()&&!continuation,"Author activity interrupts a chapter batch");scheduler.Clear(); var capped=new WordAccessScheduler();var reads=0;Action? repeat=null;repeat=()=>{reads++;capped.Post("chapter:bounded",repeat!);};capped.Post("chapter:bounded",repeat); capped.Pump();Check(reads<=16,"A timer tick cannot execute unbounded chapter continuations");capped.Clear(); } }