chore: more refactorings and optimizations

This commit is contained in:
2026-02-22 09:54:42 +01:00
parent 011f318710
commit 03657e7a84
11 changed files with 485 additions and 78 deletions

View File

@@ -640,6 +640,118 @@ describe('BlogGenerationEngine', () => {
expect(filteredCallCount).toBeLessThanOrEqual(8);
});
it('skips core sitemap and feed build phases for single-only generation', async () => {
const posts: PostData[] = [];
for (let i = 0; i < 4; i += 1) {
posts.push(makePost({
id: `single-phase-${i}`,
slug: `single-phase-${i}`,
createdAt: new Date(`2025-${String(i + 1).padStart(2, '0')}-10T10:00:00Z`),
}));
}
setupPosts(posts);
const { BlogGenerationEngine } = await import('../../src/main/engine/BlogGenerationEngine');
const engine = new BlogGenerationEngine();
const onProgress = vi.fn();
await engine.generate({
projectId: 'test',
projectName: 'Test Blog',
dataDir: tempDir,
baseUrl: 'https://example.com',
sections: ['single'],
}, onProgress);
const progressMessages = onProgress.mock.calls.map((call) => String(call[1] ?? ''));
expect(progressMessages).not.toContain('Building sitemap XML...');
expect(progressMessages).not.toContain('Building RSS and Atom feeds...');
expect(progressMessages).not.toContain('Writing sitemap and feeds...');
});
it('skips sitemap XML build phase for archive-only generation sections', async () => {
const posts: PostData[] = [];
for (let i = 0; i < 8; i += 1) {
posts.push(makePost({
id: `archive-only-${i}`,
slug: `archive-only-${i}`,
categories: [`cat-${i % 2}`],
tags: [`tag-${i % 3}`],
createdAt: new Date(`2025-${String((i % 4) + 1).padStart(2, '0')}-10T10:00:00Z`),
}));
}
setupPosts(posts);
const { BlogGenerationEngine } = await import('../../src/main/engine/BlogGenerationEngine');
const engine = new BlogGenerationEngine();
const onProgress = vi.fn();
await engine.generate({
projectId: 'test',
projectName: 'Test Blog',
dataDir: tempDir,
baseUrl: 'https://example.com',
sections: ['category', 'tag', 'date'],
}, onProgress);
const progressMessages = onProgress.mock.calls.map((call) => String(call[1] ?? ''));
expect(progressMessages).not.toContain('Building sitemap XML...');
expect(progressMessages).not.toContain('Building RSS and Atom feeds...');
expect(progressMessages).not.toContain('Writing sitemap and feeds...');
});
it('does not rebuild canonical rewrite context for every generated html file', async () => {
const posts = [
makePost({ id: '1', slug: 'p1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T10:00:00Z') }),
makePost({ id: '2', slug: 'p2', categories: ['news'], tags: ['t2'], createdAt: new Date('2025-01-14T10:00:00Z') }),
makePost({ id: '3', slug: 'p3', categories: ['news'], tags: ['t3'], createdAt: new Date('2025-01-13T10:00:00Z') }),
];
setupPosts(posts);
const pageRendererModule = await import('../../src/main/engine/PageRenderer');
const canonicalPathSpy = vi.spyOn(pageRendererModule, 'buildCanonicalPostPath');
const { BlogGenerationEngine } = await import('../../src/main/engine/BlogGenerationEngine');
const engine = new BlogGenerationEngine();
await engine.generate({
projectId: 'test',
projectName: 'Test Blog',
dataDir: tempDir,
baseUrl: 'https://example.com',
maxPostsPerPage: 1,
}, vi.fn());
expect(canonicalPathSpy.mock.calls.length).toBeLessThanOrEqual(6);
canonicalPathSpy.mockRestore();
});
it('does not re-setup engine project context for every rendered html file', async () => {
const posts = [
makePost({ id: '1', slug: 'ctx-1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T10:00:00Z') }),
makePost({ id: '2', slug: 'ctx-2', categories: ['news'], tags: ['t2'], createdAt: new Date('2025-01-14T10:00:00Z') }),
makePost({ id: '3', slug: 'ctx-3', categories: ['news'], tags: ['t3'], createdAt: new Date('2025-01-13T10:00:00Z') }),
];
setupPosts(posts);
const { BlogGenerationEngine } = await import('../../src/main/engine/BlogGenerationEngine');
const engine = new BlogGenerationEngine();
await engine.generate({
projectId: 'test',
projectName: 'Test Blog',
dataDir: tempDir,
baseUrl: 'https://example.com',
maxPostsPerPage: 1,
}, vi.fn());
expect(mockPostEngine.setProjectContext.mock.calls.length).toBeLessThanOrEqual(2);
});
it('reduces repeated in-memory filtering across category tag and date generation', async () => {
const posts: PostData[] = [];
for (let i = 0; i < 30; i += 1) {

View File

@@ -72,4 +72,77 @@ describe('BlogGenerationOutputService', () => {
const saved = await readFile(path.join(htmlDir, 'section', 'page', 'index.html'), 'utf-8');
expect(saved).toBe('<html/>');
});
it('reuses in-run hash cache to avoid repeated hash reads for same file', async () => {
const tempRoot = path.join('/tmp', makeTempName());
await mkdir(tempRoot, { recursive: true });
const filePath = path.join(tempRoot, 'cached.txt');
const hashCache = new Map<string, string | null>();
const getHash = vi.fn().mockResolvedValue(null);
const setHash = vi.fn().mockResolvedValue(undefined);
const hashFn = vi.fn().mockReturnValue('h1');
await writeFileIfHashChanged({
projectId: 'p',
filePath,
relativePath: 'cached.txt',
content: 'hello',
getGeneratedFileHash: getHash,
setGeneratedFileHash: setHash,
computeHash: hashFn,
hashCache,
});
await writeFileIfHashChanged({
projectId: 'p',
filePath,
relativePath: 'cached.txt',
content: 'hello',
getGeneratedFileHash: getHash,
setGeneratedFileHash: setHash,
computeHash: hashFn,
hashCache,
});
expect(getHash).toHaveBeenCalledTimes(1);
});
it('avoids repeated directory ensure calls when known directory cache is provided', async () => {
const tempRoot = path.join('/tmp', makeTempName());
const htmlDir = path.join(tempRoot, 'html');
await mkdir(htmlDir, { recursive: true });
const ensureDirectory = vi.fn(async (dirPath: string) => {
await mkdir(dirPath, { recursive: true });
});
const knownDirectories = new Set<string>();
await writeHtmlPage({
projectId: 'p',
htmlDir,
urlPath: 'section/page',
content: '<html/>',
getGeneratedFileHash: async () => null,
setGeneratedFileHash: async () => undefined,
computeHash: () => 'h',
ensureDirectory,
knownDirectories,
});
await writeHtmlPage({
projectId: 'p',
htmlDir,
urlPath: 'section/page',
content: '<html/>',
getGeneratedFileHash: async () => 'h',
setGeneratedFileHash: async () => undefined,
computeHash: () => 'h',
ensureDirectory,
knownDirectories,
});
expect(ensureDirectory).toHaveBeenCalledTimes(1);
});
});

View File

@@ -861,6 +861,47 @@ describe('MetaEngine', () => {
expect(metadata?.categoryMetadata?.news?.title).toBe('Newsroom');
});
it('should continue syncOnStartup when categories.json is malformed and recover from database categories', async () => {
const metaDir = metaEngine.getMetaDir();
mockFiles.set(normalizePath(`${metaDir}/project.json`), JSON.stringify({
name: 'Synced Project',
}));
mockFiles.set(normalizePath(`${metaDir}/categories.json`), '["news",');
mockPosts = [
{ categories: JSON.stringify(['db-cat']) },
];
await expect(metaEngine.syncOnStartup()).resolves.toBeUndefined();
const categories = await metaEngine.getCategories();
expect(categories).toContain('db-cat');
const metadata = await metaEngine.getProjectMetadata();
expect(metadata?.name).toBe('Synced Project');
});
it('should continue syncOnStartup when category-meta.json is malformed and keep valid project metadata', async () => {
const metaDir = metaEngine.getMetaDir();
mockFiles.set(normalizePath(`${metaDir}/project.json`), JSON.stringify({
name: 'Synced Project',
}));
mockFiles.set(normalizePath(`${metaDir}/categories.json`), JSON.stringify(['news']));
mockFiles.set(normalizePath(`${metaDir}/category-meta.json`), '{"news":');
await expect(metaEngine.syncOnStartup()).resolves.toBeUndefined();
const metadata = await metaEngine.getProjectMetadata() as any;
expect(metadata?.name).toBe('Synced Project');
expect(metadata?.categoryMetadata?.news).toEqual(
expect.objectContaining({
renderInLists: true,
showTitle: true,
title: 'news',
}),
);
});
it('should create project.json with data from database during syncOnStartup if file does not exist', async () => {
const metaDir = metaEngine.getMetaDir();
const projectPath = normalizePath(`${metaDir}/project.json`);