PlotDirector/PlotLine.Tests/IntentAssessmentTests.cs

39 lines
4.1 KiB
C#

using System.Text.Json;
using PlotLine.Services;
using PlotLine.Data;
using PlotLine.Models;
using PlotLine.ViewModels;
internal static class IntentAssessmentTests
{
public static async Task Run()
{
void Check(bool value,string name){if(!value)throw new Exception(name);}
IntentObjective[] plan=[new("purpose","Reveal the letter")];var prose="She opened the letter.\r";
string Json(string status,string quote)=>JsonSerializer.Serialize(new{items=new[]{new IntentEvidence("purpose",status,"Evidence-led suggestion",quote)}});
Check(IntentAssessmentService.Validate(Json("achieved","opened the letter"),plan,prose)[0].Status=="achieved","Verified exact quote retained");
Check(IntentAssessmentService.Validate(Json("achieved","a nonexistent sentence"),plan,prose)[0].Status=="cannot-assess","Fabricated evidence cannot claim fulfilment");
Check(IntentAssessmentService.Fingerprint(prose,plan)!=IntentAssessmentService.Fingerprint(prose+"Changed",plan),"Source revisions invalidate evidence");
Check(IntentAssessmentService.Fingerprint(prose,plan)!=IntentAssessmentService.Fingerprint(prose,[new("purpose","Hide the letter")]),"Plan changes invalidate evidence");
try{IntentAssessmentService.Validate("{\"items\":[]}",plan,prose);throw new Exception("Missing objectives accepted");}catch(ArgumentException){}
try{IntentAssessmentService.Validate("{}",plan,prose);throw new Exception("Missing evidence accepted");}catch(ArgumentException){}
var scene=new SceneEditViewModel{SceneID=22,ScenePurposeNotes=plan[0].Text};
var context=NativeWorkflowTests.Proxy<IStoryContextService>((n,a)=>Task.FromResult((int)a![0]! ==22?new StoryContextViewModel{Position=new StoryPosition(1,3,10,22),Scene=scene}:null));
var writer=NativeWorkflowTests.Proxy<IWriterWorkspaceService>((n,a)=>Task.FromResult<ChapterWorkflowViewModel?>(null));
var sources=NativeWorkflowTests.Proxy<IOnboardingBuildRepository>((n,a)=>Task.FromResult<IReadOnlyList<SceneManuscriptSourceRecord>>([new(){SceneID=22,SourceText=prose}]));
var calls=0;var fail=false;var client=NativeWorkflowTests.Proxy<IStoryIntelligenceClient>((n,a)=>{if(n=="GetConfigurationStatus")return new StoryIntelligenceClientConfigurationStatus();calls++;if(fail)throw new HttpRequestException("Synthetic failure");return Task.FromResult(new StoryIntelligenceClientResult{RawResponseText=Json("achieved","opened the letter"),Model="synthetic"});});
var allowed=true;var subscriptions=NativeWorkflowTests.Proxy<ISubscriptionService>((n,a)=>Task.FromResult(new EntitlementDecision{Allowed=allowed}));var store=new Store();var service=new IntentAssessmentService(context,writer,sources,store,client,subscriptions);
var first=await service.Analyse(7,22,CancellationToken.None);Check(first.Saved!=null&&calls==1,"Explicit assessment persists validated evidence");
await service.Analyse(7,22,CancellationToken.None);Check(calls==1,"Unchanged saved prose and plan reuse result without provider call");
scene.ScenePurposeNotes="New plan";fail=true;try{await service.Analyse(7,22,CancellationToken.None);throw new Exception("Provider failure ignored");}catch(HttpRequestException){}Check(store.Saved?.InputHash==first.Hash,"Failure preserves previous evidence and decisions");
allowed=false;try{await service.Analyse(7,22,CancellationToken.None);throw new Exception("Entitlement ignored");}catch(UnauthorizedAccessException){}
try{await service.Read(8,999);throw new Exception("Foreign scene accepted");}catch(UnauthorizedAccessException){}
using var cancel=new CancellationTokenSource();cancel.Cancel();allowed=true;try{await service.Analyse(7,22,cancel.Token);throw new Exception("Cancellation ignored");}catch(OperationCanceledException){}
}
private sealed class Store:IIntentAssessmentRepository{
public IntentAssessment? Saved;
public Task<IntentAssessment?> Read(int u,int s,string? hash=null)=>Task.FromResult(Saved);
public Task Save(int u,int s,string hash,string json,string model){Saved=new(){InputHash=hash,ResultsJson=json,Model=model,CreatedUtc=DateTime.UtcNow};return Task.CompletedTask;}
public Task Decide(int u,int s,string hash,string key,string decision)=>throw new Exception("Assessment must never change author decisions");
}
}