Commit 29e40dcf by PLN (Algolia)

blog: test de l'arbre par année sur racine jetable (useBlogRoot) — le walker…

blog: test de l'arbre par année sur racine jetable (useBlogRoot) — le walker tolère une entrée qui disparaît pendant l'énumération (les suites jest partagent le vrai content/blog en workers parallèles)
parent 68398dc9
...@@ -44,6 +44,16 @@ export const BLOG_LANGS = ['fr', 'en']; ...@@ -44,6 +44,16 @@ export const BLOG_LANGS = ['fr', 'en'];
/** A 4-digit directory under the blog root is a year group, not a post. */ /** A 4-digit directory under the blog root is a year group, not a post. */
const YEAR_GROUP = /^\d{4}$/; const YEAR_GROUP = /^\d{4}$/;
// stat that tolerates an entry vanishing mid-enumeration (the jest suites
// share the real content/blog across parallel workers, fixtures come and go).
function statDir(dir) {
try {
return fs.statSync(dir).isDirectory();
} catch {
return false;
}
}
// A folder with neither meta.md nor a language file is not a post — // A folder with neither meta.md nor a language file is not a post —
// notes, scratch, seeds-in-progress. It mints no slug and no route. // notes, scratch, seeds-in-progress. It mints no slug and no route.
function isPostDir(dir) { function isPostDir(dir) {
...@@ -65,11 +75,11 @@ export function listPostDirs(root) { ...@@ -65,11 +75,11 @@ export function listPostDirs(root) {
const posts = []; const posts = [];
for (const name of fs.readdirSync(root)) { for (const name of fs.readdirSync(root)) {
const dir = path.join(root, name); const dir = path.join(root, name);
if (!fs.statSync(dir).isDirectory()) continue; if (!statDir(dir)) continue;
if (YEAR_GROUP.test(name)) { if (YEAR_GROUP.test(name)) {
for (const child of fs.readdirSync(dir)) { for (const child of fs.readdirSync(dir)) {
const childDir = path.join(dir, child); const childDir = path.join(dir, child);
if (fs.statSync(childDir).isDirectory() && isPostDir(childDir)) { if (statDir(childDir) && isPostDir(childDir)) {
posts.push({ slug: child, dir: childDir }); posts.push({ slug: child, dir: childDir });
} }
} }
......
/* Arbre physique du blog : groupes d'année sous content/blog/, slug toujours
basename. Le walker (listPostDirs) est testé sur une racine jetable — rien
ne touche au vrai content/blog, tout est nettoyé en afterAll. */
import fs from 'fs';
import os from 'os';
import path from 'path';
import {
DEFAULT_LANG,
getAllBlogDraftParams,
getAllBlogDraftSlugs,
getAllBlogSlugs,
getBlogPost,
getBlogPosts,
listPostDirs,
resolveBlogKey,
useBlogRoot,
} from '@/lib/blog';
const META = (over = {}) => `---
title: Titre
date: '2026-05-01'
description: ''
tags: []
${Object.entries(over)
.map(([k, v]) => `${k}: ${v}`)
.join('\n')}
---
`;
function writePost(root, rel, { meta = {}, fr = true, en = false }) {
const dir = path.join(root, rel);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'meta.md'), META(meta));
if (fr) fs.writeFileSync(path.join(dir, 'fr.md'), '---\ntitle: Titre FR\n---\n\nCorps.\n');
if (en) fs.writeFileSync(path.join(dir, 'en.md'), '---\ntitle: EN title\n---\n\nBody.\n');
}
describe('blog year tree', () => {
const originalEnv = { NODE_ENV: process.env.NODE_ENV, SHOW_DRAFTS: process.env.SHOW_DRAFTS };
let root;
let restoreRoot;
const fixture = () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'blog-tree-'));
// Year group: a visible post (meta + fr only) and a draft.
writePost(root, '2026/post-meta-fr', { meta: { draft: false } });
writePost(root, '2026/the-draft', { meta: { draft: true } });
// Root-level post — the year group is optional, old layout still valid.
writePost(root, 'root-post', {});
// Ideas pad: flat seed .md, no meta.md, no lang file → not a post.
fs.mkdirSync(path.join(root, 'ideas'));
fs.writeFileSync(path.join(root, 'ideas', 'a-seed.md'), 'seed\n');
// Non-year, non-post directory: skipped, never descended into. No
// meta.md, no lang file — an fr.md alone WOULD make it a post (the
// tolerance shim for hand-created language files).
fs.mkdirSync(path.join(root, 'scratch'));
fs.writeFileSync(path.join(root, 'scratch', 'notes.txt'), 'scratch\n');
// A stray file at the root is ignored too.
fs.writeFileSync(path.join(root, 'loose.md'), 'loose\n');
};
beforeEach(() => {
fixture();
restoreRoot = useBlogRoot(root);
});
afterEach(() => {
restoreRoot();
});
afterAll(() => {
fs.rmSync(root, { recursive: true, force: true });
Object.assign(process.env, originalEnv);
});
test('the walker lists slugs as basenames, whatever the physical depth', () => {
const posts = listPostDirs(root);
expect(posts.map((p) => p.slug).sort()).toEqual(['post-meta-fr', 'root-post', 'the-draft']);
const nested = posts.find((p) => p.slug === 'post-meta-fr');
expect(nested.dir).toBe(path.join(root, '2026', 'post-meta-fr'));
});
test('non-post directories mint no slug and are not descended into', () => {
const slugs = listPostDirs(root).map((p) => p.slug);
expect(slugs).not.toContain('ideas');
expect(slugs).not.toContain('scratch');
expect(slugs).not.toContain('2026');
});
test('drafts follow the draftsVisible rule (hidden in prod, listed in dev)', () => {
const draftSlugs = () => getAllBlogSlugs();
const listed = () => getBlogPosts(DEFAULT_LANG).map((p) => p.slug);
// Dev/jest: drafts visible.
process.env.NODE_ENV = 'test';
expect(draftSlugs().sort()).toEqual(['post-meta-fr', 'root-post', 'the-draft']);
expect(listed()).toContain('the-draft');
// Prod: the draft mints no route and stays out of the listing.
process.env.NODE_ENV = 'production';
expect(draftSlugs().sort()).toEqual(['post-meta-fr', 'root-post']);
expect(listed()).not.toContain('the-draft');
});
test('draft ids stay the plain folder slug — /blog/d/<slug>', () => {
expect(getAllBlogDraftSlugs()).toEqual(['the-draft']);
expect(getAllBlogDraftParams()).toEqual([{ slug: 'the-draft' }]);
expect(getAllBlogDraftParams().map((p) => `/blog/d/${p.slug}`)).toEqual(['/blog/d/the-draft']);
});
test('a year-nested post resolves through its basename like any other', async () => {
const post = await getBlogPost('post-meta-fr');
expect(post.slug).toBe('post-meta-fr');
expect(post.lang).toBe('fr');
expect(resolveBlogKey('root-post')).toEqual({ slug: 'root-post', lang: DEFAULT_LANG });
expect(resolveBlogKey('2026/post-meta-fr')).toBeNull(); // the year is not part of any key
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment