chore: more refactorings

This commit is contained in:
2026-02-22 09:28:20 +01:00
parent 2a73db57b4
commit 7413cb137b
9 changed files with 1129 additions and 675 deletions

View File

@@ -0,0 +1,75 @@
import { mkdir, readFile } from 'node:fs/promises';
import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';
import {
normalizeGeneratedUrlPath,
urlPathToHtmlIndexPath,
writeFileIfHashChanged,
writeHtmlPage,
} from '../../src/main/engine/BlogGenerationOutputService';
function makeTempName(): string {
return `bds-generation-output-${Date.now()}-${Math.random().toString(36).slice(2)}`;
}
describe('BlogGenerationOutputService', () => {
it('normalizes URL paths and maps them to html index file paths', () => {
expect(normalizeGeneratedUrlPath('')).toBe('/');
expect(normalizeGeneratedUrlPath('/a/b/')).toBe('/a/b');
expect(urlPathToHtmlIndexPath('/tmp/html', '/')).toBe('/tmp/html/index.html');
expect(urlPathToHtmlIndexPath('/tmp/html', '/a/b')).toBe('/tmp/html/a/b/index.html');
});
it('writes only when generated hash changes', async () => {
const tempRoot = path.join('/tmp', makeTempName());
await mkdir(tempRoot, { recursive: true });
const filePath = path.join(tempRoot, 'a.txt');
const getHash = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce('same-hash');
const setHash = vi.fn().mockResolvedValue(undefined);
const hashFn = vi.fn().mockReturnValue('same-hash');
const changed = await writeFileIfHashChanged({
projectId: 'p',
filePath,
relativePath: 'a.txt',
content: 'hello',
getGeneratedFileHash: getHash,
setGeneratedFileHash: setHash,
computeHash: hashFn,
});
const unchanged = await writeFileIfHashChanged({
projectId: 'p',
filePath,
relativePath: 'a.txt',
content: 'hello',
getGeneratedFileHash: getHash,
setGeneratedFileHash: setHash,
computeHash: hashFn,
});
expect(changed).toBe(true);
expect(unchanged).toBe(false);
expect(await readFile(filePath, 'utf-8')).toBe('hello');
});
it('writes html pages under index.html route directories', async () => {
const tempRoot = path.join('/tmp', makeTempName());
const htmlDir = path.join(tempRoot, 'html');
await mkdir(htmlDir, { recursive: true });
await writeHtmlPage({
projectId: 'p',
htmlDir,
urlPath: 'section/page',
content: '<html/>',
getGeneratedFileHash: async () => null,
setGeneratedFileHash: async () => undefined,
computeHash: () => 'h',
});
const saved = await readFile(path.join(htmlDir, 'section', 'page', 'index.html'), 'utf-8');
expect(saved).toBe('<html/>');
});
});

View File

@@ -0,0 +1,73 @@
import { describe, expect, it } from 'vitest';
import type { PostData } from '../../src/main/engine/PostEngine';
import {
buildGenerationPostIndex,
estimateGenerationUnitsBySection,
} from '../../src/main/engine/GenerationPostIndexService';
function makePost(overrides: Partial<PostData> = {}): PostData {
const createdAt = overrides.createdAt ?? new Date('2025-01-02T10:00:00.000Z');
return {
id: overrides.id ?? 'post-1',
projectId: overrides.projectId ?? 'project',
title: overrides.title ?? 'Title',
slug: overrides.slug ?? 'title',
excerpt: overrides.excerpt,
content: overrides.content ?? 'Body',
status: overrides.status ?? 'published',
author: overrides.author,
createdAt,
updatedAt: overrides.updatedAt ?? createdAt,
publishedAt: overrides.publishedAt,
tags: overrides.tags ?? [],
categories: overrides.categories ?? [],
};
}
describe('GenerationPostIndexService', () => {
it('indexes posts by category tag and date partitions', () => {
const posts = [
makePost({ id: '1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T00:00:00.000Z') }),
makePost({ id: '2', categories: ['page'], tags: ['t2'], createdAt: new Date('2025-02-20T00:00:00.000Z') }),
];
const index = buildGenerationPostIndex(posts);
expect(index.postsByCategory.get('news')?.map((p) => p.id)).toEqual(['1']);
expect(index.postsByCategory.get('page')?.map((p) => p.id)).toEqual(['2']);
expect(index.postsByTag.get('t2')?.map((p) => p.id)).toEqual(['2']);
expect(index.postsByYear.get(2025)?.length).toBe(2);
expect(index.postsByYearMonth.get('2025/01')?.length).toBe(1);
expect(index.postsByYearMonthDay.get('2025/02/20')?.length).toBe(1);
});
it('estimates generation units per section using indexed counts', () => {
const posts = [
makePost({ id: '1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T00:00:00.000Z') }),
makePost({ id: '2', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-14T00:00:00.000Z') }),
makePost({ id: '3', categories: ['page'], tags: [], createdAt: new Date('2025-01-13T00:00:00.000Z') }),
];
const index = buildGenerationPostIndex(posts);
const estimate = estimateGenerationUnitsBySection({
posts,
allCategories: new Set(['news', 'page']),
allTags: new Set(['t1']),
yearsMap: new Map([[2025, new Date()]]),
yearMonthsMap: new Map([['2025/01', new Date()]]),
yearMonthDaysMap: new Map([
['2025/01/15', new Date()],
['2025/01/14', new Date()],
['2025/01/13', new Date()],
]),
maxPostsPerPage: 2,
postIndex: index,
});
expect(estimate.core).toBeGreaterThanOrEqual(4);
expect(estimate.single).toBe(3);
expect(estimate.category).toBe(2);
expect(estimate.tag).toBe(1);
expect(estimate.date).toBeGreaterThan(0);
});
});

