Files
AndreaSlopFotografie/web/src/seo.test.ts
T
SluijsensandClaude Opus 5.5 790495d700 Adds a generic WebPage structured data helper
webPage(type, path, extra) builds the schema.org page data every page
needs: the canonical URL as @id and url, name and description from the
page registry, the language, and page-specific fields last. aboutPage
now uses it and gives exactly the same output. The Portfolio and Soorten
fotoshoots pages build their structured data on it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 08:50:39 +01:00

411 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import fc from 'fast-check';
import { describe, expect, it } from 'vitest';
import aboutContent from './content/aboutContent';
import siteContent from './content/siteContent';
import { emailAddress } from './lib/emailAddress';
import { visibleSocials } from './lib/visibleSocials';
import type { RoutePath } from './navigation';
import {
aboutPage,
businessId,
canonicalUrl,
defaultOgImage,
jsonLdScript,
pageHead,
person,
personId,
professionalService,
siteHead,
siteUrl,
structuredDataScript,
webPage,
type WebPageType,
} from './seo';
import sitePages from './sitePages';
import {
anySiteContent,
knowsAboutEntries,
routePath,
} from './test/arbitraries';
const meta = (head: ReturnType<typeof pageHead>, key: string) =>
head.meta.find(
(m) =>
('name' in m && m.name === key) ||
('property' in m && m.property === key),
);
describe('canonicalUrl', () => {
it('maakt een absolute URL zonder slash, query of hash', () => {
expect(canonicalUrl('/')).toBe(`${siteUrl}/`);
expect(canonicalUrl('')).toBe(`${siteUrl}/`);
expect(canonicalUrl('/portfolio/')).toBe(`${siteUrl}/portfolio`);
expect(canonicalUrl('/contact?x=1#top')).toBe(`${siteUrl}/contact`);
expect(canonicalUrl('portfolio')).toBe(`${siteUrl}/portfolio`);
});
it('is idempotent', () => {
fc.assert(
fc.property(
routePath,
fc.constantFrom('', '/', '?a=1', '#x'),
(path, suffix) => {
const once = canonicalUrl(path + suffix);
expect(canonicalUrl(once.slice(siteUrl.length))).toBe(once);
},
),
);
});
it('begint met siteUrl en eindigt alleen bij de root op een slash (invariant)', () => {
fc.assert(
fc.property(fc.string(), (path) => {
const url = canonicalUrl(path);
const rest = url.slice(siteUrl.length);
return (
url.startsWith(siteUrl + '/') &&
!/[?#]/.test(rest) &&
(rest === '/' || !rest.endsWith('/'))
);
}),
);
});
});
describe('pageHead', () => {
it('zet titel, canonical en preview voor de homepage, zonder robots-tag', () => {
const head = pageHead('/');
expect(head.meta[0]).toEqual({
title: 'Lifestyle fotograaf in Huizen | Andrea Slop Fotografie',
});
expect(head.links).toEqual([{ rel: 'canonical', href: `${siteUrl}/` }]);
expect(meta(head, 'og:url')).toMatchObject({ content: `${siteUrl}/` });
expect(meta(head, 'og:image')).toMatchObject({
content: `${siteUrl}/og-image.jpg`,
});
expect(meta(head, 'robots')).toBeUndefined();
});
it('zet noindex op een placeholder', () => {
const head = pageHead('/portfolio');
expect(meta(head, 'robots')).toMatchObject({ content: 'noindex' });
expect(head.links[0].href).toBe(`${siteUrl}/portfolio`);
});
});
describe('jsonLdScript', () => {
it('geeft na parsen dezelfde data terug (round-trip)', () => {
fc.assert(
fc.property(fc.jsonValue(), (value) => {
expect(JSON.parse(jsonLdScript(value))).toEqual(
JSON.parse(JSON.stringify(value)),
);
}),
);
});
it('bevat nooit < of >, dus nooit een sluitende script-tag (invariant)', () => {
fc.assert(
fc.property(fc.jsonValue(), fc.string(), (value, text) => {
const out = jsonLdScript({ value, text: text + '</script>' });
return !/[<>]/.test(out);
}),
);
});
});
describe('professionalService en siteHead', () => {
it('beschrijft de fotograaf zonder e-mailadres', () => {
const data = professionalService(siteContent);
expect(data['@type']).toBe('ProfessionalService');
expect(data.address.addressLocality).toBe('Huizen');
expect(data.sameAs).toEqual(siteContent.socials.map((s) => s.href));
expect(JSON.stringify(data)).not.toContain('@andreaslop');
});
it('laat ongeldige socials weg uit sameAs', () => {
const data = professionalService({
...siteContent,
socials: [{ platform: 'facebook', href: 'http://onveilig.nl' }],
});
expect(data.sameAs).toEqual([]);
});
it('zet de gestructureerde data als ld+json-script', () => {
const head = siteHead(siteContent);
expect(head.scripts[0].type).toBe('application/ld+json');
expect(JSON.parse(head.scripts[0].children)['@type']).toBe(
'ProfessionalService',
);
});
});
describe('pageHead for the about page', () => {
it('is indexed, with its own title, description and preview image', () => {
const head = pageHead('/over-mij');
expect(head.meta[0]).toEqual({
title: 'Over mij – fotograaf in Huizen | Andrea Slop Fotografie',
});
expect(meta(head, 'robots')).toBeUndefined();
expect(meta(head, 'description')).toMatchObject({
content: sitePages['/over-mij'].description,
});
expect(meta(head, 'og:image')).toMatchObject({
content: `${siteUrl}/og-over-mij.jpg`,
});
expect(head.links[0].href).toBe(`${siteUrl}/over-mij`);
});
it('keeps title and description within what search engines show', () => {
const page = sitePages['/over-mij'];
expect(
`${page.title} | Andrea Slop Fotografie`.length,
).toBeLessThanOrEqual(60);
expect(page.description.length).toBeLessThanOrEqual(155);
});
});
describe('structured data about Andrea', () => {
const image = `${siteUrl}/og-over-mij.jpg`;
it('describes Andrea from the page content and the site socials', () => {
expect(person(aboutContent.person, siteContent, image)).toEqual({
'@type': 'Person',
'@id': personId,
name: 'Andrea Slop',
jobTitle: 'Lifestyle fotograaf',
image,
address: {
'@type': 'PostalAddress',
addressLocality: 'Huizen',
addressCountry: 'NL',
},
url: `${siteUrl}/over-mij`,
worksFor: { '@id': businessId },
knowsAbout: [
'Newbornfotografie',
'Zwangerschapsfotografie',
'Gezinsfotografie',
],
sameAs: siteContent.socials.map((social) => social.href),
});
});
it('leaves out empty lists instead of writing them', () => {
const data = person(
{ ...aboutContent.person, knowsAbout: [' ', ''] },
{ ...siteContent, socials: [] },
image,
);
expect(data).not.toHaveProperty('knowsAbout');
expect(data).not.toHaveProperty('sameAs');
});
it('links Andrea and the business both ways (invariant)', () => {
fc.assert(
fc.property(anySiteContent, (site) => {
const business = professionalService(site);
const andrea = person(aboutContent.person, site, image);
expect(business['@id']).toBe(businessId);
expect(business.founder['@id']).toBe(andrea['@id']);
expect(andrea.worksFor['@id']).toBe(business['@id']);
}),
);
});
it('lists exactly the valid https socials in sameAs (invariant)', () => {
fc.assert(
fc.property(anySiteContent, (site) => {
const valid = visibleSocials(site.socials).map((s) => s.href);
const andrea = person(aboutContent.person, site, image);
expect(andrea.sameAs ?? []).toEqual(valid);
}),
);
});
it('lists the cleaned specialisations in knowsAbout (invariant)', () => {
fc.assert(
fc.property(knowsAboutEntries, (entries) => {
const andrea = person(
{ ...aboutContent.person, knowsAbout: entries },
siteContent,
image,
);
const list = andrea.knowsAbout ?? [];
expect(
list.every(
(entry) => entry === entry.trim() && entry !== '',
),
).toBe(true);
expect(new Set(list).size).toBe(list.length);
}),
);
});
it('never contains the e-mail address, on any page (invariant)', () => {
fc.assert(
fc.property(anySiteContent, (site) => {
const address = emailAddress(site.email);
const out =
jsonLdScript(
aboutPage('/over-mij', aboutContent.person, site),
) + jsonLdScript(professionalService(site));
return address === null || !out.includes(address);
}),
);
});
it('matches the page registry and canonical URL (invariant)', () => {
fc.assert(
fc.property(anySiteContent, (site) => {
const page = aboutPage('/over-mij', aboutContent.person, site);
expect(page['@id']).toBe(canonicalUrl('/over-mij'));
expect(page.url).toBe(canonicalUrl('/over-mij'));
expect(page.name).toBe(sitePages['/over-mij'].title);
expect(page.description).toBe(
sitePages['/over-mij'].description,
);
expect(page.mainEntity.image).toBe(image);
}),
);
});
it('falls back to the site preview image for a page without its own', () => {
const page = aboutPage('/portfolio', aboutContent.person, siteContent);
expect(page.mainEntity.image).toBe(`${siteUrl}/og-image.jpg`);
});
it('wraps data in an escaped ld+json script tag', () => {
const script = structuredDataScript({ name: '</script>' });
expect(script.type).toBe('application/ld+json');
expect(script.children).not.toMatch(/[<>]/);
expect(JSON.parse(script.children)).toEqual({ name: '</script>' });
});
});
describe('webPage', () => {
const paths = Object.keys(sitePages) as RoutePath[];
const anyPath = fc.constantFrom(...paths);
const anyType = fc.constantFrom<WebPageType>(
'WebPage',
'AboutPage',
'ContactPage',
'ImageGallery',
'CollectionPage',
);
const baseKeys = [
'@context',
'@type',
'@id',
'url',
'name',
'description',
'inLanguage',
];
// Page-specific fields: any JSON, under keys the base does not use
const anyExtra = fc.dictionary(
fc.string().filter((key) => !baseKeys.includes(key)),
fc.jsonValue(),
);
it('describes a page from the registry', () => {
expect(
webPage('ContactPage', '/contact', {
about: { '@id': businessId },
}),
).toEqual({
'@context': 'https://schema.org',
'@type': 'ContactPage',
'@id': `${siteUrl}/contact`,
url: `${siteUrl}/contact`,
name: sitePages['/contact'].title,
description: sitePages['/contact'].description,
inLanguage: 'nl-NL',
about: { '@id': businessId },
});
});
it('needs no extra fields', () => {
expect(webPage('WebPage', '/')).not.toHaveProperty('mainEntity');
expect(webPage('WebPage', '/')['@id']).toBe(`${siteUrl}/`);
});
it('takes name, description and URL from the registry (invariant)', () => {
fc.assert(
fc.property(anyType, anyPath, (type, path) => {
const page = webPage(type, path);
expect(page['@context']).toBe('https://schema.org');
expect(page['@type']).toBe(type);
expect(page['@id']).toBe(canonicalUrl(path));
expect(page.url).toBe(canonicalUrl(path));
expect(page.name).toBe(sitePages[path].title);
expect(page.description).toBe(sitePages[path].description);
expect(page.inLanguage).toBe('nl-NL');
}),
);
});
it('keeps every extra field as given (invariant)', () => {
fc.assert(
fc.property(anyType, anyPath, anyExtra, (type, path, extra) => {
const page: Record<string, unknown> = webPage(
type,
path,
extra,
);
for (const [key, value] of Object.entries(extra)) {
expect(page[key]).toEqual(value);
}
}),
);
});
it('survives the ld+json script unchanged (round trip)', () => {
fc.assert(
fc.property(anyType, anyPath, anyExtra, (type, path, extra) => {
const page = webPage(type, path, extra);
expect(JSON.parse(jsonLdScript(page))).toEqual(
JSON.parse(JSON.stringify(page)),
);
}),
);
});
// aboutPage as it was before it used webPage, kept as the oracle for the
// rebuilt version
function previousAboutPage(
path: RoutePath,
content: Parameters<typeof aboutPage>[1],
site: Parameters<typeof aboutPage>[2],
) {
const page = sitePages[path];
const image = siteUrl + (page.ogImage ?? defaultOgImage).path;
return {
'@context': 'https://schema.org',
'@type': 'AboutPage',
'@id': canonicalUrl(path),
url: canonicalUrl(path),
name: page.title,
description: page.description,
inLanguage: 'nl-NL',
mainEntity: person(content, site, image),
};
}
it('leaves the about page data exactly as it was (oracle)', () => {
fc.assert(
fc.property(
anyPath,
anySiteContent,
knowsAboutEntries,
(path, site, knowsAbout) => {
const content = { ...aboutContent.person, knowsAbout };
expect(aboutPage(path, content, site)).toEqual(
previousAboutPage(path, content, site),
);
},
),
);
});
});