Files
GoClaw/server/tasks.test.ts

129 lines
4.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock db module so tests don't require a real DB connection
vi.mock("./db", () => ({
createTask: vi.fn(),
getAgentTasks: vi.fn(),
getTaskById: vi.fn(),
updateTask: vi.fn(),
deleteTask: vi.fn(),
}));
import {
createTask,
getAgentTasks,
getTaskById,
updateTask,
deleteTask,
} from "./db";
describe("Tasks Management", () => {
const testAgentId = 1;
beforeEach(() => {
vi.clearAllMocks();
});
describe("createTask", () => {
it("should create a new task", async () => {
const mockTask = {
id: 1,
agentId: testAgentId,
title: "Test Task",
description: "This is a test task",
status: "pending",
priority: "high",
createdAt: new Date(),
};
(createTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(mockTask);
const task = await createTask({
agentId: testAgentId,
title: "Test Task",
description: "This is a test task",
status: "pending",
priority: "high",
});
expect(task).toBeDefined();
expect(task?.title).toBe("Test Task");
expect(task?.status).toBe("pending");
expect(task?.priority).toBe("high");
});
it("should return null on DB error", async () => {
(createTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(null);
const task = await createTask({ agentId: 1, title: "x", status: "pending", priority: "low" });
expect(task).toBeNull();
});
});
describe("getAgentTasks", () => {
it("should retrieve all tasks for an agent", async () => {
(getAgentTasks as ReturnType<typeof vi.fn>).mockResolvedValueOnce([
{ id: 1, title: "Task 1", status: "pending" },
{ id: 2, title: "Task 2", status: "done" },
]);
const tasks = await getAgentTasks(testAgentId);
expect(Array.isArray(tasks)).toBe(true);
expect(tasks).toHaveLength(2);
});
it("should return empty array when no tasks", async () => {
(getAgentTasks as ReturnType<typeof vi.fn>).mockResolvedValueOnce([]);
const tasks = await getAgentTasks(testAgentId);
expect(tasks).toEqual([]);
});
});
describe("getTaskById", () => {
it("should return null for non-existent task", async () => {
(getTaskById as ReturnType<typeof vi.fn>).mockResolvedValueOnce(null);
const task = await getTaskById(99999);
expect(task).toBeNull();
});
it("should return task when found", async () => {
const mockTask = { id: 5, title: "Found", status: "done" };
(getTaskById as ReturnType<typeof vi.fn>).mockResolvedValueOnce(mockTask);
const task = await getTaskById(5);
expect(task).toEqual(mockTask);
});
});
describe("updateTask", () => {
it("should update task status", async () => {
const mockUpdated = { id: 1, status: "in_progress", title: "Update Test" };
(createTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ id: 1, title: "Update Test", status: "pending" });
(updateTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(mockUpdated);
const task = await createTask({ agentId: testAgentId, title: "Update Test", status: "pending", priority: "medium" });
if (task?.id) {
const updated = await updateTask(task.id, { status: "in_progress" });
expect(updated?.status).toBe("in_progress");
}
});
it("should return null for non-existent task update", async () => {
(updateTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(null);
const result = await updateTask(99999, { status: "done" });
expect(result).toBeNull();
});
});
describe("deleteTask", () => {
it("should return false for non-existent task", async () => {
(deleteTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(false);
const success = await deleteTask(99999);
expect(success).toBe(false);
});
it("should return true on successful delete", async () => {
(deleteTask as ReturnType<typeof vi.fn>).mockResolvedValueOnce(true);
const success = await deleteTask(1);
expect(success).toBe(true);
});
});
});