chore: and last refactorings

This commit is contained in:
2026-02-22 09:30:38 +01:00
parent 7413cb137b
commit 011f318710
3 changed files with 215 additions and 58 deletions

View File

@@ -0,0 +1,121 @@
import type { PostData } from './PostEngine';
function resolvePostCreatedAt(post: { createdAt: Date | string }): Date {
if (post.createdAt instanceof Date) {
return post.createdAt;
}
const parsed = new Date(post.createdAt);
return Number.isNaN(parsed.getTime()) ? new Date() : parsed;
}
export function buildApplyValidationArchives(posts: PostData[]): {
allCategories: Set<string>;
allTags: Set<string>;
years: Map<number, Date>;
yearMonths: Map<string, Date>;
yearMonthDays: Map<string, Date>;
} {
const allCategories = new Set<string>();
const allTags = new Set<string>();
const years = new Map<number, Date>();
const yearMonths = new Map<string, Date>();
const yearMonthDays = new Map<string, Date>();
for (const post of posts) {
for (const category of post.categories || []) allCategories.add(category);
for (const tag of post.tags || []) allTags.add(tag);
const createdAt = resolvePostCreatedAt(post);
const updatedAt = post.updatedAt;
const year = createdAt.getFullYear();
const month = String(createdAt.getMonth() + 1).padStart(2, '0');
const day = String(createdAt.getDate()).padStart(2, '0');
const ymKey = `${year}/${month}`;
const ymdKey = `${year}/${month}/${day}`;
if (!years.has(year) || updatedAt > years.get(year)!) {
years.set(year, updatedAt);
}
if (!yearMonths.has(ymKey) || updatedAt > yearMonths.get(ymKey)!) {
yearMonths.set(ymKey, updatedAt);
}
if (!yearMonthDays.has(ymdKey) || updatedAt > yearMonthDays.get(ymdKey)!) {
yearMonthDays.set(ymdKey, updatedAt);
}
}
return {
allCategories,
allTags,
years,
yearMonths,
yearMonthDays,
};
}
export function selectRequestedPosts(params: {
publishedPosts: PostData[];
requestedPostIds: Set<string>;
requestedPageSlugs: Set<string>;
}): {
requestedSinglePosts: PostData[];
requestedPagePosts: PostData[];
} {
const requestedSinglePosts = params.publishedPosts.filter((post) => params.requestedPostIds.has(post.id));
const requestedPagePosts = params.publishedPosts.filter((post) => {
if (!params.requestedPageSlugs.has(post.slug)) {
return false;
}
const categories = Array.isArray(post.categories) ? post.categories : [];
return categories.includes('page');
});
return {
requestedSinglePosts,
requestedPagePosts,
};
}
export function buildRequestedArchiveMaps(params: {
requestedYears: Set<number>;
requestedYearMonths: Set<string>;
requestedYearMonthDays: Set<string>;
years: Map<number, Date>;
yearMonths: Map<string, Date>;
yearMonthDays: Map<string, Date>;
}): {
requestedYearsMap: Map<number, Date>;
requestedYearMonthsMap: Map<string, Date>;
requestedYearMonthDaysMap: Map<string, Date>;
} {
const requestedYearsMap = new Map<number, Date>();
for (const year of params.requestedYears) {
const lastmod = params.years.get(year);
if (lastmod) {
requestedYearsMap.set(year, lastmod);
}
}
const requestedYearMonthsMap = new Map<string, Date>();
for (const ym of params.requestedYearMonths) {
const lastmod = params.yearMonths.get(ym);
if (lastmod) {
requestedYearMonthsMap.set(ym, lastmod);
}
}
const requestedYearMonthDaysMap = new Map<string, Date>();
for (const ymd of params.requestedYearMonthDays) {
const lastmod = params.yearMonthDays.get(ymd);
if (lastmod) {
requestedYearMonthDaysMap.set(ymd, lastmod);
}
}
return {
requestedYearsMap,
requestedYearMonthsMap,
requestedYearMonthDaysMap,
};
}

View File

