feat: Add testing framework and initial test cases for various tools and database operations
This commit is contained in:
68
tests/helpers/test-utils.ts
Normal file
68
tests/helpers/test-utils.ts
Normal 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: {},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user