Files
prometeu-studio/prometeu-app/src/test/java/p/studio/app/runtime/DirectHeadlessOracle.java
T

74 lines
3.7 KiB
Java

package p.studio.app.runtime;
import com.fasterxml.jackson.databind.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.*;
/** Independent test-only JSONL client: no Studio gateway/DTO/capture transformation. */
final class DirectHeadlessOracle implements AutoCloseable {
private final ObjectMapper mapper = new ObjectMapper();
private final Process process;
private final BufferedReader output;
private final BufferedWriter input;
private final ExecutorService reader = Executors.newVirtualThreadPerTaskExecutor();
private final Set<ProcessHandle> descendants = new HashSet<>();
private String session;
DirectHeadlessOracle(Path installation, Path cartridge) throws Exception {
process = new ProcessBuilder(installation.resolve(System.getProperty("os.name").startsWith("Windows") ? "prometeu.exe" : "prometeu").toString(),
"headless", "--stdio").redirectError(ProcessBuilder.Redirect.DISCARD).start();
output = process.inputReader(StandardCharsets.UTF_8);
input = process.outputWriter(StandardCharsets.UTF_8);
try {
var hello = event();
assertEquals("hello", hello.path("type").asText());
session = hello.path("sessionId").asText(); assertFalse(session.isBlank());
assertEquals(1, hello.path("protocolVersion").path("major").asInt());
var initialize = mapper.createObjectNode();
initialize.putObject("protocolVersion").put("major", 1).put("minor", 0);
initialize.set("requiredCapabilities", hello.path("capabilities"));
call("initialize", initialize);
call("boot", mapper.createObjectNode().put("path", cartridge.toString()));
} catch (Throwable failure) { terminate(); throw failure; }
}
private JsonNode event() throws Exception {
String line = reader.submit(output::readLine).get(10, TimeUnit.SECONDS);
assertNotNull(line, "Runtime stdout closed before reply");
process.descendants().forEach(descendants::add);
return mapper.readTree(line);
}
JsonNode call(String command, JsonNode params) throws Exception {
String id = UUID.randomUUID().toString();
var request = mapper.createObjectNode().put("requestId", id).put("command", command).set("params", params);
input.write(request.toString()); input.newLine(); input.flush();
while (true) {
var response = event();
assertEquals(session, response.path("sessionId").asText());
assertEquals(id, response.path("requestId").asText());
if (response.path("type").asText().equals("progress")) continue;
assertEquals("response", response.path("type").asText());
assertFalse(response.has("error"), response.toString());
return response.path("result");
}
}
JsonNode call(String command) throws Exception { return call(command, mapper.createObjectNode()); }
private void terminate() {
process.descendants().forEach(descendants::add);
descendants.forEach(ProcessHandle::destroyForcibly); process.destroyForcibly(); reader.shutdownNow();
try {
assertTrue(process.waitFor(5, TimeUnit.SECONDS));
for (var child : descendants) child.onExit().get(5, TimeUnit.SECONDS);
} catch (Exception failure) { throw new AssertionError("Oracle process cleanup failed", failure); }
}
@Override public void close() throws Exception {
try {
assertTrue(call("shutdown").path("closed").asBoolean());
assertTrue(process.waitFor(5, TimeUnit.SECONDS));
} finally { terminate(); }
}
}