@@ -39,6 +39,11 @@ import {
generateSinglePostPages, generateSinglePostPages,
generateTagPages, generateTagPages,
} from './RoutePageGenerationService'; } from './RoutePageGenerationService';
import {
buildApplyValidationArchives,
buildRequestedArchiveMaps,
selectRequestedPosts,
} from './ApplyValidationDataService';
const DEFAULT_MAX_POSTS_PER_PAGE = 50; const DEFAULT_MAX_POSTS_PER_PAGE = 50;
const MIN_MAX_POSTS_PER_PAGE = 1; const MIN_MAX_POSTS_PER_PAGE = 1;
@@ -568,34 +573,7 @@ export class BlogGenerationEngine {
const { publishedPosts, publishedListPosts } = await loadPublishedGenerationSets(this.postEngine, listExcludedCategories); const { publishedPosts, publishedListPosts } = await loadPublishedGenerationSets(this.postEngine, listExcludedCategories);
const generationPostIndex = buildGenerationPostIndex(publishedListPosts); const generationPostIndex = buildGenerationPostIndex(publishedListPosts);
const allCategories = new Set<string>(); const { allCategories, allTags, years, yearMonths, yearMonthDays } = buildApplyValidationArchives(publishedListPosts);
const allTags = new Set<string>();
const years = new Map<number, Date>();
const yearMonths = new Map<string, Date>();
const yearMonthDays = new Map<string, Date>();
for (const post of publishedListPosts) {
for (const category of post.categories || []) allCategories.add(category);
for (const tag of post.tags || []) allTags.add(tag);
const createdAt = resolvePostCreatedAt(post);
const updatedAt = post.updatedAt;
const year = createdAt.getFullYear();
const month = String(createdAt.getMonth() + 1).padStart(2, '0');
const day = String(createdAt.getDate()).padStart(2, '0');
const ymKey = `${year}/${month}`;
const ymdKey = `${year}/${month}/${day}`;
if (!years.has(year) || updatedAt > years.get(year)!) {
years.set(year, updatedAt);
}
if (!yearMonths.has(ymKey) || updatedAt > yearMonths.get(ymKey)!) {
yearMonths.set(ymKey, updatedAt);
}
if (!yearMonthDays.has(ymdKey) || updatedAt > yearMonthDays.get(ymdKey)!) {
yearMonthDays.set(ymdKey, updatedAt);
}
}
const targetedPlan = buildTargetedValidationPlan({ const targetedPlan = buildTargetedValidationPlan({
initialPlan: missingPathPlan, initialPlan: missingPathPlan,
@@ -629,38 +607,20 @@ export class BlogGenerationEngine {
// no-op for applyValidation // no-op for applyValidation
}; };
const requestedSinglePosts = publishedPosts.filter((post) => targetedPlan.requestedPostIds.has(post.id)); const { requestedSinglePosts, requestedPagePosts } = selectRequestedPosts({
const requestedPagePosts = publishedPosts.filter((post) => { publishedPosts,
if (!targetedPlan.requestedPageSlugs.has(post.slug)) { requestedPostIds: targetedPlan.requestedPostIds,
return false; requestedPageSlugs: targetedPlan.requestedPageSlugs,
}
const categories = Array.isArray(post.categories) ? post.categories : [];
return categories.includes('page');
}); });
const requestedYearsMap = new Map<number, Date>(); const { requestedYearsMap, requestedYearMonthsMap, requestedYearMonthDaysMap } = buildRequestedArchiveMaps({
for (const year of targetedPlan.requestedYears) { requestedYears: targetedPlan.requestedYears,
const lastmod = years.get(year); requestedYearMonths: targetedPlan.requestedYearMonths,
if (lastmod) { requestedYearMonthDays: targetedPlan.requestedYearMonthDays,
requestedYearsMap.set(year, lastmod); years,
} yearMonths,
} yearMonthDays,
});
const requestedYearMonthsMap = new Map<string, Date>();
for (const ym of targetedPlan.requestedYearMonths) {
const lastmod = yearMonths.get(ym);
if (lastmod) {
requestedYearMonthsMap.set(ym, lastmod);
}
}
const requestedYearMonthDaysMap = new Map<string, Date>();
for (const ymd of targetedPlan.requestedYearMonthDays) {
const lastmod = yearMonthDays.get(ymd);
if (lastmod) {
requestedYearMonthDaysMap.set(ymd, lastmod);
}
}
onProgress( onProgress(
48, 48,

View File

@@ -0,0 +1,76 @@
import { describe, expect, it } from 'vitest';
import type { PostData } from '../../src/main/engine/PostEngine';
import {
buildApplyValidationArchives,
buildRequestedArchiveMaps,
selectRequestedPosts,
} from '../../src/main/engine/ApplyValidationDataService';
function makePost(overrides: Partial<PostData> = {}): PostData {
const createdAt = overrides.createdAt ?? new Date('2025-01-15T10: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('ApplyValidationDataService', () => {
it('builds category tag and date archives from list posts', () => {
const posts = [
makePost({ id: '1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T00:00:00.000Z') }),
makePost({ id: '2', categories: ['page'], tags: [], createdAt: new Date('2025-02-20T00:00:00.000Z') }),
];
const result = buildApplyValidationArchives(posts);
expect(result.allCategories.has('news')).toBe(true);
expect(result.allCategories.has('page')).toBe(true);
expect(result.allTags.has('t1')).toBe(true);
expect(result.years.has(2025)).toBe(true);
expect(result.yearMonths.has('2025/01')).toBe(true);
expect(result.yearMonths.has('2025/02')).toBe(true);
expect(result.yearMonthDays.has('2025/01/15')).toBe(true);
expect(result.yearMonthDays.has('2025/02/20')).toBe(true);
});
it('selects requested single/page posts and resolves requested date maps', () => {
const publishedPosts = [
makePost({ id: 'a', slug: 'post-a', categories: ['news'], createdAt: new Date('2025-01-15T00:00:00.000Z') }),
makePost({ id: 'b', slug: 'about', categories: ['page'], createdAt: new Date('2025-01-10T00:00:00.000Z') }),
];
const selected = selectRequestedPosts({
publishedPosts,
requestedPostIds: new Set(['a']),
requestedPageSlugs: new Set(['about']),
});
expect(selected.requestedSinglePosts.map((p) => p.id)).toEqual(['a']);
expect(selected.requestedPagePosts.map((p) => p.id)).toEqual(['b']);
const archives = buildApplyValidationArchives(publishedPosts);
const requested = buildRequestedArchiveMaps({
requestedYears: new Set([2025]),
requestedYearMonths: new Set(['2025/01']),
requestedYearMonthDays: new Set(['2025/01/15']),
years: archives.years,
yearMonths: archives.yearMonths,
yearMonthDays: archives.yearMonthDays,
});
expect(requested.requestedYearsMap.has(2025)).toBe(true);
expect(requested.requestedYearMonthsMap.has('2025/01')).toBe(true);
expect(requested.requestedYearMonthDaysMap.has('2025/01/15')).toBe(true);
});
});