-- Run only against a disposable fixture owned by the named audit account. Never selects a real author's ID. IF DB_NAME()<>N'PlotDirector_Development' THROW 51000,'Wrong database',1; DECLARE @u int=(SELECT UserID FROM dbo.AppUser WHERE Email=N'pwa-browser-writeraudit-20261116@example.invalid' AND DisplayName=N'Disposable Writer Audit 20261116'); DECLARE @id uniqueidentifier,@book int,@project int; SELECT TOP(1) @id=ID,@book=BookID,@project=ProjectID FROM dbo.WriterInvestigations WHERE UserID=@u ORDER BY CreatedUtc DESC; IF @id IS NULL THROW 51000,'Disposable investigation fixture required',1; DECLARE @before varbinary(32)=HASHBYTES('SHA2_256',(SELECT Json FROM dbo.WriterInvestigations WHERE ID=@id)); DECLARE @events varbinary(32)=HASHBYTES('SHA2_256',(SELECT Event FROM dbo.WriterInvestigationEvents WHERE InvestigationID=@id ORDER BY Version FOR JSON PATH)); DECLARE @r TABLE(ID uniqueidentifier,ProjectID int,BookID int,BookTitle nvarchar(500),InvestigationJson nvarchar(max),EvaluationJson nvarchar(max),GuidanceJson nvarchar(max),GuidanceVersion int,IsActive bit,Finished bit); INSERT @r EXEC dbo.WriterInvestigationGuidanceState @Action='read',@UserID=@u,@BookID=@book,@ID=@id; IF (SELECT COUNT(*) FROM @r)<>1 OR NOT EXISTS(SELECT 1 FROM @r WHERE JSON_QUERY(EvaluationJson,'$.Recommendations') IS NOT NULL AND JSON_QUERY(InvestigationJson,'$.Presented') IS NULL) THROW 51000,'Compact saved evaluation unavailable',1; PRINT 'PASS compact canonical snapshot projection'; DELETE @r; INSERT @r EXEC dbo.WriterInvestigationGuidanceState @Action='read',@UserID=-123456,@BookID=@book,@ID=@id; IF EXISTS(SELECT 1 FROM @r) THROW 51000,'Cross-author leakage',1; INSERT @r EXEC dbo.WriterInvestigationGuidanceState @Action='read',@UserID=@u,@BookID=-1,@ID=@id; IF EXISTS(SELECT 1 FROM @r) THROW 51000,'Cross-book leakage',1; DECLARE @member int=(SELECT UserID FROM dbo.AppUser WHERE Email=N'pwa-phase15-other@example.invalid' AND DisplayName=N'Disposable Phase15 Other'); IF @member IS NOT NULL BEGIN INSERT @r EXEC dbo.WriterInvestigationGuidanceState @Action='read',@UserID=@member,@BookID=@book,@ID=@id; IF EXISTS(SELECT 1 FROM @r) THROW 51000,'Shared project member saw another author investigation',1; END; PRINT 'PASS real SQL author and book isolation, including shared project owners'; DECLARE @v int=ISNULL((SELECT Version FROM dbo.WriterInvestigationGuidance WHERE InvestigationID=@id),0); EXEC dbo.WriterInvestigationGuidanceState @Action='activate',@UserID=@u,@BookID=@book,@ID=@id,@ExpectedVersion=@v,@Json=N'{"Finding":"disconnected","Capability":"threads","Progress":{"disconnected/threads":"Done"}}'; SET @v+=1; DECLARE @rejected bit=0; BEGIN TRY EXEC dbo.WriterInvestigationGuidanceState @Action='save',@UserID=@u,@BookID=@book,@ID=@id,@ExpectedVersion=0,@Json=N'{}'; END TRY BEGIN CATCH IF ERROR_NUMBER()=51002 SET @rejected=1;ELSE THROW;END CATCH; IF @rejected<>1 THROW 51000,'Stale write accepted',1; IF NOT EXISTS(SELECT 1 FROM dbo.WriterInvestigationGuidance WHERE InvestigationID=@id AND JSON_VALUE(Json,'$.Progress."disconnected/threads"')='Done' AND Version=@v) THROW 51000,'Progress lost after stale write',1; PRINT 'PASS explicit progress durability and optimistic concurrency'; SET @rejected=0; BEGIN TRY EXEC dbo.WriterInvestigationGuidanceState @Action='activate',@UserID=-123456,@BookID=@book,@ID=@id,@ExpectedVersion=0,@Json=N'{}'; END TRY BEGIN CATCH IF ERROR_NUMBER()=51000 SET @rejected=1;ELSE THROW;END CATCH; IF @rejected<>1 THROW 51000,'Cross-author mutation accepted',1; DECLARE @clone uniqueidentifier=NEWID(),@json nvarchar(max),@ev nvarchar(max),@catalogue varchar(80); SELECT @json=JSON_MODIFY(Json,'$.ID',CONVERT(nvarchar(36),@clone)),@catalogue=CatalogueVersion FROM dbo.WriterInvestigations WHERE ID=@id; SELECT @ev=JSON_MODIFY(Event,'$.state.ID',CONVERT(nvarchar(36),@clone)) FROM dbo.WriterInvestigationEvents WHERE InvestigationID=@id AND Version=1; EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@book,@ProjectID=@project,@ID=@clone,@CatalogueVersion=@catalogue,@ExpectedVersion=0,@Json=@json,@Event=@ev; EXEC dbo.WriterInvestigationGuidanceState @Action='activate',@UserID=@u,@BookID=@book,@ID=@clone,@ExpectedVersion=0,@Json=N'{}'; IF (SELECT COUNT(*) FROM dbo.WriterInvestigationGuidance WHERE UserID=@u AND BookID=@book AND IsActive=1)<>1 OR EXISTS(SELECT 1 FROM dbo.WriterInvestigationGuidance WHERE InvestigationID=@id AND IsActive=1) THROW 51000,'Active guidance not exclusive',1; PRINT 'PASS explicit switching retains previous progress and has one active investigation per book'; DELETE dbo.WriterInvestigations WHERE ID=@clone AND UserID=@u; IF EXISTS(SELECT 1 FROM dbo.WriterInvestigationGuidance WHERE InvestigationID=@clone) THROW 51000,'Guidance cascade failed',1; PRINT 'PASS canonical investigation deletion cascades private guidance'; EXEC dbo.WriterInvestigationGuidanceState @Action='activate',@UserID=@u,@BookID=@book,@ID=@id,@ExpectedVersion=@v,@Json=N'{"Finding":"disconnected","Capability":"threads","Progress":{"disconnected/threads":"Done"}}';SET @v+=1; EXEC dbo.WriterInvestigationGuidanceState @Action='finish',@UserID=@u,@BookID=@book,@ID=@id,@ExpectedVersion=@v,@Json=N'{"Finding":"disconnected","Capability":"threads","Progress":{"disconnected/threads":"Done"}}'; IF NOT EXISTS(SELECT 1 FROM dbo.WriterInvestigationGuidance WHERE InvestigationID=@id AND IsActive=0 AND Finished=1) THROW 51000,'Finish failed',1; IF @before<>HASHBYTES('SHA2_256',(SELECT Json FROM dbo.WriterInvestigations WHERE ID=@id)) OR @events<>HASHBYTES('SHA2_256',(SELECT Event FROM dbo.WriterInvestigationEvents WHERE InvestigationID=@id ORDER BY Version FOR JSON PATH)) THROW 51000,'Canonical investigation modified',1; PRINT 'PASS lifecycle leaves diagnostic JSON and questionnaire events unchanged'; SELECT 'PASS real SQL investigation companion qualification' Result;