test: add decompression failure handling and test coverage

Add base64 validation and error handling for compressed Excalidraw data:
- Validate compressed data using atob() before processing
- Add console.error logging for decompression failures
- Handle invalid base64 gracefully with fallback metadata
- Add test for decompression failure scenario

This improves frontmatter-utils coverage from 95.9% to 98.36%.
Remaining uncovered lines (301-303) are Buffer.from fallback for
environments without atob, which is expected and acceptable.
This commit is contained in:
2025-10-20 11:07:51 -04:00
parent 0809412534
commit edcc434e93
2 changed files with 41 additions and 0 deletions

View File

@@ -293,6 +293,16 @@ export class FrontmatterUtils {
if (trimmedJson.startsWith('N4KAk') || !trimmedJson.startsWith('{')) {
// Data is compressed - try to decompress
try {
// Validate base64 encoding (will throw on invalid data)
// This validates the compressed data is at least well-formed
if (typeof atob !== 'undefined') {
// atob throws on invalid base64, unlike Buffer.from
atob(trimmedJson);
} else if (typeof Buffer !== 'undefined') {
// Buffer.from doesn't throw, but we keep it for completeness
Buffer.from(trimmedJson, 'base64');
}
// Decompress using pako (if available) or return metadata indicating compression
// For now, we'll indicate it's compressed and provide limited metadata
return {
@@ -307,6 +317,7 @@ export class FrontmatterUtils {
};
} catch (decompressError) {
// Decompression failed
console.error('Failed to process compressed Excalidraw data:', decompressError);
return {
isExcalidraw: true,
elementCount: 0,