import assert from 'node:assert/strict' import test from 'node:test' import { selectSubtree } from '../src/page-selection.js' import { normalizeBaseUrl } from '../src/utils.js' const base = normalizeBaseUrl('https://wiki.example.com') const pages = [ { id: 1, path: 'docs', locale: 'en', title: 'Docs', isPublished: true }, { id: 2, path: 'docs/fallback', locale: 'en', title: 'Fallback', isPublished: true }, { id: 3, path: 'docs/install', locale: 'en', title: 'Install', isPublished: true }, { id: 4, path: 'docs/install/linux', locale: 'en', title: 'Linux', isPublished: true }, { id: 5, path: 'docs/other', locale: 'en', title: 'Other', isPublished: true }, { id: 6, path: 'docs/draft', locale: 'en', title: 'Draft', isPublished: false }, { id: 7, path: 'docs-other', locale: 'en', title: 'Excluded sibling', isPublished: true }, { id: 8, path: 'guide', locale: 'en', title: 'Guide', isPublished: true }, ] test('selects the starting page and path descendants, not similarly named siblings', () => { const result = selectSubtree({ pages, baseUrl: base, startPage: 'docs', locale: 'en', }) assert.equal(result.locale, 'en') assert.equal(result.startPath, 'docs') assert.deepEqual( result.pages.map((page) => page.path), ['docs', 'docs/fallback', 'docs/install', 'docs/install/linux', 'docs/other'], ) assert.deepEqual( result.pages.map((page) => page.depth), [0, 1, 1, 2, 1], ) assert.equal(result.omittedUnpublished, 1) }) test('can include unpublished descendants explicitly', () => { const result = selectSubtree({ pages, baseUrl: base, startPage: 'docs', locale: 'en', includeUnpublished: true, }) assert.ok(result.pages.some((page) => page.path === 'docs/draft')) assert.equal(result.omittedUnpublished, 0) }) test('accepts a full Wiki.js page URL with a locale prefix', () => { const result = selectSubtree({ pages, baseUrl: base, startPage: 'https://wiki.example.com/en/docs/install/', }) assert.equal(result.startPath, 'docs/install') assert.deepEqual( result.pages.map((page) => page.path), ['docs/install', 'docs/install/linux'], ) }) test('handles a wiki hosted below a URL subpath', () => { const subpathBase = normalizeBaseUrl('https://example.com/wiki/') const subpathPages = pages.map((page) => ({ ...page })) const result = selectSubtree({ pages: subpathPages, baseUrl: subpathBase, startPage: 'https://example.com/wiki/en/docs/install', }) assert.equal(result.startPath, 'docs/install') assert.deepEqual( result.pages.map((page) => page.path), ['docs/install', 'docs/install/linux'], ) }) test('accepts a path with a locale prefix', () => { const result = selectSubtree({ pages, baseUrl: base, startPage: '/en/docs', }) assert.equal(result.startPath, 'docs') assert.equal(result.pages.length, 5) }) test('reports ambiguous locales and honors a default locale', () => { const multilingual = [ ...pages, { id: 20, path: 'docs', locale: 'de', title: 'Doku', isPublished: true }, ] assert.throws( () => selectSubtree({ pages: multilingual, baseUrl: base, startPage: 'docs', }), /multiple locales/, ) const result = selectSubtree({ pages: multilingual, baseUrl: base, startPage: 'docs', defaultLocale: 'de', }) assert.equal(result.locale, 'de') assert.deepEqual( result.pages.map((page) => page.path), ['docs'], ) }) test('reports a missing starting page and unknown locales', () => { assert.throws( () => selectSubtree({ pages, baseUrl: base, startPage: 'missing', locale: 'en' }), /was not found/, ) assert.throws( () => selectSubtree({ pages, baseUrl: base, startPage: 'docs', locale: 'fr' }), /Locale "fr" does not exist/, ) }) test('enforces the page limit', () => { assert.throws( () => selectSubtree({ pages, baseUrl: base, startPage: 'docs', locale: 'en', maxPages: 2, }), /above the configured limit/, ) }) test('maps the root URL to Wiki.js home', () => { const homePages = [ { id: 1, path: 'home', locale: 'en', title: 'Home', isPublished: true }, { id: 2, path: 'home/child', locale: 'en', title: 'Child', isPublished: true }, ] const result = selectSubtree({ pages: homePages, baseUrl: base, startPage: 'https://wiki.example.com/en/', }) assert.equal(result.startPath, 'home') assert.deepEqual( result.pages.map((page) => page.path), ['home', 'home/child'], ) })