import type { Payload } from 'payload' import { sqliteAdapter } from '@payloadcms/db-sqlite' import { lexicalEditor } from '@payloadcms/richtext-lexical' import os from 'node:os' import path from 'node:path' import { getPayload } from 'payload' import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest' import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' vi.mock('server-only', () => ({})) /** * Sitemap, llms.txt and robots against a real (SQLite) Payload: the batched, * `select`-ed, published-only reads must work with the database adapter, and * Site Settings → Search & AI / Under construction must reach the output. */ describe('SEO outputs against a real database', () => { let payload: Payload let configPromise: Parameters[0]['config'] const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] } as const const helpers = () => createContentHelpers({ baseUrl: 'https://acme.pl', config: configPromise, i18n }) const urls = async () => (await helpers().sitemap()).map((e) => e.url).sort() beforeAll(async () => { configPromise = buildTestConfig({ collections: [ { slug: 'users', auth: true, fields: [] }, { slug: 'pages', fields: [ { name: 'title', type: 'text' }, { name: 'slug', type: 'text', index: true, localized: true }, { name: 'noindex', type: 'checkbox' }, ], versions: { drafts: true }, }, { slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media-seo') } }, ], db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }), editor: lexicalEditor(), plugins: [iapl({ access: { authCollection: 'users' }, i18n, pages: { slug: 'pages' }, twoFactor: false })], secret: 'seo-integration-test-secret', telemetry: false, }) payload = await getPayload({ config: configPromise }) const create = async (slug: Record, extra: Record = {}) => { const doc = await payload.create({ collection: 'pages' as never, data: { slug: slug.pl, _status: 'published', title: slug.pl, ...extra } as never, locale: 'pl' as never, }) await payload.update({ id: doc.id, collection: 'pages' as never, data: { slug: slug.en } as never, locale: 'en' as never }) return doc } const home = await create({ en: 'home', pl: 'start' }) await create({ en: 'about', pl: 'o-nas' }) await create({ en: 'hidden', pl: 'ukryta' }, { noindex: true }) await create({ en: 'draft', pl: 'szkic' }, { _status: 'draft' }) await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, siteName: 'Acme' } as never }) }, 60_000) afterAll(async () => { await payload?.destroy() }) test('sitemap: published and indexable pages in every language', async () => { expect(await urls()).toEqual(['https://acme.pl/en', 'https://acme.pl/en/about', 'https://acme.pl/pl', 'https://acme.pl/pl/o-nas']) }) test('project overrides: sitemapEntries has the last word', async () => { const h = createContentHelpers({ baseUrl: 'https://acme.pl', config: configPromise, i18n, sitemapEntries: (entries) => [...entries.filter((e) => !e.url.endsWith('/en')), { url: 'https://acme.pl/custom' }], }) const list = (await h.sitemap()).map((e) => e.url) expect(list).toContain('https://acme.pl/custom') expect(list).not.toContain('https://acme.pl/en') }) test('verification codes and AI crawler rules from Site Settings → Search & AI', async () => { await payload.updateGlobal({ slug: 'site-settings' as never, data: { aiCrawlers: { policy: 'block-training', rules: [{ access: 'disallow', userAgent: 'PerplexityBot' }] }, searchVerification: { google: '' }, } as never, }) const stored = (await payload.findGlobal({ slug: 'site-settings' as never })) as { searchVerification?: { google?: string } } expect(stored.searchVerification?.google).toBe('G-tok_1') const meta = await helpers().generatePageMetadata('pl', ['o-nas']) expect(meta.verification).toEqual({ google: 'G-tok_1' }) const rules = (await helpers().getRobots()).rules as { disallow?: string[]; userAgent: string | string[] }[] expect(rules).toContainEqual({ disallow: ['/'], userAgent: ['PerplexityBot'] }) const assistants = rules.find((r) => Array.isArray(r.userAgent) && r.userAgent.includes('ClaudeBot')) expect(assistants?.userAgent).not.toContain('PerplexityBot') // the code-only handler ignores the panel expect(helpers().robots().rules).not.toContainEqual({ disallow: ['/'], userAgent: ['PerplexityBot'] }) }) test('under construction: sitemap and llms.txt list only the open pages', async () => { await payload.updateGlobal({ slug: 'site-settings' as never, data: { construction: { enabled: true } } as never, locale: 'pl' as never }) expect(await urls()).toEqual(['https://acme.pl/en', 'https://acme.pl/pl']) const llms = await (await helpers().llmsRoute()).text() expect(llms).not.toContain('o-nas') const unfiltered = createContentHelpers({ baseUrl: 'https://acme.pl', config: configPromise, i18n, underConstructionSeo: false }) expect(await unfiltered.sitemap()).toHaveLength(4) await payload.updateGlobal({ slug: 'site-settings' as never, data: { construction: { enabled: false } } as never, locale: 'pl' as never }) expect(await urls()).toHaveLength(4) }) })