Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { LlmChatConfig, LlmMessage } from "@triliumnext/commons";
import { fetchProviderModels, streamChatCompletion, type StreamCallbacks } from "./llm_chat.js";
import server from "./server.js";
/**
* Build a Response-like object whose body streams the given SSE chunks.
* Each chunk is decoded by the production code with a TextDecoder.
*/
function makeStreamResponse(chunks: string[], opts: { ok?: boolean; status?: number; statusText?: string; noBody?: boolean } = {}): Response {
const encoder = new TextEncoder();
let i = 0;
const reader = {
read: vi.fn(async () => {
if (i < chunks.length) {
return { done: false, value: encoder.encode(chunks[i++]) };
}
return { done: true, value: undefined };
}),
releaseLock: vi.fn()
};
return {
ok: opts.ok ?? true,
status: opts.status ?? 200,
statusText: opts.statusText ?? "OK",
body: opts.noBody ? null : { getReader: () => reader }
} as unknown as Response;
}
function makeCallbacks(): Record<keyof StreamCallbacks, ReturnType<typeof vi.fn>> & StreamCallbacks {
return {
onChunk: vi.fn(),
onThinking: vi.fn(),
onToolInputStart: vi.fn(),
onToolInputDelta: vi.fn(),
onToolUse: vi.fn(),
onToolApprovalRequest: vi.fn(),
onToolResult: vi.fn(),
onCitation: vi.fn(),
onUsage: vi.fn(),
onStatus: vi.fn(),
onError: vi.fn(),
onDone: vi.fn()
} as Record<keyof StreamCallbacks, ReturnType<typeof vi.fn>> & StreamCallbacks;
}
const messages: LlmMessage[] = [{ role: "user", content: "hi" }];
const config = {} as LlmChatConfig;
describe("fetchProviderModels", () => {
it("posts the provider credentials and returns the models array", async () => {
const models = [{ id: "gpt-4.1", name: "gpt", provider: "openai" }];
server.postWithTimeout = vi.fn(async () => ({ models })) as typeof server.postWithTimeout;
const query = { provider: "openai", apiKey: "sk-test", baseURL: "http://localhost:11434/v1" };
await expect(fetchProviderModels(query)).resolves.toBe(models);
// The model-selection screen shows a failure inline, so no toast repeats it.
expect(server.postWithTimeout).toHaveBeenCalledWith("llm-chat/provider-models", 60_000, query, undefined, { silentBadRequest: true });
});
it("waits as long as the provider asks for", async () => {
server.postWithTimeout = vi.fn(async () => ({ models: [] })) as typeof server.postWithTimeout;
const query = { provider: "openai" };
await fetchProviderModels(query, 90_000);
expect(server.postWithTimeout).toHaveBeenCalledWith("llm-chat/provider-models", 90_000, query, undefined, { silentBadRequest: true });
});
it("defaults to an empty array when models is absent", async () => {
server.postWithTimeout = vi.fn(async () => ({})) as typeof server.postWithTimeout;
await expect(fetchProviderModels({ provider: "openai" })).resolves.toEqual([]);
});
it("surfaces the server error message when the request fails", async () => {
// server.post rejects with the raw response body (a { message } JSON string).
server.postWithTimeout = vi.fn(async () => { throw '{"message":"Authentication failed (HTTP 401) — check the API key."}'; }) as typeof server.postWithTimeout;
await expect(fetchProviderModels({ provider: "openai", apiKey: "bad" }))
.rejects.toThrow("Authentication failed (HTTP 401) — check the API key.");
});
it("falls back to the raw rejection when it isn't a JSON error body", async () => {
server.postWithTimeout = vi.fn(async () => { throw "rejected by browser"; }) as typeof server.postWithTimeout;
await expect(fetchProviderModels({ provider: "openai" })).rejects.toThrow("rejected by browser");
});
it("uses an Error's message when the rejection is an Error object", async () => {
server.postWithTimeout = vi.fn(async () => { throw new Error("network down"); }) as typeof server.postWithTimeout;
await expect(fetchProviderModels({ provider: "openai" })).rejects.toThrow("network down");
});
it("stringifies a non-string, non-Error rejection", async () => {
server.postWithTimeout = vi.fn(async () => { throw { code: 500 }; }) as typeof server.postWithTimeout;
await expect(fetchProviderModels({ provider: "openai" })).rejects.toThrow("[object Object]");
});
});
describe("streamChatCompletion", () => {
beforeEach(() => {
server.getHeaders = vi.fn(async () => ({ "x-csrf-token": "tok" })) as typeof server.getHeaders;
});
afterEach(() => {
vi.restoreAllMocks();
});
it("posts messages/config with merged headers and consumes the stream", async () => {
const fetchMock = vi.fn(async () => makeStreamResponse([`data: ${JSON.stringify({ type: "text", content: "hello" })}\n`]));
vi.stubGlobal("fetch", fetchMock);
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toContain("llm-chat/stream");
expect(init.method).toBe("POST");
expect((init.headers as Record<string, string>)["Content-Type"]).toBe("application/json");
expect((init.headers as Record<string, string>)["x-csrf-token"]).toBe("tok");
expect(JSON.parse(init.body as string)).toEqual({ streamId: expect.any(String), messages, config });
expect(cb.onChunk).toHaveBeenCalledWith("hello");
});
it("hands a waiting tool call to the chat with a way to answer it under the stream's id", async () => {
const request = { toolCallId: "c1", toolName: "delete_note", toolInput: { noteId: "a" } };
const fetchMock = vi.fn(async () => makeStreamResponse([`data: ${JSON.stringify({ type: "tool_approval_request", ...request })}\n`]));
vi.stubGlobal("fetch", fetchMock);
server.post = vi.fn(async () => ({ answered: true })) as typeof server.post;
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onToolApprovalRequest).toHaveBeenCalledExactlyOnceWith(request, expect.any(Function));
const answer = cb.onToolApprovalRequest.mock.calls[0][1] as (approved: boolean, scope?: "chat") => Promise<void>;
await answer(false);
const { streamId } = JSON.parse((fetchMock.mock.calls[0] as unknown as [string, RequestInit])[1].body as string);
expect(server.post).toHaveBeenCalledExactlyOnceWith("llm-chat/tool-approval", { streamId, toolCallId: "c1", approved: false });
await answer(true, "chat");
expect(server.post).toHaveBeenLastCalledWith("llm-chat/tool-approval", { streamId, toolCallId: "c1", approved: true, scope: "chat" });
});
it("rethrows an AbortError from fetch (user stopped)", async () => {
const abort = new DOMException("aborted", "AbortError");
vi.stubGlobal("fetch", vi.fn(async () => { throw abort; }));
const cb = makeCallbacks();
await expect(streamChatCompletion(messages, config, cb)).rejects.toBe(abort);
expect(cb.onError).not.toHaveBeenCalled();
});
it("reports a connection failure for a non-abort Error from fetch", async () => {
vi.stubGlobal("fetch", vi.fn(async () => { throw new Error("boom"); }));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError).toHaveBeenCalledWith("Failed to connect to LLM stream: boom");
});
it("reports a connection failure for a non-Error thrown by fetch", async () => {
vi.stubGlobal("fetch", vi.fn(async () => { throw "stringly"; }));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError).toHaveBeenCalledWith("Failed to connect to LLM stream: stringly");
});
it("reports an HTTP error when the response is not ok", async () => {
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse([], { ok: false, status: 503, statusText: "Unavailable" })));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError).toHaveBeenCalledWith("HTTP 503: Unavailable");
});
it("reports a missing body when there is no reader", async () => {
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse([], { noBody: true })));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError).toHaveBeenCalledWith("No response body");
});
it("dispatches every SSE event type to the matching callback", async () => {
const events = [
{ type: "status", status: "starting_agent" },
{ type: "text", content: "T" },
{ type: "thinking", content: "TH" },
{ type: "tool_input_start", toolCallId: "c1", toolName: "search" },
{ type: "tool_input_delta", toolCallId: "c1", delta: "{" },
{ type: "tool_use", toolCallId: "c1", toolName: "search", toolInput: { q: "x" } },
{ type: "tool_result", toolCallId: "c1", toolName: "search", result: "res", isError: false },
{ type: "citation", citation: { id: "cit1" } },
{ type: "usage", usage: { totalTokens: 10 } },
{ type: "done" },
{ type: "unknown_ignored" }
];
// Each event is a complete `data: ...\n` line; include a non-data line which must be
// ignored, plus a trailing partial line left in the buffer (never flushed). The
// cross-read buffer carry-over is exercised separately in its own test above.
const chunks = [
...events.map((e) => `data: ${JSON.stringify(e)}\n`),
"ignored non-data line\n",
"data: " // trailing partial line left in the buffer (never flushed)
];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onStatus).toHaveBeenCalledWith("starting_agent");
expect(cb.onChunk).toHaveBeenCalledWith("T");
expect(cb.onThinking).toHaveBeenCalledWith("TH");
expect(cb.onToolInputStart).toHaveBeenCalledWith("c1", "search");
expect(cb.onToolInputDelta).toHaveBeenCalledWith("c1", "{");
expect(cb.onToolUse).toHaveBeenCalledWith("c1", "search", { q: "x" });
expect(cb.onToolResult).toHaveBeenCalledWith("c1", "search", "res", false);
expect(cb.onCitation).toHaveBeenCalledWith({ id: "cit1" });
expect(cb.onUsage).toHaveBeenCalledWith({ totalTokens: 10 });
expect(cb.onDone).toHaveBeenCalledTimes(1);
});
it("reassembles a single SSE data line split across two read() boundaries", async () => {
// The first chunk ends with a partial JSON line; the second chunk completes it.
// This exercises the `buffer += decode(...)` / `lines.pop()` carry-over (source 78-80):
// the partial line at the end of the first read must be completed by the next read.
const chunks = [`data: {"type":"text",`, `"content":"hi"}\n`];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onChunk).toHaveBeenCalledTimes(1);
expect(cb.onChunk).toHaveBeenCalledWith("hi");
});
it("reports an interruption when the body ends without a done or error event", async () => {
const chunks = [`data: ${JSON.stringify({ type: "text", content: "only" })}\n`];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onChunk).toHaveBeenCalledWith("only");
expect(cb.onDone).not.toHaveBeenCalled();
expect(cb.onError.mock.calls).toEqual([[
"LLM stream ended without completing — the connection closed before the response "
+ "finished (network or proxy interruption)."
]]);
});
it("ignores citation/usage events that carry no payload", async () => {
const chunks = [
`data: ${JSON.stringify({ type: "citation" })}\n`,
`data: ${JSON.stringify({ type: "usage" })}\n`
];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onCitation).not.toHaveBeenCalled();
expect(cb.onUsage).not.toHaveBeenCalled();
});
it("forwards an error event to onError, with the provider-call details when present", async () => {
const errorDetails = { statusCode: 400, url: "https://api.test/v1", responseBody: "{}" };
const chunks = [
`data: ${JSON.stringify({ type: "error", error: "model failed" })}\n`,
`data: ${JSON.stringify({ type: "error", error: "api failed", errorDetails })}\n`
];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError.mock.calls).toEqual([
["model failed", undefined],
["api failed", errorDetails]
]);
});
it("skips optional callbacks that are not provided", async () => {
const events = [
{ type: "thinking", content: "TH" },
{ type: "tool_input_start", toolCallId: "c1", toolName: "search" },
{ type: "tool_input_delta", toolCallId: "c1", delta: "{" },
{ type: "tool_use", toolCallId: "c1", toolName: "search", toolInput: { q: "x" } },
{ type: "tool_result", toolCallId: "c1", toolName: "search", result: "res" },
{ type: "citation", citation: { id: "cit1" } },
{ type: "usage", usage: { totalTokens: 1 } },
{ type: "done" }
];
const chunks = events.map((e) => `data: ${JSON.stringify(e)}\n`);
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
// Only the required callbacks are present; optional `?.` invocations must be no-ops.
const cb: StreamCallbacks = { onChunk: vi.fn(), onError: vi.fn(), onDone: vi.fn() };
await expect(streamChatCompletion(messages, config, cb)).resolves.toBeUndefined();
expect(cb.onError).not.toHaveBeenCalled();
});
it("logs and skips an SSE line that is not valid JSON", async () => {
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
const chunks = ["data: {not json}\n"];
vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(consoleError).toHaveBeenCalled();
expect(cb.onChunk).not.toHaveBeenCalled();
});
it("rethrows an AbortError raised while reading the stream", async () => {
const abort = new DOMException("aborted", "AbortError");
const reader = { read: vi.fn(async () => { throw abort; }), releaseLock: vi.fn() };
const response = { ok: true, status: 200, statusText: "OK", body: { getReader: () => reader } } as unknown as Response;
vi.stubGlobal("fetch", vi.fn(async () => response));
const cb = makeCallbacks();
await expect(streamChatCompletion(messages, config, cb)).rejects.toBe(abort);
expect(reader.releaseLock).toHaveBeenCalledTimes(1);
expect(cb.onError).not.toHaveBeenCalled();
});
it("reports a non-abort Error raised while reading the stream", async () => {
const reader = { read: vi.fn(async () => { throw new Error("read failed"); }), releaseLock: vi.fn() };
const response = { ok: true, status: 200, statusText: "OK", body: { getReader: () => reader } } as unknown as Response;
vi.stubGlobal("fetch", vi.fn(async () => response));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError.mock.calls).toEqual([["LLM stream interrupted: read failed"]]);
expect(reader.releaseLock).toHaveBeenCalledTimes(1);
});
it("reports a non-Error thrown while reading the stream", async () => {
const reader = { read: vi.fn(async () => { throw "kaput"; }), releaseLock: vi.fn() };
const response = { ok: true, status: 200, statusText: "OK", body: { getReader: () => reader } } as unknown as Response;
vi.stubGlobal("fetch", vi.fn(async () => response));
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb);
expect(cb.onError.mock.calls).toEqual([["LLM stream interrupted: kaput"]]);
});
it("passes the abort signal through to fetch", async () => {
const fetchMock = vi.fn(async () => makeStreamResponse([]));
vi.stubGlobal("fetch", fetchMock);
const controller = new AbortController();
const cb = makeCallbacks();
await streamChatCompletion(messages, config, cb, controller.signal);
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(init.signal).toBe(controller.signal);
});
});