131 lines
4.1 KiB
TypeScript
131 lines
4.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const callGatewayMock = vi.fn();
|
|
vi.mock("../../gateway/call.js", () => ({
|
|
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
}));
|
|
|
|
import { createCronTool } from "./cron-tool.js";
|
|
|
|
describe("cron tool", () => {
|
|
beforeEach(() => {
|
|
callGatewayMock.mockReset();
|
|
callGatewayMock.mockResolvedValue({ ok: true });
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"update",
|
|
{ action: "update", jobId: "job-1", patch: { foo: "bar" } },
|
|
{ id: "job-1", patch: { foo: "bar" } },
|
|
],
|
|
[
|
|
"update",
|
|
{ action: "update", id: "job-2", patch: { foo: "bar" } },
|
|
{ id: "job-2", patch: { foo: "bar" } },
|
|
],
|
|
["remove", { action: "remove", jobId: "job-1" }, { id: "job-1" }],
|
|
["remove", { action: "remove", id: "job-2" }, { id: "job-2" }],
|
|
["run", { action: "run", jobId: "job-1" }, { id: "job-1" }],
|
|
["run", { action: "run", id: "job-2" }, { id: "job-2" }],
|
|
["runs", { action: "runs", jobId: "job-1" }, { id: "job-1" }],
|
|
["runs", { action: "runs", id: "job-2" }, { id: "job-2" }],
|
|
])("%s sends id to gateway", async (action, args, expectedParams) => {
|
|
const tool = createCronTool();
|
|
await tool.execute("call1", args);
|
|
|
|
expect(callGatewayMock).toHaveBeenCalledTimes(1);
|
|
const call = callGatewayMock.mock.calls[0]?.[0] as {
|
|
method?: string;
|
|
params?: unknown;
|
|
};
|
|
expect(call.method).toBe(`cron.${action}`);
|
|
expect(call.params).toEqual(expectedParams);
|
|
});
|
|
|
|
it("prefers jobId over id when both are provided", async () => {
|
|
const tool = createCronTool();
|
|
await tool.execute("call1", {
|
|
action: "run",
|
|
jobId: "job-primary",
|
|
id: "job-legacy",
|
|
});
|
|
|
|
const call = callGatewayMock.mock.calls[0]?.[0] as {
|
|
params?: unknown;
|
|
};
|
|
expect(call?.params).toEqual({ id: "job-primary" });
|
|
});
|
|
|
|
it("normalizes cron.add job payloads", async () => {
|
|
const tool = createCronTool();
|
|
await tool.execute("call2", {
|
|
action: "add",
|
|
job: {
|
|
data: {
|
|
name: "wake-up",
|
|
schedule: { atMs: 123 },
|
|
payload: { text: "hello" },
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(callGatewayMock).toHaveBeenCalledTimes(1);
|
|
const call = callGatewayMock.mock.calls[0]?.[0] as {
|
|
method?: string;
|
|
params?: unknown;
|
|
};
|
|
expect(call.method).toBe("cron.add");
|
|
expect(call.params).toEqual({
|
|
name: "wake-up",
|
|
schedule: { kind: "at", atMs: 123 },
|
|
sessionTarget: "main",
|
|
wakeMode: "next-heartbeat",
|
|
payload: { kind: "systemEvent", text: "hello" },
|
|
});
|
|
});
|
|
|
|
it("adds recent context for systemEvent reminders when session key is available", async () => {
|
|
callGatewayMock
|
|
.mockResolvedValueOnce({
|
|
messages: [
|
|
{ role: "user", content: [{ type: "text", text: "Discussed Q2 budget" }] },
|
|
{
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "We agreed to review on Tuesday." }],
|
|
},
|
|
{ role: "user", content: [{ type: "text", text: "Remind me about the thing at 2pm" }] },
|
|
],
|
|
})
|
|
.mockResolvedValueOnce({ ok: true });
|
|
|
|
const tool = createCronTool({ agentSessionKey: "main" });
|
|
await tool.execute("call3", {
|
|
action: "add",
|
|
job: {
|
|
name: "reminder",
|
|
schedule: { atMs: 123 },
|
|
payload: { text: "Reminder: the thing." },
|
|
},
|
|
});
|
|
|
|
expect(callGatewayMock).toHaveBeenCalledTimes(2);
|
|
const historyCall = callGatewayMock.mock.calls[0]?.[0] as {
|
|
method?: string;
|
|
params?: unknown;
|
|
};
|
|
expect(historyCall.method).toBe("chat.history");
|
|
|
|
const cronCall = callGatewayMock.mock.calls[1]?.[0] as {
|
|
method?: string;
|
|
params?: { payload?: { text?: string } };
|
|
};
|
|
expect(cronCall.method).toBe("cron.add");
|
|
const text = cronCall.params?.payload?.text ?? "";
|
|
expect(text).toContain("Recent context:");
|
|
expect(text).toContain("User: Discussed Q2 budget");
|
|
expect(text).toContain("Assistant: We agreed to review on Tuesday.");
|
|
expect(text).toContain("User: Remind me about the thing at 2pm");
|
|
});
|
|
});
|