36 lines
4.2 KiB
C#
36 lines
4.2 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text.Json;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using PlotDirector.Companion.Protocol;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.Services.NativeCompanion;
|
|
internal static class NativeImportTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
void Check(bool value,string name){if(!value)throw new Exception(name);}
|
|
var reviews=new MemoryNativeReviews();var writes=new Imports();var access=NativeWorkflowTests.Proxy<IWordCompanionRepository>((n,a)=>n=="GetBookStructureAsync"?Task.FromResult((int)a![0]! ==3?new WordCompanionBookStructureResponse{BookId=3}:null):throw new Exception("Unexpected write"));
|
|
var allowed=true;var subscriptions=NativeWorkflowTests.Proxy<ISubscriptionService>((n,a)=>Task.FromResult(new EntitlementDecision{Allowed=allowed}));
|
|
var service=new NativeImportService(reviews,access,writes,new EphemeralDataProtectionProvider(),subscriptions);
|
|
var capture=new AiChapterRequest{RequestId=Guid.NewGuid(),BookId=3,ManuscriptId=Guid.NewGuid(),SessionId=Guid.NewGuid(),ChapterStart=10,ChapterEnd=30,Paragraphs=[new(){Index=1,Start=10,End=20,Text="First scene.\r"},new(){Index=2,Start=20,End=30,Text="Second scene.\r"}]};
|
|
var row=await reviews.Save(new(){ReviewID=capture.RequestId,UserID=7,BookID=3,ManuscriptID=capture.ManuscriptId,ChapterStart=10,ChapterEnd=30,ContentHash=Convert.ToHexString(SHA256.HashData(JsonSerializer.SerializeToUtf8Bytes(new{capture.ChapterStart,capture.ChapterEnd,capture.Paragraphs}))),EncryptedSource=service.Protect(capture),ResultJson=JsonSerializer.Serialize(new AiChapterResult{ChapterTitle="Opening",Scenes=[new(){Title="First",Start=10,End=20},new(){Title="Second",Start=20,End=30}]}),DecisionsJson=JsonSerializer.Serialize(new[]{new PlotLine.Controllers.NativeSceneDecision{Index=0,State="approved",Title="Edited title",Summary="Author-approved summary"},new PlotLine.Controllers.NativeSceneDecision{Index=1,State="rejected",Title="Second",Summary=""}})});
|
|
var preview=await service.Preview(7,3,[row.ReviewID]);Check(preview.Items.Single().Scenes.Single().Text=="First scene.\r"&&preview.Skipped==1&&preview.Items[0].Scenes[0].Title=="Edited title"&&writes.Count==0,"Preview uses encrypted exact prose and author decisions without writing");
|
|
var result=await service.Commit(7,preview.Ticket);Check(result.ChaptersCreated==1&&writes.Count==1,"Explicit confirmed plan reaches canonical import repository");
|
|
await service.Commit(7,preview.Ticket);Check(writes.Count==1,"Retry reuses operation receipt without another import");
|
|
var again=await service.Preview(7,3,[row.ReviewID]);Check(again.Items.Length==0&&again.Skipped==2,"Already-imported identical approved chapter matches and skips");
|
|
writes.Rows.Clear();writes.Receipts.Clear();var stale=await service.Preview(7,3,[row.ReviewID]);row.Version++;try{await service.Commit(7,stale.Ticket);throw new Exception("Stale decisions accepted");}catch(ArgumentException){}Check(writes.Count==1,"Changed review cannot commit an old confirmation");
|
|
allowed=false;try{await service.Preview(7,3,[row.ReviewID]);throw new Exception("Expired import entitlement accepted");}catch(UnauthorizedAccessException){}
|
|
try{await service.Commit(8,preview.Ticket);throw new Exception("Foreign confirmation accepted");}catch(UnauthorizedAccessException){}
|
|
Check(writes.Count==1,"Entitlement and cross-account checks preserve import data");
|
|
}
|
|
private sealed class Imports:INativeImportRepository
|
|
{
|
|
public int Count;public List<NativeImportedChapter> Rows=[];public Dictionary<Guid,NativeImportResult> Receipts=[];
|
|
public Task<IReadOnlyList<NativeImportedChapter>> List(int user,int book)=>Task.FromResult<IReadOnlyList<NativeImportedChapter>>(Rows.ToArray());
|
|
public Task<NativeImportResult?> Result(int user,Guid operation)=>Task.FromResult(Receipts.GetValueOrDefault(operation));
|
|
public Task<NativeImportResult> Commit(int user,Guid operation,NativeImportPlan plan){Count++;foreach(var item in plan.Items)Rows.Add(new(){ChapterKey=item.Key,ContentHash=item.Hash,ReviewVersion=item.Version});var result=new NativeImportResult{BuildID=1,ChaptersCreated=plan.Items.Length,ScenesCreated=plan.Items.Sum(i=>i.Scenes.Length)};Receipts[operation]=result;return Task.FromResult(result);}
|
|
}
|
|
}
|