-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-karaoke.js
More file actions
139 lines (116 loc) Β· 4.48 KB
/
Copy pathtest-karaoke.js
File metadata and controls
139 lines (116 loc) Β· 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dotenv/config';
import express from 'express';
import bodyParser from 'body-parser';
import KaraokeService from '#services/karaoke.service.js';
// Create a simple Express server to handle callbacks
const app = express();
app.use(bodyParser.json());
const PORT = process.env.PORT || 3000;
// Store task IDs and results
const tasks = new Map();
// Test function to generate a song from a repository
async function testGenerateSongFromRepo() {
try {
console.log('π§ͺ Starting test: Generate song from GitHub repository');
// Configure callback server
const callbackUrl = (process.env.CALLBACK_URL || `http://localhost:${PORT}`) + '/callback';
// Repository to analyze (change this to your target repository)
const repoUrl = 'https://github.com/near/near-cli-rs';
// Generate song
const result = await KaraokeService.generateSongFromRepo({
repoUrl,
timeRange: 'week', // 'day', 'week', or 'custom'
// For custom time range, uncomment these lines:
// startDate: new Date('2023-01-01'),
// endDate: new Date('2023-01-31'),
musicStyle: 'Rock', // Choose music style
instrumental: false, // Set to true for instrumental music
callbackUrl
});
console.log('β
Song generation initiated successfully:');
console.log(JSON.stringify(result, null, 2));
// Store task ID
tasks.set(result.song.taskId, {
status: 'pending',
repository: result.repository,
song: result.song
});
// Optionally wait for completion
console.log('β³ Waiting for song generation to complete...');
const completedTask = await KaraokeService.waitForSongCompletion(result.song.taskId);
console.log('β
Song generation completed:', completedTask);
return result;
} catch (error) {
console.error('β Test failed:', error.message);
throw error;
}
}
// Callback handler
app.post('/callback', async (req, res) => {
try {
console.log('π Received callback from Suno API');
console.log(JSON.stringify(req.body, null, 2));
// Process callback
const result = await KaraokeService.handleSunoCallback(req.body);
// Update task status in local tasks map
// `result.doAttachments` typically contains info of the DO upload, including URLs
if (req.body.data?.task_id && tasks.has(req.body.data.task_id)) {
const taskId = req.body.data.task_id;
const task = tasks.get(taskId);
// Mark the task as completed
task.status = 'completed';
// Save the result with doAttachments, etc.
task.result = result;
tasks.set(taskId, task);
}
// Send JSON response including the doAttachments so you can see the DO URLs
res.json({
status: 'success',
message: 'Callback processed',
doAttachments: result.doAttachments || []
});
} catch (error) {
console.error('β Error processing callback:', error.message);
res.status(500).json({ status: 'error', message: error.message });
}
});
// Route to check task status
app.get('/tasks/:taskId', async (req, res) => {
try {
const { taskId } = req.params;
// Check if task is in our local cache
if (tasks.has(taskId)) {
// This object should now contain doAttachments in `task.result.doAttachments` if the callback was processed
res.json({ status: 'success', task: tasks.get(taskId) });
return;
}
// If not in cache, check with Suno API
const result = await KaraokeService.checkSongStatus(taskId);
res.json({ status: 'success', task: result });
} catch (error) {
console.error('β Error checking task:', error.message);
res.status(500).json({ status: 'error', message: error.message });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`π Server running on port ${PORT}`);
console.log(`π Callback URL: http://localhost:${PORT}/callback`);
// Start the test when the server is ready
if (process.env.AUTO_START === 'true') {
console.log('π Auto-starting test...');
testGenerateSongFromRepo()
.then(() => console.log('β
Test completed successfully'))
.catch(error => console.error('β Test failed:', error));
} else {
console.log('βΉοΈ Run the test manually with: node test-karaoke.js test');
}
});
// Run test if called directly
if (process.argv[2] === 'test') {
testGenerateSongFromRepo()
.then(() => console.log('β
Test completed successfully'))
.catch(error => console.error('β Test failed:', error));
}
// Export for programmatic usage
export { testGenerateSongFromRepo };