With prefix_except_default (or prefix_and_default) @nuxtjs/i18n adds a copy of every page per other locale (/pl/contact next to /contact). The route report sent them as pages of their own, so the CMS registry got a second, untranslated page for each. They are the page's translations: the report now skips routes named with another locale's suffix (contact___pl).
149 lines
8.2 KiB
TypeScript
149 lines
8.2 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { htmlSitemapOption, paginationOption } from '../lib/options.ts'
|
|
import { localeCopySuffixes, pageMetaRobots, robotsNoindex, routePatterns, routesTemplate, staticRoutes } from '../lib/routes.ts'
|
|
import { DRIFT_PREFIX, driftWarnings, routeServes } from '../server/lib/drift.ts'
|
|
import { ROUTE_REPORT_MAX_ROUTES, routeReportBody, routeReportOutcome } from '../server/lib/routeReport.ts'
|
|
|
|
const pages = [
|
|
{ path: '/', name: 'index' },
|
|
{ path: '/blog', name: 'blog' },
|
|
{ path: '/posts/:slug()', name: 'posts-slug' },
|
|
{ path: '/:slug(.*)*', name: 'slug' },
|
|
{ path: '/konto', name: 'konto', meta: { requiresAuth: true } },
|
|
{ path: '/szkic', name: 'szkic', meta: { seo: { robots: 'noindex, follow' } } },
|
|
{ path: '/otwarty', name: 'otwarty', meta: { seo: { robots: 'index, follow' } } },
|
|
{ path: '/prywatne', name: 'prywatne' },
|
|
{ path: '/panel', name: 'panel', mode: 'client' },
|
|
{ path: '/mapa-strony', name: 'seo-sitemap-pl' },
|
|
{ path: '/ustawienia', name: 'ustawienia', children: [{ path: '', name: 'ustawienia-index' }, { path: 'profil', name: 'ustawienia-profil' }] },
|
|
]
|
|
|
|
test('pageMetaRobots: seo.robots, then requiresAuth, else the CMS decides', () => {
|
|
assert.equal(pageMetaRobots({ requiresAuth: true }), 'noindex, nofollow')
|
|
assert.equal(pageMetaRobots({ requiresAuth: true, seo: { robots: 'noindex, follow' } }), 'noindex, follow')
|
|
assert.equal(pageMetaRobots({ seo: { robots: ' index, follow ' } }), 'index, follow')
|
|
assert.equal(pageMetaRobots({ requiresAuth: false }), null)
|
|
assert.equal(pageMetaRobots({ seo: { breadcrumbs: false } }), null)
|
|
assert.equal(pageMetaRobots(undefined), null)
|
|
})
|
|
|
|
test('robotsNoindex: noindex and none keep a page out', () => {
|
|
assert.equal(robotsNoindex('noindex, follow'), true)
|
|
assert.equal(robotsNoindex('none'), true)
|
|
assert.equal(robotsNoindex('index, follow'), false)
|
|
assert.equal(robotsNoindex(null), false)
|
|
})
|
|
|
|
test('staticRoutes: indexable paths without parameters', () => {
|
|
assert.deepEqual(staticRoutes(pages, ['/prywatne/**']), ['/', '/blog', '/otwarty', '/ustawienia', '/ustawienia/profil'])
|
|
})
|
|
|
|
test('routePatterns: page paths as patterns, catch-alls marked', () => {
|
|
const out = routePatterns(pages)
|
|
assert.deepEqual(out.find((p) => p.pattern === '/posts/:slug'), { pattern: '/posts/:slug', catchAll: false })
|
|
assert.deepEqual(out.find((p) => p.pattern === '/**'), { pattern: '/**', catchAll: true })
|
|
assert.equal(new Set(out.map((p) => p.pattern)).size, out.length)
|
|
})
|
|
|
|
test('routesTemplate: an ES module with both lists', () => {
|
|
const src = routesTemplate(['/'], [{ pattern: '/', catchAll: false }])
|
|
assert.match(src, /export const staticRoutes = \["\/"\]/)
|
|
assert.match(src, /export const routePatterns = \[\{"pattern":"\/","catchAll":false\}\]/)
|
|
})
|
|
|
|
test('routeReportBody: within the plugin limits', () => {
|
|
assert.deepEqual(routeReportBody(['/', '/blog', 'nope', '/' + 'a'.repeat(2048)]).routes, ['/', '/blog'])
|
|
assert.equal(routeReportBody(Array.from({ length: 2500 }, (_, i) => `/p${i}`)).routes.length, ROUTE_REPORT_MAX_ROUTES)
|
|
})
|
|
|
|
test('routeReportOutcome: sent, absent, refused or one retry', () => {
|
|
assert.equal(routeReportOutcome(204), 'sent')
|
|
assert.equal(routeReportOutcome(200), 'sent')
|
|
assert.equal(routeReportOutcome(404), 'absent')
|
|
assert.equal(routeReportOutcome(401), 'refused')
|
|
assert.equal(routeReportOutcome(403), 'refused')
|
|
assert.equal(routeReportOutcome(503), 'retry')
|
|
assert.equal(routeReportOutcome(null), 'retry')
|
|
})
|
|
|
|
test('options: pagination and HTML sitemap with their checks', () => {
|
|
assert.deepEqual(paginationOption({ form: 'path', param: 'p', segments: { pl: 'strona', en: 'a b' } }), { form: 'path', param: 'p', segments: { pl: 'strona' } })
|
|
assert.deepEqual(paginationOption(undefined), { form: 'query', param: 'page', segments: {} })
|
|
assert.equal(htmlSitemapOption(false), false)
|
|
assert.deepEqual(htmlSitemapOption({ paths: { pl: '/mapa-strony/', en: 'sitemap', de: '/x/:id' } }), { paths: { pl: '/mapa-strony' } })
|
|
})
|
|
|
|
const layer = { pagination: { form: 'query' as const, param: 'page', segments: { pl: 'strona', en: 'page' } }, htmlSitemap: { pl: '/mapa-strony', en: '/en/sitemap' } }
|
|
const routes = routePatterns(pages)
|
|
|
|
test('routeServes: a route of the same shape or a prefixed catch-all; not the root catch-all', () => {
|
|
assert.equal(routeServes({ pattern: '/posts/:slug', catchAll: false }, '/posts/:id'), true)
|
|
assert.equal(routeServes({ pattern: '/posts/:slug', catchAll: false }, '/posts/{slug}'), true)
|
|
assert.equal(routeServes({ pattern: '/docs/**', catchAll: true }, '/docs/:a/:b'), true)
|
|
assert.equal(routeServes({ pattern: '/**', catchAll: true }, '/wpisy/:slug'), false)
|
|
})
|
|
|
|
test('driftWarnings: nothing when the CMS and the build agree', () => {
|
|
const site = {
|
|
routes: [{ type: 'post', pattern: '/posts/:slug' }],
|
|
pagination: { form: 'query' as const, param: 'page', segments: {} },
|
|
html_sitemap: { enabled: true, paths: { pl: '/mapa-strony', en: '/en/sitemap' } },
|
|
}
|
|
assert.deepEqual(driftWarnings({ site, routes, ...layer }), [])
|
|
})
|
|
|
|
test('driftWarnings: one warning per mismatch', () => {
|
|
const site = {
|
|
routes: [{ type: 'journal-post', pattern: '/wpisy/:slug' }],
|
|
pagination: { form: 'path' as const, param: 'page', segments: { pl: 'strona' } },
|
|
html_sitemap: { enabled: true, paths: { pl: '/mapa', en: '/en/sitemap' } },
|
|
}
|
|
const out = driftWarnings({ site, routes, ...layer })
|
|
assert.equal(out.length, 3)
|
|
assert.ok(out.every((w) => w.startsWith(DRIFT_PREFIX)))
|
|
assert.match(out[0]!, /journal-post URLs \(\/wpisy\/:slug\) match no Nuxt route/)
|
|
assert.match(out[1]!, /path form/)
|
|
assert.match(out[2]!, /pl HTML sitemap at \/mapa/)
|
|
})
|
|
|
|
test('driftWarnings: segments and param, a switched-off layer sitemap, a switched-off CMS sitemap', () => {
|
|
const pathLayer = { ...layer, pagination: { form: 'path' as const, param: 'page', segments: { pl: 'strona' } } }
|
|
assert.equal(driftWarnings({ site: { pagination: { form: 'path', segments: { pl: 'str' } } }, routes, ...pathLayer }).length, 1)
|
|
assert.equal(driftWarnings({ site: { pagination: { form: 'query', param: 'p' } }, routes, ...layer }).length, 1)
|
|
assert.equal(driftWarnings({ site: { html_sitemap: { enabled: true, paths: { pl: '/mapa-strony' } } }, routes, ...layer, htmlSitemap: false }).length, 1)
|
|
assert.deepEqual(driftWarnings({ site: { html_sitemap: { enabled: false, paths: { pl: '/x' } } }, routes, ...layer }), [])
|
|
assert.deepEqual(driftWarnings({ site: {}, routes, ...layer }), [])
|
|
})
|
|
|
|
test('routeServes normalises CMS patterns: {param} and :param alike, the query part ignored', () => {
|
|
const route = { pattern: '/blog/:slug', catchAll: false }
|
|
assert.equal(routeServes(route, '/blog/{slug}'), true)
|
|
assert.equal(routeServes(route, '/blog/:slug'), true)
|
|
assert.equal(routeServes(route, '/blog/{slug}?page={page}'), true)
|
|
assert.equal(routeServes(route, '/blog/{category}/{slug}'), false)
|
|
assert.equal(routeServes({ pattern: '/docs/**', catchAll: true }, '/docs/{a}/{b}'), true)
|
|
})
|
|
|
|
test('localeCopySuffixes: the other locales of a prefixed strategy, none without one', () => {
|
|
const locales = [{ code: 'en' }, { code: 'pl' }, 'de']
|
|
assert.deepEqual(localeCopySuffixes({ strategy: 'prefix_except_default', defaultLocale: 'en', locales }), ['___pl', '___de'])
|
|
assert.deepEqual(localeCopySuffixes({ defaultLocale: 'en', locales, routesNameSeparator: '__' }), ['__pl', '__de'])
|
|
assert.deepEqual(localeCopySuffixes({ strategy: 'prefix_and_default', defaultLocale: 'pl', locales }), ['___en', '___de'])
|
|
assert.deepEqual(localeCopySuffixes({ strategy: 'no_prefix', defaultLocale: 'en', locales }), [])
|
|
assert.deepEqual(localeCopySuffixes({ strategy: 'prefix_except_default', locales }), [])
|
|
assert.deepEqual(localeCopySuffixes(undefined), [])
|
|
})
|
|
|
|
test('staticRoutes leaves out the i18n copies of a page in the other locales', () => {
|
|
const localized = [
|
|
{ path: '/', name: 'index___en' },
|
|
{ path: '/pl', name: 'index___pl' },
|
|
{ path: '/contact', name: 'contact___en' },
|
|
{ path: '/pl/contact', name: 'contact___pl' },
|
|
{ path: '/pl/konto', name: 'konto___pl', meta: { requiresAuth: true } },
|
|
]
|
|
assert.deepEqual(staticRoutes(localized, [], ['___pl']), ['/', '/contact'])
|
|
assert.deepEqual(staticRoutes(localized, []), ['/', '/contact', '/pl', '/pl/contact'])
|
|
})
|