54 lines
6.1 KiB
C#
54 lines
6.1 KiB
C#
using System.Text.Json.Nodes;
|
|
using Dapper;
|
|
using Microsoft.Data.SqlClient;
|
|
using PlotLine.Models;
|
|
|
|
internal static class ProjectTransferIllustrationAssertions
|
|
{
|
|
private static void Check(bool condition,string message){if(!condition)throw new InvalidOperationException(message);}
|
|
private static async Task GuardAsync(SqlConnection db)=>Check(await db.ExecuteScalarAsync<string>("SELECT DB_NAME()") == "PlotDirector_Phase2_20261103_2305" && await db.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM sys.extended_properties WHERE class=0 AND name=N'PlotDirector.Phase2.SyntheticOnly' AND CONVERT(nvarchar(20),value)=N'true'")==1,"Illustration assertions require synthetic database.");
|
|
internal static async Task<string> SharedStateAsync(SqlConnection db)
|
|
{
|
|
await GuardAsync(db);
|
|
return await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.IllustrationLibraryItems ORDER BY IllustrationLibraryItemID FOR JSON PATH,INCLUDE_NULL_VALUES")
|
|
+ await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.StoryIntelligenceIllustrationDemands WHERE ProjectID IS NULL ORDER BY StoryIntelligenceIllustrationDemandID FOR JSON PATH,INCLUDE_NULL_VALUES");
|
|
}
|
|
internal static async Task VerifyAsync(SqlConnection db,ProjectBackupExport before,ProjectBackupExport after)
|
|
{
|
|
await GuardAsync(db);
|
|
Check(before.Data.StoryMemoryIllustrationAssignments.Count==4 && before.Data.StoryMemoryIllustrationDemands.Count==3 && before.Data.StoryIntelligenceIllustrationDemands.Count==1,"Source contains active/waiting/invalidated assignments, three demand types and only project-scoped requests.");
|
|
Check(after.Data.StoryMemoryIllustrationAssignments.Count==4 && after.Data.StoryMemoryIllustrationAssignments.All(x=>x.InvalidatedUtc.HasValue && x.IllustrationLibraryItemID is null),"Copied assignments retain history without active/shared catalogue authority.");
|
|
Check(after.Data.StoryMemoryIllustrationAssignments.Where(x=>x.InvalidationReason!.StartsWith("Imported as inactive")).Count()==3 && after.Data.StoryMemoryIllustrationAssignments.Single(x=>x.EvidenceVersion=="21R").InvalidationReason=="Author invalidated this artwork 雪","New inactivity is explicit; original invalidation history survives.");
|
|
Check(after.Data.StoryMemoryIllustrationDemands.Count==3 && after.Data.StoryMemoryIllustrationDemands.All(x=>x.QueuedIllustrationLibraryItemID is null && x.GeneratedIllustrationLibraryItemID is null),"Copied demand history cannot attach another author's private/queued catalogue rows.");
|
|
foreach(var assignment in after.Data.StoryMemoryIllustrationAssignments)
|
|
{
|
|
var exists=assignment.EntityType switch {"Character"=>after.Data.StoryMemoryCharacters.Any(x=>x.StoryMemoryCharacterID==assignment.EntityID && x.ImportSessionID==assignment.ImportSessionID),"Location"=>after.Data.StoryMemoryLocations.Any(x=>x.StoryMemoryLocationID==assignment.EntityID && x.ImportSessionID==assignment.ImportSessionID),_=>after.Data.StoryMemoryAssets.Any(x=>x.StoryMemoryAssetID==assignment.EntityID && x.ImportSessionID==assignment.ImportSessionID)};
|
|
Check(exists,"Illustration assignment follows typed memory entity/pipeline identity.");
|
|
}
|
|
Check(await db.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM dbo.IllustrationLibraryItems WHERE SourceProjectID=@id",new{id=after.Project!.ProjectID})==0,"Copy never creates/reapproves shared catalogue rows.");
|
|
}
|
|
internal static async Task VerifyRestoreGuardsAsync(SqlConnection db,ProjectBackupExport before,string json,int project)
|
|
{
|
|
await GuardAsync(db);
|
|
var sourceLibrary=await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.IllustrationLibraryItems WHERE SourceProjectID=@project ORDER BY IllustrationLibraryItemID FOR JSON PATH,INCLUDE_NULL_VALUES",new{project});
|
|
var globalDemand=await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.StoryIntelligenceIllustrationDemands WHERE ProjectID IS NULL ORDER BY StoryIntelligenceIllustrationDemandID FOR JSON PATH,INCLUDE_NULL_VALUES");
|
|
foreach(var table in new[]{"StoryMemoryIllustrationAssignments","StoryMemoryIllustrationDemands","StoryIntelligenceIllustrationDemands"})
|
|
{
|
|
var old=JsonNode.Parse(json)!;old["Data"]!.AsObject().Remove(table);
|
|
try{await db.ExecuteAsync("dbo.ProjectRestore_ApplyFromJson",new{ProjectID=project,JsonData=old.ToJsonString()},commandType:System.Data.CommandType.StoredProcedure);throw new InvalidOperationException("Missing illustration collection accepted: "+table);}
|
|
catch(SqlException ex) when(ex.Number==51023){}
|
|
}
|
|
var item=before.Data.StoryMemoryIllustrationAssignments.First(x=>x.InvalidatedUtc is null && x.IllustrationLibraryItemID.HasValue).IllustrationLibraryItemID!.Value;
|
|
try
|
|
{
|
|
await db.ExecuteAsync("UPDATE dbo.IllustrationLibraryItems SET Status=N'Generating' WHERE IllustrationLibraryItemID=@item",new{item});
|
|
try{await db.ExecuteAsync("dbo.ProjectRestore_ApplyFromJson",new{ProjectID=project,JsonData=json},commandType:System.Data.CommandType.StoredProcedure);throw new InvalidOperationException("Active illustration generation accepted.");}
|
|
catch(SqlException ex) when(ex.Number==51024){}
|
|
}
|
|
finally{await db.ExecuteAsync("UPDATE dbo.IllustrationLibraryItems SET Status=N'Approved' WHERE IllustrationLibraryItemID=@item",new{item});}
|
|
Check(sourceLibrary==await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.IllustrationLibraryItems WHERE SourceProjectID=@project ORDER BY IllustrationLibraryItemID FOR JSON PATH,INCLUDE_NULL_VALUES",new{project}),"Refused restore never mutates shared catalogue approval/provenance.");
|
|
Check(globalDemand==await db.ExecuteScalarAsync<string>("SELECT * FROM dbo.StoryIntelligenceIllustrationDemands WHERE ProjectID IS NULL ORDER BY StoryIntelligenceIllustrationDemandID FOR JSON PATH,INCLUDE_NULL_VALUES"),"Global illustration demand remains unchanged.");
|
|
Console.WriteLine("PASS illustration history: typed memory copy/remap; inactive copied assignments; no catalogue authority; three omission guards and active generation refusal.");
|
|
}
|
|
}
|