feat(admin): rebuild admin area around the Article domain
The admin area was managing a Hacker News-style Item model that the
public site never consumed, so the homepage always fell back to sample
data. Rebuild the admin area around the real public content model.
Backend:
- Remove the Item model, schema and admin API
- Admin API now manages articles at /api/v1/admin/articles (CRUD) with
/api/v1/admin/authors and /api/v1/admin/categories reference data
- Add public read-only article endpoints (/api/v1/articles,
/api/v1/articles/{id}): no auth, published-only, newest-first
- Rewrite Article schemas; ArticleRead embeds author and category
- Add idempotent seed_content.py dev seeder (3 authors, 4 categories,
6 published articles)
Frontend:
- Admin UI rewritten around articles: AdminDashboard list, new
EditArticlePage (create/edit, author/category selects, published
toggle, Lexical editor, debounced auto-save)
- Replace AdminItem types and api client with the article domain
- Update App routes, AdminLayout sidebar, DeleteConfirmation, Lexical
editor labels and Home/client comments to article terminology
Tests:
- Rewrite admin unit tests and the Playwright admin e2e spec for the
article domain; add test_articles.py for the public endpoints and
seeder
This commit is contained in:
29 files changed
+2465
-1658
No files matched your search
@@ -6,33 +6,49 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
adminDeleteItem,
|
||||
adminListItems,
|
||||
adminDeleteArticle,
|
||||
adminListArticles,
|
||||
} from '../../api/admin';
|
||||
import { AdminDashboard } from '../../pages/AdminDashboard';
|
||||
import type { AdminItemsPage } from '../../types/admin';
|
||||
import type { AdminArticlesPage } from '../../types/admin';
|
||||
|
||||
vi.mock('../../api/admin', () => ({
|
||||
adminListItems: vi.fn(),
|
||||
adminGetItem: vi.fn(),
|
||||
adminCreateItem: vi.fn(),
|
||||
adminUpdateItem: vi.fn(),
|
||||
adminDeleteItem: vi.fn(),
|
||||
adminListArticles: vi.fn(),
|
||||
adminGetArticle: vi.fn(),
|
||||
adminCreateArticle: vi.fn(),
|
||||
adminUpdateArticle: vi.fn(),
|
||||
adminDeleteArticle: vi.fn(),
|
||||
adminListAuthors: vi.fn(),
|
||||
adminListCategories: vi.fn(),
|
||||
}));
|
||||
|
||||
const items = (count: number, offset = 0) =>
|
||||
Array.from({ length: count }, (_, i) => ({
|
||||
id: i + 1 + offset,
|
||||
title: `Story ${i + 1 + offset}`,
|
||||
url: `https://example.com/story-${i + 1 + offset}`,
|
||||
points: i + offset,
|
||||
username: 'alice',
|
||||
content: `<p>Body ${i + 1 + offset}</p>`,
|
||||
created_at: `2024-05-0${(i % 9) + 1}T12:00:00Z`,
|
||||
}));
|
||||
const AUTHORS = [
|
||||
{ id: 1, name: 'Ada Lovelace', email: 'ada@example.com' },
|
||||
{ id: 2, name: 'Grace Hopper', email: 'grace@example.com' },
|
||||
];
|
||||
const CATEGORIES = [
|
||||
{ id: 1, name: 'Technology', slug: 'technology' },
|
||||
{ id: 2, name: 'Science', slug: 'science' },
|
||||
];
|
||||
|
||||
const page = (overrides: Partial<AdminItemsPage> = {}): AdminItemsPage => ({
|
||||
items: items(3),
|
||||
const article = (id: number, overrides: Record<string, unknown> = {}) => ({
|
||||
id,
|
||||
title: `Story ${id}`,
|
||||
content: `<p>Body ${id}</p>`,
|
||||
author_id: AUTHORS[(id - 1) % AUTHORS.length].id,
|
||||
category_id: CATEGORIES[(id - 1) % CATEGORIES.length].id,
|
||||
author: AUTHORS[(id - 1) % AUTHORS.length],
|
||||
category: CATEGORIES[(id - 1) % CATEGORIES.length],
|
||||
published: true,
|
||||
created_at: `2024-05-0${(id % 9) + 1}T12:00:00Z`,
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const articles = (count: number, offset = 0) =>
|
||||
Array.from({ length: count }, (_, i) => article(i + 1 + offset));
|
||||
|
||||
const page = (overrides: Partial<AdminArticlesPage> = {}): AdminArticlesPage => ({
|
||||
articles: articles(3),
|
||||
total: 3,
|
||||
page: 1,
|
||||
page_size: 20,
|
||||
@@ -47,7 +63,7 @@ function lastCallParams(): {
|
||||
sortBy: string;
|
||||
sortOrder: string;
|
||||
} {
|
||||
const calls = vi.mocked(adminListItems).mock.calls;
|
||||
const calls = vi.mocked(adminListArticles).mock.calls;
|
||||
const last = calls[calls.length - 1][0];
|
||||
return {
|
||||
page: last.page,
|
||||
@@ -71,23 +87,27 @@ function renderDashboard() {
|
||||
describe('AdminDashboard', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(adminListItems).mockResolvedValue(page());
|
||||
vi.mocked(adminDeleteItem).mockResolvedValue(undefined);
|
||||
vi.mocked(adminListArticles).mockResolvedValue(page());
|
||||
vi.mocked(adminDeleteArticle).mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
it('renders the fetched items with their details', async () => {
|
||||
it('renders the fetched articles with their details', async () => {
|
||||
renderDashboard();
|
||||
|
||||
expect(await screen.findByText('Story 1')).toBeInTheDocument();
|
||||
expect(screen.getByText('Story 2')).toBeInTheDocument();
|
||||
expect(screen.getByText('Story 3')).toBeInTheDocument();
|
||||
// All three seed items belong to "alice".
|
||||
expect(screen.getAllByText('alice').length).toBe(3);
|
||||
expect(screen.getByText('https://example.com/story-1')).toBeInTheDocument();
|
||||
// Each row shows its author and category.
|
||||
expect(screen.getAllByText('Ada Lovelace').length).toBeGreaterThan(0);
|
||||
expect(screen.getAllByText('Grace Hopper').length).toBeGreaterThan(0);
|
||||
expect(screen.getAllByText('Technology').length).toBeGreaterThan(0);
|
||||
expect(screen.getByText('Showing 1–3 of 3')).toBeInTheDocument();
|
||||
// Published badges: one per row (the "Published" column header is a
|
||||
// columnheader role, not a cell).
|
||||
expect(screen.getAllByRole('cell', { name: 'Published' })).toHaveLength(3);
|
||||
// Edit links point at the edit route.
|
||||
const editLinks = screen.getAllByRole('link', { name: /edit/i });
|
||||
expect(editLinks[0].getAttribute('href')).toBe('/admin/items/1/edit');
|
||||
expect(editLinks[0].getAttribute('href')).toBe('/admin/articles/1/edit');
|
||||
});
|
||||
|
||||
it('requests the initial page with the default parameters', async () => {
|
||||
@@ -106,7 +126,7 @@ describe('AdminDashboard', () => {
|
||||
renderDashboard();
|
||||
await screen.findByText('Story 1');
|
||||
|
||||
fireEvent.change(screen.getByLabelText('Search items'), {
|
||||
fireEvent.change(screen.getByLabelText('Search articles'), {
|
||||
target: { value: 'breaking' },
|
||||
});
|
||||
|
||||
@@ -134,13 +154,13 @@ describe('AdminDashboard', () => {
|
||||
expect(lastCallParams().sortOrder).toBe('desc');
|
||||
|
||||
// Switching columns starts on ascending again.
|
||||
fireEvent.click(screen.getByRole('button', { name: /sort by points/i }));
|
||||
expect(lastCallParams().sortBy).toBe('points');
|
||||
fireEvent.click(screen.getByRole('button', { name: /sort by creation date/i }));
|
||||
expect(lastCallParams().sortBy).toBe('created_at');
|
||||
expect(lastCallParams().sortOrder).toBe('asc');
|
||||
});
|
||||
|
||||
it('navigates pages and changes the page size', async () => {
|
||||
vi.mocked(adminListItems).mockResolvedValue(
|
||||
vi.mocked(adminListArticles).mockResolvedValue(
|
||||
page({ total: 45, page: 2, total_pages: 3 }),
|
||||
);
|
||||
renderDashboard();
|
||||
@@ -160,23 +180,23 @@ describe('AdminDashboard', () => {
|
||||
});
|
||||
|
||||
it('shows an empty state when nothing matches the search', async () => {
|
||||
vi.mocked(adminListItems).mockResolvedValue(page({ items: [], total: 0 }));
|
||||
vi.mocked(adminListArticles).mockResolvedValue(page({ articles: [], total: 0 }));
|
||||
renderDashboard();
|
||||
await screen.findByText(/no items yet/i);
|
||||
await screen.findByText(/no articles yet/i);
|
||||
});
|
||||
|
||||
it('shows the error state with a retry action', async () => {
|
||||
vi.mocked(adminListItems).mockRejectedValue(new Error('boom'));
|
||||
vi.mocked(adminListArticles).mockRejectedValue(new Error('boom'));
|
||||
renderDashboard();
|
||||
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent(/boom|went wrong/i);
|
||||
screen.getByRole('button', { name: /retry/i }).click();
|
||||
await waitFor(() =>
|
||||
expect(vi.mocked(adminListItems).mock.calls.length).toBeGreaterThanOrEqual(2),
|
||||
expect(vi.mocked(adminListArticles).mock.calls.length).toBeGreaterThanOrEqual(2),
|
||||
);
|
||||
});
|
||||
|
||||
it('deletes an item only after explicit confirmation', async () => {
|
||||
it('deletes an article only after explicit confirmation', async () => {
|
||||
renderDashboard();
|
||||
await screen.findByText('Story 1');
|
||||
|
||||
@@ -184,30 +204,30 @@ describe('AdminDashboard', () => {
|
||||
fireEvent.click(screen.getAllByRole('button', { name: /delete/i })[0]);
|
||||
const dialog = await screen.findByRole('dialog');
|
||||
expect(dialog).toHaveTextContent('Story 1');
|
||||
expect(vi.mocked(adminDeleteItem)).not.toHaveBeenCalled();
|
||||
expect(vi.mocked(adminDeleteArticle)).not.toHaveBeenCalled();
|
||||
|
||||
// Cancel: no API call.
|
||||
fireEvent.click(screen.getByRole('button', { name: /cancel/i }));
|
||||
expect(await screen.findByText('Story 1')).toBeInTheDocument();
|
||||
expect(vi.mocked(adminDeleteItem)).not.toHaveBeenCalled();
|
||||
expect(vi.mocked(adminDeleteArticle)).not.toHaveBeenCalled();
|
||||
|
||||
// Confirm: the API is called and the list refreshes.
|
||||
const listCallsBefore = vi.mocked(adminListItems).mock.calls.length;
|
||||
const listCallsBefore = vi.mocked(adminListArticles).mock.calls.length;
|
||||
fireEvent.click(screen.getAllByRole('button', { name: /delete/i })[0]);
|
||||
await screen.findByRole('dialog');
|
||||
fireEvent.click(screen.getByRole('button', { name: /yes, delete/i }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(vi.mocked(adminDeleteItem)).toHaveBeenCalledWith(1),
|
||||
expect(vi.mocked(adminDeleteArticle)).toHaveBeenCalledWith(1),
|
||||
);
|
||||
expect(
|
||||
vi.mocked(adminListItems).mock.calls.length,
|
||||
vi.mocked(adminListArticles).mock.calls.length,
|
||||
).toBeGreaterThanOrEqual(listCallsBefore + 1);
|
||||
});
|
||||
|
||||
it('keeps the dialog open and shows the error when the delete fails', async () => {
|
||||
vi.mocked(adminDeleteItem).mockRejectedValue(
|
||||
new Error('Deleting the item failed.') as Error & { status?: number },
|
||||
vi.mocked(adminDeleteArticle).mockRejectedValue(
|
||||
new Error('Deleting the article failed.') as Error & { status?: number },
|
||||
);
|
||||
renderDashboard();
|
||||
await screen.findByText('Story 1');
|
||||
@@ -217,7 +237,7 @@ describe('AdminDashboard', () => {
|
||||
fireEvent.click(screen.getByRole('button', { name: /yes, delete/i }));
|
||||
|
||||
const alert = await screen.findByRole('alert');
|
||||
expect(alert).toHaveTextContent(/deleting the item failed/i);
|
||||
expect(alert).toHaveTextContent(/deleting the article failed/i);
|
||||
// Dialog still open.
|
||||
expect(screen.getByRole('dialog')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user