import { collectLocalizedPaths, topLevelKeys, unwrapLocalized, wrapLocalized } from './localize-paths' import { Home } from '../globals/Home' import { Common } from '../globals/Common' type Doc = Record const FIELDS = [ { name: 'hero', type: 'group', fields: [ { name: 'title', type: 'text', localized: true }, { name: 'bullets', type: 'array', localized: true, fields: [{ name: 'value', type: 'text' }] }, ], }, { name: 'items', type: 'array', fields: [ { name: 'icon', type: 'text' }, { name: 'title', type: 'text', localized: true }, { name: 'features', type: 'array', localized: true, fields: [{ name: 'value', type: 'text' }] }, ], }, { name: 'address', type: 'text' }, { name: 'external', type: 'checkbox' }, ] function legacyDoc(): Doc { return { hero: { title: 'Cím', bullets: [{ id: 'b1', value: 'Egy' }] }, items: [ { id: 'r1', icon: 'star', title: 'Első', features: [{ id: 'f1', value: 'A' }] }, { id: 'r2', icon: 'bolt', title: 'Második', features: [] }, ], address: 'Budapest', external: false, } } describe('collectLocalizedPaths', () => { it('should collect localized leaves through groups and shared array rows', () => { expect(collectLocalizedPaths(FIELDS)).toEqual([ ['hero', 'title'], ['hero', 'bullets'], ['items', '[]', 'title'], ['items', '[]', 'features'], ]) }) it('should look through unnamed row/collapsible wrappers', () => { expect( collectLocalizedPaths([ { name: 'g', type: 'group', fields: [{ type: 'row', fields: [{ name: 't', type: 'text', localized: true }] }] }, { type: 'collapsible', fields: [{ name: 'u', type: 'text', localized: true }] }, ]), ).toEqual([['g', 't'], ['u']]) }) it('should throw on layout fields that could hide localized data', () => { expect(() => collectLocalizedPaths([{ type: 'tabs' }])).toThrow('unsupported field type "tabs"') }) it('should derive paths from the real Global configs', () => { const homePaths = collectLocalizedPaths(Home.fields as never) expect(homePaths).toContainEqual(['hero', 'trustBullets']) expect(homePaths).toContainEqual(['services', 'items', '[]', 'features']) expect(homePaths).not.toContainEqual(['about', 'usps', '[]', 'icon']) expect(collectLocalizedPaths(Common.fields as never)).not.toContainEqual(['footer', 'address']) }) }) describe('wrapLocalized', () => { const paths = collectLocalizedPaths(FIELDS) it('should wrap strings and arrays under the hu locale and keep shared values', () => { const doc = legacyDoc() const changed = wrapLocalized(doc, paths) expect(changed).toBe(6) expect(doc.hero).toEqual({ title: { hu: 'Cím' }, bullets: { hu: [{ id: 'b1', value: 'Egy' }] } }) expect(doc.items).toEqual([ { id: 'r1', icon: 'star', title: { hu: 'Első' }, features: { hu: [{ id: 'f1', value: 'A' }] } }, { id: 'r2', icon: 'bolt', title: { hu: 'Második' }, features: { hu: [] } }, ]) expect(doc.address).toBe('Budapest') expect(doc.external).toBe(false) }) it('should be idempotent on already migrated data', () => { const doc = legacyDoc() wrapLocalized(doc, paths) const snapshot = JSON.stringify(doc) expect(wrapLocalized(doc, paths)).toBe(0) expect(JSON.stringify(doc)).toBe(snapshot) }) it('should leave existing locale maps (e.g. Common.buttons) untouched', () => { const doc: Doc = { hero: { title: { hu: 'Cím', en: 'Title' } } } expect(wrapLocalized(doc, paths)).toBe(0) expect(doc).toEqual({ hero: { title: { hu: 'Cím', en: 'Title' } } }) }) it('should skip missing branches and null values', () => { const doc: Doc = { hero: { title: null }, items: null } expect(wrapLocalized(doc, paths)).toBe(0) expect(doc).toEqual({ hero: { title: null }, items: null }) }) it('should not descend into a row path when the stored value is not an array', () => { const doc: Doc = { items: { unexpected: true } } expect(wrapLocalized(doc, paths)).toBe(0) }) }) describe('unwrapLocalized', () => { const paths = collectLocalizedPaths(FIELDS) it('should restore the legacy shape from the hu value', () => { const doc = legacyDoc() wrapLocalized(doc, paths) ;(doc.hero as Doc).title = { hu: 'Cím', en: 'Title' } expect(unwrapLocalized(doc, paths)).toBe(6) expect(doc).toEqual(legacyDoc()) }) it('should fall back to null when the hu value is missing', () => { const doc: Doc = { hero: { title: { en: 'Title' } } } unwrapLocalized(doc, paths) expect(doc).toEqual({ hero: { title: null } }) }) }) describe('topLevelKeys', () => { it('should return unique first segments', () => { expect(topLevelKeys(collectLocalizedPaths(FIELDS))).toEqual(['hero', 'items']) }) })