Files
prometeu-studio/prometeu-app/src/test/java/p/studio/app/runtime/HeadlessTaskIntegrationTest.java
T
bquarkz 71fb80efc3
Test / Tests and coverage occurred while executing withChecks step.
Intrepid/Prometeu/Studio/pipeline/head There was a failure building this commit
feat(studio): ship JavaFX 2 Play and delegate integration tooling
Launch the JavaFX 2 project shell on Java 25 with an interactive viewport,
managed Play lifecycle and project mutation guards. Preserve headless/MCP and
save compatibility, and qualify exact runtime executable bundles.
Separate unit JaCoCo gates from opt-in integration suites and use the internal
Pong fixture for the headless visual pilot.
Move maintenance and native conformance tools into tooling; retain the VS Code
extension and update current navigation links.

Validation: tooling make verify passed, including 1064 Studio unit tests,
JaCoCo thresholds and 22 headless integration tests.
2026-11-19 17:32:03 +00:00

331 lines
24 KiB
Java

package p.studio.app.runtime;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import p.studio.api.automation.*;
import p.studio.api.project.*;
import p.studio.api.session.*;
import p.studio.api.shipper.Shipper;
import p.studio.app.StudioApplicationConfiguration;
import p.studio.core.project.ProjectLocalStudioSetupStore;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
/** Actual app composition and saved-source services; no runtime/build/LSP doubles. */
@org.junit.jupiter.api.Tag("integration")
class HeadlessTaskIntegrationTest {
@TempDir Path root;
ConfigurableApplicationContext app;
ProjectSession project;
TaskTools tasks;
LanguageQueries language;
ObjectMapper mapper;
Path source;
String task, code;
final Set<ProcessHandle> ownedProcesses = new HashSet<>();
@BeforeEach void open() throws Exception {
root = root.toRealPath();
String installation = System.getProperty("studio.test.runtimeInstall", "");
assertFalse(installation.isBlank(), "Supply -PstudioRuntimeInstall=<runtime distribution>");
app = new SpringApplicationBuilder(StudioApplicationConfiguration.class).web(WebApplicationType.NONE).headless(true).run();
mapper = app.getBean(ObjectMapper.class);
for (var ref : p.studio.compiler.FrontendRegistryService.require("pbs").documentation().orElseThrow().references(1, p.studio.AppMode.Game)) {
if (!ref.id().startsWith("pbs:example:game:1:")) continue;
Path file = root.resolve(ref.id().substring("pbs:example:game:1:".length()));
Files.createDirectories(file.getParent()); Files.writeString(file, ref.text());
}
source = root.resolve("src/main.pbs"); code = Files.readString(source);
var reference = app.getBean(ProjectCatalog.class).openProject(root);
app.getBean(ProjectLocalStudioSetupStore.class).save(reference, new ProjectLocalStudioSetup(installation, null));
project = app.getBean(ProjectSessionFactory.class).open(reference);
tasks = app.getBean(TaskTools.class); language = app.getBean(LanguageQueries.class);
assertTrue(project.automation().grant(false).toCompletableFuture().get(5, TimeUnit.SECONDS).success());
task = tasks.acquire(root).task().value();
}
@AfterEach void close() {
try { if (project != null) project.close(); }
finally { if (app != null) app.close(); }
assertTrue(ownedProcesses.stream().noneMatch(ProcessHandle::isAlive), "Managed runtime processes must stop on project closure");
}
TrackedOperation finished(String request) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
do {
var result = tasks.operation(root, task, new OperationLookup(null, request));
if (result.workerTerminated()) return result;
Thread.sleep(5);
} while (System.nanoTime() < deadline);
throw new AssertionError("Operation did not finish: " + request);
}
TrackedOperation build(String request) throws Exception {
tasks.build(root, task, request); return finished(request);
}
String successfulBuild(String request) throws Exception {
var result = build(request); assertNull(result.failure());
assertNotNull(result.buildResult()); assertTrue(result.buildResult().success(), result.buildResult().diagnostics().toString());
assertNotNull(result.buildId()); return result.buildId();
}
TaskTools.HeadlessReply call(TaskTools.HeadlessAction action, String build) {
var reply = tasks.headless(root, task, action, build, Map.of());
if (action == TaskTools.HeadlessAction.BOOT) {
String installation = Path.of(System.getProperty("studio.test.runtimeInstall")).toAbsolutePath().toString();
ProcessHandle.current().descendants().filter(process -> process.info().command().orElse("").startsWith(installation + "/"))
.forEach(ownedProcesses::add);
assertFalse(ownedProcesses.isEmpty(), "Observe the actual packaged runtime process");
}
return reply;
}
AutomationOutcome advance(String request) throws Exception {
tasks.advance(root, task, request, 3);
var result = finished(request); assertNull(result.failure(), String.valueOf(result.failure()));
assertNotNull(result.runtimeResult()); return result.runtimeResult();
}
com.fasterxml.jackson.databind.JsonNode diagnostics() {
return mapper.valueToTree(language.query(root, task, "diagnostics", Map.of("uri", source.toUri().toString()))).path("result");
}
void authorityRetained() { assertEquals(ControlState.AGENT_WORKING, project.automation().snapshot().state()); }
@Test void originalCaptureEvidenceSurvivesLaterFramesRebuildReleaseAndReopen() throws Exception {
call(TaskTools.HeadlessAction.BOOT,successfulBuild("evidence-build"));
tasks.advance(root,task,"evidence-frame",1);var original=finished("evidence-frame");
var evidence=app.getBean(EvidenceTools.class);
var saved=evidence.persist(root,task,"save-original",new OperationLookup(null,"evidence-frame"));
var artifact=saved.artifacts().getFirst();
assertArrayEquals(original.runtimeResult().capture().png(),evidence.image(root,task,artifact.id()));
advance("later-frame");
assertEquals(saved,evidence.persist(root,task,"save-original",new OperationLookup(null,"evidence-frame")));
assertTrue(evidence.metadata(root,task,artifact.id()).stale());
assertEquals(original.runtimeResult().capture().frameId(),artifact.provenance().get("frameId"));
// This derived reference has a deliberate wrong sample; independent pixel oracles are tested separately.
var codec=new p.studio.app.evidence.PngEvidenceCodec();var pixels=codec.decode(original.runtimeResult().capture().png());
byte[] different=pixels.rgba();different[0]=(byte)(Byte.toUnsignedInt(different[0])^1);
Path reference=root.resolve("known-different.png");Files.write(reference,codec.encode(new Evidence.Pixels(pixels.width(),pixels.height(),different)));
var expected=evidence.importReference(root,task,"import-delta",reference,null).artifacts().getFirst();
var report=evidence.compare(root,task,"known-failure",expected.id(),artifact.id(),Evidence.Parameters.exact(Evidence.Alpha.compare));
assertEquals(Evidence.Verdict.rejected,report.report().verdict());assertEquals(1,report.report().differentPixels());
assertEquals(new Evidence.Rect(0,0,1,1),report.report().boundingBox());
successfulBuild("evidence-rebuild");
tasks.release(root,task).toCompletableFuture().get(10,TimeUnit.SECONDS);project.close();
var ref=app.getBean(ProjectCatalog.class).openProject(root);project=app.getBean(ProjectSessionFactory.class).open(ref);
project.automation().grant(false).toCompletableFuture().get(5,TimeUnit.SECONDS);task=tasks.acquire(root).task().value();
assertArrayEquals(original.runtimeResult().capture().png(),evidence.image(root,task,artifact.id()));
assertEquals(saved,evidence.persist(root,task,"save-original",new OperationLookup(null,"evidence-frame")));
System.out.println("VISUAL_EVIDENCE "+mapper.writeValueAsString(Map.of("kind","native-checkpoint","artifact",artifact,"report",report.report(),"nativeAdvanceRequests",2,"persistenceExtraAdvanceOrCaptureRequests",0)));
}
@Test void pongPilotUsesDisposableCopyAndIndependentRegionReferences() throws Exception {
String fixture=System.getProperty("studio.test.pong");
assertNotNull(fixture,"Run headlessProcessTest to select Studio's test-projects/pong fixture");
Path originalProject=Path.of(fixture).toAbsolutePath().normalize();
assertTrue(Files.isRegularFile(originalProject.resolve("prometeu.json")),"Repository-owned Pong fixture is required");
String originalSource=Files.readString(originalProject.resolve("src/main.pbs"));
project.close();Path copy=root.resolve("Pong Headless");Files.createDirectories(copy);
for(String name:List.of("prometeu.json","src","assets")) {
Path from=originalProject.resolve(name);
if(!Files.exists(from))continue; // Pong has no external assets.
if(Files.isDirectory(from))try(var paths=Files.walk(from)) {
for(var p:paths.toList()){var target=copy.resolve(originalProject.relativize(p));if(Files.isDirectory(p))Files.createDirectories(target);else Files.copy(p,target);}
}else Files.copy(from,copy.resolve(name));
}
root=copy.toRealPath();var ref=app.getBean(ProjectCatalog.class).openProject(root);
app.getBean(ProjectLocalStudioSetupStore.class).save(ref,new ProjectLocalStudioSetup(System.getProperty("studio.test.runtimeInstall"),null));
project=app.getBean(ProjectSessionFactory.class).open(ref);project.automation().grant(false).toCompletableFuture().get(5,TimeUnit.SECONDS);task=tasks.acquire(root).task().value();
String build=successfulBuild("pong-pilot-build");call(TaskTools.HeadlessAction.BOOT,build);
var evidence=app.getBean(EvidenceTools.class);var measurements=new ArrayList<Map<String,Object>>();
var mcp=app.getBean(p.studio.app.mcp.McpToolRegistry.class);
long beforeWireBytes=0,afterWireBytes=0;int beforeCalls=0,afterCalls=0;
var legacyPaths=new ArrayList<String>();
String[] names={"menu-accent","serve-ball","moved-paddle"};
// Fixed expectations from the fixture's draw/input contract, never from a native capture.
Evidence.Rect[] regions={new Evidence.Rect(113,114,1,80),new Evidence.Rect(237,152,6,6),new Evidence.Rect(42,72,8,36)};
int[][] colors={{77,237,205},{236,244,250},{77,237,205}};
long totalPngBytes=0;
for(int i=0;i<names.length;i++) {
if(i==1)tasks.headless(root,task,TaskTools.HeadlessAction.SET_INPUT,null,Map.of("start",true));
if(i==2)tasks.headless(root,task,TaskTools.HeadlessAction.SET_INPUT,null,Map.of("start",false,"up",true));
String request="pong-"+names[i];tasks.advance(root,task,request,i==2?20:1);var operation=finished(request);
assertNull(operation.failure());assertNotNull(operation.runtimeResult().capture());
var args=mapper.createObjectNode().put("projectRoot",root.toString()).put("taskId",task);
var before=mcp.call("studio_operation_get",args.deepCopy().put("clientRequestId",request)).wire();
beforeCalls++;beforeWireBytes+=mapper.writeValueAsBytes(before).length;
byte[] legacyPng=Base64.getDecoder().decode(before.path("content").get(1).path("data").asText());
Path legacyActual=root.resolve("legacy-"+names[i]+".png");Files.write(legacyActual,legacyPng);
var persisted=mcp.call("studio_evidence_persist",args.deepCopy().put("clientRequestId","persist-"+request).put("sourceClientRequestId",request)).wire();
afterCalls++;afterWireBytes+=mapper.writeValueAsBytes(persisted).length;
assertFalse(persisted.path("isError").asBoolean(),persisted.toString());
var actual=mapper.convertValue(persisted.path("structuredContent").path("value"),Evidence.Publication.class).artifacts().getFirst();
Path reference=root.resolve("reference-"+names[i]+".png");
byte[] rgba=new byte[480*270*4];
for(int pixel=0;pixel<rgba.length;pixel+=4){
rgba[pixel]=(byte)colors[i][0];rgba[pixel+1]=(byte)colors[i][1];rgba[pixel+2]=(byte)colors[i][2];rgba[pixel+3]=(byte)255;
}
Files.write(reference,new p.studio.app.evidence.PngEvidenceCodec().encode(new Evidence.Pixels(480,270,rgba)));
legacyPaths.add(reference.toString());legacyPaths.add(legacyActual.toString());
var imported=mcp.call("studio_evidence_import",args.deepCopy().put("clientRequestId","import-"+request).put("path",reference.toString())).wire();
afterCalls++;afterWireBytes+=mapper.writeValueAsBytes(imported).length;
assertFalse(imported.path("isError").asBoolean(),imported.toString());
var expected=mapper.convertValue(imported.path("structuredContent").path("value"),Evidence.Publication.class).artifacts().getFirst();
Evidence.Rect roi=regions[i];
var params=new Evidence.Parameters(roi,List.of(),Evidence.Alpha.compare,0,0);
var comparisonArgs=args.deepCopy().put("clientRequestId","compare-"+request).put("expectedId",expected.id()).put("actualId",actual.id());
var parametersNode=(com.fasterxml.jackson.databind.node.ObjectNode)mapper.valueToTree(params);
if(roi==null)parametersNode.remove("roi");comparisonArgs.set("parameters",parametersNode);
var compared=mcp.call("studio_evidence_compare",comparisonArgs).wire();afterCalls++;afterWireBytes+=mapper.writeValueAsBytes(compared).length;
assertFalse(compared.path("isError").asBoolean(),compared.toString());
var report=mapper.convertValue(compared.path("structuredContent").path("value"),Evidence.Publication.class);
assertEquals(Evidence.Verdict.passed,report.report().verdict(),names[i]);
assertEquals((long)roi.width()*roi.height(),report.report().comparedPixels());
assertEquals(0,report.report().differentPixels());
assertArrayEquals(legacyPng,evidence.image(root,task,actual.id()));
totalPngBytes+=legacyPng.length;
measurements.add(Map.of("case",names[i],"referencePath",reference.getFileName().toString(),"actual",actual,"report",report.report()));
}
// Execute the former external comparison path against the same checkpoints; count actual calls/bytes/scripts.
Path script=root.resolve("LegacyVisualCompare.java");
Files.writeString(script,"""
import javax.imageio.ImageIO;import java.io.File;
class LegacyVisualCompare {public static void main(String[] args)throws Exception {
for(int n=0;n<args.length;n+=2) {
var a=ImageIO.read(new File(args[n]));var b=ImageIO.read(new File(args[n+1]));long count=0;
if(a.getWidth()!=b.getWidth()||a.getHeight()!=b.getHeight())count=-1;
else for(int y=0;y<a.getHeight();y++)for(int x=0;x<a.getWidth();x++)if(a.getRGB(x,y)!=b.getRGB(x,y))count++;
System.out.println((n/2)+","+count);
}
}}
""");
var command=new ArrayList<String>(List.of(Path.of(System.getProperty("java.home"),"bin","java").toString(),script.toString()));command.addAll(legacyPaths);
var legacy=new ProcessBuilder(command).redirectErrorStream(true).start();assertTrue(legacy.waitFor(30,TimeUnit.SECONDS));
String legacyOutput=new String(legacy.getInputStream().readAllBytes(),java.nio.charset.StandardCharsets.UTF_8);assertEquals(0,legacy.exitValue(),legacyOutput);
assertEquals(originalSource,Files.readString(originalProject.resolve("src/main.pbs")),"Build and runtime must use only the disposable copy");
var output=Map.of("kind","pong-pilot","limitations","Solid-color references specify only the declared ROIs independently of native captures. These checks cover menu, serve and input-driven paddle movement, not full-game visual equivalence.",
"results",measurements,"measurement",Map.of("sharedGuestAdvanceRequests",3,"before",Map.of("mcpCalls",beforeCalls,"wireResponseBytes",beforeWireBytes,"imagePngBytes",totalPngBytes,"externalComparisonScripts",1,"externalScriptOutput",legacyOutput),
"after",Map.of("mcpCalls",afterCalls,"wireResponseBytes",afterWireBytes,"imageTransferBytesForMetrics",0,"externalComparisonScripts",0),
"basis","Actual local MCP adapter dispatches and serialized response bytes, excluding shared build/advance/asynchronous polling. Both comparison paths use the same three retained guest checkpoints. Legacy full-image exact metrics differ from ROI-specific checks by design."));
System.out.println("VISUAL_EVIDENCE "+mapper.writeValueAsString(output));
}
@Test void actualWatchdogPreservesNativeCauseZeroProgressAndDoesNotReplayOnLookup() throws Exception {
Files.writeString(source, code.replace("fn frame() -> void {", "fn frame() -> void { while (true) { }"));
call(TaskTools.HeadlessAction.BOOT, successfulBuild("watchdog-build"));
tasks.advance(root, task, "watchdog", 1);
var result = finished("watchdog");
assertNull(result.failure());
assertEquals(AutomationOutcome.Kind.INFRASTRUCTURE_FAILURE, result.runtimeResult().kind());
assertEquals(0L, result.progress().completedFrames());
assertEquals("watchdog_expired", ((Map<?, ?>)result.observation().nativeDetails().get("error")).get("code"));
assertTrue(result.observation().captureStale());
assertEquals("watchdog", result.observation().context().get("clientRequestId"));
var artifactHashes = new LinkedHashMap<String, String>();
for (var file : finished("watchdog-build").buildResult().emittedArtifacts())
if (Files.isRegularFile(file)) artifactHashes.put(root.relativize(file).toString(),
HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file))));
System.out.println("HEADLESS_OBSERVATION " + mapper.writeValueAsString(Map.of(
"sourceSha256", HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(source))),
"artifacts", artifactHashes, "observation", result.observation().toMap())));
var before = call(TaskTools.HeadlessAction.STATE, null).values().get("state");
var lookup = tasks.operation(root, task, new OperationLookup(null, "watchdog"));
assertEquals(result.observation(), lookup.observation());
assertEquals(before, call(TaskTools.HeadlessAction.STATE, null).values().get("state"));
assertNull(call(TaskTools.HeadlessAction.FAULT, null).values().get("fault"));
authorityRetained();
Files.writeString(source, code);
call(TaskTools.HeadlessAction.BOOT, successfulBuild("corrected-watchdog"));
assertEquals(AutomationOutcome.Kind.SUCCEEDED, advance("after-watchdog").kind());
}
@Test void savedEditsRebuildRealArtifactsAndCorrelateDevWithoutSnapshots() throws Exception {
assertTrue(diagnostics().isEmpty());
assertThrows(ControlDeniedException.class, () -> app.getBean(Shipper.class).prepare(project.projectReference(), ignored -> {}));
String first = successfulBuild("first");
String session = (String) call(TaskTools.HeadlessAction.BOOT, first).values().get("sessionId");
var initial = advance("initial");
assertEquals(AutomationOutcome.Kind.SUCCEEDED, initial.kind());
assertEquals(first, initial.capture().buildId());
assertArrayEquals(initial.capture().png(), project.automation().snapshot().capture().png());
call(TaskTools.HeadlessAction.RESET, null);
assertTrue(project.automation().snapshot().stale());
assertEquals(RuntimeObservation.Lifecycle.UNBOOTED, project.automation().snapshot().runtimeObservation().lifecycle());
var historical = finished("initial").observation();
assertEquals(first, historical.buildId()); assertEquals(3L, historical.progress().completedFrames());
assertNotEquals(historical.lifecycle(), finished("initial").currentObservation().lifecycle());
assertEquals(AutomationFailure.Code.NO_FRAME_AVAILABLE, assertThrows(AutomationException.class,
() -> call(TaskTools.HeadlessAction.CAPTURE, null)).failure().code());
assertEquals(session, call(TaskTools.HeadlessAction.BOOT, first).values().get("sessionId"));
assertArrayEquals(initial.capture().png(), advance("rebooted").capture().png());
// Native agent-style edit: same project files, no clean-tree or apply step.
Files.writeString(source, code.replace("Color.rgb(16, 20, 28)", "Color.rgb(80, 40, 20)"));
assertTrue(diagnostics().isEmpty());
String second = successfulBuild("second");
assertNotEquals(first, second);
assertTrue(project.automation().snapshot().stale());
assertTrue(finished("initial").captureStale());
assertEquals(AutomationFailure.Code.SESSION_NOT_FOUND, assertThrows(AutomationException.class,
() -> call(TaskTools.HeadlessAction.STATE, null)).failure().code());
assertEquals(AutomationFailure.Code.BUILD_NOT_FOUND, assertThrows(AutomationException.class,
() -> call(TaskTools.HeadlessAction.BOOT, first)).failure().code());
assertTrue(ownedProcesses.stream().noneMatch(ProcessHandle::isAlive), "Rebuild must stop the previous runtime");
assertNotEquals(session, call(TaskTools.HeadlessAction.BOOT, second).values().get("sessionId"));
var changed = advance("changed");
assertFalse(Arrays.equals(initial.capture().png(), changed.capture().png()));
assertEquals(second, project.automation().snapshot().capture().buildId());
assertFalse(project.automation().snapshot().stale());
authorityRetained();
assertTrue(tasks.release(root, task).toCompletableFuture().get(7, TimeUnit.SECONDS).success());
assertTrue(project.automation().snapshot().manualAllowed());
assertTrue(ownedProcesses.stream().noneMatch(ProcessHandle::isAlive), "Release must stop the runtime before returning control");
assertTrue(Files.readString(source).contains("Color.rgb(80, 40, 20)"));
}
@Test void compileAndGuestFailuresKeepAuthorityForCorrectiveBuilds() throws Exception {
String original = successfulBuild("original");
call(TaskTools.HeadlessAction.BOOT, original); advance("before-error");
Files.writeString(source, "fn frame( {");
assertFalse(diagnostics().isEmpty());
var failed = build("compile-error");
assertFalse(failed.buildResult().success()); assertNull(failed.buildId());
assertFalse(failed.buildResult().diagnostics().isEmpty());
assertFalse(project.automation().snapshot().diagnostics().isEmpty());
assertTrue(project.automation().snapshot().stale()); authorityRetained();
assertEquals(AutomationFailure.Code.BUILD_NOT_FOUND, assertThrows(AutomationException.class,
() -> call(TaskTools.HeadlessAction.BOOT, original)).failure().code());
Files.writeString(source, code.replace("x = x + 1;", "x = x / (x - x);"));
assertTrue(diagnostics().isEmpty());
call(TaskTools.HeadlessAction.BOOT, successfulBuild("guest-error"));
var fault = advance("fault");
assertEquals(AutomationOutcome.Kind.GUEST_FAULT, fault.kind());
assertFalse(fault.diagnostics().isEmpty());
assertNotNull(fault.observation().nativeDetails().get("fault"));
assertNotNull(call(TaskTools.HeadlessAction.FAULT, null).values().get("fault"));
authorityRetained();
Files.writeString(source, code);
assertTrue(diagnostics().isEmpty());
call(TaskTools.HeadlessAction.BOOT, successfulBuild("corrected"));
assertEquals(AutomationOutcome.Kind.SUCCEEDED, advance("working-again").kind());
authorityRetained();
}
@Test void unavailableRuntimeIsExplicitAndCanBeConfiguredWithinTheSameTask() throws Exception {
String build = successfulBuild("build");
var setup = app.getBean(ProjectLocalStudioSetupStore.class);
setup.save(project.projectReference(), ProjectLocalStudioSetup.defaults());
assertEquals(AutomationFailure.Code.RUNTIME_UNAVAILABLE, assertThrows(AutomationException.class,
() -> call(TaskTools.HeadlessAction.BOOT, build)).failure().code());
authorityRetained();
setup.save(project.projectReference(), new ProjectLocalStudioSetup(System.getProperty("studio.test.runtimeInstall"), null));
call(TaskTools.HeadlessAction.BOOT, build);
assertEquals(AutomationOutcome.Kind.SUCCEEDED, advance("configured").kind());
authorityRetained();
}
}