import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { appendAgentRuntimeContext, resolveHouseholdTimezone } from "../../src/modules/agent/tools"; import { createApiToolExecutor, resolveInternalApiBase, } 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("resolveInternalApiBase", () => { it("uses loopback instead of the public request origin", () => { const original = process.env.INTERNAL_API_BASE_URL; const originalPort = process.env.PORT; delete process.env.INTERNAL_API_BASE_URL; process.env.PORT = "3000"; const base = resolveInternalApiBase(new Request("https://fam.ginnoir.com/api/agent/chat")); assert.equal(base, "http://127.0.0.1:3000"); if (original === undefined) delete process.env.INTERNAL_API_BASE_URL; else process.env.INTERNAL_API_BASE_URL = original; if (originalPort === undefined) delete process.env.PORT; else process.env.PORT = originalPort; }); it("honors INTERNAL_API_BASE_URL when set", () => { const original = process.env.INTERNAL_API_BASE_URL; process.env.INTERNAL_API_BASE_URL = "http://127.0.0.1:3010/"; const base = resolveInternalApiBase(new Request("https://fam.ginnoir.com/api/agent/chat")); assert.equal(base, "http://127.0.0.1:3010"); if (original === undefined) delete process.env.INTERNAL_API_BASE_URL; else process.env.INTERNAL_API_BASE_URL = original; }); }); 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.includes("/api/v1/calendars") && (!init?.method || init.method === "GET")) { return Response.json([ { id: "cal-1", name: "Family" }, { id: "cal-2", name: "Work" }, ]); } if (url.includes("/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("https://fam.ginnoir.com/api/agent/chat", { headers: { cookie: "authjs.session-token=test" }, }), ); 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); assert.match(posts[0]!.path, /^http:\/\/127\.0\.0\.1:3000\/api\/v1\/events/); 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.includes("/api/v1/calendars") && (!init?.method || init.method === "GET")) { return Response.json([ { id: "cal-1", name: "Family" }, { id: "cal-2", name: "Work" }, ]); } if (url.includes("/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; }); it("returns a clear error when the internal API is unreachable", async () => { const originalFetch = globalThis.fetch; globalThis.fetch = (async () => { throw new TypeError("fetch failed"); }) as typeof fetch; const execute = createApiToolExecutor(new Request("https://fam.ginnoir.com/api/agent/chat")); const result = JSON.parse(await execute("list_calendars", "{}")) as { status: number; body: { error?: string }; }; assert.equal(result.status, 502); assert.match(String(result.body.error), /unreachable/i); globalThis.fetch = originalFetch; }); });