-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
386 lines (336 loc) · 9.32 KB
/
test.js
File metadata and controls
386 lines (336 loc) · 9.32 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Base URL for API endpoints
const API_BASE_URL = 'http://localhost:3000/api/1';
// Sample HTML content
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Document</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
color: #333;
border-bottom: 1px solid #ccc;
padding-bottom: 10px;
}
p {
line-height: 1.6;
}
</style>
</head>
<body>
<h1>Document Generation Test</h1>
<p>This is a test document to verify that the document generation API is working correctly.</p>
<p>The API should convert this HTML content into the requested format.</p>
<p>Current date and time: ${new Date().toLocaleString()}</p>
</body>
</html>
`;
// Sample HTML with table for Excel test
const htmlWithTable = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Excel Test</title>
</head>
<body>
<h1>Excel Generation Test</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John Doe</td>
<td>john@example.com</td>
<td>${new Date().toLocaleDateString()}</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>jane@example.com</td>
<td>${new Date().toLocaleDateString()}</td>
</tr>
<tr>
<td>3</td>
<td>Bob Johnson</td>
<td>bob@example.com</td>
<td>${new Date().toLocaleDateString()}</td>
</tr>
</tbody>
</table>
</body>
</html>
`;
// Sample HTML with headings for PowerPoint test
const htmlWithHeadings = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PowerPoint Test</title>
</head>
<body>
<h1>PowerPoint Generation Test</h1>
<p>This is the introduction slide content.</p>
<h2>Second Slide</h2>
<p>This is the content for the second slide.</p>
<p>It has multiple paragraphs of information.</p>
<h2>Third Slide</h2>
<p>This is the content for the third slide.</p>
<ul>
<li>Bullet point 1</li>
<li>Bullet point 2</li>
<li>Bullet point 3</li>
</ul>
<h2>Conclusion</h2>
<p>This is the final slide with a conclusion.</p>
<p>Thank you for your attention!</p>
</body>
</html>
`;
// Sample HTML for EPUB test
const htmlForEpub = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EPUB Test</title>
</head>
<body>
<h1>EPUB Generation Test</h1>
<p>This is a test document for EPUB generation.</p>
<h2>Chapter 1: Introduction</h2>
<p>This is the first chapter of our test book.</p>
<p>EPUB is an e-book file format that uses the ".epub" file extension.</p>
<h2>Chapter 2: Features</h2>
<p>EPUB files can be read on many devices and applications.</p>
<p>The format is designed for reflowable content, meaning the text display can be optimized for the particular display device.</p>
<h2>Chapter 3: Conclusion</h2>
<p>This test demonstrates the conversion of HTML content to EPUB format.</p>
<p>Generated on: ${new Date().toLocaleString()}</p>
</body>
</html>
`;
// Sample HTML for Markdown conversion test
const htmlForMarkdown = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Markdown Test</title>
</head>
<body>
<h1>HTML to Markdown Test</h1>
<p>This is a <strong>bold text</strong> and this is <em>italic text</em>.</p>
<h2>Lists</h2>
<h3>Unordered List</h3>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h3>Ordered List</h3>
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
<h2>Code</h2>
<p>Inline <code>code</code> example.</p>
<pre><code>// Code block
function hello() {
console.log("Hello, world!");
}
</code></pre>
<h2>Table</h2>
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
<h2>Link and Image</h2>
<p><a href="https://example.com">Example Link</a></p>
<p>Current date and time: ${new Date().toLocaleString()}</p>
</body>
</html>
`;
// Sample Markdown content
const markdown = `
# Markdown Test
This is a **bold text** and this is *italic text*.
## Lists
### Unordered List
- Item 1
- Item 2
- Item 3
### Ordered List
1. First item
2. Second item
3. Third item
## Code
Inline \`code\` example.
\`\`\`javascript
// Code block
function hello() {
console.log("Hello, world!");
}
\`\`\`
## Table
| Header 1 | Header 2 |
| -------- | -------- |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
## Link and Image
[Example Link](https://example.com)
Current date and time: ${new Date().toLocaleString()}
`;
/**
* Generic function to test an API endpoint
* @param {string} endpoint - API endpoint path
* @param {Object} requestBody - Request body
* @param {string} outputPath - Path to save the output
* @param {string} description - Test description
* @param {Function} processResponse - Function to process the response
* @returns {Promise<boolean>} - Whether the test passed
*/
async function testEndpoint(endpoint, requestBody, outputPath, description, processResponse) {
try {
console.log(`Sending request to ${description}...`);
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`API request failed: ${errorData.error || response.statusText}`);
}
// Process the response based on the endpoint
const result = await processResponse(response);
// Save the result to a file
fs.writeFileSync(outputPath, result);
console.log(`${description} successfully generated and saved to: ${outputPath}`);
return true;
} catch (error) {
console.error(`${description} test failed:`, error.message);
return false;
}
}
async function runTests() {
console.log('Starting API tests...');
// Test HTML to PDF
const pdfResult = await testEndpoint(
'/html-to-pdf',
{ html },
path.join(__dirname, 'test-output.pdf'),
'HTML to PDF API',
async (response) => Buffer.from(await response.arrayBuffer())
);
// Test HTML to DOC
const docResult = await testEndpoint(
'/html-to-doc',
{ html, filename: 'test-document.doc' },
path.join(__dirname, 'test-output.doc'),
'HTML to DOC API',
async (response) => Buffer.from(await response.arrayBuffer())
);
// Test HTML to Excel
const excelResult = await testEndpoint(
'/html-to-excel',
{ html: htmlWithTable, filename: 'test-data.xlsx', sheetName: 'TestData' },
path.join(__dirname, 'test-output.xlsx'),
'HTML to Excel API',
async (response) => Buffer.from(await response.arrayBuffer())
);
// Test HTML to PowerPoint
const pptResult = await testEndpoint(
'/html-to-ppt',
{ html: htmlWithHeadings, filename: 'test-presentation.pptx', title: 'Test Presentation' },
path.join(__dirname, 'test-output.pptx'),
'HTML to PowerPoint API',
async (response) => Buffer.from(await response.arrayBuffer())
);
// Test HTML to EPUB
const epubResult = await testEndpoint(
'/html-to-epub',
{
html: htmlForEpub,
filename: 'test-book.epub',
title: 'Test Book',
author: 'API Test'
},
path.join(__dirname, 'test-output.epub'),
'HTML to EPUB API',
async (response) => Buffer.from(await response.arrayBuffer())
);
// Test HTML to Markdown
const htmlToMarkdownResult = await testEndpoint(
'/html-to-markdown',
{ html: htmlForMarkdown },
path.join(__dirname, 'test-output.md'),
'HTML to Markdown API',
async (response) => {
const result = await response.json();
return result.markdown;
}
);
// Test Markdown to HTML
const markdownToHtmlResult = await testEndpoint(
'/markdown-to-html',
{ markdown },
path.join(__dirname, 'test-output.html'),
'Markdown to HTML API',
async (response) => {
const result = await response.json();
return result.html;
}
);
// Summary
console.log('\nTest Results:');
console.log(`HTML to PDF: ${pdfResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`HTML to DOC: ${docResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`HTML to Excel: ${excelResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`HTML to PowerPoint: ${pptResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`HTML to EPUB: ${epubResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`HTML to Markdown: ${htmlToMarkdownResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
console.log(`Markdown to HTML: ${markdownToHtmlResult ? 'SUCCESS ✓' : 'FAILED ✗'}`);
if (pdfResult && docResult && excelResult && pptResult && epubResult && htmlToMarkdownResult && markdownToHtmlResult) {
console.log('\nAll tests passed successfully!');
} else {
console.log('\nSome tests failed. Check the error messages above.');
}
}
// Wait for the server to start before running the tests
setTimeout(runTests, 2000);