Initial commit: Discord-Claude Gateway with event-driven agent runtime

This commit is contained in:
2026-02-22 13:59:57 -05:00
parent b4f340b610
commit f2247ea3ac
28 changed files with 2056 additions and 205 deletions

View File

@@ -2,6 +2,17 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { CronScheduler, type CronJob } from "../../src/cron-scheduler.js";
import type { Event } from "../../src/event-queue.js";
vi.mock("../../src/logger.js", () => ({
logger: {
info: vi.fn(),
debug: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
import { logger } from "../../src/logger.js";
// Mock node-cron
vi.mock("node-cron", () => {
const tasks: Array<{ expression: string; callback: () => void; stopped: boolean }> = [];
@@ -57,6 +68,7 @@ describe("CronScheduler", () => {
beforeEach(() => {
mockCron._clearTasks();
vi.mocked(logger.warn).mockClear();
scheduler = new CronScheduler();
});
@@ -185,7 +197,6 @@ Instruction: This should not be parsed either`;
});
it("skips jobs with invalid cron expressions and logs a warning", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const enqueue: EnqueueFn = (event) =>
({ ...event, id: 1, timestamp: new Date() }) as Event;
@@ -195,14 +206,10 @@ Instruction: This should not be parsed either`;
scheduler.start(jobs, enqueue);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining("bad-job")
);
expect(warnSpy).toHaveBeenCalledWith(
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({ name: "bad-job" }),
expect.stringContaining("invalid cron expression")
);
warnSpy.mockRestore();
});
it("schedules valid jobs and skips invalid ones in the same batch", () => {
@@ -211,7 +218,6 @@ Instruction: This should not be parsed either`;
enqueued.push(event);
return { ...event, id: enqueued.length, timestamp: new Date() } as Event;
};
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const jobs: CronJob[] = [
{ name: "bad-job", expression: "invalid", instruction: "Bad" },
@@ -220,14 +226,12 @@ Instruction: This should not be parsed either`;
scheduler.start(jobs, enqueue);
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledTimes(1);
// Only the valid job should have been scheduled — fire all
mockCron._fireAll();
expect(enqueued).toHaveLength(1);
expect(enqueued[0].payload).toEqual({ instruction: "Good", jobName: "good-job" });
warnSpy.mockRestore();
});
});