clanker-agent/packages/coding-agent/test/gateway-steer.test.ts
Harivansh Rathi 536241053c refactor: finish companion rename migration
Complete the remaining pi-to-companion rename across companion-os, web, vm-orchestrator, docker, and archived fixtures.

Verification:
- semantic rg sweeps for Pi/piConfig/getPi/.pi runtime references
- npm run check in apps/companion-os (fails in this worktree: biome not found)

Co-authored-by: Codex <noreply@openai.com>
2026-03-10 07:39:32 -05:00

127 lines
3.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { GatewayRuntime } from "../src/core/gateway/runtime.js";
function createMockSession() {
return {
sessionId: "session-1",
sessionName: undefined,
messages: [],
prompt: vi.fn().mockResolvedValue(undefined),
steer: vi.fn().mockResolvedValue(undefined),
abort: vi.fn().mockResolvedValue(undefined),
dispose: vi.fn(),
subscribe: vi.fn(() => () => {}),
sessionManager: {
getSessionDir: () => "/tmp/companion-gateway-test",
},
};
}
function createRuntime(session = createMockSession()) {
return new GatewayRuntime({
config: {
bind: "127.0.0.1",
port: 0,
session: {
idleMinutes: 5,
maxQueuePerSession: 4,
},
webhook: {
enabled: false,
basePath: "/webhooks",
},
},
primarySessionKey: "primary",
primarySession: session as never,
createSession: async () => session as never,
});
}
function addManagedSession(
runtime: GatewayRuntime,
sessionKey: string,
session: ReturnType<typeof createMockSession>,
processing: boolean,
) {
const managedSession = {
sessionKey,
session,
queue: [],
processing,
activeAssistantMessage: null,
pendingToolResults: [],
createdAt: Date.now(),
lastActiveAt: Date.now(),
listeners: new Set(),
unsubscribe: () => {},
};
(runtime as unknown as { sessions: Map<string, unknown> }).sessions.set(
sessionKey,
managedSession,
);
return managedSession;
}
describe("GatewayRuntime steer handling", () => {
it("steers the active session instead of queueing a second prompt", async () => {
const session = createMockSession();
const runtime = createRuntime(session);
addManagedSession(runtime, "chat", session, true);
const result = await (
runtime as unknown as {
handleSteer: (
sessionKey: string,
text: string,
) => Promise<{
ok: true;
mode: "steer" | "queued";
sessionKey: string;
}>;
}
).handleSteer("chat", "keep going");
expect(result).toEqual({
ok: true,
mode: "steer",
sessionKey: "chat",
});
expect(session.steer).toHaveBeenCalledWith("keep going");
expect(session.prompt).not.toHaveBeenCalled();
});
it("queues a prompt immediately when steer races an idle session", async () => {
const session = createMockSession();
const runtime = createRuntime(session);
addManagedSession(runtime, "chat", session, false);
const result = await (
runtime as unknown as {
handleSteer: (
sessionKey: string,
text: string,
) => Promise<{
ok: true;
mode: "steer" | "queued";
sessionKey: string;
}>;
}
).handleSteer("chat", "pick this up next");
expect(result).toEqual({
ok: true,
mode: "queued",
sessionKey: "chat",
});
expect(session.steer).not.toHaveBeenCalled();
await vi.waitFor(() => {
expect(session.prompt).toHaveBeenCalledWith("pick this up next", {
images: undefined,
source: "extension",
});
});
});
});