Feature/python api image discovery (#34)

* Expose chat.analyzeMediaImage in Python API for batch image metadata generation

* Fix updateMedia losing linkedPostIds by reading existing sidecar before overwriting

* Also preserve author from sidecar when DB value is null (data drift)

* Extend MetadataDiffEngine to cover media, scripts, and templates

* Redesign MetadataDiffPanel: item-first view with field pills, filtering, and per-item multi-field diffs

* Fix task:progress startsWith crash (taskId not id) and nested button violation in field pills

* Populate field diffs for file-missing items and show fileMissing badge in UI

* feat: extended meta diff

* feat: meta diff als reconstructs orphans

* chore: updated documentation

---------

Co-authored-by: hugo <hugoms@me.com>
This commit is contained in:
Georg Bauer
2026-03-04 22:37:43 +01:00
committed by GitHub
parent 08ef72a802
commit c4a032346c
23 changed files with 3170 additions and 349 deletions

View File

@@ -1090,6 +1090,114 @@ tags: ["nature", "sunset"]`;
);
});
it('should preserve linkedPostIds from existing sidecar when updating metadata', async () => {
const fs = await import('fs/promises');
const filePath = '/mock/media/linked.jpg';
const sidecarPath = `${filePath}.meta`;
// Pre-populate sidecar with linkedPostIds
mockFiles.set(normalizePath(sidecarPath), `---
id: linked-media-id
originalName: "linked.jpg"
mimeType: image/jpeg
size: 1024
createdAt: 2026-01-01T00:00:00.000Z
updatedAt: 2026-01-01T00:00:00.000Z
author: "Original Author"
tags: ["nature", "photo"]
linkedPostIds: ["post-1", "post-2"]`);
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: 'linked-media-id',
projectId: 'default',
originalName: 'linked.jpg',
mimeType: 'image/jpeg',
size: 1024,
filePath,
title: 'Old title',
author: 'Original Author',
tags: '["nature", "photo"]',
createdAt: new Date(),
updatedAt: new Date(),
}),
});
return chain;
});
vi.mocked(fs.writeFile).mockClear();
await mediaEngine.updateMedia('linked-media-id', { title: 'New title' });
// Verify sidecar was written
expect(fs.writeFile).toHaveBeenCalledWith(
sidecarPath,
expect.any(String),
expect.anything()
);
// The written sidecar content must preserve linkedPostIds AND author
const writtenContent = vi.mocked(fs.writeFile).mock.calls.find(
c => normalizePath(c[0] as string) === normalizePath(sidecarPath)
)?.[1] as string;
expect(writtenContent).toContain('linkedPostIds: ["post-1", "post-2"]');
expect(writtenContent).toContain('author: "Original Author"');
expect(writtenContent).toContain('title: "New title"');
});
it('should preserve author from sidecar when DB has null author', async () => {
const fs = await import('fs/promises');
const filePath = '/mock/media/author-drift.jpg';
const sidecarPath = `${filePath}.meta`;
// Sidecar has author but DB does not (data drift)
mockFiles.set(normalizePath(sidecarPath), `---
id: author-drift-id
originalName: "author-drift.jpg"
mimeType: image/jpeg
size: 2048
createdAt: 2026-01-01T00:00:00.000Z
updatedAt: 2026-01-01T00:00:00.000Z
author: "hugo"
tags: []
linkedPostIds: ["post-x"]`);
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: 'author-drift-id',
projectId: 'default',
originalName: 'author-drift.jpg',
mimeType: 'image/jpeg',
size: 2048,
filePath,
title: 'Old title',
author: null, // DB has null, sidecar has "hugo"
tags: '[]',
createdAt: new Date(),
updatedAt: new Date(),
}),
});
return chain;
});
vi.mocked(fs.writeFile).mockClear();
await mediaEngine.updateMedia('author-drift-id', { alt: 'New alt text' });
const writtenContent = vi.mocked(fs.writeFile).mock.calls.find(
c => normalizePath(c[0] as string) === normalizePath(sidecarPath)
)?.[1] as string;
expect(writtenContent).toContain('author: "hugo"');
expect(writtenContent).toContain('alt: "New alt text"');
expect(writtenContent).toContain('linkedPostIds: ["post-x"]');
});
it('should update FTS index', async () => {
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();

View File

@@ -6,7 +6,13 @@
*/
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { MetadataDiffEngine, PostMetadataDiff, DiffGroup, DiffField } from '../../src/main/engine/MetadataDiffEngine';
import {
MetadataDiffEngine, PostMetadataDiff, DiffGroup, DiffField,
MediaMetadataDiff, MediaDiffField,
ScriptMetadataDiff, ScriptDiffField,
TemplateMetadataDiff, TemplateDiffField,
OrphanFile,
} from '../../src/main/engine/MetadataDiffEngine';
import { resetMockCounters } from '../utils/factories';
// Mock posts data store - used for single-item .get() queries
@@ -154,11 +160,13 @@ vi.mock('../../src/main/engine/TaskManager', () => ({
// Track the mock function for PostEngine.syncPublishedPostFile
const mockSyncPublishedPostFile = vi.fn(async () => true);
const mockImportOrphanFile = vi.fn(async () => ({ id: 'imported-id', title: 'Imported' }));
// Mock PostEngine
vi.mock('../../src/main/engine/PostEngine', () => ({
getPostEngine: vi.fn(() => ({
syncPublishedPostFile: mockSyncPublishedPostFile,
importOrphanFile: mockImportOrphanFile,
})),
}));
@@ -172,8 +180,9 @@ describe('MetadataDiffEngine', () => {
mockFileData.clear();
mockAllPostsRows = [];
mockSyncPublishedPostFile.mockClear();
mockImportOrphanFile.mockClear();
resetMockCounters();
engine = new MetadataDiffEngine({ syncPublishedPostFile: mockSyncPublishedPostFile } as any);
engine = new MetadataDiffEngine({ syncPublishedPostFile: mockSyncPublishedPostFile, importOrphanFile: mockImportOrphanFile } as any);
engine.setProjectContext('test-project');
});
@@ -382,6 +391,73 @@ Content here`);
expect(result?.differences.language?.fileValue).toBe('');
});
it('should populate differences with DB values when file is missing', async () => {
const dbPost = {
id: 'post-1',
projectId: 'test-project',
title: 'Published Post',
slug: 'published-post',
status: 'published',
filePath: '/mock/userData/posts/2024/01/non-existent.md',
tags: '["tag1", "tag2"]',
categories: '["cat1"]',
excerpt: 'Some excerpt',
author: 'Author Name',
language: 'de',
createdAt: new Date('2024-01-15'),
updatedAt: new Date('2024-01-15'),
publishedAt: new Date('2024-01-15'),
};
// File intentionally NOT added to mockFileData → readPostFile returns null
mockPosts.set('post-1', dbPost);
const result = await engine.comparePostMetadata('post-1');
expect(result).not.toBeNull();
expect(result?.hasDifferences).toBe(true);
expect(result?.fileMissing).toBe(true);
// DB values should appear in differences so the UI shows what fields exist
expect(result?.differences.tags).toEqual({ dbValue: ['tag1', 'tag2'], fileValue: null });
expect(result?.differences.categories).toEqual({ dbValue: ['cat1'], fileValue: null });
expect(result?.differences.title).toEqual({ dbValue: 'Published Post', fileValue: null });
expect(result?.differences.excerpt).toEqual({ dbValue: 'Some excerpt', fileValue: null });
expect(result?.differences.author).toEqual({ dbValue: 'Author Name', fileValue: null });
expect(result?.differences.language).toEqual({ dbValue: 'de', fileValue: null });
});
it('should omit empty DB fields from differences when file is missing', async () => {
const dbPost = {
id: 'post-2',
projectId: 'test-project',
title: 'Minimal Post',
slug: 'minimal-post',
status: 'published',
filePath: '/mock/userData/posts/2024/01/gone.md',
tags: '[]',
categories: '[]',
excerpt: null,
author: null,
language: null,
createdAt: new Date('2024-01-15'),
updatedAt: new Date('2024-01-15'),
};
mockPosts.set('post-2', dbPost);
const result = await engine.comparePostMetadata('post-2');
expect(result).not.toBeNull();
expect(result?.hasDifferences).toBe(true);
expect(result?.fileMissing).toBe(true);
// Title always present since it's non-null
expect(result?.differences.title).toEqual({ dbValue: 'Minimal Post', fileValue: null });
// Empty arrays / nulls should be omitted
expect(result?.differences.tags).toBeUndefined();
expect(result?.differences.categories).toBeUndefined();
expect(result?.differences.excerpt).toBeUndefined();
expect(result?.differences.author).toBeUndefined();
expect(result?.differences.language).toBeUndefined();
});
it('should return hasDifferences=false when metadata matches', async () => {
const dbPost = {
id: 'post-1',
@@ -448,6 +524,14 @@ Content here`);
],
});
// Mock the second query that gets ALL post file paths (for orphan detection)
mockLocalClient.execute.mockResolvedValueOnce({
rows: [
{ file_path: '/mock/userData/posts/2024/01/post-1.md' },
{ file_path: '/mock/userData/posts/2024/01/post-2.md' },
],
});
// Queue the posts for sequential .get() calls in comparePostMetadata
mockPostsGetQueue = [
{
@@ -512,6 +596,187 @@ Content`);
expect(result.postsWithDifferences).toBe(1);
expect(result.differences.length).toBe(1);
expect(result.differences[0].postId).toBe('post-1');
expect(result.orphanFiles).toEqual([]);
});
it('should detect orphan files when postsBaseDir is provided', async () => {
const { readdir } = await import('fs/promises');
const mockReaddir = vi.mocked(readdir);
// Mock published posts query - one post in DB
mockLocalClient.execute.mockResolvedValueOnce({
rows: [
{
id: 'post-1',
title: 'Post 1',
slug: 'post-1',
file_path: '/mock/posts/2024/01/post-1.md',
tags: '[]',
categories: '[]',
excerpt: null,
author: null,
},
],
});
// Mock the all-posts query
mockLocalClient.execute.mockResolvedValueOnce({
rows: [{ file_path: '/mock/posts/2024/01/post-1.md' }],
});
// Queue the post for comparePostMetadata
mockPostsGetQueue = [
{
id: 'post-1',
projectId: 'test-project',
title: 'Post 1',
slug: 'post-1',
status: 'published',
filePath: '/mock/posts/2024/01/post-1.md',
tags: '[]',
categories: '[]',
createdAt: new Date('2024-01-15'),
updatedAt: new Date('2024-01-15'),
},
];
// File matches DB (no differences)
mockFileData.set('/mock/posts/2024/01/post-1.md', `---
id: post-1
title: "Post 1"
slug: post-1
status: published
tags: []
categories: []
createdAt: 2024-01-15T00:00:00.000Z
updatedAt: 2024-01-15T00:00:00.000Z
---
Content`);
// Orphan file exists on disk
mockFileData.set('/mock/posts/2024/01/orphan-post.md', `---
id: orphan-1
title: "Orphan Post"
slug: orphan-post
status: published
tags: []
categories: []
createdAt: 2024-01-15T00:00:00.000Z
updatedAt: 2024-01-15T00:00:00.000Z
---
Content`);
// Mock readdir to return directory structure
mockReaddir
.mockResolvedValueOnce([
{ name: '2024', isDirectory: () => true, isFile: () => false } as any,
] as any)
.mockResolvedValueOnce([
{ name: '01', isDirectory: () => true, isFile: () => false } as any,
] as any)
.mockResolvedValueOnce([
{ name: 'post-1.md', isDirectory: () => false, isFile: () => true } as any,
{ name: 'orphan-post.md', isDirectory: () => false, isFile: () => true } as any,
] as any);
const result = await engine.scanAllPublishedPosts(
(current, total) => {},
'/mock/posts',
);
expect(result.orphanFiles).toHaveLength(1);
expect(result.orphanFiles[0].slug).toBe('orphan-post');
expect(result.orphanFiles[0].title).toBe('Orphan Post');
expect(result.orphanFiles[0].id).toBe('orphan-1');
expect(result.orphanFiles[0].filePath).toBe('/mock/posts/2024/01/orphan-post.md');
});
it('should return empty orphanFiles when no postsBaseDir is provided', async () => {
// Mock published posts query - empty
mockLocalClient.execute.mockResolvedValueOnce({ rows: [] });
// Mock the all-posts query
mockLocalClient.execute.mockResolvedValueOnce({ rows: [] });
const result = await engine.scanAllPublishedPosts((current, total) => {});
expect(result.orphanFiles).toEqual([]);
});
it('should not flag draft posts as orphans', async () => {
const { readdir } = await import('fs/promises');
const mockReaddir = vi.mocked(readdir);
// No published posts
mockLocalClient.execute.mockResolvedValueOnce({ rows: [] });
// All posts query returns a draft that has a file
mockLocalClient.execute.mockResolvedValueOnce({
rows: [{ file_path: '/mock/posts/2024/01/draft-post.md' }],
});
// Mock readdir to find the draft file
mockReaddir
.mockResolvedValueOnce([
{ name: '2024', isDirectory: () => true, isFile: () => false } as any,
] as any)
.mockResolvedValueOnce([
{ name: '01', isDirectory: () => true, isFile: () => false } as any,
] as any)
.mockResolvedValueOnce([
{ name: 'draft-post.md', isDirectory: () => false, isFile: () => true } as any,
] as any);
const result = await engine.scanAllPublishedPosts(
(current, total) => {},
'/mock/posts',
);
// Draft post file should NOT be flagged as orphan since it's in the DB
expect(result.orphanFiles).toEqual([]);
});
});
describe('importOrphanFiles', () => {
it('should import orphan files and report success/failed counts', async () => {
mockImportOrphanFile
.mockResolvedValueOnce({ id: 'id-1', title: 'First' })
.mockResolvedValueOnce(null) // parse failure
.mockResolvedValueOnce({ id: 'id-3', title: 'Third' });
const result = await engine.importOrphanFiles([
'/posts/2024/01/first.md',
'/posts/2024/01/bad.md',
'/posts/2024/01/third.md',
]);
expect(result.success).toBe(2);
expect(result.failed).toBe(1);
expect(mockImportOrphanFile).toHaveBeenCalledTimes(3);
});
it('should handle exceptions from importOrphanFile gracefully', async () => {
mockImportOrphanFile
.mockRejectedValueOnce(new Error('DB constraint error'));
const result = await engine.importOrphanFiles(['/posts/2024/01/crash.md']);
expect(result.success).toBe(0);
expect(result.failed).toBe(1);
});
it('should report progress during import', async () => {
mockImportOrphanFile.mockResolvedValue({ id: 'id', title: 'Post' });
const progressCalls: [number, number, string][] = [];
await engine.importOrphanFiles(
['/a.md', '/b.md', '/c.md', '/d.md', '/e.md'],
(current, total, message) => progressCalls.push([current, total, message]),
);
// Progress should be reported at i=4 (5th item, i+1=5 is divisible by 5)
expect(progressCalls.length).toBe(1);
expect(progressCalls[0][0]).toBe(5);
expect(progressCalls[0][1]).toBe(5);
});
});
@@ -755,4 +1020,367 @@ Content here`);
expect(mockLocalDb.update).toHaveBeenCalledTimes(1);
});
});
// ── Media diff tests ──
describe('compareMediaMetadata', () => {
let mediaEngine: MetadataDiffEngine;
const mockReadSidecarFile = vi.fn();
beforeEach(() => {
mediaEngine = new MetadataDiffEngine(
undefined,
{ readSidecarFile: mockReadSidecarFile, getMedia: vi.fn(), updateMedia: vi.fn() } as any,
);
mediaEngine.setProjectContext('test-project');
});
it('should return null when media not found in DB', async () => {
// (mock DB returns undefined for .get())
const result = await mediaEngine.compareMediaMetadata('nonexistent');
expect(result).toBeNull();
});
it('should detect title difference between DB and sidecar', async () => {
const dbMedia = {
id: 'media-1',
projectId: 'test-project',
originalName: 'photo.jpg',
filePath: '/mock/media/photo.jpg',
title: 'DB Title',
alt: 'alt text',
caption: '',
author: '',
tags: '[]',
};
mockPosts.set('media-1', dbMedia);
// The select chain's .get() will return this via mockPosts
// Override: media uses same mock DB, so route DB response:
mockPostsGetQueue = [dbMedia];
mockReadSidecarFile.mockResolvedValueOnce({
id: 'media-1',
originalName: 'photo.jpg',
title: 'File Title',
alt: 'alt text',
caption: '',
author: '',
tags: [],
});
const result = await mediaEngine.compareMediaMetadata('media-1');
expect(result).not.toBeNull();
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.title).toEqual({ dbValue: 'DB Title', fileValue: 'File Title' });
});
it('should detect tag differences between DB and sidecar', async () => {
const dbMedia = {
id: 'media-2',
projectId: 'test-project',
originalName: 'photo.jpg',
filePath: '/mock/media/photo.jpg',
title: '',
alt: '',
caption: '',
author: '',
tags: '["tag1","tag2"]',
};
mockPostsGetQueue = [dbMedia];
mockReadSidecarFile.mockResolvedValueOnce({
id: 'media-2',
originalName: 'photo.jpg',
title: '',
alt: '',
caption: '',
author: '',
tags: ['tag1'],
});
const result = await mediaEngine.compareMediaMetadata('media-2');
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.tags).toEqual({ dbValue: ['tag1', 'tag2'], fileValue: ['tag1'] });
});
it('should return hasDifferences=false when metadata matches', async () => {
const dbMedia = {
id: 'media-3',
projectId: 'test-project',
originalName: 'photo.jpg',
filePath: '/mock/media/photo.jpg',
title: 'Same',
alt: 'Same alt',
caption: '',
author: '',
tags: '["t1"]',
};
mockPostsGetQueue = [dbMedia];
mockReadSidecarFile.mockResolvedValueOnce({
title: 'Same',
alt: 'Same alt',
caption: '',
author: '',
tags: ['t1'],
});
const result = await mediaEngine.compareMediaMetadata('media-3');
expect(result?.hasDifferences).toBe(false);
});
it('should flag when sidecar file is missing', async () => {
const dbMedia = {
id: 'media-4',
projectId: 'test-project',
originalName: 'photo.jpg',
filePath: '/mock/media/photo.jpg',
title: '',
alt: '',
caption: '',
author: '',
tags: '[]',
};
mockPostsGetQueue = [dbMedia];
mockReadSidecarFile.mockResolvedValueOnce(null);
const result = await mediaEngine.compareMediaMetadata('media-4');
expect(result?.hasDifferences).toBe(true);
});
});
// ── Script diff tests ──
describe('compareScriptMetadata', () => {
let scriptEngine: MetadataDiffEngine;
const mockReadScriptFileWithMetadata = vi.fn();
beforeEach(() => {
scriptEngine = new MetadataDiffEngine(
undefined,
undefined,
{ readScriptFileWithMetadata: mockReadScriptFileWithMetadata, getScript: vi.fn(), updateScript: vi.fn() } as any,
);
scriptEngine.setProjectContext('test-project');
});
it('should skip draft scripts', async () => {
mockPostsGetQueue = [{
id: 'script-1',
projectId: 'test-project',
title: 'Draft Script',
slug: 'draft-script',
status: 'draft',
kind: 'utility',
entrypoint: 'render',
enabled: true,
version: 1,
filePath: '/mock/scripts/draft.py',
}];
const result = await scriptEngine.compareScriptMetadata('script-1');
expect(result).toBeNull();
});
it('should detect title difference between DB and file', async () => {
mockPostsGetQueue = [{
id: 'script-2',
projectId: 'test-project',
title: 'DB Title',
slug: 'my-script',
status: 'published',
kind: 'macro',
entrypoint: 'render',
enabled: true,
version: 3,
filePath: '/mock/scripts/my-script.py',
}];
mockReadScriptFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'File Title', kind: 'macro', entrypoint: 'render', enabled: true, version: 3 },
body: '',
});
const result = await scriptEngine.compareScriptMetadata('script-2');
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.title).toEqual({ dbValue: 'DB Title', fileValue: 'File Title' });
});
it('should detect version difference', async () => {
mockPostsGetQueue = [{
id: 'script-3',
projectId: 'test-project',
title: 'Script',
slug: 'script',
status: 'published',
kind: 'utility',
entrypoint: 'render',
enabled: true,
version: 5,
filePath: '/mock/scripts/script.py',
}];
mockReadScriptFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'Script', kind: 'utility', entrypoint: 'render', enabled: true, version: 3 },
body: '',
});
const result = await scriptEngine.compareScriptMetadata('script-3');
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.version).toEqual({ dbValue: 5, fileValue: 3 });
});
it('should return hasDifferences=false when metadata matches', async () => {
mockPostsGetQueue = [{
id: 'script-4',
projectId: 'test-project',
title: 'Same',
slug: 'same',
status: 'published',
kind: 'utility',
entrypoint: 'render',
enabled: true,
version: 1,
filePath: '/mock/scripts/same.py',
}];
mockReadScriptFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'Same', kind: 'utility', entrypoint: 'render', enabled: true, version: 1 },
body: '',
});
const result = await scriptEngine.compareScriptMetadata('script-4');
expect(result?.hasDifferences).toBe(false);
});
});
// ── Template diff tests ──
describe('compareTemplateMetadata', () => {
let templateEngine: MetadataDiffEngine;
const mockReadTemplateFileWithMetadata = vi.fn();
beforeEach(() => {
templateEngine = new MetadataDiffEngine(
undefined,
undefined,
undefined,
{ readTemplateFileWithMetadata: mockReadTemplateFileWithMetadata, getTemplate: vi.fn(), updateTemplate: vi.fn() } as any,
);
templateEngine.setProjectContext('test-project');
});
it('should skip draft templates', async () => {
mockPostsGetQueue = [{
id: 'tpl-1',
projectId: 'test-project',
title: 'Draft Template',
slug: 'draft-tpl',
status: 'draft',
kind: 'post',
enabled: true,
version: 1,
filePath: '/mock/templates/draft.liquid',
}];
const result = await templateEngine.compareTemplateMetadata('tpl-1');
expect(result).toBeNull();
});
it('should detect kind difference between DB and file', async () => {
mockPostsGetQueue = [{
id: 'tpl-2',
projectId: 'test-project',
title: 'Template',
slug: 'template',
status: 'published',
kind: 'post',
enabled: true,
version: 1,
filePath: '/mock/templates/template.liquid',
}];
mockReadTemplateFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'Template', kind: 'list', enabled: true, version: 1 },
body: '',
});
const result = await templateEngine.compareTemplateMetadata('tpl-2');
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.kind).toEqual({ dbValue: 'post', fileValue: 'list' });
});
it('should detect enabled difference', async () => {
mockPostsGetQueue = [{
id: 'tpl-3',
projectId: 'test-project',
title: 'Tpl',
slug: 'tpl',
status: 'published',
kind: 'post',
enabled: true,
version: 1,
filePath: '/mock/templates/tpl.liquid',
}];
mockReadTemplateFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'Tpl', kind: 'post', enabled: false, version: 1 },
body: '',
});
const result = await templateEngine.compareTemplateMetadata('tpl-3');
expect(result?.hasDifferences).toBe(true);
expect(result?.differences.enabled).toEqual({ dbValue: true, fileValue: false });
});
it('should return hasDifferences=false when metadata matches', async () => {
mockPostsGetQueue = [{
id: 'tpl-4',
projectId: 'test-project',
title: 'Same',
slug: 'same',
status: 'published',
kind: 'partial',
enabled: true,
version: 2,
filePath: '/mock/templates/same.liquid',
}];
mockReadTemplateFileWithMetadata.mockResolvedValueOnce({
metadata: { title: 'Same', kind: 'partial', enabled: true, version: 2 },
body: '',
});
const result = await templateEngine.compareTemplateMetadata('tpl-4');
expect(result?.hasDifferences).toBe(false);
});
});
// ── getTableStats with expanded counts ──
describe('getTableStats (expanded)', () => {
it('should include script and template counts', async () => {
mockLocalClient.execute
.mockResolvedValueOnce({ rows: [{ count: 10 }] }) // total posts
.mockResolvedValueOnce({ rows: [{ count: 8 }] }) // published posts
.mockResolvedValueOnce({ rows: [{ count: 2 }] }) // draft posts
.mockResolvedValueOnce({ rows: [{ count: 50 }] }) // total media
.mockResolvedValueOnce({ rows: [{ count: 5 }] }) // total scripts
.mockResolvedValueOnce({ rows: [{ count: 4 }] }) // published scripts
.mockResolvedValueOnce({ rows: [{ count: 7 }] }) // total templates
.mockResolvedValueOnce({ rows: [{ count: 6 }] }); // published templates
const stats = await engine.getTableStats();
expect(stats).toEqual({
totalPosts: 10,
publishedPosts: 8,
draftPosts: 2,
totalMedia: 50,
totalScripts: 5,
publishedScripts: 4,
totalTemplates: 7,
publishedTemplates: 6,
});
});
});
});

View File

@@ -3507,4 +3507,180 @@ Content with [link](/posts/other-post)`);
expect(post!.language).toBe('it');
});
});
describe('syncPublishedPostFile', () => {
it('should recreate the file when it is missing', async () => {
const filePath = '/mock/userData/projects/default/posts/2024/01/my-post.md';
// Mock: DB returns a published post, but the file does NOT exist on disk
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: 'post-missing-file',
projectId: 'default',
title: 'My Post',
slug: 'my-post',
content: 'Body from database',
status: 'published',
filePath,
tags: '["tag1"]',
categories: '["cat1"]',
createdAt: new Date('2024-01-15T10:00:00.000Z'),
updatedAt: new Date('2024-01-20T10:00:00.000Z'),
}),
});
return chain;
});
// File does NOT exist (not in mockFiles)
const result = await postEngine.syncPublishedPostFile('post-missing-file');
expect(result).toBe(true);
// Verify the file was recreated via writeFile
expect(fs.writeFile).toHaveBeenCalled();
// Check the file content was written with DB body
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
const writtenContent = writeCall[1] as string;
expect(writtenContent).toContain('Body from database');
expect(writtenContent).toContain('title: My Post');
expect(writtenContent).toContain('tag1');
});
it('should update DB filePath when slug changed causes different path', async () => {
const oldFilePath = '/mock/userData/projects/default/posts/2024/01/old-slug.md';
// Mock: DB post has the new slug but old filePath
const mockUpdate = vi.fn(() => ({
set: vi.fn(() => ({
where: vi.fn(() => Promise.resolve()),
})),
}));
vi.mocked(mockLocalDb.update).mockImplementation(mockUpdate as any);
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue({
id: 'post-slug-changed',
projectId: 'default',
title: 'New Title',
slug: 'new-slug',
content: 'Some content',
status: 'published',
filePath: oldFilePath,
tags: '[]',
categories: '[]',
createdAt: new Date('2024-01-15T10:00:00.000Z'),
updatedAt: new Date('2024-01-20T10:00:00.000Z'),
}),
});
return chain;
});
// Old file does not exist (slug changed)
const result = await postEngine.syncPublishedPostFile('post-slug-changed');
expect(result).toBe(true);
// writePostFile writes to new-slug.md, which differs from oldFilePath
const writeCall = vi.mocked(fs.writeFile).mock.calls[0];
const writtenPath = writeCall[0] as string;
expect(writtenPath).toContain('new-slug.md');
expect(writtenPath).not.toContain('old-slug.md');
// DB filePath should be updated
expect(mockUpdate).toHaveBeenCalled();
});
});
describe('importOrphanFile', () => {
it('should import an orphan file into the database as published', async () => {
const orphanPath = '/mock/userData/posts/2024/03/orphan-post.md';
mockFiles.set(orphanPath, `---
id: orphan-id-123
title: "Orphan Post Title"
slug: orphan-post
createdAt: "2024-03-10T12:00:00.000Z"
updatedAt: "2024-03-10T12:00:00.000Z"
tags:
- imported
categories:
- blog
---
This is the orphan body content.`);
// select → get returns undefined (no existing post with that id/slug)
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockResolvedValue(undefined),
});
return chain;
});
const result = await postEngine.importOrphanFile(orphanPath);
expect(result).not.toBeNull();
expect(result!.title).toBe('Orphan Post Title');
expect(result!.slug).toBe('orphan-post');
expect(result!.status).toBe('published');
expect(result!.tags).toEqual(['imported']);
expect(result!.categories).toEqual(['blog']);
// Should have inserted into DB
expect(mockLocalDb.insert).toHaveBeenCalled();
});
it('should return null when the file cannot be parsed', async () => {
// File does not exist at all
const result = await postEngine.importOrphanFile('/nonexistent/path.md');
expect(result).toBeNull();
});
it('should deduplicate slug when it already exists', async () => {
const orphanPath = '/mock/userData/posts/2024/01/existing-slug.md';
mockFiles.set(orphanPath, `---
title: "Duplicate Slug Post"
slug: existing-slug
createdAt: "2024-01-01T00:00:00.000Z"
updatedAt: "2024-01-01T00:00:00.000Z"
tags: []
categories: []
---
Body.`);
// ensureUniquePostIdentity flow:
// 1. select → get: id check → undefined (available)
// 2. isSlugAvailable('existing-slug') → found (taken)
// 3. generateUniqueSlug → isSlugAvailable('existing-slug') again → found (taken)
// 4. isSlugAvailable('existing-slug-2') → undefined (available)
let selectCallCount = 0;
vi.mocked(mockLocalDb.select).mockImplementation(() => {
const chain = createSelectChain();
chain.where = vi.fn().mockReturnValue({
...chain,
get: vi.fn().mockImplementation(() => {
selectCallCount++;
// id check: available
if (selectCallCount === 1) return Promise.resolve(undefined);
// slug check: taken (both the direct check and generateUniqueSlug re-check)
if (selectCallCount <= 3) return Promise.resolve({ id: 'other-post' });
// slug-2 check: available
return Promise.resolve(undefined);
}),
});
return chain;
});
const result = await postEngine.importOrphanFile(orphanPath);
expect(result).not.toBeNull();
// Should have been deduplicated
expect(result!.slug).toBe('existing-slug-2');
});
});
});