import { describe, expect, test } from 'vitest' import { buildSectionHeaderField, buildSectionSettingsField } from '../src/modules/blocks/sectionFields.js' import { buildLinkField } from '../src/modules/links/buildLinkField.js' import { resolveLinkHref } from '../src/modules/links/resolveLinkHref.js' import { homeSlugFrom, resolveLink, resolveNav } from '../src/modules/links/resolveNav.js' const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } as const const opts = { config, homeSlug: 'start', locale: 'pl' } const page = (slug: string) => ({ type: 'internal' as const, label: slug, page: { slug } }) describe('buildLinkField interface names', () => { test('each shape has its own type name, so generate:types never depends on field order', () => { expect(buildLinkField().interfaceName).toBe('CmsLink') expect(buildLinkField({ appearances: false }).interfaceName).toBe('CmsLinkPlain') expect(buildLinkField({ disableLabel: true }).interfaceName).toBe('CmsLinkNoLabel') expect(buildLinkField({ appearances: false, disableLabel: true }).interfaceName).toBe('CmsLinkPlainNoLabel') expect(buildLinkField({ appearances: false, interfaceName: 'CardLink' }).interfaceName).toBe('CardLink') }) }) describe('resolveLink', () => { test('internal page → next/link, no target', () => { expect(resolveLink(page('kontakt'), opts)).toEqual({ href: '/pl/kontakt', internal: true, label: 'kontakt', newTab: false, targetProps: {} }) }) test('files always open in a new tab with rel; appearance is passed through', () => { expect(resolveLink({ type: 'file', appearance: 'primary', file: { url: '/media/cennik.pdf' }, label: 'Cennik' }, opts)).toMatchObject({ appearance: 'primary', href: '/media/cennik.pdf', internal: false, targetProps: { rel: 'noopener noreferrer', target: '_blank' }, }) }) test('nowhere → null: unsafe URL, missing page, empty link', () => { expect(resolveLink({ type: 'external', url: 'javascript:alert(1)' }, opts)).toBeNull() expect(resolveLink({ type: 'internal', page: 5 }, opts)).toBeNull() expect(resolveLink(null, opts)).toBeNull() }) test('the homepage collapses to the locale root', () => { expect(resolveLink(page('start'), opts)?.href).toBe('/pl') }) }) describe('resolveNav', () => { test('nested items resolved; dead or unlabeled items dropped', () => { const items = [ { link: page('oferta'), subItems: [{ link: page('www') }, { link: { type: 'internal', label: 'x', page: null } }] }, { link: { type: 'external', url: 'https://x.pl' } }, // no label { link: page('kontakt') }, ] const nav = resolveNav(items, opts) expect(nav.map((i) => [i.href, i.children.map((c) => c.href)])).toEqual([ ['/pl/oferta', ['/pl/www']], ['/pl/kontakt', []], ]) }) test('custom item shape and junk input', () => { expect(resolveNav([{ children: [{ to: page('a') }], to: page('b') }], { ...opts, childrenField: 'children', linkField: 'to' })[0].children[0].href).toBe('/pl/a') expect(resolveNav(null, opts)).toEqual([]) expect(resolveNav('x', opts)).toEqual([]) }) test('homeSlugFrom reads a populated homepage only', () => { expect(homeSlugFrom({ homepage: { slug: 'start' } })).toBe('start') expect(homeSlugFrom({ homepage: 3 })).toBeUndefined() expect(homeSlugFrom(null)).toBeUndefined() }) }) describe('section fields', () => { test('defaults match the documented shape (types SectionHeaderData / SectionSettings)', () => { const header = buildSectionHeaderField() expect(header).toMatchObject({ name: 'header', interfaceName: 'SectionHeaderData' }) expect(header.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['eyebrow', 'heading', 'intro']) const settings = buildSectionSettingsField() expect(settings).toMatchObject({ name: 'settings', admin: { position: 'sidebar' }, interfaceName: 'SectionSettings' }) expect(settings.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['anchor', 'background']) }) test('the project decides: own backgrounds, no anchor, extra fields', () => { const s = buildSectionSettingsField({ anchor: false, backgrounds: [{ label: 'Brand', value: 'brand' }], fields: [{ name: 'spacing', type: 'text' }] }) expect(s.fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['background', 'spacing']) expect(s.fields[0]).toMatchObject({ defaultValue: 'brand' }) expect(buildSectionSettingsField({ backgrounds: false }).fields.map((f) => ('name' in f ? f.name : ''))).toEqual(['anchor']) }) }) describe('project-defined link styles', () => { test('appearances accept the project\'s own names and panel labels', async () => { const { buildLinkField } = await import('../src/modules/links/buildLinkField.js') const field = buildLinkField({ appearances: ['primary', { label: 'Highlighted', value: 'highlight' }] }) const select = field.fields.find((f) => 'name' in f && f.name === 'appearance') as { defaultValue: string; options: unknown[] } // a default style named by value gets its panel label; the project's own label is kept expect(select.options).toEqual([ { label: { en: 'Main button', pl: 'Przycisk główny' }, value: 'primary' }, { label: 'Highlighted', value: 'highlight' }, ]) expect(select.defaultValue).toBe('primary') expect(resolveLink({ ...page('oferta'), appearance: 'highlight' }, opts)?.appearance).toBe('highlight') }) }) describe('resolveSystemLinks', () => { test('href and label (page title) from System Pages; unassigned roles skipped', async () => { const { resolveSystemLinks } = await import('../src/modules/links/resolveNav.js') const settings = { cookiePolicy: null, privacyPolicy: { slug: 'polityka-prywatnosci', title: 'Polityka prywatności' }, termsOfService: { slug: 'regulamin', title: 'Regulamin' }, } expect(resolveSystemLinks(settings, opts).map((l) => [l.role, l.href, l.label])).toEqual([ ['privacyPolicy', '/pl/polityka-prywatnosci', 'Polityka prywatności'], ['termsOfService', '/pl/regulamin', 'Regulamin'], ]) expect(resolveSystemLinks(settings, { ...opts, roles: ['termsOfService'] })).toHaveLength(1) expect(resolveSystemLinks(null, opts)).toEqual([]) }) }) describe('links to content entries (polymorphic page relation)', () => { test('buildLinkField with several collections makes a polymorphic relation', () => { const field = buildLinkField({ relationTo: ['pages', 'categories'] }) const page = JSON.stringify(field.fields) expect(page).toContain('"relationTo":["pages","categories"]') }) test('an entry resolves under its archive page; a page stays a page; no archive → null', () => { const entry = { type: 'internal' as const, page: { relationTo: 'categories', value: { slug: 'pierscionki' } } } const archives = { categories: 'kategorie' } expect(resolveLinkHref(entry, { archives, config, locale: 'pl' })).toBe('/pl/kategorie/pierscionki') expect(resolveLinkHref({ type: 'internal', page: { relationTo: 'pages', value: { slug: 'kontakt' } } }, { archives, config, locale: 'pl' })).toBe('/pl/kontakt') expect(resolveLinkHref(entry, { config, locale: 'pl' })).toBeNull() }) })