feat: wiki like linkage for posts

This commit is contained in:
2026-02-27 16:36:45 +01:00
parent f9527b384b
commit bd10825e74
12 changed files with 390 additions and 18 deletions

View File

@@ -1,7 +1,8 @@
import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, act } from '@testing-library/react';
import { InsertModal } from '../../../src/renderer/components/InsertModal/InsertModal';
import { useAppStore } from '../../../src/renderer/store/appStore';
describe('InsertModal format hints', () => {
it('shows canonical post link format hint in internal link mode', () => {
@@ -30,3 +31,170 @@ describe('InsertModal format hints', () => {
expect(screen.getByText('Canonical: /media/YYYY/MM/file.ext')).toBeInTheDocument();
});
});
describe('InsertModal create post', () => {
const mockOnInsertLink = vi.fn();
const mockOnClose = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
useAppStore.setState({ tabs: [], activeTabId: 'current-post' });
});
it('does not show create option when query is shorter than 2 characters', () => {
render(
<InsertModal
mode="link"
onInsertLink={mockOnInsertLink}
onInsertImage={vi.fn()}
onClose={mockOnClose}
/>
);
const input = screen.getByPlaceholderText('Search posts by title or content...');
fireEvent.input(input, { target: { value: 'a' } });
expect(screen.queryByText(/Create post/)).not.toBeInTheDocument();
});
it('does not show create option in image mode', async () => {
(window.electronAPI.media.search as ReturnType<typeof vi.fn>).mockResolvedValue([]);
render(
<InsertModal
mode="image"
onInsertLink={vi.fn()}
onInsertImage={vi.fn()}
onClose={mockOnClose}
/>
);
const input = screen.getByPlaceholderText('Search media by name, title, or alt text...');
fireEvent.input(input, { target: { value: 'test query' } });
// Wait for search to complete by finding the no-results message
await screen.findByText(/No.*found/i);
expect(screen.queryByText(/Create post/)).not.toBeInTheDocument();
});
it('shows create option when search has no exact title match', async () => {
(window.electronAPI.posts.search as ReturnType<typeof vi.fn>).mockResolvedValue([
{ id: 'p1', title: 'Different Title', slug: 'different-title', excerpt: 'Some text' },
]);
render(
<InsertModal
mode="link"
onInsertLink={mockOnInsertLink}
onInsertImage={vi.fn()}
onClose={mockOnClose}
/>
);
const input = screen.getByPlaceholderText('Search posts by title or content...');
fireEvent.input(input, { target: { value: 'My New Post' } });
// Wait for search results to render
await screen.findByText('Different Title');
expect(screen.getByText('Create post "My New Post"')).toBeInTheDocument();
});
it('does not show create option when an exact title match exists', async () => {
(window.electronAPI.posts.search as ReturnType<typeof vi.fn>).mockResolvedValue([
{ id: 'p1', title: 'My New Post', slug: 'my-new-post', excerpt: '' },
]);
render(
<InsertModal
mode="link"
onInsertLink={mockOnInsertLink}
onInsertImage={vi.fn()}
onClose={mockOnClose}
/>
);
const input = screen.getByPlaceholderText('Search posts by title or content...');
fireEvent.input(input, { target: { value: 'My New Post' } });
// Wait for results to render (slug appears in the result path)
await screen.findByText(/my-new-post/);
expect(screen.queryByText('Create post "My New Post"')).not.toBeInTheDocument();
});
it('creates post and inserts link when create option is clicked', async () => {
(window.electronAPI.posts.search as ReturnType<typeof vi.fn>).mockResolvedValue([]);
(window.electronAPI.posts.create as ReturnType<typeof vi.fn>).mockResolvedValue({
id: 'new-post-id',
title: 'New Post Title',
slug: 'new-post-title',
content: '',
status: 'draft',
tags: ['tag1'],
categories: ['article'],
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
});
render(
<InsertModal
mode="link"
onInsertLink={mockOnInsertLink}
onInsertImage={vi.fn()}
onClose={mockOnClose}
currentPostTags={['tag1']}
currentPostCategories={['article']}
/>
);
const input = screen.getByPlaceholderText('Search posts by title or content...');
fireEvent.input(input, { target: { value: 'New Post Title' } });
// Wait for the create option to appear after debounced search completes
const createButton = await screen.findByText('Create post "New Post Title"');
await act(async () => {
fireEvent.click(createButton);
});
expect(window.electronAPI.posts.create).toHaveBeenCalledWith({
title: 'New Post Title',
tags: ['tag1'],
categories: ['article'],
});
expect(mockOnInsertLink).toHaveBeenCalledWith('/posts/new-post-title', 'New Post Title');
expect(mockOnClose).toHaveBeenCalled();
// Check that the tab was opened in the background
const storeState = useAppStore.getState();
expect(storeState.tabs).toContainEqual(
expect.objectContaining({ type: 'post', id: 'new-post-id', isTransient: false })
);
expect(storeState.activeTabId).toBe('current-post');
});
it('shows create option when no results exist (standalone)', async () => {
(window.electronAPI.posts.search as ReturnType<typeof vi.fn>).mockResolvedValue([]);
render(
<InsertModal
mode="link"
onInsertLink={mockOnInsertLink}
onInsertImage={vi.fn()}
onClose={mockOnClose}
/>
);
const input = screen.getByPlaceholderText('Search posts by title or content...');
fireEvent.input(input, { target: { value: 'Nonexistent Post' } });
// Wait for create option to appear (replaces no-results message)
expect(await screen.findByText('Create post "Nonexistent Post"')).toBeInTheDocument();
// The "no results" message should not appear when create option is shown
expect(screen.queryByText(/No posts found/i)).not.toBeInTheDocument();
});
});

View File

@@ -215,4 +215,50 @@ describe('AppStore', () => {
expectTypeOf<TaskProgress>().toEqualTypeOf<SharedTaskProgress>();
});
});
describe('Tab Management', () => {
beforeEach(() => {
setState({
tabs: [],
activeTabId: null,
});
});
it('should open a tab in the background without changing activeTabId', () => {
// Set up an existing active tab
getStore().openTab({ type: 'post', id: 'existing-post', isTransient: false });
expect(getStore().activeTabId).toBe('existing-post');
// Open a new tab in the background
getStore().openTabInBackground({ type: 'post', id: 'background-post', isTransient: false });
expect(getStore().tabs).toHaveLength(2);
expect(getStore().tabs[1].id).toBe('background-post');
expect(getStore().activeTabId).toBe('existing-post');
});
it('should not duplicate a tab when opening in background if it already exists', () => {
getStore().openTab({ type: 'post', id: 'post-1', isTransient: false });
getStore().openTabInBackground({ type: 'post', id: 'post-1', isTransient: false });
expect(getStore().tabs).toHaveLength(1);
});
it('should pin an existing transient tab when opening in background as non-transient', () => {
getStore().openTab({ type: 'post', id: 'post-1', isTransient: true });
getStore().openTabInBackground({ type: 'post', id: 'post-1', isTransient: false });
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0].isTransient).toBe(false);
});
it('should preserve null activeTabId when opening background tab with no prior active tab', () => {
setState({ activeTabId: null, tabs: [] });
getStore().openTabInBackground({ type: 'post', id: 'bg-post', isTransient: false });
expect(getStore().tabs).toHaveLength(1);
expect(getStore().activeTabId).toBeNull();
});
});
});