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 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 }).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 }) }) })