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

1.7 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import { ClaudeCodeHandler } from "../../src/mitm/handlers/claudeCode.ts";
import { runHandler } from "./_mitmHandlerHarness.ts";

test("claude-code handler — happy path forwards to /v1/messages", async () => {
  const r = await runHandler(
    new ClaudeCodeHandler(),
    { model: "claude-3.5-sonnet", messages: [] },
    "claude-opus-4.5"
  );
  assert.ok(r.fetchCalled);
  assert.equal(r.status, 200);
  assert.ok(r.fetchUrl?.endsWith("/v1/messages"));
  const sent = JSON.parse(r.fetchBody);
  assert.equal(sent.model, "claude-opus-4.5");
});

test("claude-code handler — strips ALL consecutive trailing assistant turns, not just one", async () => {
  const r = await runHandler(
    new ClaudeCodeHandler(),
    {
      model: "claude-3.5-sonnet",
      messages: [
        { role: "user", content: "hi" },
        { role: "assistant", content: "partial reply 1" },
        { role: "assistant", content: "partial reply 2" },
      ],
    },
    "claude-opus-4.5"
  );
  assert.ok(r.fetchCalled);
  const sent = JSON.parse(r.fetchBody);
  assert.equal(sent.messages.length, 1);
  assert.equal(sent.messages[0].role, "user");
});

test("claude-code handler — never collapses messages to empty when the ENTIRE history is trailing assistant turns", async () => {
  const r = await runHandler(
    new ClaudeCodeHandler(),
    {
      model: "claude-3.5-sonnet",
      messages: [{ role: "assistant", content: "lone assistant turn" }],
    },
    "claude-opus-4.5"
  );
  assert.ok(r.fetchCalled);
  const sent = JSON.parse(r.fetchBody);
  assert.equal(sent.messages.length, 1);
  assert.equal(sent.messages[0].content, "lone assistant turn");
});