import { describe, expect, it } from 'vitest'; import { MAX_REDIRECTS, REDIRECT_FORMAT_VERSION, redirectProblems, redirectRules, redirectTargets, redirectsJson, type RedirectRule, } from './redirectRules'; // What the host does with the file (the format is in docs/redirect-rules.md); // the host has its own implementation and tests function resolve(rules: readonly RedirectRule[], rawPath: string) { const path = rawPath.toLowerCase().replace(/\/+$/, '') || '/'; return rules.find((rule) => rule.from.toLowerCase() === path); } describe('redirectRules', () => { it('has no problems', () => { expect(redirectProblems(redirectRules)).toEqual([]); }); it('sends every 301 to a page of the site or the sitemap', () => { for (const rule of redirectRules.filter((r) => r.status === 301)) { expect(redirectTargets).toContain(rule.to); } }); it.each([ ['/sitemap_index.xml', 301, '/sitemap.xml'], ['/wp-sitemap.xml', 301, '/sitemap.xml'], ['/page-sitemap.xml', 301, '/sitemap.xml'], ['/portfolio-item-sitemap.xml', 301, '/sitemap.xml'], ['/masonry-gallery-category-sitemap.xml', 301, '/sitemap.xml'], ['/portfolio-tag-sitemap.xml', 301, '/sitemap.xml'], ['/testimonials-category-sitemap.xml', 301, '/sitemap.xml'], ['/portfolio-item/', 301, '/portfolio'], ['/portfolio-item/decoration/', 301, '/portfolio'], ['/portfolio-item/wedding-dress/', 301, '/portfolio'], ['/portfolio-item/best-girls/', 301, '/portfolio'], ['/portfolio-item/mm-1/', 301, '/portfolio'], ['/portfolio-item/wedding-cake/', 301, '/portfolio'], ['/portfolio-item/mm2/', 301, '/portfolio'], ['/portfolio-item/cake-smash-1/', 301, '/portfolio'], ['/portfolio-item/hairstyles/', 301, '/portfolio'], ['/portfolio-item/360/', 301, '/portfolio'], ['/portfolio-item/366/', 301, '/portfolio'], ['/portfolio-item/353/', 301, '/portfolio'], ['/portfolio-item/homepage-foto/', 301, '/portfolio'], ['/portfolio-item/getting-ready/', 301, '/portfolio'], ['/portfolio-item/m1/', 301, '/portfolio'], ['/portfolio-item/f1/', 301, '/portfolio'], ['/Portfolio-Item/MM1', undefined, undefined], ['/portfolio-item/MM-1', 301, '/portfolio'], ['/portfolio-tag/homepage/', 301, '/portfolio'], ['/portfolio-tag/portfolio-gallerij/', 301, '/portfolio'], ['/portfolio-tag/simple/', 301, '/portfolio'], ['/testimonials-category/photo/', 301, '/'], ['/masonry-gallery-category/test-masonry-gallery/', 410, undefined], ['/feed/', 410, undefined], ['/comments/feed/', 410, undefined], ])('%s gives %s %s', (path, status, to) => { const rule = resolve(redirectRules, path); expect(rule?.status).toBe(status); expect(rule?.to).toBe(to); }); it('leaves the real pages and their canonical variants to the host', () => { for (const path of [ '/', '/over-mij', '/over-mij/', '/portfolio', '/soorten-fotoshoots/newborn', '/contact', '/robots.txt', '/sitemap.xml', '/assets/index-abc.js', '/admin/', '/api/v1/offerings', '/wp-login.php', ]) { expect(resolve(redirectRules, path)).toBeUndefined(); } }); }); describe('redirectProblems', () => { const rule = (extra: Partial): RedirectRule => ({ from: '/a', to: '/b', status: 301, ...extra, }); it.each([ [rule({ from: 'a' }), 'not a usable path'], [rule({ from: '/' }), 'not a usable path'], [rule({ from: '//a' }), 'not a usable path'], [rule({ from: '/a/../b' }), 'not a usable path'], [rule({ from: '/a?x=1' }), 'not a usable path'], [rule({ from: '/a#b' }), 'not a usable path'], [rule({ from: '/Admin/x' }), 'answered by the host'], [rule({ from: '/api/v1' }), 'answered by the host'], [rule({ from: '/health' }), 'answered by the host'], [rule({ to: undefined }), 'needs a path'], [rule({ to: 'https://evil.example/' }), 'needs a path'], [rule({ to: '//evil.example' }), 'needs a path'], [rule({ to: '/A/' }), 'the same'], [rule({ status: 410 }), 'only a 301 has a target'], [rule({ status: 404 }), 'only a 301 has a target'], ])('flags %j', (bad, text) => { expect(redirectProblems([bad]).join()).toContain(text); }); it('flags a duplicate, also in another case or with a slash', () => { expect(redirectProblems([rule({}), rule({})]).join()).toContain( 'duplicate', ); expect( redirectProblems([ rule({ from: '/A' }), rule({ from: '/a/' }), ]).join(), ).toContain('duplicate'); }); it('flags a chain', () => { expect( redirectProblems([ rule({ from: '/a', to: '/b' }), rule({ from: '/b', to: '/c' }), ]).join(), ).toContain('is a rule too'); }); it('flags too many rules', () => { const many = Array.from({ length: MAX_REDIRECTS + 1 }, (_, i) => rule({ from: `/p${i}`, to: '/x' }), ); expect(redirectProblems(many).join()).toContain('more than'); }); it('accepts a 404 and a 410 without a target', () => { expect( redirectProblems([ rule({ status: 404, to: undefined }), rule({ from: '/b', status: 410, to: undefined }), ]), ).toEqual([]); }); }); describe('redirectsJson', () => { it('writes the version and the redirects', () => { const parsed = JSON.parse(redirectsJson()); expect(parsed.version).toBe(REDIRECT_FORMAT_VERSION); expect(parsed.redirects).toEqual(redirectRules); expect(Object.keys(parsed)).toEqual(['version', 'redirects']); expect(redirectsJson().endsWith('\n')).toBe(true); }); it('stays far below the size limit of the host', () => { expect(redirectsJson().length).toBeLessThan(100_000); }); it('refuses a list with problems', () => { expect(() => redirectsJson([{ from: 'x', status: 404 }])).toThrow( 'Invalid redirect rules', ); }); });