|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | + |
| 3 | +import { buildMirroredTemplateFixture } from './build-mirrored-template-fixture.js'; |
| 4 | + |
| 5 | +describe('buildMirroredTemplateFixture', () => { |
| 6 | + test('generates one page per (template, axis value, page index) combination', () => { |
| 7 | + const { pages, templates } = buildMirroredTemplateFixture(); |
| 8 | + expect(templates).toHaveLength(8); |
| 9 | + expect(pages).toHaveLength(8 * 4 * 6); |
| 10 | + }); |
| 11 | + |
| 12 | + test('respects custom axisValues and pagesPerTemplate', () => { |
| 13 | + const { pages } = buildMirroredTemplateFixture({ |
| 14 | + axisValues: ['a', 'b'], |
| 15 | + pagesPerTemplate: 2, |
| 16 | + }); |
| 17 | + expect(pages).toHaveLength(8 * 2 * 2); |
| 18 | + expect(new Set(pages.map((p) => p.axisValue))).toEqual(new Set(['a', 'b'])); |
| 19 | + }); |
| 20 | + |
| 21 | + test('every page carries its own axis-specific stylesheet href', () => { |
| 22 | + const { pages } = buildMirroredTemplateFixture({ axisValues: ['en', 'zh'] }); |
| 23 | + for (const p of pages) { |
| 24 | + expect( |
| 25 | + p.signals.stylesheetHrefs.some((h) => |
| 26 | + h.includes(`/${p.axisValue}/${p.template}/`), |
| 27 | + ), |
| 28 | + ).toBe(true); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + test('wrapperTag "main" and "div" wrap byte-identical inner content', () => { |
| 33 | + // Strips exactly the wrapper tag pair (not any other `<div>` in the |
| 34 | + // page — HEADER/FOOTER contain plenty). The wrapper's closing tag is |
| 35 | + // found by searching backward from `<footer`, since FOOTER (which |
| 36 | + // itself contains `</div>`) always immediately follows the wrapper. |
| 37 | + /** |
| 38 | + * |
| 39 | + * @param html |
| 40 | + * @param open |
| 41 | + * @param close |
| 42 | + */ |
| 43 | + function unwrap(html: string, open: string, close: string): string { |
| 44 | + const start = html.indexOf(open); |
| 45 | + const footerStart = html.indexOf('<footer'); |
| 46 | + expect(start).toBeGreaterThanOrEqual(0); |
| 47 | + expect(footerStart).toBeGreaterThan(start); |
| 48 | + const end = html.lastIndexOf(close, footerStart); |
| 49 | + expect(end).toBeGreaterThan(start); |
| 50 | + return ( |
| 51 | + html.slice(0, start) + |
| 52 | + html.slice(start + open.length, end) + |
| 53 | + html.slice(end + close.length) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const withMain = buildMirroredTemplateFixture({ wrapperTag: 'main' }); |
| 58 | + const withDiv = buildMirroredTemplateFixture({ wrapperTag: 'div' }); |
| 59 | + expect(withMain.pages).toHaveLength(withDiv.pages.length); |
| 60 | + for (const [i, mainPage] of withMain.pages.entries()) { |
| 61 | + const divPage = withDiv.pages[i]!; |
| 62 | + const mainInner = unwrap(mainPage.signals.html, '<main>', '</main>'); |
| 63 | + const divInner = unwrap(divPage.signals.html, '<div class="main-area">', '</div>'); |
| 64 | + expect(mainInner).toBe(divInner); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + test('paths encode the axis value at a fixed segment position', () => { |
| 69 | + const { pages } = buildMirroredTemplateFixture({ axisValues: ['en', 'zh'] }); |
| 70 | + for (const p of pages) { |
| 71 | + expect(p.signals.paths[0]).toBe(p.axisValue); |
| 72 | + expect(p.signals.paths[1]).toBe(p.template); |
| 73 | + } |
| 74 | + }); |
| 75 | +}); |
0 commit comments