import { down, up } from '../migrations/20260929_localize_globals' type Doc = Record function fakeCollection(docs: Doc[]) { return { findOne: jest.fn(async (filter: { globalType: string }) => docs.find((d) => d.globalType === filter.globalType) ?? null), find: jest.fn(() => ({ async *[Symbol.asyncIterator]() { yield* docs }, })), updateOne: jest.fn(async (_filter: Doc, _update: { $set: Doc }, _options?: unknown) => ({})), } } function setup() { const globals = fakeCollection([ { _id: 'g-home', globalType: 'home', hero: { title: 'Cím', trustBullets: [{ value: 'Egy' }] }, cta: { title: 'CTA' } }, { _id: 'g-common', globalType: 'common', buttons: { contact: { hu: 'Kapcsolat', en: 'Contact' } } }, ]) const homeVersions = fakeCollection([ { _id: 'v1', version: { hero: { title: 'Régi cím' } } }, { _id: 'v2', version: { hero: { title: { hu: 'Már migrált' } } } }, ]) const payload = { db: { globals: { collection: globals }, versions: { home: { collection: homeVersions } } }, logger: { info: jest.fn() }, } const args = { payload, session: undefined, req: {} } as never return { args, globals, homeVersions, payload } } describe('20260929_localize_globals migration', () => { it('should wrap legacy values of the published global under hu and $set only touched keys', async () => { const { args, globals } = setup() await up(args) const homeCall = globals.updateOne.mock.calls.find(([filter]) => filter._id === 'g-home') expect(homeCall).toBeDefined() const [, update] = homeCall! expect(update.$set.hero).toEqual({ title: { hu: 'Cím' }, trustBullets: { hu: [{ value: 'Egy' }] } }) expect(update.$set.cta).toEqual({ title: { hu: 'CTA' } }) expect(update.$set).not.toHaveProperty('_id') expect(update.$set).not.toHaveProperty('globalType') }) it('should not rewrite a global whose values are already localized', async () => { const { args, globals } = setup() await up(args) const ids = globals.updateOne.mock.calls.map(([filter]) => filter._id) expect(ids).not.toContain('g-common') }) it('should migrate legacy version records and skip migrated ones', async () => { const { args, homeVersions } = setup() await up(args) expect(homeVersions.updateOne).toHaveBeenCalledTimes(1) expect(homeVersions.updateOne).toHaveBeenCalledWith( { _id: 'v1' }, { $set: { version: { hero: { title: { hu: 'Régi cím' } } } } }, { session: undefined }, ) }) it('should log a per-global summary', async () => { const { args, payload } = setup() await up(args) expect(payload.logger.info).toHaveBeenCalledWith('MITHOME-111 locale migration: home — 4 value(s) rewritten') }) it('should restore the hu value on down', async () => { const { args, globals } = setup() await down(args) const commonCall = globals.updateOne.mock.calls.find(([filter]) => filter._id === 'g-common') const [, update] = commonCall! expect(update.$set.buttons).toEqual({ contact: 'Kapcsolat' }) }) })