Replaced by an explicit list of forms only (ten exported names, runtime env names, project file names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are not touched. package.json keeps the five old bin names beside the five new ones. The old names stop working in this commit and come back as aliases in the next; docs follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
102 lines
3.8 KiB
TypeScript
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 (IAPL_NOINDEX) sends nothing even in production', async () => {
|
|
vi.stubEnv('IAPL_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')
|
|
})
|
|
})
|