From 353c294f671b79a7b69490001c762ea8a5beba68 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Fri, 13 Jun 2025 13:47:07 -0400 Subject: [PATCH] add unit tests for extract-media-from-html.ts --- .../misc/extract-media-from-html.ts | 297 ++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 packages/backend/test/unit/core/activitypub/misc/extract-media-from-html.ts diff --git a/packages/backend/test/unit/core/activitypub/misc/extract-media-from-html.ts b/packages/backend/test/unit/core/activitypub/misc/extract-media-from-html.ts new file mode 100644 index 0000000000..0e24934b19 --- /dev/null +++ b/packages/backend/test/unit/core/activitypub/misc/extract-media-from-html.ts @@ -0,0 +1,297 @@ +/* + * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { extractMediaFromHtml } from '@/core/activitypub/misc/extract-media-from-html.js'; + +describe(extractMediaFromHtml, () => { + it('should return empty for invalid input', () => { + const result = extractMediaFromHtml(' { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should return empty for input without attachments', () => { + const result = extractMediaFromHtml('
No media here!
'); + expect(result).toEqual([]); + }); + + it('should extract img tags', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Image', + url: 'https://example.com/img.png', + name: null, + }]); + }); + + it('should ignore img tags without src', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract picture tags with img', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Image', + url: 'https://example.com/picture.png', + name: null, + }]); + }); + + it('should ignore picture tags without img', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should ignore picture tags without src', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract object tags', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Document', + url: 'https://example.com/object.dat', + name: null, + }]); + }); + + it('should ignore object tags without data', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract object tags with img fallback', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Image', + url: 'https://example.com/object.png', + name: null, + }]); + }); + + it('should ignore object tags with empty img fallback', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract embed tags', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Document', + url: 'https://example.com/embed.dat', + name: null, + }]); + }); + + it('should ignore embed tags without src', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract audio tags', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Audio', + url: 'https://example.com/audio.mp3', + name: null, + }]); + }); + + it('should ignore audio tags without src', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract video tags', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([{ + type: 'Video', + url: 'https://example.com/video.mp4', + name: null, + }]); + }); + + it('should ignore video tags without src', () => { + const result = extractMediaFromHtml(''); + expect(result).toEqual([]); + }); + + it('should extract alt text from alt property', () => { + const result = extractMediaFromHtml(` + img tag + picture tag + + object tag + +