|
| 1 | +import { describe, expect, test, vi } from 'vitest' |
| 2 | + |
| 3 | +import type { Page } from '@/types' |
| 4 | + |
| 5 | +vi.mock('@/frame/lib/read-json-file', () => ({ |
| 6 | + readCompressedJsonFileFallback: () => ({}), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('../../lib/exception-redirects', () => ({ |
| 10 | + default: () => ({}), |
| 11 | +})) |
| 12 | + |
| 13 | +const { default: precompileRedirects } = await import('../../lib/precompile') |
| 14 | +const { default: generateRedirectsForPermalinks } = await import('../../lib/permalinks') |
| 15 | + |
| 16 | +// Minimal stand-in for a Page instance. precompileRedirects() only relies on |
| 17 | +// `languageCode`, `permalinks`, and `buildRedirects()`. |
| 18 | +function makePage( |
| 19 | + languageCode: string, |
| 20 | + permalinks: { pageVersion: string; hrefWithoutLanguage: string }[], |
| 21 | + redirectFrom: string[], |
| 22 | +): Page { |
| 23 | + const fullPermalinks = permalinks.map((permalink) => ({ |
| 24 | + languageCode, |
| 25 | + title: 'Title', |
| 26 | + href: `/${languageCode}${permalink.hrefWithoutLanguage}`, |
| 27 | + ...permalink, |
| 28 | + })) |
| 29 | + return { |
| 30 | + languageCode, |
| 31 | + permalinks: fullPermalinks, |
| 32 | + redirect_from: redirectFrom, |
| 33 | + buildRedirects: () => generateRedirectsForPermalinks(fullPermalinks, redirectFrom), |
| 34 | + } as unknown as Page |
| 35 | +} |
| 36 | + |
| 37 | +describe('precompileRedirects', () => { |
| 38 | + test('removes a redirect_from-generated redirect that clobbers a live old-page permalink, but keeps it for versions where the old page is absent', async () => { |
| 39 | + // The old page only exists in GHES 3.14. |
| 40 | + const oldPage = makePage( |
| 41 | + 'en', |
| 42 | + [ |
| 43 | + { |
| 44 | + pageVersion: 'enterprise-server@3.14', |
| 45 | + hrefWithoutLanguage: '/enterprise-server@3.14/foo', |
| 46 | + }, |
| 47 | + ], |
| 48 | + [], |
| 49 | + ) |
| 50 | + |
| 51 | + // The replacement page exists in both 3.14 and 3.15, and declares |
| 52 | + // `redirect_from: ['/foo']`, which would otherwise clobber the old |
| 53 | + // page's live permalink in 3.14. |
| 54 | + const newPage = makePage( |
| 55 | + 'en', |
| 56 | + [ |
| 57 | + { |
| 58 | + pageVersion: 'enterprise-server@3.14', |
| 59 | + hrefWithoutLanguage: '/enterprise-server@3.14/bar', |
| 60 | + }, |
| 61 | + { |
| 62 | + pageVersion: 'enterprise-server@3.15', |
| 63 | + hrefWithoutLanguage: '/enterprise-server@3.15/bar', |
| 64 | + }, |
| 65 | + ], |
| 66 | + ['/foo'], |
| 67 | + ) |
| 68 | + |
| 69 | + const redirects = await precompileRedirects([oldPage, newPage]) |
| 70 | + |
| 71 | + // The old page's live permalink in 3.14 must not be clobbered by the |
| 72 | + // replacement page's redirect_from. |
| 73 | + expect(redirects['/enterprise-server@3.14/foo']).toBeUndefined() |
| 74 | + |
| 75 | + // But in 3.15, where the old page doesn't exist, the redirect must |
| 76 | + // still be there. |
| 77 | + expect(redirects['/enterprise-server@3.15/foo']).toBe('/enterprise-server@3.15/bar') |
| 78 | + }) |
| 79 | +}) |
0 commit comments