|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { expandImports } from '../packages/ts-ssg/src/mdx/imports' |
| 6 | +import { ensureLegalContent } from './ensureLegalContent' |
| 7 | + |
| 8 | +async function withContentDir(run: (directory: string) => Promise<void>) { |
| 9 | + const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'purestack-legal-')) |
| 10 | + try { |
| 11 | + await run(directory) |
| 12 | + } finally { |
| 13 | + await fs.rm(directory, { recursive: true, force: true }) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +describe('private legal content', () => { |
| 18 | + it('creates importable placeholders when files are missing', async () => { |
| 19 | + await withContentDir(async (contentDir) => { |
| 20 | + await ensureLegalContent(contentDir) |
| 21 | + for (const page of ['imprint', 'privacy']) { |
| 22 | + const content = await expandImports( |
| 23 | + `<import-content src="./_private/${page}.mdx"/>`, |
| 24 | + { contentDir, sourceRelPath: `legal/${page}.mdx` }, |
| 25 | + ) |
| 26 | + expect(content).toContain('Content required.') |
| 27 | + expect(content).toContain(`legal/_private/${page}.mdx`) |
| 28 | + expect(content).toContain('<h1') |
| 29 | + expect(content).not.toContain('Legal basis:') |
| 30 | + } |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it.each(['', '<p>Existing private legal content.</p>'])( |
| 35 | + 'preserves an existing file and creates only the missing file (%j)', |
| 36 | + async (content) => { |
| 37 | + await withContentDir(async (contentDir) => { |
| 38 | + const directory = path.join(contentDir, 'legal', '_private') |
| 39 | + await fs.mkdir(directory, { recursive: true }) |
| 40 | + const filePath = path.join(directory, 'imprint.mdx') |
| 41 | + await fs.writeFile(filePath, content) |
| 42 | + await ensureLegalContent(contentDir) |
| 43 | + await ensureLegalContent(contentDir) |
| 44 | + expect(await fs.readFile(filePath, 'utf8')).toBe(content) |
| 45 | + expect( |
| 46 | + await fs.readFile(path.join(directory, 'privacy.mdx'), 'utf8'), |
| 47 | + ).toContain('Content required.') |
| 48 | + }) |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + it('safely handles concurrent initialization', async () => { |
| 53 | + await withContentDir(async (contentDir) => { |
| 54 | + await Promise.all([ |
| 55 | + ensureLegalContent(contentDir), |
| 56 | + ensureLegalContent(contentDir), |
| 57 | + ]) |
| 58 | + expect( |
| 59 | + await fs.readdir(path.join(contentDir, 'legal', '_private')), |
| 60 | + ).toEqual(['imprint.mdx', 'privacy.mdx']) |
| 61 | + }) |
| 62 | + }) |
| 63 | +}) |
0 commit comments