Spaces:
Running
Running
| // test-api.js - Test script to verify the API is working | |
| // Run this with: node test-api.js | |
| const API_URL = process.env.REUBENOS_URL || 'https://mcp-1st-birthday-reuben-os.hf.space'; | |
| async function testAPI() { | |
| const sessionId = 'session_1763722877048_527d6bb8b7473568'; | |
| console.log('π Testing Reuben OS API...\n'); | |
| console.log(`API URL: ${API_URL}`); | |
| console.log(`Session ID: ${sessionId}\n`); | |
| try { | |
| // Test 1: Save a regular file | |
| console.log('π Test 1: Saving a regular file...'); | |
| const saveResponse = await fetch(`${API_URL}/api/mcp-handler`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| sessionId: sessionId, | |
| action: 'save_file', | |
| fileName: 'test.txt', | |
| content: 'This is a test file created at ' + new Date().toISOString() | |
| }) | |
| }); | |
| const saveData = await saveResponse.json(); | |
| console.log('Save response:', saveData); | |
| console.log('β File save:', saveData.success ? 'SUCCESS' : 'FAILED'); | |
| console.log(''); | |
| // Test 2: Deploy a quiz | |
| console.log('π― Test 2: Deploying a quiz...'); | |
| const quizData = { | |
| title: 'Test Quiz', | |
| description: 'A simple test quiz', | |
| questions: [ | |
| { | |
| id: 'q1', | |
| question: 'Is this working?', | |
| type: 'true-false', | |
| correctAnswer: true, | |
| points: 1 | |
| } | |
| ] | |
| }; | |
| const quizResponse = await fetch(`${API_URL}/api/mcp-handler`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| sessionId: sessionId, | |
| action: 'deploy_quiz', | |
| fileName: 'quiz.json', | |
| content: JSON.stringify(quizData, null, 2) | |
| }) | |
| }); | |
| const quizResponseData = await quizResponse.json(); | |
| console.log('Quiz response:', quizResponseData); | |
| console.log('β Quiz deploy:', quizResponseData.success ? 'SUCCESS' : 'FAILED'); | |
| console.log(''); | |
| // Test 3: Retrieve files | |
| console.log('π Test 3: Retrieving files for session...'); | |
| const getResponse = await fetch(`${API_URL}/api/mcp-handler?sessionId=${sessionId}`, { | |
| method: 'GET', | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| const getData = await getResponse.json(); | |
| console.log('Retrieve response:', { | |
| success: getData.success, | |
| fileCount: getData.count, | |
| files: getData.files?.map(f => ({ | |
| name: f.name, | |
| size: f.size, | |
| isQuiz: f.isQuiz | |
| })) | |
| }); | |
| console.log('β File retrieval:', getData.success ? 'SUCCESS' : 'FAILED'); | |
| if (getData.success && getData.files) { | |
| console.log('\nπ Files found:'); | |
| getData.files.forEach(file => { | |
| console.log(` - ${file.name} (${file.size} bytes)${file.isQuiz ? ' [QUIZ]' : ''}`); | |
| if (file.name === 'quiz.json' && file.content) { | |
| console.log(' Quiz content preview:', file.content.substring(0, 100) + '...'); | |
| } | |
| }); | |
| } | |
| // Test 4: Save to public folder | |
| console.log('\nπ’ Test 4: Saving to public folder...'); | |
| const publicResponse = await fetch(`${API_URL}/api/mcp-handler`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| sessionId: sessionId, | |
| action: 'save_public', | |
| fileName: 'public_test.txt', | |
| content: 'This is a public file' | |
| }) | |
| }); | |
| const publicData = await publicResponse.json(); | |
| console.log('Public save response:', publicData); | |
| console.log('β Public save:', publicData.success ? 'SUCCESS' : 'FAILED'); | |
| // Summary | |
| console.log('\n' + '='.repeat(50)); | |
| console.log('π TEST SUMMARY:'); | |
| console.log(` Session ID: ${sessionId}`); | |
| console.log(` Files in session: ${getData.count || 0}`); | |
| console.log(` Quiz detected: ${getData.files?.some(f => f.isQuiz) ? 'YES' : 'NO'}`); | |
| console.log('='.repeat(50)); | |
| } catch (error) { | |
| console.error('β Test failed with error:', error); | |
| console.error('\nMake sure:'); | |
| console.error('1. Your Next.js server is running (npm run dev)'); | |
| console.error('2. The API endpoint is accessible'); | |
| console.error('3. The /tmp directory is writable'); | |
| } | |
| } | |
| // Run the test | |
| testAPI(); |