-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-test-data.ts
More file actions
175 lines (155 loc) · 5.44 KB
/
seed-test-data.ts
File metadata and controls
175 lines (155 loc) · 5.44 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* StudioBot.ai - Test Data Seeding Script
* Seeds the database with test data for integration testing
* Run before test-api.ts to ensure tests pass with real data
*/
import http from 'http';
const BASE_URL = 'http://localhost:3000';
// Helper to make HTTP requests
function makeRequest(
method: string,
path: string,
body?: any
): Promise<{ status: number; data: any }> {
return new Promise((resolve, reject) => {
const url = new URL(path, BASE_URL);
const options = {
method,
hostname: url.hostname,
port: url.port || 3000,
path: url.pathname + url.search,
headers: {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
try {
resolve({ status: res.statusCode || 200, data: JSON.parse(data) });
} catch {
resolve({ status: res.statusCode || 200, data });
}
});
});
req.on('error', reject);
if (body) req.write(JSON.stringify(body));
req.end();
});
}
async function seedTestData() {
console.log('🌱 Seeding test data...\n');
// Step 1: Create test user
console.log('1️⃣ Creating test user...');
const testUser = {
username: `testuser_${Date.now()}`,
email: `test_${Date.now()}@example.com`,
password: 'TestPassword123!',
};
const registerRes = await makeRequest('POST', '/api/auth/register', testUser);
if (registerRes.status !== 201) {
console.error('❌ Failed to register user:', registerRes.data);
return;
}
const userId = registerRes.data.data?.id || 'test-user-id';
console.log(`✅ User created: ${userId}`);
// Step 2: Upload test video
console.log('\n2️⃣ Uploading test video...');
const uploadRes = await makeRequest('POST', '/api/videos/upload', {
user_id: userId,
source_url: 'https://exemple.com/test-video.mp4',
title: 'Test Video for Integration Tests',
description: 'This is a test video used for API testing',
});
let videoId = 'test-video-id';
if (uploadRes.status === 201 && uploadRes.data.data?.id) {
videoId = uploadRes.data.data.id;
console.log(`✅ Video uploaded: ${videoId}`);
} else {
console.log(`⚠️ Video upload returned status ${uploadRes.status}, using placeholder ID`);
}
// Step 3: Create test clip
console.log('\n3️⃣ Creating test clip...');
const clipRes = await makeRequest('POST', '/api/clips/create', {
video_id: videoId,
user_id: userId,
title: 'Test Clip from Integration Tests',
description: 'This clip was created for testing',
start_time: 10,
end_time: 30,
});
let clipId = 'test-clip-id';
if (clipRes.status === 201 && clipRes.data.data?.id) {
clipId = clipRes.data.data.id;
console.log(`✅ Clip created: ${clipId}`);
} else {
console.log(`⚠️ Clip creation returned status ${clipRes.status}`);
}
// Step 4: Create test short
console.log('\n4️⃣ Creating test short...');
const shortRes = await makeRequest('POST', '/api/shorts/create', {
clip_id: clipId,
user_id: userId,
title: 'Test Short Video',
description: 'Short form content for testing',
});
let shortId = 'test-short-id';
if (shortRes.status === 201 && shortRes.data.data?.id) {
shortId = shortRes.data.data.id;
console.log(`✅ Short created: ${shortId}`);
} else {
console.log(`⚠️ Short creation returned status ${shortRes.status}`);
}
// Step 5: Generate test thumbnail
console.log('\n5️⃣ Generating test thumbnail...');
const thumbnailRes = await makeRequest('POST', '/api/thumbnails/generate', {
user_id: userId,
video_id: videoId,
timestamp: 15,
text: 'Test Thumbnail',
});
let thumbnailId = 'test-thumb-id';
if (thumbnailRes.status === 201 && thumbnailRes.data.data?.id) {
thumbnailId = thumbnailRes.data.data.id;
console.log(`✅ Thumbnail generated: ${thumbnailId}`);
} else {
console.log(`⚠️ Thumbnail generation returned status ${thumbnailRes.status}`);
}
// Step 6: Create test distribution
console.log('\n6️⃣ Creating test distribution...');
const distRes = await makeRequest('POST', '/api/distributions/publish', {
user_id: userId,
video_id: videoId,
platforms: ['youtube'],
title: 'Test Distribution',
description: 'Test video for distribution',
});
let distId = 'test-dist-id';
if (distRes.status === 201 && distRes.data.data?.id) {
distId = distRes.data.data.id;
console.log(`✅ Distribution created: ${distId}`);
} else {
console.log(`⚠️ Distribution creation returned status ${distRes.status}`);
}
// Output test data for reference
console.log('\n' + '='.repeat(60));
console.log('TEST DATA SUMMARY');
console.log('='.repeat(60));
console.log(`\nUser ID: ${userId}`);
console.log(`Video ID: ${videoId}`);
console.log(`Clip ID: ${clipId}`);
console.log(`Short ID: ${shortId}`);
console.log(`Thumbnail ID: ${thumbnailId}`);
console.log(`Distribution ID: ${distId}`);
console.log(`\nLogin Credentials:`);
console.log(` Email: ${testUser.email}`);
console.log(` Password: ${testUser.password}`);
console.log('\n' + '='.repeat(60));
console.log('✅ Test data seeding complete!\n');
console.log('You can now run: npx ts-node test-api.ts\n');
}
seedTestData().catch((error) => {
console.error('❌ Error seeding test data:', error);
process.exit(1);
});