|
| 1 | +import type { StreamFn } from "openclaw/plugin-sdk/agent-core"; |
| 2 | +import { createAssistantMessageEventStream } from "openclaw/plugin-sdk/llm"; |
| 3 | +import { registerSingleProviderPlugin } from "openclaw/plugin-sdk/plugin-test-runtime"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import openrouterPlugin from "./index.js"; |
| 6 | + |
| 7 | +async function captureProviderPayload( |
| 8 | + modelId: string, |
| 9 | + thinkingLevel: string, |
| 10 | + payload: Record<string, unknown>, |
| 11 | +) { |
| 12 | + const provider = await registerSingleProviderPlugin(openrouterPlugin); |
| 13 | + const baseStreamFn = vi.fn((...args: Parameters<StreamFn>): ReturnType<StreamFn> => { |
| 14 | + void args[2]?.onPayload?.(payload, args[0]); |
| 15 | + return createAssistantMessageEventStream(); |
| 16 | + }); |
| 17 | + const wrapped = provider.wrapStreamFn?.({ |
| 18 | + provider: "openrouter", |
| 19 | + modelId, |
| 20 | + thinkingLevel, |
| 21 | + streamFn: baseStreamFn, |
| 22 | + } as never); |
| 23 | + |
| 24 | + void wrapped?.( |
| 25 | + { |
| 26 | + provider: "openrouter", |
| 27 | + api: "openai-completions", |
| 28 | + id: modelId, |
| 29 | + baseUrl: "https://openrouter.ai/api/v1", |
| 30 | + compat: {}, |
| 31 | + } as never, |
| 32 | + { messages: [] }, |
| 33 | + {}, |
| 34 | + ); |
| 35 | + |
| 36 | + expect(baseStreamFn).toHaveBeenCalledOnce(); |
| 37 | + return payload; |
| 38 | +} |
| 39 | + |
| 40 | +describe("OpenRouter chat owner invariants", () => { |
| 41 | + it.each([ |
| 42 | + "openrouter/anthropic/claude-sonnet-5", |
| 43 | + "openrouter/deepseek/deepseek-v4-pro", |
| 44 | + "openrouter/moonshotai/kimi-k3", |
| 45 | + "z-ai/glm-5.2", |
| 46 | + "openrouter/z-ai/glm-5.2", |
| 47 | + "~anthropic/claude-opus-latest", |
| 48 | + "~moonshotai/kimi-latest", |
| 49 | + ])("recognizes the live upstream cacheable model reference %s", async (modelId) => { |
| 50 | + const provider = await registerSingleProviderPlugin(openrouterPlugin); |
| 51 | + |
| 52 | + expect(provider.isCacheTtlEligible?.({ provider: "openrouter", modelId } as never)).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it.each(["openai/gpt-5.4", "~openai/gpt-5.4", "openrouter/openai/gpt-5.4"])( |
| 56 | + "does not infer provider cache support for unrelated model reference %s", |
| 57 | + async (modelId) => { |
| 58 | + const provider = await registerSingleProviderPlugin(openrouterPlugin); |
| 59 | + |
| 60 | + expect(provider.isCacheTtlEligible?.({ provider: "openrouter", modelId } as never)).toBe( |
| 61 | + false, |
| 62 | + ); |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + it("preserves assistant prefill when the provider reasoning object disables reasoning", async () => { |
| 67 | + const payload = await captureProviderPayload("anthropic/claude-opus-5", "off", { |
| 68 | + reasoning: { enabled: false }, |
| 69 | + messages: [ |
| 70 | + { role: "user", content: "Return JSON." }, |
| 71 | + { role: "assistant", content: "{" }, |
| 72 | + ], |
| 73 | + }); |
| 74 | + |
| 75 | + expect(payload.messages).toEqual([ |
| 76 | + { role: "user", content: "Return JSON." }, |
| 77 | + { role: "assistant", content: "{" }, |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it.each(["~anthropic/claude-opus-latest", "openrouter/~anthropic/claude-opus-latest"])( |
| 82 | + "removes unsupported assistant prefill for reasoning model alias %s", |
| 83 | + async (modelId) => { |
| 84 | + const payload = await captureProviderPayload(modelId, "high", { |
| 85 | + reasoning: { effort: "high" }, |
| 86 | + messages: [ |
| 87 | + { role: "user", content: "Continue." }, |
| 88 | + { role: "assistant", content: "{" }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + |
| 92 | + expect(payload.messages).toEqual([{ role: "user", content: "Continue." }]); |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + it("recognizes the live dated DeepSeek V4 model in its owner thinking profile", async () => { |
| 97 | + const provider = await registerSingleProviderPlugin(openrouterPlugin); |
| 98 | + |
| 99 | + expect( |
| 100 | + provider.resolveThinkingProfile?.({ |
| 101 | + provider: "openrouter", |
| 102 | + modelId: "deepseek/deepseek-v4-flash-0731", |
| 103 | + } as never), |
| 104 | + ).toMatchObject({ defaultLevel: "high" }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("backfills reasoning replay for the live dated DeepSeek V4 model", async () => { |
| 108 | + const payload = await captureProviderPayload("deepseek/deepseek-v4-flash-0731", "high", { |
| 109 | + messages: [{ role: "assistant", content: "done" }], |
| 110 | + }); |
| 111 | + |
| 112 | + expect(payload.reasoning).toEqual({ effort: "high" }); |
| 113 | + expect(payload.messages).toEqual([ |
| 114 | + { role: "assistant", content: "done", reasoning_content: "" }, |
| 115 | + ]); |
| 116 | + }); |
| 117 | +}); |
0 commit comments