fix: editor-preview looks at draft again

This commit is contained in:
2026-02-20 22:15:55 +01:00
parent 1543af6edc
commit 5a2a6c9edb
8 changed files with 104 additions and 15 deletions

View File

@@ -299,6 +299,47 @@ describe('PreviewServer', () => {
expect(diagonalHtml).toContain('data-orientation="mixed-diagonal"');
});
it('serves draft content for single post route when draft query flag and postId are provided', async () => {
const publishedPost = makePost({
id: 'post-1',
slug: 'shared-slug',
title: 'Published Title',
content: 'Published body',
status: 'published',
createdAt: new Date('2025-01-03T10:00:00.000Z'),
});
const draftPost = makePost({
id: 'post-2',
slug: 'shared-slug',
title: 'Draft Title',
content: 'Draft-only body',
status: 'draft',
createdAt: new Date('2025-01-03T10:00:00.000Z'),
});
server = new PreviewServer({
postEngine: makeEngine([publishedPost, draftPost]),
settingsEngine: makeSettings(50),
getActiveProjectContext: async () => ({ projectId: 'default' }),
});
await server.start(0);
const publishedResponse = await fetch(`${server.getBaseUrl()}/posts/shared-slug`);
expect(publishedResponse.status).toBe(200);
const publishedHtml = await publishedResponse.text();
expect(publishedHtml).toContain('Published Title');
expect(publishedHtml).toContain('Published body');
expect(publishedHtml).not.toContain('Draft-only body');
const draftResponse = await fetch(`${server.getBaseUrl()}/posts/shared-slug?draft=true&postId=post-2`);
expect(draftResponse.status).toBe(200);
const draftHtml = await draftResponse.text();
expect(draftHtml).toContain('Draft Title');
expect(draftHtml).toContain('Draft-only body');
expect(draftHtml).not.toContain('Published body');
});
it('uses selected pico theme stylesheet from project metadata', async () => {
server = new PreviewServer({
postEngine: makeEngine([makePost()]),

View File

@@ -765,6 +765,19 @@ describe('IPC Handlers', () => {
expect(result).toBeNull();
});
it('should return draft preview URL when draft option is enabled', async () => {
mockPostEngine.getPost.mockResolvedValue(createMockPost({
id: 'post-1',
slug: 'my-post',
createdAt: new Date('2026-02-16T12:00:00.000Z'),
}));
const result = await invokeHandler('posts:getPreviewUrl', 'post-1', { draft: true });
expect(mockPostEngine.getPost).toHaveBeenCalledWith('post-1');
expect(result).toBe('http://127.0.0.1:4123/2026/02/16/my-post?draft=true&postId=post-1');
});
});
describe('posts:getAll', () => {

View File

@@ -161,7 +161,7 @@ describe('Editor visual mode persistence', () => {
(window as any).electronAPI.posts.get = vi.fn().mockResolvedValue(createPost());
(window as any).electronAPI.posts.hasPublishedVersion = vi.fn().mockReturnValue(neverSettles);
(window as any).electronAPI.posts.update = vi.fn().mockResolvedValue(null);
(window as any).electronAPI.posts.getPreviewUrl = vi.fn().mockResolvedValue('http://127.0.0.1:4123/2026/02/16/test-post');
(window as any).electronAPI.posts.getPreviewUrl = vi.fn().mockResolvedValue('http://127.0.0.1:4123/2026/02/16/test-post?draft=true&postId=post-1');
(window as any).electronAPI.meta.getCategories = vi.fn().mockReturnValue(neverSettles);
useAppStore.setState({
@@ -202,7 +202,7 @@ describe('Editor visual mode persistence', () => {
});
});
it('uses canonical preview server URL in preview mode iframe', async () => {
it('uses editor preview HTML in preview mode iframe', async () => {
const { getByTitle, container } = render(<PostEditor postId="post-1" />);
await act(async () => {
@@ -216,11 +216,12 @@ describe('Editor visual mode persistence', () => {
await Promise.resolve();
});
expect((window as any).electronAPI.posts.getPreviewUrl).toHaveBeenCalledWith('post-1');
expect((window as any).electronAPI.posts.getPreviewUrl).toHaveBeenCalledWith('post-1', { draft: true });
const frame = container.querySelector('.editor-preview-frame') as HTMLIFrameElement | null;
expect(frame).not.toBeNull();
expect(frame?.getAttribute('src')).toBe('http://127.0.0.1:4123/2026/02/16/test-post');
expect(frame?.getAttribute('src')).toBe('http://127.0.0.1:4123/2026/02/16/test-post?draft=true&postId=post-1');
expect(frame?.getAttribute('srcdoc')).toBeNull();
expect(container.querySelector('.preview-content')).toBeNull();
});