Files
famapp/tests/unit/agent-chat.test.ts
T
ginnoir e5e509081c
CI / checks (push) Failing after 2m12s
CI / build (push) Successful in 4m50s
feat: per-user assistant name and system prompt customization
Each user can rename the AI assistant and edit their own system prompt in Settings.
2026-07-04 23:26:17 -05:00

87 lines
3.0 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { createMockLlmClient } from "../../src/lib/llm/mock";
import { getLlmConfig } from "../../src/lib/llm/config";
import { runAgentChat } from "../../src/modules/agent/server/run";
describe("mock llm provider", () => {
it("returns a plain assistant message by default", async () => {
const client = createMockLlmClient();
const result = await client.chatCompletion({
messages: [{ role: "user", content: "hello" }],
tools: [],
});
assert.equal(result.message.role, "assistant");
assert.ok(result.message.content?.includes("mock provider"));
});
it("requests add_list_item when the user mentions milk", async () => {
const client = createMockLlmClient();
const result = await client.chatCompletion({
messages: [{ role: "user", content: "add milk to shopping list" }],
tools: [
{
type: "function",
function: {
name: "add_list_item",
description: "add",
parameters: { type: "object", properties: {} },
},
},
],
});
assert.equal(result.message.tool_calls?.[0]?.function.name, "add_list_item");
});
});
describe("getLlmConfig", () => {
it("uses mock provider when base url is unset", () => {
const original = process.env.LLM_BASE_URL;
const originalProvider = process.env.LLM_PROVIDER;
delete process.env.LLM_BASE_URL;
delete process.env.LLM_PROVIDER;
const config = getLlmConfig();
assert.equal(config.provider, "mock");
if (original === undefined) delete process.env.LLM_BASE_URL;
else process.env.LLM_BASE_URL = original;
if (originalProvider === undefined) delete process.env.LLM_PROVIDER;
else process.env.LLM_PROVIDER = originalProvider;
});
});
describe("runAgentChat", () => {
it("executes tool calls and returns a final assistant message", async () => {
const executed: string[] = [];
const result = await runAgentChat({
messages: [{ role: "user", content: "add milk to the shopping list" }],
request: new Request("http://localhost:3000/api/agent/chat"),
llm: createMockLlmClient(),
executeTool: async (name) => {
executed.push(name);
return JSON.stringify({ status: 201, body: { id: "item-1", text: "milk" } });
},
});
assert.deepEqual(executed, ["add_list_item"]);
assert.equal(result.message.role, "assistant");
assert.ok(result.message.content.length > 0);
assert.equal(result.toolCalls.length, 1);
assert.equal(result.toolCalls[0]?.name, "add_list_item");
assert.equal(result.toolCalls[0]?.status, 201);
});
it("accepts a custom system prompt override", async () => {
const result = await runAgentChat({
messages: [{ role: "user", content: "hello" }],
request: new Request("http://localhost:3000/api/agent/chat"),
systemPrompt: "You are a pirate.",
llm: createMockLlmClient(),
});
assert.equal(result.message.role, "assistant");
assert.ok(result.message.content.length > 0);
});
});