Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.8 KB
Raw Blame
// Characterization of runPluginOnRequestHook — the plugin onRequest gate extracted from
// handleChatCore's request entry (chatCore god-file decomposition, #3501). Hooks are in-memory.
// Locks: the discriminated result — blocked (403 Response) vs body-rewrite vs pass-through — and
// fail-open on a throwing hook.
import { test, afterEach } from "node:test";
import assert from "node:assert/strict";

const { registerHook, unregisterHook } = await import("../../src/lib/plugins/hooks.ts");
const { runPluginOnRequestHook } =
  await import("../../open-sse/handlers/chatCore/pluginOnRequest.ts");

const PLUGIN = "test-onrequest-plugin";

afterEach(() => {
  unregisterHook("onRequest", PLUGIN);
});

function baseArgs(overrides: Record<string, unknown> = {}) {
  return {
    requestId: "req-1",
    body: { messages: [{ role: "user", content: "hi" }] },
    model: "gpt-x",
    provider: "openai",
    apiKeyInfo: null,
    ...overrides,
  } as Parameters<typeof runPluginOnRequestHook>[0];
}

test("no registered hooks → pass-through (blocked:false, no body)", async () => {
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, false);
});

test("headers passed to the hook are visible in PluginContext", async () => {
  let capturedCtx: Record<string, unknown> | undefined;
  registerHook("onRequest", "test-ctx-headers", async (ctx: Record<string, unknown>) => {
    capturedCtx = ctx;
    return {};
  });
  const testHeaders = { "x-trace-id": "abc-123", "x-request-id": "req-456" };
  const gate = await runPluginOnRequestHook(baseArgs({ headers: testHeaders }));
  assert.equal(gate.blocked, false);
  assert.ok(capturedCtx, "expected the hook to be invoked");
  assert.deepEqual(capturedCtx!.headers, testHeaders);
});

test("no headers arg → backward compatible (undefined in ctx)", async () => {
  let capturedCtx: Record<string, unknown> | undefined;
  registerHook("onRequest", "test-ctx-noheaders", async (ctx: Record<string, unknown>) => {
    capturedCtx = ctx;
    return {};
  });
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, false);
  assert.ok(capturedCtx, "expected the hook to be invoked");
  assert.equal(capturedCtx!.headers, undefined);
});

test("a blocking hook → blocked:true with a 403 JSON Response", async () => {
  registerHook("onRequest", PLUGIN, async () => ({
    blocked: true,
    response: { error: "nope" },
  }));
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, true);
  if (!gate.blocked) return;
  assert.equal(gate.response.status, 403);
  const payload = await gate.response.json();
  assert.deepEqual(payload, { error: "nope" });
});

test("a blocking hook without a response → generic plugin_block 403", async () => {
  registerHook("onRequest", PLUGIN, async () => ({ blocked: true }));
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, true);
  if (!gate.blocked) return;
  assert.equal(gate.response.status, 403);
  const payload = (await gate.response.json()) as { error?: { type?: string } };
  assert.equal(payload.error?.type, "plugin_block");
});

test("a body-rewriting hook → blocked:false with the new body", async () => {
  const rewritten = { messages: [{ role: "user", content: "rewritten" }] };
  registerHook("onRequest", PLUGIN, async () => ({ body: rewritten }));
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, false);
  if (gate.blocked) return;
  assert.equal("response" in gate, false);
  assert.deepEqual(gate.body, rewritten);
});

test("a throwing hook → fail-open pass-through (blocked:false)", async () => {
  registerHook("onRequest", PLUGIN, async () => {
    throw new Error("boom");
  });
  const gate = await runPluginOnRequestHook(baseArgs());
  assert.equal(gate.blocked, false);
});