eval-with-arg.test.ts•4.08 kB
import { test, expect, describe } from './test-fixtures.js';
describe('Eval with Argument Test', () => {
test('should execute function with argument', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: '(arg) => arg.value * 2',
arg: { value: 21 }
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toBe('$results[0] = // eval\n42');
});
test('should execute function without argument', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: '() => "hello world"',
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toBe('$results[0] = // eval\nhello world');
});
test('should execute async function with argument', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: 'async (arg) => { await new Promise(resolve => setTimeout(resolve, 10)); return arg.name.toUpperCase(); }',
arg: { name: 'test' }
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toBe('$results[0] = // eval\nTEST');
});
test('should execute function with complex argument object', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: '(arg) => { console.log("Processing:", arg.user.name); return arg.data.items.length; }',
arg: {
user: { name: 'alice' },
data: { items: ['a', 'b', 'c'] }
}
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toContain('$results[0] = // eval\n3');
expect(content[0]?.text).toContain('[LOG] Processing: alice');
});
test('should execute function that accesses global MCP context with argument', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: '(arg) => { console.log("Checking for MCP context"); return typeof listServers === "function" && arg.testValue === 42; }',
arg: { testValue: 42 }
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toContain('$results[0] = // eval\ntrue');
expect(content[0]?.text).toContain('[LOG] Checking for MCP context');
});
test('should use help function via MCP tool', async ({ client }) => {
const result = await client.callTool({
name: 'help',
arguments: {
server: 'filesystem'
},
});
const content = result.content as Array<{type: string, text: string}>;
const helpText = content[0]?.text || '';
// Should return TypeScript declarations in markdown format
expect(helpText).toContain('## Server: filesystem');
expect(helpText).toContain('TypeScript Declarations');
expect(helpText).toContain('declare const filesystem:');
});
test('should use help function via eval global context', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: 'async (arg) => { const helpInfo = await help(arg.server); return helpInfo.server === arg.server; }',
arg: { server: 'filesystem' }
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toBe('$results[0] = // eval\ntrue');
});
test('should call filesystem server tools via eval', async ({ client }) => {
const result = await client.callTool({
name: 'eval',
arguments: {
function: 'async () => { const files = await filesystem.listDirectory({ path: "." }); return files.length > 0; }',
},
});
const content = result.content as Array<{type: string, text: string}>;
expect(content[0]?.text).toBe('$results[0] = // eval\ntrue');
});
});