PlotDirector/PlotLine.Tests/Sql/ExceptionReview.sql

81 lines
7.8 KiB
Transact-SQL

-- Run against DEVELOPMENT only, after migration 200. Every fixture write rolls back.
SET NOCOUNT ON;
SET XACT_ABORT ON;
SET QUOTED_IDENTIFIER ON;
IF DB_NAME()<>N'PlotDirector_Development' THROW 53900,'Development test only.',1;
IF COL_LENGTH('dbo.StoryIntelligenceReviewCandidates','ReviewPriority') IS NULL THROW 53901,'Migration missing.',1;
BEGIN TRANSACTION;
DECLARE @Book int,@Project int,@Scene int,@A int,@B int,@Generation int,@Candidate int,@Canonical int,
@Key nvarchar(450)=CONCAT(N'regression-',NEWID()),@Payload nvarchar(max),@Assessment nvarchar(max)=N'{"priority":"Tracked","reasons":["Regression evidence"],"version":1}';
SELECT TOP(1) @Book=b.BookID,@Project=b.ProjectID,@Scene=s.SceneID FROM dbo.Books b
JOIN dbo.Chapters ch ON ch.BookID=b.BookID JOIN dbo.Scenes s ON s.ChapterID=ch.ChapterID
WHERE b.IsArchived=0 AND ch.IsArchived=0 AND s.IsArchived=0
AND (SELECT COUNT(*) FROM dbo.Characters c WHERE c.ProjectID=b.ProjectID AND c.IsArchived=0)>=2 ORDER BY b.BookID,s.SceneID;
SELECT @A=MIN(CharacterID),@B=MAX(CharacterID) FROM dbo.Characters WHERE ProjectID=@Project AND IsArchived=0;
IF @A IS NULL OR @A=@B THROW 53902,'Test needs two characters and a scene.',1;
UPDATE dbo.StoryIntelligenceReviewCandidateGenerations SET IsCurrent=0 WHERE BookID=@Book AND Module=N'Knowledge';
INSERT dbo.StoryIntelligenceReviewCandidateGenerations(ProjectID,BookID,Module,Status,Source) VALUES(@Project,@Book,N'Knowledge',N'Ready',N'Regression');
SET @Generation=CONVERT(int,SCOPE_IDENTITY());
DECLARE @KnowledgeState int=(SELECT TOP(1) KnowledgeStateID FROM dbo.KnowledgeStates WHERE StateName=N'Knows');
SET @Payload=(SELECT @Key [key],@A characterID,@KnowledgeState knowledgeStateID,N'Regression fact' knowledgeStatement,N'Regression evidence' evidence,
JSON_QUERY((SELECT @Scene sceneID,1 chapterNumber,1 sceneNumber,1 sortOrder FOR JSON PATH)) observations FOR JSON PATH,WITHOUT_ARRAY_WRAPPER);
INSERT dbo.StoryIntelligenceReviewCandidates(ReviewCandidateGenerationID,ProjectID,BookID,Module,StableKey,DisplayName,CandidatePayloadJson)
VALUES(@Generation,@Project,@Book,N'Knowledge',@Key,N'Regression fact',@Payload);
SET @Candidate=CONVERT(int,SCOPE_IDENTITY());
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
SELECT @Canonical=DecisionCanonicalID FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateID=@Candidate AND DecisionStatus=N'AutoAccepted';
IF @Canonical IS NULL OR NOT EXISTS(SELECT 1 FROM dbo.CharacterKnowledge WHERE CharacterKnowledgeID=@Canonical AND SceneID=@Scene AND CharacterID=@A AND KnowledgeStateID=@KnowledgeState)
THROW 53903,'Tracked knowledge missing from canonical story state.',1;
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
IF (SELECT COUNT(*) FROM dbo.CharacterKnowledge WHERE SourceReviewKey=@Key)<>1 THROW 53904,'Retry duplicated knowledge.',1;
-- Explicit reviewed/ignored rows cannot be reassessed.
UPDATE dbo.StoryIntelligenceReviewCandidates SET DecisionStatus=N'Reviewed',DecisionAction=N'Ignore',DecisionCanonicalID=NULL WHERE ReviewCandidateID=@Candidate;
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
IF NOT EXISTS(SELECT 1 FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateID=@Candidate AND DecisionStatus=N'Reviewed' AND DecisionAction=N'Ignore') THROW 53905,'Author decision overwritten.',1;
-- A regenerated candidate inherits the prior author's decision instead of reaccepting it.
UPDATE dbo.StoryIntelligenceReviewCandidateGenerations SET IsCurrent=0 WHERE ReviewCandidateGenerationID=@Generation;
INSERT dbo.StoryIntelligenceReviewCandidateGenerations(ProjectID,BookID,Module,Status,Source) VALUES(@Project,@Book,N'Knowledge',N'Ready',N'Regression');
SET @Generation=CONVERT(int,SCOPE_IDENTITY());
INSERT dbo.StoryIntelligenceReviewCandidates(ReviewCandidateGenerationID,ProjectID,BookID,Module,StableKey,DisplayName,CandidatePayloadJson)
VALUES(@Generation,@Project,@Book,N'Knowledge',@Key,N'Regression fact',@Payload);
SET @Candidate=CONVERT(int,SCOPE_IDENTITY());
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
IF NOT EXISTS(SELECT 1 FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateID=@Candidate AND DecisionAction=N'Ignore') THROW 53906,'Prior generation decision lost.',1;
UPDATE dbo.StoryIntelligenceReviewCandidateGenerations SET IsCurrent=0 WHERE BookID=@Book AND Module=N'Relationships';
INSERT dbo.StoryIntelligenceReviewCandidateGenerations(ProjectID,BookID,Module,Status,Source) VALUES(@Project,@Book,N'Relationships',N'Ready',N'Regression');
SET @Generation=CONVERT(int,SCOPE_IDENTITY());
DECLARE @Type int=(SELECT TOP(1) RelationshipTypeID FROM dbo.RelationshipTypes WHERE TypeName=N'Other'),
@State int=(SELECT TOP(1) RelationshipStateID FROM dbo.RelationshipStates WHERE StateName=N'Friendly'),@Direction int=0,@RKey nvarchar(450),@FirstID int;
WHILE @Direction<2
BEGIN
SET @RKey=CONCAT(@Key,N'-r-',@Direction);
SET @Payload=(SELECT @RKey [key],CASE WHEN @Direction=0 THEN @A ELSE @B END characterAID,CASE WHEN @Direction=0 THEN @B ELSE @A END characterBID,
@Scene firstSceneID,@Type relationshipTypeID,@State relationshipStateID,N'Development' proposalKind,
CAST(0 AS bit) isReciprocal,CAST(0 AS bit) isKnownToOtherCharacter,N'Regression development' development FOR JSON PATH,WITHOUT_ARRAY_WRAPPER);
INSERT dbo.StoryIntelligenceReviewCandidates(ReviewCandidateGenerationID,ProjectID,BookID,Module,StableKey,DisplayName,CandidatePayloadJson)
VALUES(@Generation,@Project,@Book,N'Relationships',@RKey,N'Regression pair',@Payload);
SET @Candidate=CONVERT(int,SCOPE_IDENTITY());
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
EXEC dbo.StoryIntelligenceReview_Assess @Candidate,@Generation,@Payload,@Assessment;
SELECT @Canonical=DecisionCanonicalID FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateID=@Candidate;
IF (SELECT COUNT(*) FROM dbo.RelationshipEvents WHERE SourceReviewKey=@RKey AND SceneID=@Scene)<>1 THROW 53907,'Relationship scene evidence lost or duplicated.',1;
IF @Direction=0 SET @FirstID=@Canonical;
ELSE IF @FirstID=@Canonical THROW 53908,'Directional tracks flattened.',1;
SET @Direction+=1;
END;
DECLARE @Summary TABLE(Module nvarchar(40),IdentifiedCount int,TrackedCount int,OptionalCount int,AttentionCount int,UnassessedCount int,DecidedCount int,PairCount int);
INSERT @Summary EXEC dbo.StoryIntelligenceReview_Summary @Book;
IF NOT EXISTS(SELECT 1 FROM @Summary WHERE Module=N'Relationships' AND IdentifiedCount=2 AND TrackedCount=2 AND AttentionCount=0 AND PairCount=1)
THROW 53909,'Summary confuses extraction with attention.',1;
-- Repreparation retains settled observations, and a save cannot overwrite their payload.
EXEC dbo.StoryIntelligenceReviewCandidateGeneration_Begin @Project,@Book,N'Relationships',N'Regression';
SELECT @Generation=ReviewCandidateGenerationID FROM dbo.StoryIntelligenceReviewCandidateGenerations WHERE BookID=@Book AND Module=N'Relationships' AND IsCurrent=1;
IF (SELECT COUNT(*) FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateGenerationID=@Generation AND DecisionStatus=N'AutoAccepted')<>2
THROW 53910,'Settled observations disappeared during preparation.',1;
SET @Payload=JSON_MODIFY(@Payload,'$.development',N'Overwritten evidence');
EXEC dbo.StoryIntelligenceReviewCandidate_Save @ReviewCandidateGenerationID=@Generation,@ProjectID=@Project,@BookID=@Book,@Module=N'Relationships',@StableKey=@RKey,@DisplayName=N'Regression pair',@SortOrder=1,@CandidatePayloadJson=@Payload;
IF EXISTS(SELECT 1 FROM dbo.StoryIntelligenceReviewCandidates WHERE ReviewCandidateGenerationID=@Generation AND JSON_VALUE(CandidatePayloadJson,'$.development')=N'Overwritten evidence')
THROW 53911,'Settled evidence was overwritten.',1;
ROLLBACK;
SELECT N'PASS: canonical writes, retry safety, direction, scene events, decisions, generation preservation and counts; all fixtures rolled back.';