using Dapper; using PlotLine.Data; using PlotLine.Models; internal static class ProjectTransferNarrativeAssertions { private static void Check(bool condition,string message){if(!condition)throw new InvalidOperationException(message);} internal static async Task VerifyCopyAsync(ProjectBackupExport before,ProjectBackupExport after,ISqlConnectionFactory factory,bool legacyNullRun=false) { var data=after.Data; Check(before.Data.WholeBookPlotSynthesisRuns.Count==2 && before.Data.StoryIntelligenceNarrativeCandidates.Count==6 && before.Data.StoryIntelligenceNarrativeCandidateEvidence.Count==6 && before.Data.StoryIntelligenceNarrativeCandidateEvents.Count==2 && before.Data.ThreadEvents.Count==3,"Backup includes completed/failed synthesis, every review state, ignored events and both accepted source forms."); var run=data.WholeBookPlotSynthesisRuns.Single(x=>x.Status=="Completed"); var explicitCandidate=data.StoryIntelligenceNarrativeCandidates.Single(x=>x.CandidateKey=="Thread:question-Ω"); var fallbackCandidate=data.StoryIntelligenceNarrativeCandidates.Single(x=>x.CandidateKey=="Thread:evidence-雨"); var plotCandidate=data.StoryIntelligenceNarrativeCandidates.Single(x=>x.CandidateType=="PlotLine"); var merged=data.StoryIntelligenceNarrativeCandidates.Single(x=>x.Status=="Merged"); var candidateEvent=data.StoryIntelligenceNarrativeCandidateEvents.Single(x=>x.EventKey=="arrival:Ω"); var explicitCanonical=data.ThreadEvents.Single(x=>x.EventTitle=="Accepted synthesis event Ω"); var fallbackCanonical=data.ThreadEvents.Single(x=>x.EventTitle=="Accepted evidence event 雨"); var evidence=data.StoryIntelligenceNarrativeCandidateEvidence.Single(x=>x.NarrativeCandidateID==fallbackCandidate.NarrativeCandidateID); int? linkedRun=legacyNullRun ? null : run.WholeBookPlotSynthesisRunID; Check(explicitCandidate.WholeBookPlotSynthesisRunID==linkedRun && fallbackCandidate.WholeBookPlotSynthesisRunID==linkedRun && candidateEvent.WholeBookPlotSynthesisRunID==linkedRun,"Narrative source run associations follow the copied graph."); Check(explicitCanonical.SourceReferenceID==candidateEvent.NarrativeCandidateEventID && explicitCanonical.SourceKey==$"{linkedRun}:{explicitCandidate.NarrativeCandidateID}:arrival:Ω" && candidateEvent.AcceptedThreadEventID==explicitCanonical.ThreadEventID,"Explicit narrative candidate-event/canonical provenance remaps in both directions."); Check(fallbackCanonical.SourceReferenceID==fallbackCandidate.NarrativeCandidateID && fallbackCanonical.SourceKey==$"{linkedRun}:{fallbackCandidate.NarrativeCandidateID}:{evidence.SceneID}:{evidence.SortOrder}","Evidence-only narrative source remaps candidate and original evidence scene identities."); Check(explicitCandidate.ParentNarrativeCandidateID==plotCandidate.NarrativeCandidateID && merged.MergeTargetNarrativeCandidateID==explicitCandidate.NarrativeCandidateID,"Narrative parent and merge targets retain their meaning."); Check(run.ResultJson==before.Data.WholeBookPlotSynthesisRuns.Single(x=>x.Status=="Completed").ResultJson,"Provider result JSON is retained exactly as audit, including original embedded source identifiers."); var reader=new StoryIntelligenceNarrativeCandidateRepository(factory); var records=await reader.ListByBookAsync(data.Books.Single().BookID); Check(records.Count==6 && records.Count(x=>x.Status=="Accepted")==3 && records.Any(x=>x.Status=="Pending") && records.Any(x=>x.Status=="Rejected") && records.Any(x=>x.Status=="Merged") && records.Sum(x=>x.Evidence.Count)==6 && records.Sum(x=>x.Events.Count)==2 && records.SelectMany(x=>x.Events).Any(x=>x.IsIgnored),"Production narrative reader retains all settled/pending states and ignored event evidence."); using var db=factory.CreateConnection(); Check(await db.ExecuteScalarAsync("SELECT DB_NAME()") == "PlotDirector_Phase2_20261103_2305" && await db.ExecuteScalarAsync("SELECT COUNT(*) FROM sys.extended_properties WHERE class=0 AND name=N'PlotDirector.Phase2.SyntheticOnly' AND CONVERT(nvarchar(20),value)=N'true'")==1,"Narrative replay is synthetic-only."); foreach(var item in new[]{explicitCanonical,fallbackCanonical})for(var attempt=0;attempt<2;attempt++) { // Same production procedure and metadata parameters used by narrative materialization. var id=await db.QuerySingleAsync("dbo.ThreadEvent_Save",new { ThreadEventID=0,item.PlotThreadID,item.SceneID,item.EventTypeID,item.PlotEventTypeID,item.TargetPlotLineID, item.EventTitle,item.EventDescription,item.Source,item.SourceReferenceID,item.SourceKey,item.Confidence },commandType:System.Data.CommandType.StoredProcedure); Check(id==item.ThreadEventID,"Copied narrative source keys remain idempotent through production event persistence."); } Check(await db.ExecuteScalarAsync("SELECT COUNT(*) FROM dbo.ThreadEvents WHERE PlotThreadID=@id",new{id=explicitCanonical.PlotThreadID})==3,"Replayed copied narrative sources create no duplicate canonical events."); } }