|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { getLogoDataUri, loadLogo, loadFont } from './og-utils'; |
| 4 | + |
| 5 | +const PACKAGE_ROOT = path.resolve(__dirname, '../../..'); |
| 6 | +const FIXTURES = path.resolve(__dirname, '__fixtures__'); |
| 7 | + |
| 8 | +describe('getLogoDataUri', () => { |
| 9 | + test('svg file returns svg mime type', () => { |
| 10 | + const data = Buffer.from('<svg></svg>'); |
| 11 | + const result = getLogoDataUri(data, '/logo.svg'); |
| 12 | + expect(result).toStartWith('data:image/svg+xml;base64,'); |
| 13 | + }); |
| 14 | + |
| 15 | + test('png file returns png mime type', () => { |
| 16 | + const data = Buffer.from('fake-png'); |
| 17 | + const result = getLogoDataUri(data, '/logo.png'); |
| 18 | + expect(result).toStartWith('data:image/png;base64,'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('jpg file returns jpeg mime type', () => { |
| 22 | + const data = Buffer.from('fake-jpg'); |
| 23 | + const result = getLogoDataUri(data, '/photo.jpg'); |
| 24 | + expect(result).toStartWith('data:image/jpeg;base64,'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('returns null for unsupported format', () => { |
| 28 | + const data = Buffer.from('fake-webp'); |
| 29 | + const result = getLogoDataUri(data, '/logo.webp'); |
| 30 | + expect(result).toBeNull(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('encodes data as base64', () => { |
| 34 | + const content = '<svg xmlns="http://www.w3.org/2000/svg"></svg>'; |
| 35 | + const data = Buffer.from(content); |
| 36 | + const result = getLogoDataUri(data, '/icon.svg'); |
| 37 | + const base64 = result!.split(',')[1]; |
| 38 | + expect(Buffer.from(base64, 'base64').toString()).toBe(content); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('loadLogo', () => { |
| 43 | + test('returns null for nonexistent file', async () => { |
| 44 | + const result = await loadLogo('/nonexistent', '/logo.svg'); |
| 45 | + expect(result).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('strips leading slash from logo path', async () => { |
| 49 | + const result = await loadLogo('/nonexistent', '/nested/logo.svg'); |
| 50 | + expect(result).toBeNull(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('loadFont', () => { |
| 55 | + test('loads Inter font from package', async () => { |
| 56 | + const font = await loadFont(PACKAGE_ROOT); |
| 57 | + expect(font.byteLength).toBeGreaterThan(0); |
| 58 | + }); |
| 59 | + |
| 60 | + test('throws for invalid path', async () => { |
| 61 | + expect(loadFont('/nonexistent')).rejects.toThrow(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments