fix(backend): track Pi turn lifecycle

This commit is contained in:
User
2026-07-14 20:39:45 +02:00
parent 37cc42bebf
commit aba8666f16
4 changed files with 143 additions and 4 deletions
+28
View File
@@ -155,6 +155,34 @@ test("createFor does not prompt until start is called", async () => {
mgr.teardown("sid-deferred");
});
test("a created runtime is active during configure/bootstrap", () => {
const child = recordingChild();
const mgr = new PiProcessManager(loadConfig({}), { spawnFn: () => child as any });
const runtime = mgr.createFor("starting-id", {});
expect(runtime.bridge.turnState()).toBe("running");
mgr.teardown("starting-id");
});
test("start reactivates the runtime before sending the prompt", () => {
const child = recordingChild();
const mgr = new PiProcessManager(loadConfig({}), { spawnFn: () => child as any });
const runtime = mgr.createFor("restart-id", {});
child.stdout.emit("data", `${JSON.stringify({ type: "agent_end", messages: [] })}\n`);
expect(runtime.bridge.turnState()).toBe("idle");
let stateAtWrite = runtime.bridge.turnState();
child.stdin.write = (data: unknown) => {
stateAtWrite = runtime.bridge.turnState();
child._writes.push(String(data));
return true;
};
mgr.start("restart-id", runtime, { question: "q" });
expect(stateAtWrite).toBe("running");
expect(runtime.bridge.turnState()).toBe("running");
mgr.teardown("restart-id");
});
test("production spawn uses explicit Pi path and passes portable data root without rewriting PATH", async () => {
vi.stubEnv("PATH", "/usr/local/bin:/usr/bin");
vi.stubEnv("PI_PROVIDER_API_KEY", "provider-secret");