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 { beforeEach, describe, expect, it, vi } from "vitest";
import { spawnSubagentDirect } from "./subagent-spawn.js"; import { spawnSubagentDirect } from "./subagent-spawn.js";
import { installAcceptedSubagentGatewayMock } from "./test-helpers/subagent-gateway.js";
type TestAgentConfig = { type TestAgentConfig = {
id?: string; id?: string;
@ -100,20 +101,7 @@ function createConfigOverride(overrides?: Record<string, unknown>) {
} }
function setupGatewayMock() { function setupGatewayMock() {
hoisted.callGatewayMock.mockImplementation( installAcceptedSubagentGatewayMock(hoisted.callGatewayMock);
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 {};
},
);
} }
function getRegisteredRun() { function getRegisteredRun() {
@ -122,6 +110,27 @@ function getRegisteredRun() {
| undefined; | 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", () => { describe("spawnSubagentDirect workspace inheritance", () => {
beforeEach(() => { beforeEach(() => {
hoisted.callGatewayMock.mockClear(); hoisted.callGatewayMock.mockClear();
@ -149,44 +158,16 @@ describe("spawnSubagentDirect workspace inheritance", () => {
}, },
}); });
const result = await spawnSubagentDirect( await expectAcceptedWorkspace({
{ agentId: "ops",
task: "inspect workspace", expectedWorkspaceDir: "/tmp/workspace-ops",
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",
}); });
}); });
it("preserves the inherited workspace for same-agent spawns", async () => { it("preserves the inherited workspace for same-agent spawns", async () => {
const result = await spawnSubagentDirect( await expectAcceptedWorkspace({
{ agentId: "main",
task: "inspect workspace", expectedWorkspaceDir: "/tmp/requester-workspace",
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",
}); });
}); });
}); });

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 } : {},
);
}