feat: Add testing framework and initial test cases for various tools and database operations

This commit is contained in:
ethan.chen
2026-01-07 10:05:04 +08:00
parent 372b52b214
commit 47ecc40186
23 changed files with 1781 additions and 40 deletions

View File

@@ -0,0 +1,68 @@
/**
* Test utilities and helpers
*/
import { mkdtempSync, rmSync, existsSync } from "fs";
import { join } from "path";
import { tmpdir } from "os";
export interface TestContext {
tempDir: string;
cleanup: () => void;
}
/**
* Create a temporary directory for testing
*/
export function createTempDir(prefix = "mcp-test-"): TestContext {
const tempDir = mkdtempSync(join(tmpdir(), prefix));
return {
tempDir,
cleanup: () => {
if (existsSync(tempDir)) {
rmSync(tempDir, { recursive: true, force: true });
}
},
};
}
/**
* Set test environment variables
*/
export function setTestEnv(env: Record<string, string>): () => void {
const originalEnv: Record<string, string | undefined> = {};
for (const [key, value] of Object.entries(env)) {
originalEnv[key] = process.env[key];
process.env[key] = value;
}
return () => {
for (const [key, originalValue] of Object.entries(originalEnv)) {
if (originalValue === undefined) {
delete process.env[key];
} else {
process.env[key] = originalValue;
}
}
};
}
/**
* Wait for a specified time (for async operations)
*/
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Mock HTTP response helper
*/
export function createMockResponse(data: unknown, status = 200) {
return {
status,
data,
headers: {},
};
}