Skip to content

Commit 7b84a3e

Browse files
committed
Add test scripts for CreatePrefabTool
Added simple and updated test scripts for the CreatePrefabTool MCP server wrapper to verify registration, parameter handling, and successful execution scenarios.
1 parent ec57b2a commit 7b84a3e

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

Server~/testCreatePrefabTool.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Simple test for CreatePrefabTool
2+
console.log('Testing CreatePrefabTool implementation...');
3+
4+
// Import the tool
5+
import { z } from 'zod';
6+
import { registerCreatePrefabTool } from './build/tools/createPrefabTool.js';
7+
8+
// Mock MCP server
9+
const mockServer = {
10+
tool: (name, description, paramsSchema, handler) => {
11+
console.log(`Registered tool: ${name}`);
12+
console.log(`Description: ${description}`);
13+
console.log(`Parameters schema:`, paramsSchema);
14+
15+
// Test the handler with sample data
16+
if (name === 'create_prefab') {
17+
console.log('\nTesting create_prefab tool handler...');
18+
19+
// Test with minimal parameters
20+
const testParams1 = {
21+
scriptName: 'TestScript',
22+
prefabName: 'TestPrefab'
23+
};
24+
25+
console.log('Test 1 - Minimal parameters:', testParams1);
26+
27+
// Test with field values
28+
const testParams2 = {
29+
scriptName: 'PlayerController',
30+
prefabName: 'Player',
31+
fieldValues: '{"health": 100, "speed": 5.5}'
32+
};
33+
34+
console.log('Test 2 - With field values:', testParams2);
35+
36+
console.log('Tool registration successful!');
37+
}
38+
}
39+
};
40+
41+
// Mock MCP Unity bridge
42+
const mockMcpUnity = {
43+
sendRequest: async (method, params) => {
44+
console.log(`Mock sendRequest called with method: ${method}, params:`, params);
45+
// Return a mock response
46+
return {
47+
success: true,
48+
message: `Prefab created successfully from script ${params.scriptName}`,
49+
prefabPath: `Assets/Prefabs/${params.prefabName}.prefab`
50+
};
51+
}
52+
};
53+
54+
// Mock logger
55+
const mockLogger = {
56+
info: (msg) => console.log(`[INFO] ${msg}`),
57+
error: (msg) => console.log(`[ERROR] ${msg}`)
58+
};
59+
60+
// Test the registration
61+
try {
62+
registerCreatePrefabTool(mockServer, mockMcpUnity, mockLogger);
63+
console.log('\n✅ All tests passed!');
64+
} catch (error) {
65+
console.error('\n❌ Test failed:', error);
66+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Updated test for CreatePrefabTool with optional scriptName
2+
console.log('Testing updated CreatePrefabTool implementation...');
3+
4+
// Import the tool
5+
import { z } from 'zod';
6+
import { registerCreatePrefabTool } from './build/tools/createPrefabTool.js';
7+
8+
// Mock MCP server
9+
const mockServer = {
10+
tool: (name, description, paramsSchema, handler) => {
11+
console.log(`Registered tool: ${name}`);
12+
console.log(`Description: ${description}`);
13+
console.log(`Parameters schema:`, paramsSchema);
14+
15+
// Test the handler with sample data
16+
if (name === 'create_prefab') {
17+
console.log('\nTesting create_prefab tool handler...');
18+
19+
// Test with minimal parameters (only prefab name)
20+
const testParams1 = {
21+
prefabName: 'TestPrefab'
22+
};
23+
24+
console.log('Test 1 - Only prefab name:', testParams1);
25+
26+
// Test with script name and prefab name
27+
const testParams2 = {
28+
scriptName: 'TestScript',
29+
prefabName: 'TestPrefab'
30+
};
31+
32+
console.log('Test 2 - With script name:', testParams2);
33+
34+
// Test with all parameters
35+
const testParams3 = {
36+
scriptName: 'PlayerController',
37+
prefabName: 'Player',
38+
fieldValues: '{"health": 100, "speed": 5.5}'
39+
};
40+
41+
console.log('Test 3 - With all parameters:', testParams3);
42+
43+
console.log('Tool registration successful!');
44+
}
45+
}
46+
};
47+
48+
// Mock MCP Unity bridge
49+
const mockMcpUnity = {
50+
sendRequest: async (method, params) => {
51+
console.log(`Mock sendRequest called with method: ${method}, params:`, params);
52+
// Return a mock response
53+
return {
54+
success: true,
55+
message: params.scriptName ?
56+
`Prefab created successfully from script ${params.scriptName}` :
57+
`Prefab created successfully without script`,
58+
prefabPath: `Assets/Prefabs/${params.prefabName}.prefab`
59+
};
60+
}
61+
};
62+
63+
// Mock logger
64+
const mockLogger = {
65+
info: (msg) => console.log(`[INFO] ${msg}`),
66+
error: (msg) => console.log(`[ERROR] ${msg}`)
67+
};
68+
69+
// Test the registration
70+
try {
71+
registerCreatePrefabTool(mockServer, mockMcpUnity, mockLogger);
72+
console.log('\n✅ All tests passed!');
73+
} catch (error) {
74+
console.error('\n❌ Test failed:', error);
75+
}

0 commit comments

Comments
 (0)