// Test script for note context menu functionality // Run with: node test-note-context-menu.mjs const http = require('http'); const BASE_URL = 'http://localhost:3000'; async function testEndpoint(method, path, data = null) { return new Promise((resolve, reject) => { const options = { hostname: 'localhost', port: 3000, path: path, method: method, headers: { 'Content-Type': 'application/json', }, }; const req = http.request(options, (res) => { let body = ''; res.on('data', (chunk) => { body += chunk; }); res.on('end', () => { try { const response = { statusCode: res.statusCode, headers: res.headers, body: body ? JSON.parse(body) : null }; resolve(response); } catch (e) { resolve({ statusCode: res.statusCode, headers: res.headers, body: body }); } }); }); req.on('error', (err) => { reject(err); }); if (data) { req.write(JSON.stringify(data)); } req.end(); }); } async function runTests() { console.log('๐Ÿงช Testing Note Context Menu Endpoints\n'); try { // Test 1: Create a test note console.log('1๏ธโƒฃ Creating test note...'); const createData = { fileName: 'test-context-menu-note', folderPath: '/', frontmatter: { status: 'test', color: '#3B82F6', favoris: false }, content: '# Test Note\n\nThis is a test note for context menu functionality.' }; const createResponse = await testEndpoint('POST', '/api/vault/notes', createData); console.log(` Status: ${createResponse.statusCode}`); if (createResponse.statusCode === 200) { console.log(' โœ… Note created successfully'); console.log(` ๐Ÿ“ Note ID: ${createResponse.body.id}`); const noteId = createResponse.body.id; // Test 2: Update note frontmatter (toggle favorite) console.log('\n2๏ธโƒฃ Testing frontmatter update (toggle favorite)...'); const updateData = { frontmatter: { favoris: true, color: '#22C55E' } }; const updateResponse = await testEndpoint('PATCH', `/api/vault/notes/${noteId}`, updateData); console.log(` Status: ${updateResponse.statusCode}`); if (updateResponse.statusCode === 200) { console.log(' โœ… Frontmatter updated successfully'); console.log(` โญ Favorite: ${updateResponse.body.frontmatter.favoris}`); console.log(` ๐ŸŽจ Color: ${updateResponse.body.frontmatter.color}`); } else { console.log(' โŒ Frontmatter update failed'); console.log(` Error: ${updateResponse.body?.error || 'Unknown error'}`); } // Test 3: Delete note (move to trash) console.log('\n3๏ธโƒฃ Testing note deletion (move to trash)...'); const deleteResponse = await testEndpoint('DELETE', `/api/vault/notes/${noteId}`); console.log(` Status: ${deleteResponse.statusCode}`); if (deleteResponse.statusCode === 200) { console.log(' โœ… Note moved to trash successfully'); console.log(` ๐Ÿ—‘๏ธ Trash path: ${deleteResponse.body.trashPath}`); } else { console.log(' โŒ Note deletion failed'); console.log(` Error: ${deleteResponse.body?.error || 'Unknown error'}`); } } else { console.log(' โŒ Note creation failed'); console.log(` Error: ${createResponse.body?.error || 'Unknown error'}`); } // Test 4: Test invalid note ID console.log('\n4๏ธโƒฃ Testing invalid note ID...'); const invalidResponse = await testEndpoint('PATCH', '/api/vault/notes/nonexistent-note', { frontmatter: { test: true } }); console.log(` Status: ${invalidResponse.statusCode}`); if (invalidResponse.statusCode === 404) { console.log(' โœ… Correctly returned 404 for nonexistent note'); } else { console.log(' โŒ Should have returned 404'); } console.log('\n๐ŸŽ‰ All tests completed!'); } catch (error) { console.error('โŒ Test failed with error:', error.message); console.log('\n๐Ÿ’ก Make sure the server is running on http://localhost:3000'); console.log(' Start the server with: npm start'); } } // Check if server is running async function checkServer() { try { await testEndpoint('GET', '/api/vault/metadata'); return true; } catch (error) { return false; } } async function main() { console.log('๐Ÿ” Checking if server is running...'); const serverRunning = await checkServer(); if (!serverRunning) { console.log('โŒ Server is not running on http://localhost:3000'); console.log('๐Ÿ’ก Please start the server first with: npm start'); process.exit(1); } console.log('โœ… Server is running\n'); await runTests(); } main();