Files
famapp/tests/unit/agent-tools-runtime.test.ts
T
ginnoir d3e79edb9e
CI / checks (push) Has been cancelled
fix(agent): raise tool rounds and ease calendar creates
Local models were burning the 8-round budget on simple event adds.
Raise the limit, resolve calendars by name/default, and inject the
household clock so relative dates work.
2026-07-08 21:40:44 -05:00

118 lines
4.3 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { appendAgentRuntimeContext, resolveHouseholdTimezone } from "../../src/modules/agent/tools";
import { createApiToolExecutor } from "../../src/modules/agent/tool-executor";
describe("appendAgentRuntimeContext", () => {
it("appends current time and timezone to the prompt", () => {
const original = process.env.HOUSEHOLD_TIMEZONE;
process.env.HOUSEHOLD_TIMEZONE = "America/Chicago";
const now = new Date("2026-07-09T14:30:00.000Z");
const result = appendAgentRuntimeContext("Be helpful.", now);
assert.match(result, /^Be helpful\./);
assert.match(result, /Current time:/);
assert.match(result, /America\/Chicago/);
assert.match(result, /2026-07-09T14:30:00\.000Z/);
assert.match(result, /Resolve relative dates from this clock/);
if (original === undefined) delete process.env.HOUSEHOLD_TIMEZONE;
else process.env.HOUSEHOLD_TIMEZONE = original;
});
});
describe("resolveHouseholdTimezone", () => {
it("prefers HOUSEHOLD_TIMEZONE over TZ", () => {
const originalHousehold = process.env.HOUSEHOLD_TIMEZONE;
const originalTz = process.env.TZ;
process.env.HOUSEHOLD_TIMEZONE = "America/New_York";
process.env.TZ = "UTC";
assert.equal(resolveHouseholdTimezone(), "America/New_York");
if (originalHousehold === undefined) delete process.env.HOUSEHOLD_TIMEZONE;
else process.env.HOUSEHOLD_TIMEZONE = originalHousehold;
if (originalTz === undefined) delete process.env.TZ;
else process.env.TZ = originalTz;
});
});
describe("create_event calendar resolution", () => {
it("uses the first calendar when calendarId and calendarName are omitted", async () => {
const originalFetch = globalThis.fetch;
const posts: Array<{ path: string; body: unknown }> = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
return Response.json([
{ id: "cal-1", name: "Family" },
{ id: "cal-2", name: "Work" },
]);
}
if (url.endsWith("/api/v1/events") && init?.method === "POST") {
const body = JSON.parse(String(init.body));
posts.push({ path: url, body });
return Response.json({ id: "evt-1", ...body }, { status: 201 });
}
return new Response("not found", { status: 404 });
}) as typeof fetch;
const execute = createApiToolExecutor(new Request("http://localhost:3000/api/agent/chat"));
const result = JSON.parse(
await execute(
"create_event",
JSON.stringify({
title: "Dentist",
startAt: "2026-07-10T15:00:00.000Z",
endAt: "2026-07-10T16:00:00.000Z",
}),
),
) as { status: number; body: { calendarId?: string } };
assert.equal(result.status, 201);
assert.equal(result.body.calendarId, "cal-1");
assert.equal(posts.length, 1);
globalThis.fetch = originalFetch;
});
it("matches calendarName case-insensitively", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = String(input);
if (url.endsWith("/api/v1/calendars") && (!init?.method || init.method === "GET")) {
return Response.json([
{ id: "cal-1", name: "Family" },
{ id: "cal-2", name: "Work" },
]);
}
if (url.endsWith("/api/v1/events") && init?.method === "POST") {
const body = JSON.parse(String(init.body));
return Response.json({ id: "evt-1", ...body }, { status: 201 });
}
return new Response("not found", { status: 404 });
}) as typeof fetch;
const execute = createApiToolExecutor(new Request("http://localhost:3000/api/agent/chat"));
const result = JSON.parse(
await execute(
"create_event",
JSON.stringify({
calendarName: "work",
title: "Standup",
startAt: "2026-07-10T15:00:00.000Z",
endAt: "2026-07-10T15:30:00.000Z",
}),
),
) as { status: number; body: { calendarId?: string } };
assert.equal(result.status, 201);
assert.equal(result.body.calendarId, "cal-2");
globalThis.fetch = originalFetch;
});
});