import { mapMatrixAssetToSquizLink, mapMatrixAssetToSquizImage, type MatrixAssetLike, } from './mapMatrixAssetToSquizPrimitive'; describe('mapMatrixAssetToSquizPrimitive', () => { describe('mapMatrixAssetToSquizLink', () => { it('should map asset and link to SquizLink with text from matrixLinkAsset', () => { const asset: MatrixAssetLike = { url: 'https://example.com/page', type: 'link', attributes: { name: 'Fallback Name' }, }; const matrixLinkAsset = { matrixAssetId: '123', matrixIdentifier: 'prod', text: 'Click here', target: '_blank' as const, }; const result = mapMatrixAssetToSquizLink(asset, matrixLinkAsset); expect(result).toEqual({ text: 'Click here', url: 'https://example.com/page', target: '_blank', }); }); it('should fall back to asset.attributes.name when matrixLinkAsset.text is empty', () => { const asset: MatrixAssetLike = { url: 'https://example.com/page', type: 'link', attributes: { name: 'Link Name' }, }; const matrixLinkAsset = { matrixAssetId: '123', matrixIdentifier: 'prod', text: '', target: '_self' as const, }; const result = mapMatrixAssetToSquizLink(asset, matrixLinkAsset); expect(result.text).toBe('Link Name'); expect(result.url).toBe('https://example.com/page'); expect(result.target).toBe('_self'); }); it('should fall back to empty string when neither text nor attributes.name present', () => { const asset: MatrixAssetLike = { url: 'https://example.com/page', type: 'link', attributes: {}, }; const matrixLinkAsset = { matrixAssetId: '123', matrixIdentifier: 'prod', target: '_parent' as const, }; const result = mapMatrixAssetToSquizLink(asset, matrixLinkAsset); expect(result).toEqual({ text: '', url: 'https://example.com/page', target: '_parent', }); }); it('should use empty url when asset.url is missing', () => { const asset = { url: undefined as unknown as string, type: 'link', attributes: {}, } as MatrixAssetLike; const matrixLinkAsset = { matrixAssetId: '123', matrixIdentifier: 'prod', text: 'Link', target: '_top' as const, }; const result = mapMatrixAssetToSquizLink(asset, matrixLinkAsset); expect(result.url).toBe(''); }); }); describe('mapMatrixAssetToSquizImage', () => { it('should throw when asset type is not image or image_variety', () => { const asset: MatrixAssetLike = { url: 'https://example.com/file.pdf', type: 'document', attributes: {}, }; expect(() => mapMatrixAssetToSquizImage(asset)).toThrow('Matrix asset is not an image: document'); }); it('should map image asset to SquizImage shape', () => { const asset: MatrixAssetLike = { url: 'https://example.com/photo.jpg', type: 'image', attributes: { title: 'Photo Title', alt: 'Alt text', caption: 'Caption text', width: 800, height: 600, size: 102400, }, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.name).toBe('Photo Title'); expect(result.alt).toBe('Alt text'); expect(result.caption).toBe('Caption text'); expect(result.imageVariations.original).toMatchObject({ url: 'https://example.com/photo.jpg', width: 800, height: 600, byteSize: 102400, mimeType: 'image/jpeg', aspectRatio: '4:3', sha1Hash: '', }); expect(result.imageVariations.aspectRatios).toEqual([]); }); it('should use variety_ prefix for image_variety type', () => { const asset: MatrixAssetLike = { url: 'https://example.com/variety.webp', type: 'image_variety', attributes: { title: 'Variety', variety_width: 400, variety_height: 300, variety_size: 5000, }, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.imageVariations.original).toMatchObject({ url: 'https://example.com/variety.webp', width: 400, height: 300, byteSize: 5000, mimeType: 'image/webp', }); }); it('should map mime type from url extension', () => { const cases = [ { url: 'https://example.com/a.jpg', mime: 'image/jpeg' }, { url: 'https://example.com/b.jpeg', mime: 'image/jpeg' }, { url: 'https://example.com/c.png', mime: 'image/png' }, { url: 'https://example.com/d.gif', mime: 'image/gif' }, { url: 'https://example.com/e.webp', mime: 'image/webp' }, { url: 'https://example.com/f.svg', mime: 'image/svg+xml' }, { url: 'https://example.com/g.jpg?query=1', mime: 'image/jpeg' }, ]; cases.forEach(({ url, mime }) => { const result = mapMatrixAssetToSquizImage({ url, type: 'image', attributes: { width: 1, height: 1, size: 1 }, }); expect(result.imageVariations.original.mimeType).toBe(mime); }); }); it('should default to image/jpeg when extension is unknown', () => { const result = mapMatrixAssetToSquizImage({ url: 'https://example.com/file.xyz', type: 'image', attributes: { width: 1, height: 1, size: 1 }, }); expect(result.imageVariations.original.mimeType).toBe('image/jpeg'); }); it('should include aspect ratios from additional.varieties', () => { const asset: MatrixAssetLike = { url: 'https://example.com/main.jpg', type: 'image', attributes: { title: 'Main', width: 1600, height: 900, size: 200000, }, additional: { varieties: [ { url: 'https://example.com/16x9.jpg', type: 'image_variety', attributes: { variety_width: 1600, variety_height: 900, variety_size: 100000, }, }, ], }, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.imageVariations.aspectRatios).toHaveLength(1); expect(result.imageVariations.aspectRatios![0]).toMatchObject({ url: 'https://example.com/16x9.jpg', width: 1600, height: 900, }); }); it('should handle missing additional or varieties', () => { const asset: MatrixAssetLike = { url: 'https://example.com/photo.jpg', type: 'image', attributes: { width: 100, height: 100, size: 0 }, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.imageVariations.aspectRatios).toEqual([]); }); it('should calculate aspect ratio for variety (e.g. 16:9)', () => { const asset: MatrixAssetLike = { url: 'https://example.com/v.jpg', type: 'image_variety', attributes: { variety_width: 1920, variety_height: 1080, variety_size: 0, }, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.imageVariations.original.aspectRatio).toBe('16:9'); }); it('should default aspect ratio to 4:3 when dimensions missing for variety', () => { const asset: MatrixAssetLike = { url: 'https://example.com/v.jpg', type: 'image_variety', attributes: {}, }; const result = mapMatrixAssetToSquizImage(asset); expect(result.imageVariations.original.aspectRatio).toBe('4:3'); }); }); });