001project_wildgrowth/backend/test-notebook-api.js

198 lines
5.8 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 测试 Notebook API 和 Note API 扩展
* 使用方法:需要先启动服务器,然后运行此脚本
* node test-notebook-api.js
*/
const axios = require('axios');
const BASE_URL = 'http://localhost:3000';
let authToken = '';
let testUserId = '';
// 测试用户登录(需要先有一个测试用户)
async function login() {
try {
// 这里需要替换为实际的测试用户手机号和验证码
// 或者使用已有的 token
console.log('⚠️ 请先手动获取 token或修改此脚本使用实际的登录接口');
console.log(' 示例:使用 Postman 或 curl 登录后获取 token');
return null;
} catch (error) {
console.error('登录失败:', error.message);
return null;
}
}
// 测试创建笔记本
async function testCreateNotebook() {
console.log('\n📝 测试 1: 创建笔记本');
try {
const response = await axios.post(
`${BASE_URL}/api/notebooks`,
{
title: '测试笔记本',
description: '这是一个测试笔记本',
},
{
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
}
);
console.log('✅ 创建笔记本成功');
console.log(' 响应:', JSON.stringify(response.data, null, 2));
return response.data.data.notebook;
} catch (error) {
console.error('❌ 创建笔记本失败:', error.response?.data || error.message);
return null;
}
}
// 测试获取笔记本列表
async function testGetNotebooks() {
console.log('\n📚 测试 2: 获取笔记本列表');
try {
const response = await axios.get(
`${BASE_URL}/api/notebooks`,
{
headers: {
'Authorization': `Bearer ${authToken}`,
},
}
);
console.log('✅ 获取笔记本列表成功');
console.log(' 笔记本数量:', response.data.data.total);
console.log(' 笔记本列表:', JSON.stringify(response.data.data.notebooks, null, 2));
return response.data.data.notebooks;
} catch (error) {
console.error('❌ 获取笔记本列表失败:', error.response?.data || error.message);
return null;
}
}
// 测试创建笔记(带笔记本)
async function testCreateNoteWithNotebook(notebookId) {
console.log('\n📄 测试 3: 创建笔记(带笔记本)');
try {
const response = await axios.post(
`${BASE_URL}/api/notes`,
{
notebook_id: notebookId,
type: 'thought',
content: '这是一条测试想法笔记',
// 不提供 course_id 和 node_id测试独立笔记
},
{
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json',
},
}
);
console.log('✅ 创建笔记成功');
console.log(' 响应:', JSON.stringify(response.data, null, 2));
return response.data;
} catch (error) {
console.error('❌ 创建笔记失败:', error.response?.data || error.message);
return null;
}
}
// 测试获取笔记(按笔记本过滤)
async function testGetNotesByNotebook(notebookId) {
console.log('\n🔍 测试 4: 获取笔记(按笔记本过滤)');
try {
const response = await axios.get(
`${BASE_URL}/api/notes?notebook_id=${notebookId}`,
{
headers: {
'Authorization': `Bearer ${authToken}`,
},
}
);
console.log('✅ 获取笔记成功');
console.log(' 笔记数量:', response.data.data.total);
console.log(' 笔记列表:', JSON.stringify(response.data.data.notes, null, 2));
return response.data.data.notes;
} catch (error) {
console.error('❌ 获取笔记失败:', error.response?.data || error.message);
return null;
}
}
// 测试删除笔记本(级联删除笔记)
async function testDeleteNotebook(notebookId) {
console.log('\n🗑 测试 5: 删除笔记本(级联删除笔记)');
try {
const response = await axios.delete(
`${BASE_URL}/api/notebooks/${notebookId}`,
{
headers: {
'Authorization': `Bearer ${authToken}`,
},
}
);
console.log('✅ 删除笔记本成功');
console.log(' 响应:', JSON.stringify(response.data, null, 2));
return true;
} catch (error) {
console.error('❌ 删除笔记本失败:', error.response?.data || error.message);
return false;
}
}
// 主测试函数
async function runTests() {
console.log('🚀 开始测试 Notebook API 和 Note API 扩展\n');
console.log('⚠️ 注意:此测试需要:');
console.log(' 1. 服务器正在运行 (npm run dev)');
console.log(' 2. 有效的认证 token');
console.log(' 3. 测试用户已存在\n');
// 提示用户输入 token
if (!authToken) {
console.log('请提供认证 token或修改脚本中的 authToken 变量)');
console.log('示例:从浏览器开发者工具或 Postman 中获取 token\n');
return;
}
// 测试流程
const notebook = await testCreateNotebook();
if (!notebook) {
console.log('\n❌ 测试失败:无法创建笔记本');
return;
}
const notebooks = await testGetNotebooks();
if (!notebooks || notebooks.length === 0) {
console.log('\n❌ 测试失败:无法获取笔记本列表');
return;
}
const note = await testCreateNoteWithNotebook(notebook.id);
if (!note) {
console.log('\n❌ 测试失败:无法创建笔记');
return;
}
const notes = await testGetNotesByNotebook(notebook.id);
if (!notes || notes.length === 0) {
console.log('\n❌ 测试失败:无法获取笔记列表');
return;
}
// 清理:删除测试数据
await testDeleteNotebook(notebook.id);
console.log('\n✅ 所有测试完成!');
}
// 运行测试
if (require.main === module) {
runTests().catch(console.error);
}
module.exports = { runTests };