refactor(openrouter-tests): share state dir helper

This commit is contained in:
Peter Steinberger 2026-03-17 07:36:15 +00:00
parent bb13dd0c01
commit 68f3e537d3

View File

@ -3,6 +3,16 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
async function withOpenRouterStateDir(run: (stateDir: string) => Promise<void>) {
const stateDir = mkdtempSync(join(tmpdir(), "openclaw-openrouter-capabilities-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
try {
await run(stateDir);
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
}
describe("openrouter-model-capabilities", () => {
afterEach(() => {
vi.resetModules();
@ -11,46 +21,42 @@ describe("openrouter-model-capabilities", () => {
});
it("uses top-level OpenRouter max token fields when top_provider is absent", async () => {
const stateDir = mkdtempSync(join(tmpdir(), "openclaw-openrouter-capabilities-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
await withOpenRouterStateDir(async () => {
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(
JSON.stringify({
data: [
{
id: "acme/top-level-max-completion",
name: "Top Level Max Completion",
architecture: { modality: "text+image->text" },
supported_parameters: ["reasoning"],
context_length: 65432,
max_completion_tokens: 12345,
pricing: { prompt: "0.000001", completion: "0.000002" },
},
{
id: "acme/top-level-max-output",
name: "Top Level Max Output",
modality: "text+image->text",
context_length: 54321,
max_output_tokens: 23456,
pricing: { prompt: "0.000003", completion: "0.000004" },
},
],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
),
),
);
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
new Response(
JSON.stringify({
data: [
{
id: "acme/top-level-max-completion",
name: "Top Level Max Completion",
architecture: { modality: "text+image->text" },
supported_parameters: ["reasoning"],
context_length: 65432,
max_completion_tokens: 12345,
pricing: { prompt: "0.000001", completion: "0.000002" },
},
{
id: "acme/top-level-max-output",
name: "Top Level Max Output",
modality: "text+image->text",
context_length: 54321,
max_output_tokens: 23456,
pricing: { prompt: "0.000003", completion: "0.000004" },
},
],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
),
),
);
const module = await import("./openrouter-model-capabilities.js");
try {
const module = await import("./openrouter-model-capabilities.js");
await module.loadOpenRouterModelCapabilities("acme/top-level-max-completion");
expect(module.getOpenRouterModelCapabilities("acme/top-level-max-completion")).toMatchObject({
@ -65,47 +71,39 @@ describe("openrouter-model-capabilities", () => {
contextWindow: 54321,
maxTokens: 23456,
});
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
});
it("does not refetch immediately after an awaited miss for the same model id", async () => {
const stateDir = mkdtempSync(join(tmpdir(), "openclaw-openrouter-capabilities-"));
process.env.OPENCLAW_STATE_DIR = stateDir;
await withOpenRouterStateDir(async () => {
const fetchSpy = vi.fn(
async () =>
new Response(
JSON.stringify({
data: [
{
id: "acme/known-model",
name: "Known Model",
architecture: { modality: "text->text" },
context_length: 1234,
},
],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
),
);
vi.stubGlobal("fetch", fetchSpy);
const fetchSpy = vi.fn(
async () =>
new Response(
JSON.stringify({
data: [
{
id: "acme/known-model",
name: "Known Model",
architecture: { modality: "text->text" },
context_length: 1234,
},
],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
),
);
vi.stubGlobal("fetch", fetchSpy);
const module = await import("./openrouter-model-capabilities.js");
try {
const module = await import("./openrouter-model-capabilities.js");
await module.loadOpenRouterModelCapabilities("acme/missing-model");
expect(module.getOpenRouterModelCapabilities("acme/missing-model")).toBeUndefined();
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(module.getOpenRouterModelCapabilities("acme/missing-model")).toBeUndefined();
expect(fetchSpy).toHaveBeenCalledTimes(2);
} finally {
rmSync(stateDir, { recursive: true, force: true });
}
});
});
});