feat(studio): add AI workspace with Codex ACP integration
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package p.studio.app.acp;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.ai.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CodexAcpConnectionTest {
|
||||
@TempDir Path root;
|
||||
private CodexAcpConnection connection;
|
||||
private static <T>T await(CompletionStage<T> operation)throws Exception{return operation.toCompletableFuture().get(25,TimeUnit.SECONDS);}
|
||||
private AcpSettings fixture(String mode){var command=AcpFixture.javaCommand(mode);return new AcpSettings(command.getFirst(),command.subList(1,command.size()));}
|
||||
private CodexAcpConnection open(String mode)throws Exception {
|
||||
connection=new CodexAcpConnection(root,new ObjectMapper());await(connection.connect(fixture(mode)));return connection;
|
||||
}
|
||||
@AfterEach void close()throws Exception{if(connection!=null)await(connection.closeAsync());}
|
||||
|
||||
@Test void loginStreamingIndependentSessionsAndCancellationUseRealStdio()throws Exception {
|
||||
var c=open("login");assertEquals(AgentConnection.State.CONNECTED,c.status().state());
|
||||
await(c.login());assertEquals(AgentConnection.State.READY,c.status().state());
|
||||
String first=await(c.newSession()),second=await(c.newSession());assertNotEquals(first,second);
|
||||
var events=new CopyOnWriteArrayList<AgentConnection.Event>();c.events(events::add);
|
||||
var turn=c.prompt(second,"stream");await(turn.accepted());assertEquals("end_turn",await(turn.completion()));
|
||||
assertEquals("first second",events.stream().filter(e->e.kind()==AgentConnection.Kind.TEXT).map(AgentConnection.Event::text).reduce("",String::concat));
|
||||
assertTrue(events.stream().allMatch(e->e.sessionId().equals(second)));
|
||||
assertTrue(events.stream().noneMatch(e->e.text().contains("PRIVATE_THOUGHT")));
|
||||
var held=c.prompt(first,"hold");await(held.accepted());
|
||||
assertThrows(ExecutionException.class,()->await(c.prompt(second,"overlap").completion()));
|
||||
await(c.cancel(first));assertEquals("cancelled",await(held.completion()));
|
||||
}
|
||||
@Test void permissionIsNeverGrantedWithoutAnExplicitChoice()throws Exception {
|
||||
var c=open("normal");String id=await(c.newSession());
|
||||
var choice=new CompletableFuture<String>();c.permissions(p->{assertEquals(id,p.sessionId());return choice;});
|
||||
var turn=c.prompt(id,"permission");assertFalse(turn.completion().toCompletableFuture().isDone());
|
||||
choice.complete("invalid-option");await(turn.completion());
|
||||
assertTrue(Files.readString(root.resolve("permission.json")).contains("cancelled"));
|
||||
c.permissions(p->CompletableFuture.completedFuture("yes"));await(c.prompt(id,"permission").completion());
|
||||
assertTrue(Files.readString(root.resolve("permission.json")).contains("selected"));
|
||||
var requested=new CountDownLatch(1);c.permissions(p->{requested.countDown();return new CompletableFuture<>();});
|
||||
var pending=c.prompt(id,"permission");assertTrue(requested.await(5,TimeUnit.SECONDS));await(c.cancel(id));await(pending.completion());
|
||||
assertTrue(Files.readString(root.resolve("permission.json")).contains("cancelled"));
|
||||
}
|
||||
@Test void rejectionDoesNotAcknowledgeDeliveryAndFailureCanReconnect()throws Exception {
|
||||
var c=open("normal");String id=await(c.newSession());var rejected=c.prompt(id,"reject");
|
||||
assertThrows(ExecutionException.class,()->await(rejected.accepted()));assertThrows(ExecutionException.class,()->await(rejected.completion()));
|
||||
assertThrows(ExecutionException.class,()->await(c.prompt(id,"crash").completion()));
|
||||
assertEquals(AgentConnection.State.FAILED,c.status().state());await(c.connect(fixture("normal")));
|
||||
assertThrows(ExecutionException.class,()->await(c.prompt(id,"old session").completion()));
|
||||
await(c.prompt(await(c.newSession()),"new session").completion());
|
||||
}
|
||||
@Test void incompatibleAndApiOnlyAgentsFailAndCloseOwnedProcesses()throws Exception {
|
||||
connection=new CodexAcpConnection(root,new ObjectMapper());
|
||||
assertThrows(ExecutionException.class,()->await(connection.connect(fixture("incompatible"))));
|
||||
assertEquals(AgentConnection.State.FAILED,connection.status().state());
|
||||
assertThrows(ExecutionException.class,()->await(connection.connect(fixture("api-only"))));
|
||||
}
|
||||
@Test void closeWaitsForTurnAndEntireOwnedProcessTree()throws Exception {
|
||||
var c=open("tree");String id=await(c.newSession());var held=c.prompt(id,"hold");await(held.accepted());
|
||||
long parent=Long.parseLong(Files.readString(root.resolve("parent.pid"))),child=Long.parseLong(Files.readString(root.resolve("child.pid")));
|
||||
await(c.closeAsync());assertEquals("cancelled",await(held.completion()));
|
||||
assertFalse(ProcessHandle.of(parent).map(ProcessHandle::isAlive).orElse(false));
|
||||
assertFalse(ProcessHandle.of(child).map(ProcessHandle::isAlive).orElse(false));
|
||||
assertEquals(AgentConnection.State.CLOSED,c.status().state());
|
||||
}
|
||||
@Test void unresponsiveCancellationTerminatesTheProcessBeforeResolving()throws Exception {
|
||||
var c=open("ignore-cancel");String id=await(c.newSession());var held=c.prompt(id,"hold");await(held.accepted());
|
||||
long pid=Long.parseLong(Files.readString(root.resolve("parent.pid")));long start=System.nanoTime();
|
||||
await(c.cancel(id));assertTrue(System.nanoTime()-start<TimeUnit.SECONDS.toNanos(20));
|
||||
assertFalse(ProcessHandle.of(pid).map(ProcessHandle::isAlive).orElse(false));
|
||||
assertTrue(held.completion().toCompletableFuture().isCompletedExceptionally());
|
||||
assertEquals(AgentConnection.State.FAILED,c.status().state());
|
||||
}
|
||||
@Test void malformedFramesFailTheTurnAndCleanupAllowsANewConnection()throws Exception {
|
||||
var c=open("normal");String id=await(c.newSession());
|
||||
assertThrows(ExecutionException.class,()->await(c.prompt(id,"malformed").completion()));
|
||||
await(c.connect(fixture("normal")));await(c.prompt(await(c.newSession()),"stream").completion());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user