Files
websitedev/proto/src/__tests__/cta-links-migration.test.ts
T
Do SikiandClaude Opus 5.5 0eb6594acc feat(cms): localize legal pages with a lossless migration (MITHOME-112)
Legal page title, last-updated text and section titles/contents become
localized; slug and section ids stay shared. A Payload migration wraps
existing values into { hu: value } on every legal page and its whole
version history, like the globals migration (MITHOME-111).

The migration helper now handles collections as well as globals
(rewriteDocs) and can scope version rewrites to the latest working copy,
which the upcoming English import needs. Migration tests share an
in-memory Mongo fake that honours query filters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 10:54:44 +02:00

84 lines
3.4 KiB
TypeScript

import { down, toPageLink, up } from '../migrations/20260929_cta_page_links'
import { up as localizeUp } from '../migrations/20260929_localize_globals'
import { fakeArgs, fakeCollection } from './helpers/fake-mongo'
type Doc = Record<string, unknown>
const heroWith = (primaryHref: unknown, secondaryHref: unknown) => ({
title: 'Cím',
cta: {
primary: { text: { hu: 'Kapcsolat' }, href: primaryHref, external: false },
secondary: { text: { hu: 'Webmail' }, href: secondaryHref, external: true },
},
})
function setup(primaryHref: unknown = { hu: '/kapcsolat' }, secondaryHref: unknown = { hu: 'https://mail.mozdit.hu' }) {
const home: Doc = { _id: 'g-home', globalType: 'home', hero: heroWith(primaryHref, secondaryHref) }
const version: Doc = { _id: 'v1', version: { hero: heroWith({ hu: '/hu/kapcsolat' }, 'https://mail.mozdit.hu') } }
const globals = fakeCollection([home])
const versions = fakeCollection([version])
const { args, payload } = fakeArgs({ globals, versions: { home: versions } })
return { args, home, version, payload }
}
const cta = (doc: Doc) => (doc.hero as { cta: Record<string, Doc> }).cta
describe('20260929_cta_page_links migration (MITHOME-123)', () => {
it('should turn the legacy internal path into a page link and drop the href', async () => {
const { args, home } = setup()
await up(args)
expect(cta(home).primary).toEqual({ text: { hu: 'Kapcsolat' }, linkType: 'page', page: 'contact', external: false })
})
it('should keep external URLs as URL links', async () => {
const { args, home } = setup()
await up(args)
expect(cta(home).secondary).toMatchObject({ linkType: 'url', href: { hu: 'https://mail.mozdit.hu' } })
})
it('should migrate version records, including legacy string hrefs', async () => {
const { args, version } = setup()
await up(args)
const versionCta = cta(version.version as Doc)
expect(versionCta.primary).toMatchObject({ linkType: 'page', page: 'contact' })
expect(versionCta.secondary).toMatchObject({ linkType: 'url', href: 'https://mail.mozdit.hu' })
})
it('should be idempotent', async () => {
const { args, home, payload } = setup()
await up(args)
const snapshot = JSON.stringify(home)
await up(args)
expect(JSON.stringify(home)).toBe(snapshot)
expect(payload.logger.info).toHaveBeenLastCalledWith('MITHOME-123 CTA link migration: home — 0 CTA(s) rewritten')
})
it('should give the same result regardless of order with the locale migration', async () => {
const localeFirst = setup('/kapcsolat', 'https://mail.mozdit.hu')
await localizeUp(localeFirst.args)
await up(localeFirst.args)
const ctaFirst = setup('/kapcsolat', 'https://mail.mozdit.hu')
await up(ctaFirst.args)
await localizeUp(ctaFirst.args)
expect(cta(ctaFirst.home)).toEqual(cta(localeFirst.home))
})
it('should treat a missing href as a URL link without inventing a target', () => {
const link: Doc = { text: { hu: 'x' } }
expect(toPageLink(link)).toBe(1)
expect(link).toEqual({ text: { hu: 'x' }, linkType: 'url' })
})
it('should restore a hu href on down', async () => {
const { args, home } = setup()
await up(args)
await down(args)
expect(cta(home).primary).toEqual({ text: { hu: 'Kapcsolat' }, href: { hu: '/hu/kapcsolat' }, external: false })
expect(cta(home).secondary).toEqual({ text: { hu: 'Webmail' }, href: { hu: 'https://mail.mozdit.hu' }, external: true })
})
})