server.test.tsā¢5.89 kB
import { ClipboardServer } from '../server.js';
import { DatabaseManager } from '../lib/database.js';
import path from 'path';
import fs from 'fs';
import os from 'os';
describe('ClipboardServer', () => {
let testDbPath: string;
let dbManager: DatabaseManager;
let server: ClipboardServer;
beforeEach(() => {
// Create temp database for testing
const uniqueId = `${Date.now()}-${process.pid}-${Math.random().toString(36).slice(2, 9)}`;
testDbPath = path.join(os.tmpdir(), `test-clipboard-${uniqueId}.db`);
dbManager = new DatabaseManager(testDbPath);
});
afterEach(() => {
dbManager.close();
const keyPath = dbManager.getEncryptionKeyPath();
if (keyPath && fs.existsSync(keyPath)) {
fs.unlinkSync(keyPath);
}
if (fs.existsSync(testDbPath)) {
fs.unlinkSync(testDbPath);
}
});
describe('Server Initialization', () => {
test('should initialize with server info', () => {
server = new ClipboardServer(dbManager);
const serverInfo = server.getServerInfo();
expect(serverInfo).toBeDefined();
expect(serverInfo.name).toBe('cut-copy-paste-mcp');
expect(serverInfo.version).toBeDefined();
});
test('should register all required tools', () => {
server = new ClipboardServer(dbManager);
const tools = server.getRegisteredTools();
expect(tools).toBeDefined();
expect(tools.length).toBe(6);
const toolNames = tools.map((t) => t.name);
expect(toolNames).toContain('copy_lines');
expect(toolNames).toContain('cut_lines');
expect(toolNames).toContain('paste_lines');
expect(toolNames).toContain('show_clipboard');
expect(toolNames).toContain('undo_last_paste');
expect(toolNames).toContain('get_operation_history');
});
test('should create session on initialization', () => {
server = new ClipboardServer(dbManager);
const sessionId = server.getSessionId();
expect(sessionId).toBeDefined();
expect(typeof sessionId).toBe('string');
expect(sessionId.length).toBeGreaterThan(0);
});
});
describe('Tool Registration', () => {
beforeEach(() => {
server = new ClipboardServer(dbManager);
});
test('copy_lines tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const copyTool = tools.find((t) => t.name === 'copy_lines');
expect(copyTool).toBeDefined();
expect(copyTool?.description).toContain('Copy lines from a file');
expect(copyTool?.inputSchema).toBeDefined();
expect(copyTool?.inputSchema.properties).toHaveProperty('file_path');
expect(copyTool?.inputSchema.properties).toHaveProperty('start_line');
expect(copyTool?.inputSchema.properties).toHaveProperty('end_line');
expect(copyTool?.inputSchema.required).toContain('file_path');
expect(copyTool?.inputSchema.required).toContain('start_line');
expect(copyTool?.inputSchema.required).toContain('end_line');
});
test('cut_lines tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const cutTool = tools.find((t) => t.name === 'cut_lines');
expect(cutTool).toBeDefined();
expect(cutTool?.description).toContain('Cut lines from a file');
expect(cutTool?.inputSchema).toBeDefined();
expect(cutTool?.inputSchema.properties).toHaveProperty('file_path');
expect(cutTool?.inputSchema.properties).toHaveProperty('start_line');
expect(cutTool?.inputSchema.properties).toHaveProperty('end_line');
});
test('paste_lines tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const pasteTool = tools.find((t) => t.name === 'paste_lines');
expect(pasteTool).toBeDefined();
expect(pasteTool?.description).toContain('Paste clipboard content');
expect(pasteTool?.inputSchema).toBeDefined();
expect(pasteTool?.inputSchema.properties).toHaveProperty('targets');
expect(pasteTool?.inputSchema.required).toContain('targets');
});
test('show_clipboard tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const showTool = tools.find((t) => t.name === 'show_clipboard');
expect(showTool).toBeDefined();
expect(showTool?.description).toContain('Display current clipboard');
expect(showTool?.inputSchema).toBeDefined();
});
test('undo_last_paste tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const undoTool = tools.find((t) => t.name === 'undo_last_paste');
expect(undoTool).toBeDefined();
expect(undoTool?.description).toContain('Undo the most recent paste');
expect(undoTool?.inputSchema).toBeDefined();
});
test('get_operation_history tool should have correct schema', () => {
const tools = server.getRegisteredTools();
const historyTool = tools.find((t) => t.name === 'get_operation_history');
expect(historyTool).toBeDefined();
expect(historyTool?.description).toContain('operation history');
expect(historyTool?.inputSchema).toBeDefined();
expect(historyTool?.inputSchema.properties).toHaveProperty('limit');
});
});
describe('Session Management', () => {
test('should cleanup expired sessions on startup', () => {
// Create server which should trigger cleanup
server = new ClipboardServer(dbManager);
// This should not throw
expect(() => server.cleanupExpiredSessions()).not.toThrow();
});
test('should maintain session throughout server lifetime', () => {
server = new ClipboardServer(dbManager);
const sessionId1 = server.getSessionId();
const sessionId2 = server.getSessionId();
// Session should remain the same
expect(sessionId1).toBe(sessionId2);
});
});
});