Skip to content

Commit dd747ce

Browse files
vladholubievclaude
andcommitted
fix: improve LibreOffice conversion error detection and add comprehensive test setup
Fixed conversion error handling to check for actual conversion success pattern instead of relying on stderr presence, as LibreOffice outputs status messages to stderr even on successful conversions. Added organized test structure with batch conversion support for multiple file types. - Fix conversion success detection by checking for '->' pattern in output - Add test-data directory with sample DOCX and HTML files - Implement batch conversion testing with volume mounts - Use library's canBeConvertedToPDF validation function - Include generated PDFs for regression testing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 108d04a commit dd747ce

File tree

8 files changed

+153
-11
lines changed

8 files changed

+153
-11
lines changed

src/convert.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ export async function convertTo(filename: string, format: string): Promise<strin
4242
await cleanupTempFiles();
4343
}
4444

45-
if (err) {
45+
// Check if conversion was successful by looking for the output pattern in logs
46+
const logsStr = logs.toString();
47+
48+
if (!logsStr.includes('->')) {
4649
throw new Error(`Cannot generate PDF preview for .${path.extname(outputFilename)} file`, {
47-
cause: logs,
50+
cause: err || logs,
4851
});
4952
}
5053

51-
return getConvertedFilePath(logs.toString());
54+
return getConvertedFilePath(logsStr);
5255
}

test/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FROM public.ecr.aws/shelf/lambda-libreoffice-base:7.6-node20-x86_64
22

33
COPY test-bundled.js ${LAMBDA_TASK_ROOT}/test.js
4+
COPY test-data ${LAMBDA_TASK_ROOT}/test-data
45
CMD [ "test.handler" ]

test/test-data/test-document.docx

83.5 KB
Binary file not shown.

test/test-data/test-document.pdf

76.2 KB
Binary file not shown.

test/test-data/test.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Test HTML Document</title>
6+
<style>
7+
body {
8+
font-family: Arial, sans-serif;
9+
margin: 40px;
10+
}
11+
h1 {
12+
color: #333;
13+
}
14+
.info {
15+
background: #f0f0f0;
16+
padding: 10px;
17+
border-radius: 5px;
18+
}
19+
</style>
20+
</head>
21+
<body>
22+
<h1>Test HTML Document</h1>
23+
<p>This is a test HTML document for PDF conversion testing.</p>
24+
25+
<div class="info">
26+
<h2>Test Information</h2>
27+
<ul>
28+
<li>Generated for AWS Lambda LibreOffice testing</li>
29+
<li>Conversion type: HTML to PDF</li>
30+
<li>Package type: ESM with CommonJS transpilation</li>
31+
</ul>
32+
</div>
33+
34+
<p>
35+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut
36+
labore et dolore magna aliqua.
37+
</p>
38+
</body>
39+
</html>

test/test-data/test.pdf

35.9 KB
Binary file not shown.

test/test.js

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,85 @@
1-
import {writeFileSync} from 'fs';
2-
import {convertTo} from './lib/index.js';
1+
import {copyFileSync, existsSync, readdirSync} from 'fs';
2+
import {basename, extname} from 'path';
3+
import {canBeConvertedToPDF, convertTo} from './lib/index.js';
34

45
export const handler = async () => {
5-
writeFileSync('/tmp/test.txt', Buffer.from('Hello World!'));
6+
const results = [];
67

7-
const convertedFilePath = await convertTo('test.txt', `pdf`);
8+
try {
9+
// Get all files from test-data directory
10+
const testDataDir = './test-data';
811

9-
console.log({convertedFilePath});
12+
if (!existsSync(testDataDir)) {
13+
throw new Error(`Test data directory ${testDataDir} not found`);
14+
}
15+
16+
const files = readdirSync(testDataDir);
17+
const supportedFiles = files.filter(
18+
file =>
19+
// Skip already converted PDFs and use the validation function
20+
!file.endsWith('.pdf') && canBeConvertedToPDF(file)
21+
);
22+
23+
console.log(`Found ${supportedFiles.length} files to convert:`, supportedFiles);
24+
25+
// Process each file
26+
for (const file of supportedFiles) {
27+
try {
28+
console.log(`\nProcessing: ${file}`);
29+
30+
// Copy file to /tmp for conversion
31+
const sourcePath = `${testDataDir}/${file}`;
32+
const tmpPath = `/tmp/${file}`;
33+
copyFileSync(sourcePath, tmpPath);
34+
console.log(`Copied to ${tmpPath}`);
35+
36+
// Convert to PDF
37+
const convertedFilePath = await convertTo(file, 'pdf');
38+
console.log(`Converted to: ${convertedFilePath}`);
39+
40+
// Copy the PDF to test-data directory (mounted volume)
41+
const outputFileName = `${basename(file, extname(file))}.pdf`;
42+
const outputPath = `/tmp/test-data/${outputFileName}`;
43+
44+
if (existsSync('/tmp/test-data')) {
45+
copyFileSync(convertedFilePath, outputPath);
46+
console.log(`PDF saved to: ${outputPath}`);
47+
results.push({
48+
input: file,
49+
output: outputFileName,
50+
status: 'success',
51+
});
52+
} else {
53+
console.log('Output directory /tmp/test-data not found - volume not mounted?');
54+
results.push({
55+
input: file,
56+
output: outputFileName,
57+
status: 'no-output-dir',
58+
});
59+
}
60+
} catch (error) {
61+
console.error(`Failed to convert ${file}:`, error.message);
62+
results.push({
63+
input: file,
64+
status: 'error',
65+
error: error.message,
66+
});
67+
}
68+
}
69+
70+
console.log('\n=== Conversion Summary ===');
71+
console.log(JSON.stringify(results, null, 2));
72+
73+
return {
74+
statusCode: 200,
75+
body: JSON.stringify({
76+
message: `Processed ${results.length} files`,
77+
results,
78+
}),
79+
};
80+
} catch (error) {
81+
console.error('Batch conversion failed:', error);
82+
console.error('Error stack:', error.stack);
83+
throw error;
84+
}
1085
};

test/test.sh

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,31 @@ cp ./dist/test.js ./test-bundled.js
1010

1111
podman build --platform linux/amd64 -t lo-lambda-test .
1212

13-
(sleep 7; curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}') &
14-
(sleep 30; CID=$(cat ./cid) && podman stop $CID && rm ./cid) &
13+
# Ensure test-data directory exists
14+
mkdir -p ./test-data
1515

16-
podman run --platform linux/amd64 -p 9000:8080 --rm --cidfile ./cid lo-lambda-test
16+
# Run container with volume mount for test-data
17+
echo "Starting container with volume mount for test-data..."
18+
echo "Input files in test-data:"
19+
ls -la test-data/
20+
21+
CONTAINER_NAME="lo-lambda-test-$(date +%s)"
22+
23+
# Invoke the Lambda function after a delay
24+
(sleep 7; curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"batch conversion"}') &
25+
26+
# Stop after 30 seconds
27+
(sleep 30; podman stop ${CONTAINER_NAME}) &
28+
29+
# Mount the test-data directory for both input and output
30+
podman run --platform linux/amd64 \
31+
-p 9000:8080 \
32+
--name ${CONTAINER_NAME} \
33+
-v $(pwd)/test-data:/tmp/test-data:Z \
34+
--rm \
35+
lo-lambda-test
36+
37+
echo ""
38+
echo "=== Conversion Complete ==="
39+
echo "Generated PDFs in test-data:"
40+
ls -la test-data/*.pdf 2>/dev/null || echo "No PDFs generated"

0 commit comments

Comments
 (0)