feat: Add testing framework and initial test cases for various tools and database operations
This commit is contained in:
92
tests/unit/tools/common/tasks.test.ts
Normal file
92
tests/unit/tools/common/tasks.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* @Date: 2026-01-07 09:11:15
|
||||
* @LastEditors: 陈子健
|
||||
* @LastEditTime: 2026-01-07 10:04:50
|
||||
* @FilePath: /cloud-mcp/tests/unit/tools/common/tasks.test.ts
|
||||
*/
|
||||
/**
|
||||
* Tasks tools tests
|
||||
*/
|
||||
|
||||
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
|
||||
import { registerTaskTools } from "../../../../src/tools/common/tasks.js";
|
||||
import { callTool } from "../../../helpers/tool-helper.js";
|
||||
import { createTempDir } from "../../../helpers/test-utils.js";
|
||||
import { setupTestDatabase } from "../../../helpers/database-helper.js";
|
||||
|
||||
describe("Tasks Tools", () => {
|
||||
let testContext: ReturnType<typeof createTempDir>;
|
||||
let cleanupDb: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
testContext = createTempDir();
|
||||
cleanupDb = setupTestDatabase(testContext);
|
||||
registerTaskTools();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanupDb();
|
||||
testContext.cleanup();
|
||||
});
|
||||
|
||||
test("should add task", async () => {
|
||||
const result = await callTool("task_add", {
|
||||
title: "Test Task",
|
||||
description: "This is a test task",
|
||||
});
|
||||
|
||||
expect(result.content[0].text).toContain("added successfully");
|
||||
expect(result.content[0].text).toContain("Test Task");
|
||||
});
|
||||
|
||||
test("should list tasks", async () => {
|
||||
// Add a task first
|
||||
await callTool("task_add", {
|
||||
title: "Test Task",
|
||||
});
|
||||
|
||||
const result = await callTool("task_list", {});
|
||||
|
||||
expect(result.content[0].text).toContain("Tasks");
|
||||
expect(result.content[0].text).toContain("Test Task");
|
||||
});
|
||||
|
||||
test("should filter tasks by completion status", async () => {
|
||||
// Add a task
|
||||
await callTool("task_add", {
|
||||
title: "Test Task",
|
||||
});
|
||||
|
||||
const allTasks = await callTool("task_list", {});
|
||||
const pendingTasks = await callTool("task_list", { completed: false });
|
||||
|
||||
expect(allTasks.content[0].text).toContain("Tasks");
|
||||
expect(pendingTasks.content[0].text).toContain("Pending");
|
||||
});
|
||||
|
||||
test("should complete task", async () => {
|
||||
// Add a task
|
||||
const addResult = await callTool("task_add", {
|
||||
title: "Test Task",
|
||||
});
|
||||
|
||||
// Extract ID
|
||||
const idMatch = addResult.content[0].text.match(/ID: ([a-f0-9-]+)/);
|
||||
if (!idMatch) {
|
||||
throw new Error("Could not extract ID");
|
||||
}
|
||||
const id = idMatch[1];
|
||||
|
||||
// Complete it
|
||||
const result = await callTool("task_complete", { id });
|
||||
|
||||
expect(result.content[0].text).toContain("marked as completed");
|
||||
});
|
||||
|
||||
test("should handle empty tasks list", async () => {
|
||||
const result = await callTool("task_list", {});
|
||||
|
||||
// The message varies based on completion status filter
|
||||
expect(result.content[0].text).toMatch(/No.*tasks|Use task_add/i);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user