|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import path from 'node:path' |
| 5 | +import sharp from 'sharp' |
| 6 | +import { describe, expect, it, vi } from 'vitest' |
| 7 | +import type { ContentMeta } from '@/lib/content/schema' |
| 8 | +import { |
| 9 | + buildArticleJsonLd, |
| 10 | + buildAuthorMetadata, |
| 11 | + buildCollectionPageJsonLd, |
| 12 | + buildIndexMetadata, |
| 13 | + buildPostMetadata, |
| 14 | +} from '@/lib/content/seo' |
| 15 | +import { buildLandingMetadata, LANDING_SOCIAL_IMAGE } from '@/lib/landing/seo' |
| 16 | +import teamAuthor from '@/content/authors/sim.json' |
| 17 | + |
| 18 | +vi.mock('@/lib/core/utils/urls', () => ({ SITE_URL: 'https://example.com' })) |
| 19 | + |
| 20 | +const SECTIONS = [ |
| 21 | + { name: 'Blog', basePath: '/blog', description: 'Latest posts' }, |
| 22 | + { name: 'Library', basePath: '/library', description: 'Guides and comparisons' }, |
| 23 | + { name: 'Customers', basePath: '/customers', description: 'Customer stories' }, |
| 24 | +] |
| 25 | + |
| 26 | +const POST: ContentMeta = { |
| 27 | + slug: 'example', |
| 28 | + title: 'Example article', |
| 29 | + description: 'An article with its own cover image.', |
| 30 | + date: '2026-01-01T00:00:00.000Z', |
| 31 | + author: teamAuthor, |
| 32 | + authors: [teamAuthor], |
| 33 | + tags: ['guides'], |
| 34 | + ogImage: '/blog/example/cover.jpg', |
| 35 | + ogImageWidth: 1600, |
| 36 | + ogImageHeight: 900, |
| 37 | + canonical: 'https://example.com/blog/example', |
| 38 | + draft: false, |
| 39 | + featured: false, |
| 40 | + technical: true, |
| 41 | +} |
| 42 | + |
| 43 | +describe('content social images', () => { |
| 44 | + it.each(SECTIONS)('$name shares the landing image across Open Graph and Twitter', (section) => { |
| 45 | + const metadata = buildIndexMetadata(section, { pageNum: 1 }) |
| 46 | + const landing = buildLandingMetadata({ |
| 47 | + title: 'Home', |
| 48 | + description: 'The workspace', |
| 49 | + path: '', |
| 50 | + }) |
| 51 | + |
| 52 | + expect(metadata.openGraph?.images).toEqual([ |
| 53 | + { |
| 54 | + ...LANDING_SOCIAL_IMAGE, |
| 55 | + url: `https://example.com${LANDING_SOCIAL_IMAGE.url}`, |
| 56 | + alt: `Sim ${section.name}`, |
| 57 | + }, |
| 58 | + ]) |
| 59 | + expect(metadata.twitter?.images).toEqual({ |
| 60 | + url: `https://example.com${LANDING_SOCIAL_IMAGE.url}`, |
| 61 | + alt: `Sim ${section.name}`, |
| 62 | + }) |
| 63 | + expect(landing.openGraph?.images).toEqual([{ ...LANDING_SOCIAL_IMAGE, alt: 'Home' }]) |
| 64 | + expect(metadata.alternates?.canonical).toBe(`https://example.com${section.basePath}`) |
| 65 | + }) |
| 66 | + |
| 67 | + it('declares the actual dimensions and format of the shared image', async () => { |
| 68 | + const metadata = await sharp(path.join('public', LANDING_SOCIAL_IMAGE.url)).metadata() |
| 69 | + |
| 70 | + expect(metadata).toMatchObject({ |
| 71 | + width: LANDING_SOCIAL_IMAGE.width, |
| 72 | + height: LANDING_SOCIAL_IMAGE.height, |
| 73 | + format: 'png', |
| 74 | + }) |
| 75 | + expect(LANDING_SOCIAL_IMAGE.type).toBe('image/png') |
| 76 | + }) |
| 77 | + |
| 78 | + it('preserves filtered titles, canonical URLs, and noindex policy', () => { |
| 79 | + const metadata = buildIndexMetadata(SECTIONS[0], { tag: 'guides', pageNum: 2 }) |
| 80 | + |
| 81 | + expect(metadata.title).toBe('Blog | guides | Page 2') |
| 82 | + expect(metadata.alternates?.canonical).toBe('https://example.com/blog') |
| 83 | + expect(metadata.robots).toEqual({ index: false, follow: true }) |
| 84 | + expect(metadata.openGraph?.images).toEqual( |
| 85 | + buildIndexMetadata(SECTIONS[0], { pageNum: 1 }).openGraph?.images |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('retains article-specific artwork and dimensions', () => { |
| 90 | + const metadata = buildPostMetadata(POST) |
| 91 | + |
| 92 | + expect(metadata.openGraph?.images).toEqual([ |
| 93 | + { |
| 94 | + url: `https://example.com${POST.ogImage}`, |
| 95 | + width: 1600, |
| 96 | + height: 900, |
| 97 | + alt: POST.title, |
| 98 | + }, |
| 99 | + ]) |
| 100 | + expect(metadata.twitter?.images).toEqual([POST.ogImage]) |
| 101 | + expect(buildArticleJsonLd(POST).image[0].url).toBe(`https://example.com${POST.ogImage}`) |
| 102 | + }) |
| 103 | + |
| 104 | + it('uses the current square logo for publishers and the team avatar', async () => { |
| 105 | + const article = buildArticleJsonLd(POST) |
| 106 | + const collection = buildCollectionPageJsonLd(SECTIONS[0], [POST]) |
| 107 | + |
| 108 | + expect(teamAuthor.avatarUrl).toBe('/brandbook/logo/small.png') |
| 109 | + expect(article.publisher.logo.url).toBe(`https://example.com${teamAuthor.avatarUrl}`) |
| 110 | + expect(collection.publisher.logo.url).toBe(article.publisher.logo.url) |
| 111 | + expect(await sharp(path.join('public', teamAuthor.avatarUrl)).metadata()).toMatchObject({ |
| 112 | + width: 1200, |
| 113 | + height: 1200, |
| 114 | + format: 'png', |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it.each([teamAuthor.avatarUrl, 'https://example.com/avatar.jpg'])( |
| 119 | + 'keeps the author avatar without inventing dimensions for %s', |
| 120 | + (avatarUrl) => { |
| 121 | + const metadata = buildAuthorMetadata(SECTIONS[0], teamAuthor.id, { |
| 122 | + ...teamAuthor, |
| 123 | + avatarUrl, |
| 124 | + }) |
| 125 | + |
| 126 | + expect(metadata.openGraph?.images).toEqual([{ url: avatarUrl, alt: teamAuthor.name }]) |
| 127 | + } |
| 128 | + ) |
| 129 | +}) |
0 commit comments