Files
adolf/extensions/openrouter/music-generation-provider.test.ts
alvis bedb527145
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Vendor OpenClaw source as Adolf fork baseline
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11),
free to diverge. Tree copied sans upstream .git; upstream remote added for
future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19.
Preserves docs/ARCHITECTURE.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
2026-07-05 09:36:54 +00:00

356 lines
11 KiB
TypeScript

// Openrouter tests cover music generation provider plugin behavior.
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
import { expectExplicitMusicGenerationCapabilities } from "openclaw/plugin-sdk/provider-test-contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { buildOpenRouterMusicGenerationProvider } from "./music-generation-provider.js";
const {
assertOkOrThrowHttpErrorMock,
postJsonRequestMock,
resolveApiKeyForProviderMock,
resolveProviderHttpRequestConfigMock,
} = vi.hoisted(() => ({
assertOkOrThrowHttpErrorMock: vi.fn(async () => {}),
postJsonRequestMock: vi.fn(),
resolveApiKeyForProviderMock: vi.fn(async () => ({
apiKey: "openrouter-key",
source: "env",
mode: "api-key",
})),
resolveProviderHttpRequestConfigMock: vi.fn((params: Record<string, unknown>) => ({
baseUrl: params.baseUrl ?? params.defaultBaseUrl,
allowPrivateNetwork: false,
headers: new Headers(params.defaultHeaders as HeadersInit | undefined),
dispatcherPolicy: undefined,
})),
}));
vi.mock("openclaw/plugin-sdk/provider-auth-runtime", () => ({
resolveApiKeyForProvider: resolveApiKeyForProviderMock,
}));
vi.mock("openclaw/plugin-sdk/provider-http", async (importOriginal) => {
const original = await importOriginal<typeof import("openclaw/plugin-sdk/provider-http")>();
return {
...original,
assertOkOrThrowHttpError: assertOkOrThrowHttpErrorMock,
postJsonRequest: postJsonRequestMock,
resolveProviderHttpRequestConfig: resolveProviderHttpRequestConfigMock,
};
});
function sseResponse(lines: string[], options?: { releaseLock?: () => void }): Response {
const encoder = new TextEncoder();
if (!options?.releaseLock) {
return new Response(
new ReadableStream({
start(controller) {
for (const line of lines) {
controller.enqueue(encoder.encode(line));
}
controller.close();
},
}),
{ status: 200, headers: { "content-type": "text/event-stream" } },
);
}
const chunks: Array<ReadableStreamReadResult<Uint8Array>> = lines.map((line) => ({
done: false,
value: encoder.encode(line),
}));
chunks.push({ done: true, value: undefined });
const reader = {
read: async () => chunks.shift() ?? { done: true, value: undefined },
cancel: async () => undefined,
releaseLock: options.releaseLock,
} as ReadableStreamDefaultReader<Uint8Array>;
return {
ok: true,
status: 200,
headers: new Headers({ "content-type": "text/event-stream" }),
body: {
getReader: () => reader,
},
} as Response;
}
function sseResponseLines(params: {
audio?: string;
transcript?: string;
done?: boolean;
}): string[] {
const lines: string[] = [];
if (params.audio || params.transcript) {
lines.push(
`data: ${JSON.stringify({
choices: [
{
delta: {
audio: {
...(params.audio ? { data: params.audio } : {}),
...(params.transcript ? { transcript: params.transcript } : {}),
},
},
},
],
})}\n`,
);
}
if (params.done) {
lines.push("data: [DONE]\n");
}
return lines;
}
function stalledSseResponse(line: string): Response {
const encoder = new TextEncoder();
return new Response(
new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(line));
},
cancel() {},
}),
{ status: 200, headers: { "content-type": "text/event-stream" } },
);
}
function postRequest(): Record<string, unknown> {
const request = postJsonRequestMock.mock.calls[0]?.[0];
if (!request || typeof request !== "object" || Array.isArray(request)) {
throw new Error("expected OpenRouter music request");
}
return request as Record<string, unknown>;
}
function resetOpenRouterMusicMocks() {
assertOkOrThrowHttpErrorMock.mockResolvedValue(undefined);
postJsonRequestMock.mockReset();
resolveApiKeyForProviderMock.mockResolvedValue({
apiKey: "openrouter-key",
source: "env",
mode: "api-key",
});
resolveProviderHttpRequestConfigMock.mockImplementation((params: Record<string, unknown>) => ({
baseUrl: params.baseUrl ?? params.defaultBaseUrl,
allowPrivateNetwork: false,
headers: new Headers(params.defaultHeaders as HeadersInit | undefined),
dispatcherPolicy: undefined,
}));
}
describe("openrouter music generation provider", () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.useRealTimers();
resetOpenRouterMusicMocks();
});
afterEach(() => {
resetOpenRouterMusicMocks();
vi.restoreAllMocks();
vi.useRealTimers();
});
it("declares explicit mode capabilities", () => {
expectExplicitMusicGenerationCapabilities(buildOpenRouterMusicGenerationProvider());
});
it("streams OpenRouter audio chunks into a generated music asset", async () => {
const release = vi.fn(async () => {});
const audioBase64 = Buffer.from("wav-bytes").toString("base64");
postJsonRequestMock.mockResolvedValue({
response: sseResponse([
`data: ${JSON.stringify({ choices: [{ delta: { audio: { transcript: "line " } } }] })}\n`,
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: audioBase64.slice(0, 4) } } }] })}\n`,
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: audioBase64.slice(4), transcript: "two" } } }] })}\n`,
"data: [DONE]\n",
]),
release,
});
const result = await buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "",
prompt: "bright soundtrack",
cfg: {},
instrumental: true,
format: "wav",
});
expect(postRequest().url).toBe("https://openrouter.ai/api/v1/chat/completions");
expect(postRequest().body).toEqual({
model: "google/lyria-3-pro-preview",
messages: [
{
role: "user",
content:
"bright soundtrack\n\nInstrumental only. No vocals, no sung lyrics, no spoken word.",
},
],
modalities: ["text", "audio"],
audio: { format: "wav" },
stream: true,
});
expect(result.tracks[0]?.mimeType).toBe("audio/wav");
expect(result.tracks[0]?.buffer).toEqual(Buffer.from("wav-bytes"));
expect(result.lyrics).toEqual(["line two"]);
expect(release).toHaveBeenCalledOnce();
});
it("releases OpenRouter audio stream readers after completion", async () => {
const releaseLock = vi.fn();
postJsonRequestMock.mockResolvedValue({
response: sseResponse(
sseResponseLines({
audio: Buffer.from("wav-bytes").toString("base64"),
done: true,
}),
{ releaseLock },
),
release: vi.fn(async () => {}),
});
await expect(
buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-pro-preview",
prompt: "release stream reader",
cfg: {},
}),
).resolves.toMatchObject({
tracks: [{ mimeType: "audio/wav" }],
});
expect(releaseLock).toHaveBeenCalledTimes(1);
});
it("decodes independently padded OpenRouter audio chunks", async () => {
postJsonRequestMock.mockResolvedValue({
response: sseResponse([
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: Buffer.from("a").toString("base64") } } }] })}\n`,
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: Buffer.from("b").toString("base64") } } }] })}\n`,
"data: [DONE]\n",
]),
release: vi.fn(async () => {}),
});
const result = await buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-pro-preview",
prompt: "chunked soundtrack",
cfg: {},
});
expect(result.tracks[0]?.buffer).toEqual(Buffer.from("ab"));
});
it("sends reference images as multimodal message content", async () => {
postJsonRequestMock.mockResolvedValue({
response: sseResponse([
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: Buffer.from("mp3").toString("base64") } } }] })}\n`,
"data: [DONE]\n",
]),
release: vi.fn(async () => {}),
});
await buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-clip-preview",
prompt: "score this image",
cfg: {},
format: "mp3",
inputImages: [{ buffer: Buffer.from("png"), mimeType: "image/png" }],
});
expect(postRequest().body).toEqual(
expect.objectContaining({
model: "google/lyria-3-clip-preview",
audio: { format: "mp3" },
messages: [
{
role: "user",
content: [
{ type: "text", text: "score this image" },
{
type: "image_url",
image_url: {
url: `data:image/png;base64,${Buffer.from("png").toString("base64")}`,
},
},
],
},
],
}),
);
});
it("times out stalled OpenRouter audio streams after headers", async () => {
postJsonRequestMock.mockResolvedValue({
response: stalledSseResponse(
`data: ${JSON.stringify({ choices: [{ delta: { audio: { transcript: "start" } } }] })}\n`,
),
release: vi.fn(async () => {}),
});
await expect(
buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-clip-preview",
prompt: "never finish",
cfg: {},
timeoutMs: 1,
}),
).rejects.toThrow("OpenRouter music generation timed out after 1ms");
});
it("caps oversized OpenRouter music stream timeouts", async () => {
vi.useFakeTimers();
vi.setSystemTime(1_000);
const timeoutSpy = vi.spyOn(globalThis, "setTimeout");
try {
postJsonRequestMock.mockResolvedValue({
response: sseResponse(["data: [DONE]\n"]),
release: vi.fn(async () => {}),
});
await expect(
buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-clip-preview",
prompt: "huge timeout",
cfg: {},
timeoutMs: Number.MAX_SAFE_INTEGER,
}),
).rejects.toThrow("OpenRouter music generation response missing audio data");
expect(postRequest().timeoutMs).toBe(MAX_TIMER_TIMEOUT_MS);
const streamTimeoutMs = timeoutSpy.mock.calls.at(-1)?.[1];
expect(streamTimeoutMs).toBeGreaterThan(MAX_TIMER_TIMEOUT_MS - 1_000);
expect(streamTimeoutMs).toBeLessThanOrEqual(MAX_TIMER_TIMEOUT_MS);
} finally {
timeoutSpy.mockRestore();
vi.useRealTimers();
}
});
it("rejects OpenRouter streams that end before completion", async () => {
postJsonRequestMock.mockResolvedValue({
response: sseResponse([
`data: ${JSON.stringify({ choices: [{ delta: { audio: { data: Buffer.from("partial").toString("base64") } } }] })}\n`,
]),
release: vi.fn(async () => {}),
});
await expect(
buildOpenRouterMusicGenerationProvider().generateMusic({
provider: "openrouter",
model: "google/lyria-3-clip-preview",
prompt: "interrupted",
cfg: {},
}),
).rejects.toThrow("OpenRouter music generation stream ended before completion");
});
});