PlotDirector/PlotLine.Tests/Sql/WriterInvestigationsRegression.sql

45 lines
4.3 KiB
Transact-SQL

-- Development only; migration273 and synthetic catalogue required. All test writes roll back.
IF DB_NAME()<>N'PlotDirector_Development' THROW 51000,'Wrong database',1;
DECLARE @u int=TRY_CONVERT(int,SESSION_CONTEXT(N'InvestigationTestUserID')),@b int=TRY_CONVERT(int,SESSION_CONTEXT(N'InvestigationTestBookID')),@p int=TRY_CONVERT(int,SESSION_CONTEXT(N'InvestigationTestProjectID')),@otherbook int=TRY_CONVERT(int,SESSION_CONTEXT(N'InvestigationTestOtherBookID'));
IF @u IS NULL OR @b IS NULL OR @p IS NULL OR @otherbook IS NULL THROW 51000,'Configure disposable fixture session context first',1;
IF NOT EXISTS(SELECT 1 FROM dbo.Projects p JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID WHERE p.ProjectID=@p AND p.ProjectName=N'Disposable Writer Audit 20261116' AND a.UserID=@u AND a.AccessRole=N'Owner') THROW 51000,'Disposable fixture guard failed',1;
DECLARE @id uniqueidentifier=NEWID(),@json nvarchar(max),@event nvarchar(max)=N'{"test":true}';
SET @json=(SELECT @id ID,@u UserID,@b BookID,@p ProjectID,'synthetic-3d1-v1' CatalogueVersion,1 Version,'story' Concern FOR JSON PATH,WITHOUT_ARRAY_WRAPPER);
BEGIN TRANSACTION;
EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@b,@ProjectID=@p,@ID=@id,@CatalogueVersion='synthetic-3d1-v1',@ExpectedVersion=0,@Json=@json,@Event=@event;
SET @json=JSON_MODIFY(@json,'$.Version',2);
EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@b,@ProjectID=@p,@ID=@id,@CatalogueVersion='synthetic-3d1-v1',@ExpectedVersion=1,@Json=@json,@Event=@event;
IF (SELECT COUNT(*) FROM dbo.WriterInvestigationEvents WHERE InvestigationID=@id)<>2 THROW 51001,'Event history missing',1;
IF NOT EXISTS(SELECT 1 FROM dbo.WriterInvestigations WHERE ID=@id AND Version=2 AND CatalogueVersion='synthetic-3d1-v1') THROW 51001,'Resume/version state lost',1;
EXEC dbo.WriterInvestigation @Action='read',@UserID=@u,@BookID=@b,@ID=@id;
ROLLBACK;
SELECT 'PASS persisted update/resume, immutable catalogue binding, atomic event history; rollback clean' Result;
BEGIN TRY
BEGIN TRANSACTION;
SET @json=JSON_MODIFY(@json,'$.Version',1);
EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@b,@ProjectID=@p,@ID=@id,@CatalogueVersion='synthetic-3d1-v1',@ExpectedVersion=0,@Json=@json,@Event=@event;
SET @json=JSON_MODIFY(@json,'$.Version',3);
EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@b,@ProjectID=@p,@ID=@id,@CatalogueVersion='synthetic-3d1-v1',@ExpectedVersion=2,@Json=@json,@Event=@event;
THROW 51001,'Stale save accepted',1;
END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK;IF ERROR_NUMBER()<>51002 THROW; END CATCH;
IF EXISTS(SELECT 1 FROM dbo.WriterInvestigations WHERE ID=@id) THROW 51001,'Stale rollback leaked',1;
SELECT 'PASS stale optimistic save rejected without leaked history' Result;
BEGIN TRY EXEC dbo.WriterInvestigation @Action='read',@UserID=@u,@BookID=@otherbook,@ID=@id;THROW 51001,'Cross-book read accepted',1;END TRY BEGIN CATCH IF ERROR_NUMBER()<>51000 THROW;END CATCH;
SELECT 'PASS cross-book identity denied' Result;
BEGIN TRY
BEGIN TRANSACTION;
EXEC dbo.User_Create @Email=N'investigation-other-disposable@example.invalid',@DisplayName=N'Disposable investigation SQL actor',@PasswordHash=N'not-a-login-hash',@EmailConfirmed=1;
DECLARE @otheruser int=(SELECT UserID FROM dbo.AppUser WHERE Email=N'investigation-other-disposable@example.invalid');
INSERT dbo.ProjectUserAccess(ProjectID,UserID,AccessRole,IsActive) VALUES(@p,@otheruser,N'Collaborator',1);
SET @json=JSON_MODIFY(@json,'$.Version',1);
EXEC dbo.WriterInvestigation @Action='save',@UserID=@u,@BookID=@b,@ProjectID=@p,@ID=@id,@CatalogueVersion='synthetic-3d1-v1',@ExpectedVersion=0,@Json=@json,@Event=@event;
EXEC dbo.WriterInvestigation @Action='read',@UserID=@otheruser,@BookID=@b,@ID=@id;
THROW 51001,'Other project member read private investigation',1;
END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK;IF ERROR_NUMBER()<>51000 THROW; END CATCH;
SELECT 'PASS private investigation denied to another active project member; rollback clean' Result;
BEGIN TRY
DECLARE @catalogue nvarchar(max)=(SELECT Json FROM dbo.WriterInvestigationCatalogues WHERE Version='synthetic-3d1-v1');
EXEC dbo.WriterInvestigationCatalogue @Action='publish',@Version='synthetic-3d1-v1',@Json=@catalogue,@Actor=N'test';THROW 51001,'Published version overwritten',1;
END TRY BEGIN CATCH IF ERROR_NUMBER()<>51002 THROW;END CATCH;
SELECT 'PASS catalogue version overwrite rejected' Result;