View File

@@ -0,0 +1,34 @@
import { describe, expect, it, vi } from 'vitest';
import { createGenerationRouteRenderer } from '../../src/main/engine/GenerationRouteRendererFactory';
describe('GenerationRouteRendererFactory', () => {
it('normalizes route keys and memoizes html rendering calls', async () => {
const renderWithContext = vi.fn().mockResolvedValue('<html>ok</html>');
const renderRoute = createGenerationRouteRenderer({
renderWithContext,
context: {
projectContext: {
projectId: 'p',
dataDir: '/tmp',
projectName: 'P',
projectDescription: 'D',
},
metadata: {
name: 'P',
description: 'D',
},
menu: { items: [] },
maxPostsPerPage: 50,
},
});
const a = await renderRoute('/foo/');
const b = await renderRoute('/foo');
expect(a).toBe('<html>ok</html>');
expect(b).toBe('<html>ok</html>');
expect(renderWithContext).toHaveBeenCalledTimes(1);
expect(renderWithContext).toHaveBeenCalledWith('/foo', expect.any(Object));
});
});

View File

@@ -0,0 +1,61 @@
import { describe, expect, it, vi } from 'vitest';
import type { PostData } from '../../src/main/engine/PostEngine';
import { generateRootPages, generateSinglePostPages } from '../../src/main/engine/RoutePageGenerationService';
function makePost(overrides: Partial<PostData> = {}): PostData {
const createdAt = overrides.createdAt ?? new Date('2025-01-02T10:00:00.000Z');
return {
id: overrides.id ?? 'post-1',
projectId: overrides.projectId ?? 'project',
title: overrides.title ?? 'Title',
slug: overrides.slug ?? 'title',
excerpt: overrides.excerpt,
content: overrides.content ?? 'Body',
status: overrides.status ?? 'published',
author: overrides.author,
createdAt,
updatedAt: overrides.updatedAt ?? createdAt,
publishedAt: overrides.publishedAt,
tags: overrides.tags ?? [],
categories: overrides.categories ?? [],
};
}
describe('RoutePageGenerationService', () => {
it('renders root and paginated list routes', async () => {
const posts = [makePost({ id: '1' }), makePost({ id: '2' }), makePost({ id: '3' })];
const renderRoute = vi.fn().mockResolvedValue('<html>ok</html>');
const writePage = vi.fn().mockResolvedValue(true);
const count = await generateRootPages({
projectId: 'p',
posts,
maxPostsPerPage: 2,
renderRoute,
writePage,
onPageGenerated: () => {},
});
expect(count).toBe(2);
expect(renderRoute).toHaveBeenCalledWith('/');
expect(renderRoute).toHaveBeenCalledWith('/page/2');
});
it('renders canonical single post routes', async () => {
const posts = [makePost({ id: '1', slug: 'hello', createdAt: new Date('2025-01-15T10:00:00.000Z') })];
const renderRoute = vi.fn().mockResolvedValue('<html>ok</html>');
const writePage = vi.fn().mockResolvedValue(true);
const count = await generateSinglePostPages({
projectId: 'p',
posts,
renderRoute,
writePage,
onPageGenerated: () => {},
});
expect(count).toBe(1);
expect(renderRoute).toHaveBeenCalledWith('/2025/01/15/hello');
expect(writePage).toHaveBeenCalledWith('p', '2025/01/15/hello', '<html>ok</html>');
});
});