- Added PATCH endpoint for updating note frontmatter with YAML parsing and merging - Added DELETE endpoint to move notes to .trash directory with timestamped filenames - Implemented note context menu with actions like duplicate, share, favorite, and delete - Added color picker to context menu with gradient background visualization - Extended NoteFrontmatter type with readOnly and color properties - Added YAML frontmatter parser with support
160 lines
4.8 KiB
JavaScript
160 lines
4.8 KiB
JavaScript
// 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();
|