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

@@ -793,6 +793,36 @@ excalidraw-plugin`;
});
describe('error handling', () => {
test('handles decompression failure gracefully', () => {
// Mock atob to throw an error to simulate decompression failure
// This covers the catch block for compressed data decompression errors
const originalAtob = global.atob;
global.atob = jest.fn(() => {
throw new Error('Invalid base64 string');
});
const content = `excalidraw-plugin
## Drawing
\`\`\`compressed-json
N4KAkARALgngDgUwgLgAQQQDwMYEMA2AlgCYBOuA7hADTgQBuCpAzoQPYB2KqATL
\`\`\``;
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
const result = FrontmatterUtils.parseExcalidrawMetadata(content);
expect(result.isExcalidraw).toBe(true);
expect(result.elementCount).toBe(0);
expect(result.hasCompressedData).toBe(true);
expect(result.metadata).toEqual({ compressed: true });
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to process compressed Excalidraw data:',
expect.anything()
);
consoleErrorSpy.mockRestore();
global.atob = originalAtob;
});
test('handles JSON parse error gracefully', () => {
const content = `excalidraw-plugin
\`\`\`json