|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import { test } from 'node:test'; |
| 4 | + |
| 5 | +import { LANDING_LOCALES, localizedHref } from '../app/i18n.ts'; |
| 6 | + |
| 7 | +const PPT_TUTORIAL_PATH = '/tutorials/open-design-ai-ppt-tutorial/'; |
| 8 | +const EVENT_PARTIALS = [ |
| 9 | + 'open-design-osaka-meetup-main', |
| 10 | + 'open-design-shanghai-workshop-main', |
| 11 | +] as const; |
| 12 | + |
| 13 | +function localizeInternalLinks(html: string, locale: (typeof LANDING_LOCALES)[number]['code']) { |
| 14 | + return html.replace( |
| 15 | + /href="(\/[^\"]+)"/g, |
| 16 | + (_match, pathname: string) => `href="${localizedHref(pathname, locale)}"`, |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +test('event recaps keep canonical-only tutorial links valid in every active locale', async () => { |
| 21 | + for (const eventPartial of EVENT_PARTIALS) { |
| 22 | + for (const { code } of LANDING_LOCALES) { |
| 23 | + const localeSuffix = code === 'en' ? '' : `.${code}`; |
| 24 | + const partial = await readFile( |
| 25 | + new URL(`../app/_partials/${eventPartial}${localeSuffix}.html`, import.meta.url), |
| 26 | + 'utf8', |
| 27 | + ); |
| 28 | + const rendered = localizeInternalLinks(partial, code); |
| 29 | + |
| 30 | + assert.match( |
| 31 | + partial, |
| 32 | + new RegExp(`href="${PPT_TUTORIAL_PATH}"`), |
| 33 | + `${eventPartial}.${code}: fixture no longer links to the canonical tutorial`, |
| 34 | + ); |
| 35 | + assert.match( |
| 36 | + rendered, |
| 37 | + new RegExp(`href="${PPT_TUTORIAL_PATH}"`), |
| 38 | + `${eventPartial}.${code}: canonical tutorial link was rewritten`, |
| 39 | + ); |
| 40 | + assert.doesNotMatch( |
| 41 | + rendered, |
| 42 | + new RegExp(`href="/${code}${PPT_TUTORIAL_PATH}"`), |
| 43 | + `${eventPartial}.${code}: generated link points at a missing localized tutorial route`, |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | +}); |
0 commit comments