Files
vue-journal-layer/tests/routes.test.ts
Jakub Zych 102da43f76 feat: opt-in Journal search page and JournalSearchBox
- journal.routes.search (off by default) registers journal-search, the
  results page for ?q= in the API's relevance order (no sort is sent),
  server-rendered with a Polish-aware result count.
- JournalSearchBox: an icon button and a WAI-ARIA combobox with debounced
  ?fields=suggest suggestions, stale answers dropped, query words marked
  ignoring diacritics; a plain GET form without JS.
- The module keeps the search path out of the route cache and the sitemap
  and marks it noindex, follow.
- Client: suggestPosts and the fields option; useJournalLinks.searchLink.
2026-10-30 18:14:05 +01:00

120 lines
5.0 KiB
TypeScript

// journal.routes: defaults, overrides, disabling, validation and path filling.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
DEFAULT_JOURNAL_ROUTES,
JOURNAL_ROUTE_NAMES,
fillJournalPath,
normalizeJournalRoutes,
} from '../app/utils/routes.ts'
test('no input gives the defaults', () => {
assert.deepEqual(normalizeJournalRoutes(), { ...DEFAULT_JOURNAL_ROUTES })
assert.deepEqual(normalizeJournalRoutes(null), { ...DEFAULT_JOURNAL_ROUTES })
assert.deepEqual(normalizeJournalRoutes({}), { ...DEFAULT_JOURNAL_ROUTES })
})
test('the defaults round-trip through normalization', () => {
const once = normalizeJournalRoutes({ ...DEFAULT_JOURNAL_ROUTES })
assert.deepEqual(normalizeJournalRoutes(once), once)
})
test('the result is a copy, never the frozen defaults', () => {
const routes = normalizeJournalRoutes()
assert.notEqual(routes, DEFAULT_JOURNAL_ROUTES)
assert.ok(Object.isFrozen(DEFAULT_JOURNAL_ROUTES))
})
test('an override replaces one key and keeps the others', () => {
const routes = normalizeJournalRoutes({ category: '/:category' })
assert.equal(routes.category, '/:category')
assert.equal(routes.index, DEFAULT_JOURNAL_ROUTES.index)
assert.equal(routes.post, DEFAULT_JOURNAL_ROUTES.post)
assert.equal(routes.tag, DEFAULT_JOURNAL_ROUTES.tag)
assert.equal(routes.author, DEFAULT_JOURNAL_ROUTES.author)
})
test('a site URL map normalizes as given (grzybyfunkcjonalne shape)', () => {
const input = { index: '/blog', post: '/blog/:slug', category: '/:category', author: '/autor/:author', tag: false, search: '/szukaj' }
assert.deepEqual(normalizeJournalRoutes(input), input)
})
test('false disables a route; null and undefined keep the default', () => {
const routes = normalizeJournalRoutes({ tag: false, author: null, post: undefined })
assert.equal(routes.tag, false)
assert.equal(routes.author, DEFAULT_JOURNAL_ROUTES.author)
assert.equal(routes.post, DEFAULT_JOURNAL_ROUTES.post)
})
test('unknown keys are ignored', () => {
assert.deepEqual(normalizeJournalRoutes({ archive: '/archive' }), { ...DEFAULT_JOURNAL_ROUTES })
})
test('a post path without :slug throws naming journal.routes.post', () => {
assert.throws(() => normalizeJournalRoutes({ post: '/blog/:id' }), /journal\.routes\.post/)
})
test('a param prefix is not the param (:slugs is not :slug)', () => {
assert.throws(() => normalizeJournalRoutes({ post: '/blog/:slugs' }), /journal\.routes\.post/)
assert.equal(normalizeJournalRoutes({ post: '/blog/:slug(.*)' }).post, '/blog/:slug(.*)')
})
test('a category path without :category throws naming journal.routes.category', () => {
assert.throws(() => normalizeJournalRoutes({ category: '/kategoria' }), /journal\.routes\.category/)
})
test('tag and author paths need their params', () => {
assert.throws(() => normalizeJournalRoutes({ tag: '/tags/:slug' }), /journal\.routes\.tag/)
assert.throws(() => normalizeJournalRoutes({ author: '/autor' }), /journal\.routes\.author/)
})
test('a path without a leading / throws naming the key', () => {
assert.throws(() => normalizeJournalRoutes({ index: 'blog' }), /journal\.routes\.index/)
assert.throws(() => normalizeJournalRoutes({ post: 'blog/:slug' }), /journal\.routes\.post/)
})
test('true and non-string values throw', () => {
assert.throws(() => normalizeJournalRoutes({ index: true }), /journal\.routes\.index/)
assert.throws(() => normalizeJournalRoutes({ index: 42 }), /journal\.routes\.index/)
})
test('route names are stable', () => {
assert.deepEqual({ ...JOURNAL_ROUTE_NAMES }, {
index: 'journal-index',
post: 'journal-post',
category: 'journal-category',
tag: 'journal-tag',
author: 'journal-author',
search: 'journal-search',
})
})
test('the search route is off by default and takes a plain path', () => {
assert.equal(DEFAULT_JOURNAL_ROUTES.search, false)
assert.equal(normalizeJournalRoutes().search, false)
assert.equal(normalizeJournalRoutes({ search: '/szukaj' }).search, '/szukaj')
assert.throws(() => normalizeJournalRoutes({ search: 'szukaj' }), /journal\.routes\.search/)
})
test('fillJournalPath encodes a value', () => {
assert.equal(fillJournalPath('/blog/:slug', { slug: 'a b' }), '/blog/a%20b')
assert.equal(fillJournalPath('/blog/:slug', { slug: 'zażółć' }), '/blog/za%C5%BC%C3%B3%C5%82%C4%87')
})
test('fillJournalPath encodes a slug with / per segment', () => {
assert.equal(fillJournalPath('/blog/:slug', { slug: '2024/a b?' }), '/blog/2024/a%20b%3F')
})
test('fillJournalPath fills several params and numbers', () => {
assert.equal(fillJournalPath('/:category/:slug', { category: 'cordyceps', slug: 7 }), '/cordyceps/7')
})
test('fillJournalPath drops a custom matcher and a modifier with the param', () => {
assert.equal(fillJournalPath('/blog/:slug(.*)', { slug: 'a/b' }), '/blog/a/b')
assert.equal(fillJournalPath('/blog/:slug?', { slug: 'x' }), '/blog/x')
})
test('fillJournalPath leaves unknown params empty', () => {
assert.equal(fillJournalPath('/autor/:author', {}), '/autor/')
})