Files
bitsforfree/tests/seo.test.js
T
2026-09-01 20:12:59 -03:00

114 lines
4.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import sharp from 'sharp';
import { feedJson, manifest, renderIndex, sitemapXml } from '../src/seo.js';
const socialPreviewPath = fileURLToPath(new URL('../public/assets/social-preview-v2.78d008df9860.jpg', import.meta.url));
const id = 'a'.repeat(64);
const meme = {
id,
createdAt: '2026-07-24T12:00:00.000Z',
width: 1200,
height: 900,
mime: 'image/webp',
moderationScore: 92
};
const template = [
'<title>__SEO_TITLE__</title>',
'<meta name="description" content="__SEO_DESCRIPTION__">',
'<link rel="canonical" href="__SEO_CANONICAL__">',
'<meta property="og:image" content="__SEO_IMAGE__">',
'<meta property="og:image:type" content="__SEO_IMAGE_TYPE__">',
'<meta name="twitter:image" content="__SEO_IMAGE__">',
'<script nonce="__CSP_NONCE__">__SEO_JSON_LD__</script>'
].join('');
test('renders a meme permalink with meme-specific share metadata', () => {
const html = renderIndex({
template,
baseUrl: 'https://memes.example',
nonce: 'test-nonce',
approvedCount: 1,
meme
});
assert.match(html, /<title>Meme 0xAAAA\.\.\.AAAA \| The Meme Protocol<\/title>/);
assert.match(html, new RegExp(`canonical" href="https://memes\\.example/meme/${id}"`));
assert.match(html, new RegExp(`og:image" content="https://memes\\.example/media/${id}"`));
assert.match(html, /"@type":"ImageObject"/);
assert.match(html, /nonce="test-nonce"/);
});
test('renders the homepage with the dedicated social preview image', () => {
const html = renderIndex({
template,
baseUrl: 'https://memes.example',
nonce: 'home-nonce',
approvedCount: 12
});
assert.match(html, /og:image" content="https:\/\/memes\.example\/assets\/social-preview-v2\.78d008df9860\.jpg"/);
assert.match(html, /og:image:type" content="image\/jpeg"/);
assert.match(html, /twitter:image" content="https:\/\/memes\.example\/assets\/social-preview-v2\.78d008df9860\.jpg"/);
assert.doesNotMatch(html, /yoonect-logo\.png/);
});
test('keeps the social preview small and broadly compatible', async () => {
const metadata = await sharp(socialPreviewPath).metadata();
const stats = fs.statSync(socialPreviewPath);
assert.equal(metadata.format, 'jpeg');
assert.deepEqual([metadata.width, metadata.height], [1200, 630]);
assert.equal(metadata.hasAlpha, false);
assert.equal(metadata.exif, undefined);
assert.equal(metadata.xmp, undefined);
assert.ok(stats.size < 300_000, `expected social preview under 300 KB, got ${stats.size}`);
});
test('uses the supplied favicon suite in the page and web app manifest', () => {
const index = fs.readFileSync(new URL('../public/index.html', import.meta.url), 'utf8');
const appManifest = manifest('https://memes.example');
assert.match(index, /\/assets\/favicon\/favicon\.svg\?v=20260814/);
assert.match(index, /\/assets\/favicon\/favicon-96x96\.png\?v=20260814/);
assert.match(index, /\/favicon\.ico\?v=20260814/);
assert.match(index, /\/assets\/favicon\/apple-touch-icon\.png\?v=20260814/);
assert.doesNotMatch(index, /rel="icon"[^>]+yoonect-logo\.png/);
assert.deepEqual(appManifest.icons.map(({ sizes, purpose }) => ({ sizes, purpose })), [
{ sizes: '192x192', purpose: 'maskable' },
{ sizes: '512x512', purpose: 'maskable' }
]);
assert.match(appManifest.icons[0].src, /web-app-manifest-192x192\.png\?v=20260814$/);
assert.match(appManifest.icons[1].src, /web-app-manifest-512x512\.png\?v=20260814$/);
});
test('renders a shareable lore page with text-file metadata', () => {
const html = renderIndex({
template,
baseUrl: 'https://memes.example',
nonce: 'lore-nonce',
approvedCount: 1,
lore: true
});
assert.match(html, /<title>Lore \| The Meme Protocol<\/title>/);
assert.match(html, /canonical" href="https:\/\/memes\.example\/lore"/);
assert.match(html, /og:image" content="https:\/\/memes\.example\/assets\/social-preview-v2\.78d008df9860\.jpg"/);
assert.match(html, /"@type":"CreativeWork"/);
assert.match(html, /https:\/\/memes\.example\/assets\/meme-protocol-lore\.txt/);
});
test('discovery feeds point readers to viewer permalinks', () => {
const feed = feedJson('https://memes.example', [meme]);
const sitemap = sitemapXml('https://memes.example', [meme]);
assert.match(sitemap, /https:\/\/memes\.example\/lore/);
assert.equal(feed.items[0].url, `https://memes.example/meme/${id}`);
assert.equal(feed.items[0].image, `https://memes.example/media/${id}`);
assert.match(sitemap, new RegExp(`https://memes\\.example/meme/${id}`));
});