Files
ipal-kit/test/indexNow.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 94e563055f SEO/GEO/GSC: sitemap index + caching, Search & AI settings, getRobots
- Sitemap: batched reads (500), published-only where, field select; sitemap
  index over maxUrls (default 10k) with ?part=N; Cache-Control; in-process
  memo; runtime errors → 503 Retry-After (empty urlset only during next build)
- Site Settings → Search & AI tab (seo.searchSettings: false removes it):
  Google/Bing/Yandex verification (paste code or whole meta tag) added to
  page metadata; AI crawler policy + per-bot exceptions; llms.txt note
- buildRobots aiBotRules; getRobots helper reads the panel; more AI agents
- Under construction: sitemap and llms.txt list only open pages
- collectSiteIndex: no silent 1000 cap; llms routes send Cache-Control
- IndexNow: nothing sent with IPAL_NOINDEX
- Unknown route metadata is noindex
- Tests (unit + SQLite integration) and docs/seo.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:09:01 +02:00

102 lines
3.8 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()
})
test('staging copy (IPAL_NOINDEX) sends nothing even in production', async () => {
vi.stubEnv('IPAL_NOINDEX', '1')
await run({ _status: 'published' })
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')
})
})