import { test } from "node:test"; import assert from "node:assert/strict"; import { faultReply, jsonReply, reply, startFakeOllama, toolCallsReply, } from "../../testing/llm/fakeOllama.mjs"; import { estimateTokens, NUM_CTX_MAX, ollamaChat, OllamaError, OllamaFaultError, promptEstimate, PromptTooLargeError, toOllamaTools, toolCallsOf, unloadOllama, } from "./ollama"; test("estimateTokens: 3.5 characters a token, over a string or the JSON of a value", () => { assert.equal(estimateTokens("x".repeat(35)), 10); assert.equal(estimateTokens({ a: 1 }), Math.ceil(JSON.stringify({ a: 1 }).length / 3.5)); }); test("ollamaChat: stream false, think false on a thinking model, num_ctx clamped to 12k, the format passed", async () => { const ol = await startFakeOllama({ turns: [jsonReply({ terms: ["oven"] }), reply("plain")] }); try { const cfg = { baseUrl: ol.url + "/", numCtx: 32_768, temperature: 0.2 }; const schema = { type: "object", properties: { terms: { type: "array" } } }; const r = await ollamaChat(cfg, { model: "qwen3:8b", messages: [{ role: "user", content: "hi" }], format: schema }); assert.deepEqual(JSON.parse(r.message.content), { terms: ["oven"] }); assert.ok(r.promptTokens > 0 && r.evalTokens > 0); const body = ol.requests[0]; assert.equal(body.stream, false); assert.equal(body.think, false); assert.equal(body.options.num_ctx, NUM_CTX_MAX); assert.equal(body.options.temperature, 0.2); assert.deepEqual(body.format, schema); // A model with no reasoning mode is not sent `think` at all. await ollamaChat(cfg, { model: "qwen2.5:7b", messages: [{ role: "user", content: "hi" }] }); assert.equal("think" in ol.requests[1], false); } finally { await ol.close(); } }); test("ollamaChat: a prompt over the cap is refused BEFORE it is sent", async () => { const ol = await startFakeOllama({ turns: [reply("never")] }); try { const big = "word ".repeat(8000); // ~11.4k tokens at 3.5 chars await assert.rejects( ollamaChat({ baseUrl: ol.url }, { model: "qwen3:8b", messages: [{ role: "user", content: big }] }), (e) => e instanceof PromptTooLargeError && e.estimate > 8000, ); // A lower cap from the caller is honoured; a higher one is clamped to 8k. await assert.rejects( ollamaChat({ baseUrl: ol.url, promptCapTokens: 300 }, { model: "qwen3:8b", messages: [{ role: "user", content: "x".repeat(2000) }] }), PromptTooLargeError, ); await assert.rejects( ollamaChat({ baseUrl: ol.url, promptCapTokens: 50_000 }, { model: "qwen3:8b", messages: [{ role: "user", content: big }] }), PromptTooLargeError, ); assert.equal(ol.requests.length, 0); } finally { await ol.close(); } }); test("ollamaChat: done:false is a GPU fault, an HTTP error is an error", async () => { const ol = await startFakeOllama({ turns: [faultReply()] }); try { await assert.rejects( ollamaChat({ baseUrl: ol.url }, { model: "qwen3:8b", messages: [{ role: "user", content: "x" }] }), (e) => e instanceof OllamaFaultError && /GPU fault/.test(e.message), ); await assert.rejects( ollamaChat({ baseUrl: ol.url }, { model: "llama-none", messages: [{ role: "user", content: "x" }] }), (e) => e instanceof OllamaError && e.status === 404, ); } finally { await ol.close(); } }); test("tools both ways: AgentTool → function; tool_calls (object or string arguments) → name + input", async () => { const ol = await startFakeOllama({ turns: [ toolCallsReply([{ name: "lookup", arguments: { q: "oven" } }]), () => ({ message: { role: "assistant", content: "", tool_calls: [{ function: { name: "lookup", arguments: '{"q":"tile"}' } }] }, done: true, }), ], }); try { const tools = toOllamaTools([ { name: "lookup", description: "Look it up.", inputSchema: { type: "object", properties: { q: { type: "string" } } } }, ]); assert.deepEqual(tools[0], { type: "function", function: { name: "lookup", description: "Look it up.", parameters: { type: "object", properties: { q: { type: "string" } } } }, }); const a = await ollamaChat({ baseUrl: ol.url }, { model: "qwen3:8b", messages: [{ role: "user", content: "x" }], tools }); assert.deepEqual(toolCallsOf(a.message), [{ name: "lookup", input: { q: "oven" } }]); assert.deepEqual(ol.requests[0].tools, tools); const b = await ollamaChat({ baseUrl: ol.url }, { model: "qwen3:8b", messages: [{ role: "user", content: "x" }], tools }); assert.deepEqual(toolCallsOf(b.message), [{ name: "lookup", input: { q: "tile" } }]); } finally { await ol.close(); } }); test("unloadOllama: keep_alive 0", async () => { const ol = await startFakeOllama({ turns: [reply("x")] }); try { await ollamaChat({ baseUrl: ol.url }, { model: "qwen3:8b", messages: [{ role: "user", content: "x" }] }); assert.deepEqual(ol.loaded(), ["qwen3:8b"]); await unloadOllama({ baseUrl: ol.url }, "qwen3:8b"); assert.deepEqual(ol.unloads, ["qwen3:8b"]); assert.deepEqual(ol.loaded(), []); await assert.rejects(unloadOllama({ baseUrl: ol.url }, "nope:1b"), /404/); } finally { await ol.close(); } }); test("promptEstimate counts messages, tools and format", () => { const m = [{ role: "user", content: "x".repeat(350) }]; assert.ok(promptEstimate({ messages: m, tools: [{ a: "y".repeat(350) }] }) > promptEstimate({ messages: m })); });