-- Disposable synthetic database only. New author/book fixture is always rolled back. IF DB_NAME()<>N'PlotDirector_Phase2_20261103_2305' OR NOT EXISTS(SELECT 1 FROM sys.extended_properties WHERE class=0 AND name=N'PlotDirector.Phase2.SyntheticOnly' AND CONVERT(nvarchar(20),value)=N'true') THROW 51000,'Guarded synthetic target required',1; SET XACT_ABORT OFF; BEGIN TRANSACTION; DECLARE @Level int,@User int,@Other int,@Project int,@Book int,@Preview uniqueidentifier=NEWID(); INSERT dbo.AppUser(Email,DisplayName,PasswordHash) VALUES(CONVERT(nvarchar(36),NEWID())+N'@capture.invalid',N'Capture fixture',N'not-a-credential');SET @User=SCOPE_IDENTITY(); INSERT dbo.AppUser(Email,DisplayName,PasswordHash) VALUES(CONVERT(nvarchar(36),NEWID())+N'@capture.invalid',N'Other capture fixture',N'not-a-credential');SET @Other=SCOPE_IDENTITY(); INSERT dbo.SubscriptionLevel(Name,DisplayName,MaxBooks,MaxChaptersPerBook,MaxScenesPerBook,MaxCharactersPerProject,MaxStorageMB,MaxCollaborators,SortOrder,RequiresStripeSubscription) VALUES(CONVERT(nvarchar(36),NEWID()),N'Capture fixture',2,100,100,100,100,0,0,0);SET @Level=SCOPE_IDENTITY(); INSERT dbo.UserSubscription(UserID,SubscriptionLevelID,IsActive,IsTrial,TrialExpiryDateUTC) VALUES(@User,@Level,1,1,'2030-01-01'); INSERT dbo.Projects(ProjectName) VALUES(N'Native capture fixture');SET @Project=SCOPE_IDENTITY(); INSERT dbo.ProjectUserAccess(ProjectID,UserID,AccessRole,IsActive) VALUES(@Project,@User,N'Owner',1); INSERT dbo.Books(ProjectID,BookTitle,BookNumber,SortOrder) VALUES(@Project,N'Native capture fixture',1,1);SET @Book=SCOPE_IDENTITY(); DECLARE @Payload nvarchar(max)=N'{"Chapters":[{"ChapterText":"Exact prose.\r\nSecond line."}],"NativeRevision":3}'; EXEC dbo.OnboardingManuscriptCapture @Action='save',@UserID=@User,@PreviewID=@Preview,@OnboardingID=4,@BookID=@Book,@Payload=@Payload; EXEC dbo.OnboardingManuscriptCapture @Action='save',@UserID=@User,@PreviewID=@Preview,@OnboardingID=4,@BookID=@Book,@Payload=@Payload; IF (SELECT COUNT(*) FROM dbo.OnboardingManuscriptCaptures WHERE PreviewID=@Preview)<>1 THROW 51000,'Duplicate capture',1; CREATE TABLE #Recovered(Payload nvarchar(max)); INSERT #Recovered EXEC dbo.OnboardingManuscriptCapture @Action='get',@UserID=@User,@PreviewID=@Preview; IF NOT EXISTS(SELECT 1 FROM #Recovered WHERE Payload=@Payload) THROW 51000,'Prose was not recovered exactly',1; TRUNCATE TABLE #Recovered; INSERT #Recovered EXEC dbo.OnboardingManuscriptCapture @Action='get',@UserID=@Other,@PreviewID=@Preview; IF EXISTS(SELECT 1 FROM #Recovered) THROW 51000,'Cross-account capture leak',1; EXEC dbo.OnboardingManuscriptCapture @Action='clear',@UserID=@User,@OnboardingID=4; TRUNCATE TABLE #Recovered; INSERT #Recovered EXEC dbo.OnboardingManuscriptCapture @Action='get',@UserID=@User,@OnboardingID=4; IF EXISTS(SELECT 1 FROM #Recovered) THROW 51000,'Cleared wizard resurrected an older capture',1; INSERT #Recovered EXEC dbo.OnboardingManuscriptCapture @Action='get',@UserID=@User,@PreviewID=@Preview; IF NOT EXISTS(SELECT 1 FROM #Recovered) THROW 51000,'Explicit source recovery was destroyed by wizard reset',1; DELETE dbo.ProjectUserAccess WHERE ProjectID=@Project AND UserID=@User; TRUNCATE TABLE #Recovered; INSERT #Recovered EXEC dbo.OnboardingManuscriptCapture @Action='get',@UserID=@User,@PreviewID=@Preview; IF EXISTS(SELECT 1 FROM #Recovered) THROW 51000,'Revoked access still reads prose',1; DELETE dbo.Books WHERE BookID=@Book; IF EXISTS(SELECT 1 FROM dbo.OnboardingManuscriptCaptures WHERE PreviewID=@Preview) THROW 51000,'Book deletion left source prose behind',1; ROLLBACK; PRINT 'PASS durable exact prose, idempotency, account boundaries, revoked access and book-deletion cleanup; fixtures rolled back'; -- Invalid writes are separate rollback-safe probes because XACT_ABORT is enabled by the procedure. DECLARE @Denied bit=0; BEGIN TRY EXEC dbo.OnboardingManuscriptCapture @Action='save',@UserID=-1,@PreviewID=@Preview,@OnboardingID=4,@BookID=-1,@Payload=N'{}'; END TRY BEGIN CATCH SET @Denied=1;IF @@TRANCOUNT>0 ROLLBACK;END CATCH; IF @Denied=0 THROW 51000,'Inaccessible book accepted source',1; PRINT 'PASS unauthorised persistence refused without mutation';