RotNot / src /lib /__tests__ /llm-hf.test.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
4.74 kB
import { createHuggingFaceClient, toOpenAiMessages } from "../llm-hf";
function mockFetch(payload: unknown, ok = true, status = 200) {
const calls: { url: string; init: RequestInit }[] = [];
const fn = (async (url: string, init: RequestInit) => {
calls.push({ url, init });
return {
ok,
status,
json: async () => payload,
text: async () => JSON.stringify(payload),
} as Response;
}) as unknown as typeof fetch;
return { fn, calls };
}
const CHAT_PAYLOAD = {
choices: [{ message: { content: "PONG" }, finish_reason: "stop" }],
usage: { prompt_tokens: 5, completion_tokens: 1 },
};
describe("toOpenAiMessages", () => {
it("prepends a system message when system is provided", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
system: "be terse",
messages: [{ role: "user", content: "hi" }],
});
expect(out[0]).toEqual({ role: "system", content: "be terse" });
expect(out[1]).toEqual({ role: "user", content: "hi" });
});
it("flattens array content blocks into a string", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
messages: [
{ role: "user", content: [{ type: "text", text: "a" }, { type: "text", text: "b" }] },
],
});
expect(out[0]).toEqual({ role: "user", content: "ab" });
});
it("omits a system message when none is given", () => {
const out = toOpenAiMessages({
model: "x",
max_tokens: 10,
messages: [{ role: "user", content: "hi" }],
});
expect(out).toHaveLength(1);
expect(out[0].role).toBe("user");
});
});
describe("createHuggingFaceClient", () => {
it("POSTs to the chat/completions endpoint with bearer auth and the configured model", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({
apiKey: "hf_test",
model: "meta-llama/Llama-3.1-8B-Instruct",
fetchImpl: fn,
});
await client.messages.create({
model: "claude-haiku-4-5-20251001", // should be ignored
max_tokens: 50,
messages: [{ role: "user", content: "hi" }],
});
expect(calls).toHaveLength(1);
expect(calls[0].url).toBe("https://router.huggingface.co/v1/chat/completions");
const headers = calls[0].init.headers as Record<string, string>;
expect(headers.Authorization).toBe("Bearer hf_test");
const body = JSON.parse(calls[0].init.body as string);
expect(body.model).toBe("meta-llama/Llama-3.1-8B-Instruct");
expect(body.max_tokens).toBe(50);
});
it("returns an Anthropic-shaped result with content[0].text", async () => {
const { fn } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
const res = await client.messages.create({
model: "m",
max_tokens: 10,
messages: [{ role: "user", content: "say pong" }],
});
expect(res.content[0].type).toBe("text");
expect(res.content[0].text).toBe("PONG");
expect(res.stop_reason).toBe("stop");
});
it("passes temperature when provided", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
await client.messages.create({
model: "m",
max_tokens: 10,
temperature: 0.2,
messages: [{ role: "user", content: "x" }],
});
const body = JSON.parse(calls[0].init.body as string);
expect(body.temperature).toBe(0.2);
});
it("honors a custom base URL and trims a trailing slash", async () => {
const { fn, calls } = mockFetch(CHAT_PAYLOAD);
const client = createHuggingFaceClient({
apiKey: "k",
baseUrl: "https://example.com/v1/",
fetchImpl: fn,
});
await client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] });
expect(calls[0].url).toBe("https://example.com/v1/chat/completions");
});
it("throws with status and body on a non-ok response", async () => {
const { fn } = mockFetch({ error: "bad token" }, false, 401);
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
await expect(
client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] }),
).rejects.toThrow(/401/);
});
it("defaults to a string even if the provider returns no content", async () => {
const { fn } = mockFetch({ choices: [{ message: {} }] });
const client = createHuggingFaceClient({ apiKey: "k", fetchImpl: fn });
const res = await client.messages.create({ model: "m", max_tokens: 10, messages: [{ role: "user", content: "x" }] });
expect(res.content[0].text).toBe("");
});
});