mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 07:30:43 +00:00
test: share gateway deleted-agent guard mocks
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user