#!/usr/bin/env node
/**
* Test script to verify FoundryVTT connection and basic functionality
* Run with: npm run test-connection
*/
import dotenv from 'dotenv';
import { FoundryClient } from '../src/foundry/client.js';
import { config } from '../src/config/index.js';
import { logger } from '../src/utils/logger.js';
dotenv.config();
async function testConnection() {
console.log('π§ͺ FoundryVTT MCP Server - Connection Test\n');
try {
console.log('π Configuration:');
console.log(` URL: ${config.foundry.url}`);
console.log(` REST Module: ${config.foundry.useRestModule ? 'β
' : 'β'}`);
console.log(` API Key: ${config.foundry.apiKey ? 'β
Configured' : 'β Not set'}`);
console.log(` Username: ${config.foundry.username ? 'β
Configured' : 'β Not set'}\n`);
// Initialize client
const client = new FoundryClient({
baseUrl: config.foundry.url,
useRestModule: config.foundry.useRestModule,
apiKey: config.foundry.apiKey,
username: config.foundry.username,
password: config.foundry.password,
timeout: config.foundry.timeout,
});
console.log('π Testing connection...');
const connected = await client.testConnection();
if (connected) {
console.log('β
Connection successful!\n');
} else {
console.log('β Connection failed\n');
return;
}
// Test dice rolling
console.log('π² Testing dice rolling...');
try {
const roll = await client.rollDice('1d20+5', 'Connection test');
console.log(` Result: ${roll.total} (${roll.breakdown})`);
console.log('β
Dice rolling works!\n');
} catch (error) {
console.log(`β Dice rolling failed: ${error instanceof Error ? error.message : error}\n`);
}
// Test actor search
console.log('π₯ Testing actor search...');
try {
const actors = await client.searchActors({ limit: 3 });
console.log(` Found ${actors.length} actors`);
if (actors.length > 0) {
actors.forEach(actor => {
console.log(` - ${actor.name} (${actor.type})`);
});
console.log('β
Actor search works!\n');
} else {
console.log('βΉοΈ No actors found (may require REST API module)\n');
}
} catch (error) {
console.log(`β οΈ Actor search limited: ${error instanceof Error ? error.message : error}\n`);
}
// Test scene info
console.log('πΊοΈ Testing scene information...');
try {
const scene = await client.getCurrentScene();
console.log(` Current scene: ${scene.name}`);
console.log(` Dimensions: ${scene.width}x${scene.height}`);
console.log('β
Scene information works!\n');
} catch (error) {
console.log(`β οΈ Scene info limited: ${error instanceof Error ? error.message : error}\n`);
}
// Test WebSocket connection
console.log('π Testing WebSocket connection...');
try {
await client.connectWebSocket();
console.log('β
WebSocket connection established!\n');
// Give it a moment to connect
await new Promise(resolve => setTimeout(resolve, 2000));
await client.disconnect();
console.log('β
WebSocket disconnected cleanly\n');
} catch (error) {
console.log(`β οΈ WebSocket connection issues: ${error instanceof Error ? error.message : error}\n`);
}
console.log('π Connection test completed!');
console.log('\nπ Summary:');
console.log(' - Basic connection: β
');
console.log(' - Dice rolling: β
');
console.log(` - Data access: ${config.foundry.useRestModule ? 'β
Full' : 'β οΈ Limited'}`);
console.log(` - WebSocket: ${config.foundry.useRestModule ? 'β
' : 'β οΈ Basic'}`);
if (!config.foundry.useRestModule) {
console.log('\nπ‘ Tips for enhanced functionality:');
console.log(' 1. Install the "Foundry REST API" module in FoundryVTT');
console.log(' 2. Get an API key from https://foundryvtt-rest-api-relay.fly.dev/');
console.log(' 3. Set USE_REST_MODULE=true and FOUNDRY_API_KEY in .env');
console.log(' 4. Restart the MCP server');
}
} catch (error) {
console.error('β Test failed:', error);
console.log('\nπ§ Troubleshooting:');
console.log(' 1. Ensure FoundryVTT is running');
console.log(' 2. Check FOUNDRY_URL in .env file');
console.log(' 3. Verify network connectivity');
console.log(' 4. Review the setup guide: SETUP_GUIDE.md');
process.exit(1);
}
}
// Helper to check if script is run directly
if (import.meta.url === `file://${process.argv[1]}`) {
testConnection().catch(console.error);
}
export { testConnection };