Files
ipal-kit/test/indexNow.test.ts
T
radoslaw.smolinskiandClaude Opus 5.5 c9a50abc33 SEO/GEO tools and project requests: site index, IndexNow, page JSON-LD, fixes
GEO / SEO
- collectSiteIndex: one list of public docs per locale (pages + archive
  entries) on sitemap rules; llms.txt/llms-full and MCP list_pages now include
  blog/content entries and every language. Fixes hard-coded "## Strony" heading
  and homepage not collapsing to the locale root in llms.txt.
- Markdown frontmatter: canonical url + updated date.
- Metadata: X/Twitter card, OG image width/height/alt, og:type article with
  published/modified times for entries.
- Fix: Site Settings "defaultShareImage" was never used — now the OG fallback.
- getPageJsonLd (createContentHelpers): WebPage/CollectionPage/BlogPosting,
  BreadcrumbList from the real route, FAQPage via a project mapper.
- IndexNow (opt-in ipalKit({ indexNow: true })): submit published URLs in all
  languages, key generated in Site Integrations or INDEXNOW_KEY, key route.
- sameAs profiles in Site Settings + sameAsUrls(); sitemap <image:image>.

From project reports
- Fix: package exports were missing ./env and ./fluid.css although the docs
  import them (TS2307); regression test checks every documented subpath.
- Fix: plugins run before Payload adds its default users collection, so roles
  and 2FA had nothing to attach to — ipalKit injects an auth collection when
  the project has none (project's own one wins). Audit no longer errors on it.
- createMarkdownRoute, createSubmitAction (/server, headers injected).
- formatOpeningHours (PL/EN/DE text → schema.org), used by
  buildLocalBusinessJsonLd, which also takes a schema.org `type`.
- buildNavigationJsonLd straight from the header global.
- buildCsp googleMaps: full Maps JS API domain set; values de-duplicated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-16 02:19:08 +02:00

96 lines
3.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import type { I18nConfig } from '../src/modules/i18n/types.js'
import { buildIndexNowHook, createIndexNowKeyRoute, indexNowFields } from '../src/modules/seo/indexNow.js'
const config: I18nConfig = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
function fakeReq(doc: Record<string, unknown>, settings: Record<string, unknown>) {
return {
payload: {
findByID: vi.fn(async () => doc),
findGlobal: vi.fn(async ({ slug }: { slug: string }) => (slug === 'site-integrations' ? { indexNowKey: 'k123' } : settings)),
},
}
}
const flush = () => new Promise((r) => setTimeout(r, 0))
describe('IndexNow hook', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(null, { status: 202 }))
vi.stubEnv('NODE_ENV', 'production')
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://acme.pl')
})
afterEach(() => {
vi.restoreAllMocks()
vi.unstubAllEnvs()
})
const run = async (row: Record<string, unknown>, opts = {}, settings: Record<string, unknown> = {}) => {
const req = fakeReq({ slug: { en: 'about', pl: 'o-nas' } }, settings)
const hook = buildIndexNowHook({ config, ...opts })
await hook({ collection: { slug: 'pages' }, context: {}, doc: { id: 1, ...row }, req } as never)
await flush()
await flush()
}
test('published page → every language submitted with key and keyLocation', async () => {
await run({ _status: 'published' })
expect(fetchSpy).toHaveBeenCalledOnce()
const body = JSON.parse((fetchSpy.mock.calls[0][1] as RequestInit).body as string)
expect(body).toEqual({
host: 'acme.pl',
key: 'k123',
keyLocation: 'https://acme.pl/indexnow-key.txt',
urlList: ['https://acme.pl/pl/o-nas', 'https://acme.pl/en/about'],
})
})
test('archive entry gets the archive prefix; no archive page → nothing sent', async () => {
await run({}, { contentCollection: 'posts' }, { postsArchive: { slug: { en: 'blog', pl: 'blog-pl' } } })
const body = JSON.parse((fetchSpy.mock.calls[0][1] as RequestInit).body as string)
expect(body.urlList).toEqual(['https://acme.pl/pl/blog-pl/o-nas', 'https://acme.pl/en/blog/about'])
fetchSpy.mockClear()
await run({}, { contentCollection: 'posts' }, {})
expect(fetchSpy).not.toHaveBeenCalled()
})
test('drafts, noindex, dev and localhost send nothing', async () => {
await run({ _status: 'draft' })
await run({ noindex: true })
vi.stubEnv('NODE_ENV', 'development')
await run({})
vi.stubEnv('NODE_ENV', 'production')
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'http://localhost:3000')
await run({})
expect(fetchSpy).not.toHaveBeenCalled()
})
})
describe('IndexNow key', () => {
afterEach(() => vi.unstubAllEnvs())
test('field generates a 32-char key when empty and keeps an existing one', () => {
const hook = (indexNowFields[0] as { hooks: { beforeChange: ((a: { value: unknown }) => string)[] } }).hooks.beforeChange[0]
expect(hook({ value: '' })).toMatch(/^[0-9a-f]{32}$/)
expect(hook({ value: ' mine ' })).toBe('mine')
})
test('key route: env wins, 404 without a key', async () => {
const payload = { findGlobal: async () => ({}) } as never
expect((await createIndexNowKeyRoute({ getCachedPayload: async () => payload })()).status).toBe(404)
vi.stubEnv('INDEXNOW_KEY', 'fromenv')
const res = await createIndexNowKeyRoute({ getCachedPayload: async () => payload })()
expect(await res.text()).toBe('fromenv')
})
})