test: share gateway deleted-agent guard mocks

This commit is contained in:
Peter Steinberger
2026-04-20 18:46:20 +01:00
parent aa36c077fc
commit 537f4689f9
3 changed files with 46 additions and 50 deletions

View File

@@ -1,37 +1,19 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ErrorCodes } from "../protocol/index.js";
import type { RespondFn } from "./types.js";
const loadSessionEntryMock = vi.fn();
const resolveDeletedAgentIdFromSessionKeyMock = vi.fn();
vi.mock("../session-utils.js", async () => {
const actual = await vi.importActual<typeof import("../session-utils.js")>("../session-utils.js");
return {
...actual,
loadSessionEntry: (...args: unknown[]) => loadSessionEntryMock(...args),
resolveDeletedAgentIdFromSessionKey: (...args: unknown[]) =>
resolveDeletedAgentIdFromSessionKeyMock(...args),
};
});
import { chatHandlers } from "./chat.js";
import {
mockDeletedAgentSession,
resetDeletedAgentSessionMocks,
} from "./deleted-agent-guard.test-helpers.js";
import type { RespondFn } from "./types.js";
describe("chat.send deleted-agent guard", () => {
beforeEach(() => {
loadSessionEntryMock.mockReset();
resolveDeletedAgentIdFromSessionKeyMock.mockReset();
resetDeletedAgentSessionMocks();
});
it("rejects keys belonging to a deleted agent", async () => {
const orphanKey = "agent:deleted-agent:main";
loadSessionEntryMock.mockReturnValue({
cfg: {},
canonicalKey: orphanKey,
storePath: "/tmp/sessions.json",
entry: { sessionId: "sess-orphan" },
});
resolveDeletedAgentIdFromSessionKeyMock.mockReturnValue("deleted-agent");
const orphanKey = mockDeletedAgentSession();
const respond = vi.fn() as unknown as RespondFn;

View File

@@ -0,0 +1,32 @@
import { vi } from "vitest";
const deletedAgentSessionMocks = vi.hoisted(() => ({
loadSessionEntry: vi.fn(),
resolveDeletedAgentIdFromSessionKey: vi.fn(),
}));
vi.mock("../session-utils.js", async () => {
const actual = await vi.importActual<typeof import("../session-utils.js")>("../session-utils.js");
return {
...actual,
loadSessionEntry: deletedAgentSessionMocks.loadSessionEntry,
resolveDeletedAgentIdFromSessionKey:
deletedAgentSessionMocks.resolveDeletedAgentIdFromSessionKey,
};
});
export function resetDeletedAgentSessionMocks(): void {
deletedAgentSessionMocks.loadSessionEntry.mockReset();
deletedAgentSessionMocks.resolveDeletedAgentIdFromSessionKey.mockReset();
}
export function mockDeletedAgentSession(orphanKey = "agent:deleted-agent:main"): string {
deletedAgentSessionMocks.loadSessionEntry.mockReturnValue({
cfg: {},
canonicalKey: orphanKey,
storePath: "/tmp/sessions.json",
entry: { sessionId: "sess-orphan" },
});
deletedAgentSessionMocks.resolveDeletedAgentIdFromSessionKey.mockReturnValue("deleted-agent");
return orphanKey;
}

View File

@@ -1,38 +1,20 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ErrorCodes } from "../protocol/index.js";
import type { GatewayRequestContext, RespondFn } from "./types.js";
const loadSessionEntryMock = vi.fn();
const resolveDeletedAgentIdFromSessionKeyMock = vi.fn();
vi.mock("../session-utils.js", async () => {
const actual = await vi.importActual<typeof import("../session-utils.js")>("../session-utils.js");
return {
...actual,
loadSessionEntry: (...args: unknown[]) => loadSessionEntryMock(...args),
resolveDeletedAgentIdFromSessionKey: (...args: unknown[]) =>
resolveDeletedAgentIdFromSessionKeyMock(...args),
};
});
import {
mockDeletedAgentSession,
resetDeletedAgentSessionMocks,
} from "./deleted-agent-guard.test-helpers.js";
import { sessionsHandlers } from "./sessions.js";
import type { GatewayRequestContext, RespondFn } from "./types.js";
describe("sessions.send / sessions.steer deleted-agent guard", () => {
beforeEach(() => {
loadSessionEntryMock.mockReset();
resolveDeletedAgentIdFromSessionKeyMock.mockReset();
resetDeletedAgentSessionMocks();
});
for (const method of ["sessions.send", "sessions.steer"] as const) {
it(`${method} rejects keys belonging to a deleted agent`, async () => {
const orphanKey = "agent:deleted-agent:main";
loadSessionEntryMock.mockReturnValue({
cfg: {},
canonicalKey: orphanKey,
storePath: "/tmp/sessions.json",
entry: { sessionId: "sess-orphan" },
});
resolveDeletedAgentIdFromSessionKeyMock.mockReturnValue("deleted-agent");
const orphanKey = mockDeletedAgentSession();
const respond = vi.fn() as unknown as RespondFn;
const context = {