test: share subagent gateway mock setup

This commit is contained in:
Peter Steinberger 2026-03-14 02:21:13 +00:00
parent 013ad58f3c
commit 7aedb6d442
2 changed files with 38 additions and 48 deletions

View File

@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { spawnSubagentDirect } from "./subagent-spawn.js";
import { installAcceptedSubagentGatewayMock } from "./test-helpers/subagent-gateway.js";
type TestAgentConfig = {
id?: string;
@ -100,20 +101,7 @@ function createConfigOverride(overrides?: Record<string, unknown>) {
}
function setupGatewayMock() {
hoisted.callGatewayMock.mockImplementation(
async (opts: { method?: string; params?: Record<string, unknown> }) => {
if (opts.method === "sessions.patch") {
return { ok: true };
}
if (opts.method === "sessions.delete") {
return { ok: true };
}
if (opts.method === "agent") {
return { runId: "run-1" };
}
return {};
},
);
installAcceptedSubagentGatewayMock(hoisted.callGatewayMock);
}
function getRegisteredRun() {
@ -122,6 +110,27 @@ function getRegisteredRun() {
| undefined;
}
async function expectAcceptedWorkspace(params: { agentId: string; expectedWorkspaceDir: string }) {
const result = await spawnSubagentDirect(
{
task: "inspect workspace",
agentId: params.agentId,
},
{
agentSessionKey: "agent:main:main",
agentChannel: "telegram",
agentAccountId: "123",
agentTo: "456",
workspaceDir: "/tmp/requester-workspace",
},
);
expect(result.status).toBe("accepted");
expect(getRegisteredRun()).toMatchObject({
workspaceDir: params.expectedWorkspaceDir,
});
}
describe("spawnSubagentDirect workspace inheritance", () => {
beforeEach(() => {
hoisted.callGatewayMock.mockClear();
@ -149,44 +158,16 @@ describe("spawnSubagentDirect workspace inheritance", () => {
},
});
const result = await spawnSubagentDirect(
{
task: "inspect workspace",
await expectAcceptedWorkspace({
agentId: "ops",
},
{
agentSessionKey: "agent:main:main",
agentChannel: "telegram",
agentAccountId: "123",
agentTo: "456",
workspaceDir: "/tmp/requester-workspace",
},
);
expect(result.status).toBe("accepted");
expect(getRegisteredRun()).toMatchObject({
workspaceDir: "/tmp/workspace-ops",
expectedWorkspaceDir: "/tmp/workspace-ops",
});
});
it("preserves the inherited workspace for same-agent spawns", async () => {
const result = await spawnSubagentDirect(
{
task: "inspect workspace",
await expectAcceptedWorkspace({
agentId: "main",
},
{
agentSessionKey: "agent:main:main",
agentChannel: "telegram",
agentAccountId: "123",
agentTo: "456",
workspaceDir: "/tmp/requester-workspace",
},
);
expect(result.status).toBe("accepted");
expect(getRegisteredRun()).toMatchObject({
workspaceDir: "/tmp/requester-workspace",
expectedWorkspaceDir: "/tmp/requester-workspace",
});
});
});

View File

@ -0,0 +1,9 @@
export function installAcceptedSubagentGatewayMock(mock: {
mockImplementation: (
impl: (opts: { method?: string; params?: unknown }) => Promise<unknown>,
) => unknown;
}) {
mock.mockImplementation(async ({ method }) =>
method === "agent" ? { runId: "run-1" } : method?.startsWith("sessions.") ? { ok: true } : {},
);
}