Files
websitedev/proto/src/__tests__/migrate-docs.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

60 lines
2.6 KiB
TypeScript

import { rewriteDocs } from '../lib/migrate-global-docs'
import { fakeArgs, fakeCollection } from './helpers/fake-mongo'
type Doc = Record<string, unknown>
const upper = (data: Doc) => {
if (typeof data.title !== 'string') return 0
data.title = data.title.toUpperCase()
return 1
}
describe('rewriteDocs (MITHOME-112)', () => {
it('should rewrite every document of a collection and all its versions by default', async () => {
const docs = fakeCollection([
{ _id: 'a', slug: 'adatvedelem', title: 'a' },
{ _id: 'b', slug: 'hasznalati-feltetelek', title: 'b' },
])
const versions = fakeCollection([
{ _id: 'v1', latest: false, version: { title: 'old' } },
{ _id: 'v2', latest: true, version: { title: 'new' } },
])
const { args } = fakeArgs({ collections: { 'legal-pages': docs }, versions: { 'legal-pages': versions } })
const changed = await rewriteDocs(args, { kind: 'collection', slug: 'legal-pages' }, ['title'], upper)
expect(changed).toBe(4)
expect(docs.updateOne).toHaveBeenCalledWith({ _id: 'a' }, { $set: { title: 'A' } }, { session: undefined })
expect(versions.updateOne).toHaveBeenCalledTimes(2)
})
it('should only touch the latest version when scoped to latest', async () => {
const versions = fakeCollection([
{ _id: 'v1', latest: false, version: { title: 'old' } },
{ _id: 'v2', latest: true, version: { title: 'new' } },
])
const { args } = fakeArgs({ collections: { 'legal-pages': fakeCollection([]) }, versions: { 'legal-pages': versions } })
await rewriteDocs(args, { kind: 'collection', slug: 'legal-pages' }, ['title'], upper, { versions: 'latest' })
expect(versions.updateOne).toHaveBeenCalledTimes(1)
expect(versions.updateOne).toHaveBeenCalledWith({ _id: 'v2' }, { $set: { version: { title: 'NEW' } } }, { session: undefined })
})
it('should only read the targeted global', async () => {
const globals = fakeCollection([
{ _id: 'h', globalType: 'home', title: 'home' },
{ _id: 'c', globalType: 'common', title: 'common' },
])
const { args } = fakeArgs({ globals })
expect(await rewriteDocs(args, { kind: 'global', slug: 'home' }, ['title'], upper)).toBe(1)
expect(globals.docs[1].title).toBe('common')
})
it('should skip a collection that does not exist and version records without data', async () => {
const versions = fakeCollection([{ _id: 'v', version: null }])
const { args } = fakeArgs({ versions: { nope: versions } })
expect(await rewriteDocs(args, { kind: 'collection', slug: 'nope' }, ['title'], upper)).toBe(0)
})
})