feat(m6): board with threads/comments, markdown, tests, e2e

- BoardRepository (thread/comment CRUD, soft-delete, search, pagination,
  pinned-first ordering) + BoardService (membership permission,
  author/admin edit/delete, comment->board_commented notification,
  board_posted/comment_added activity logs, category validation)
- APIs: threads list/create/detail/edit/delete, comments create/edit/delete
- MarkdownBody (react-markdown + remark-gfm + rehype-sanitize),
  ThreadForm, CommentForm, board list + thread detail screens, ProjectNav link
- Unit tests (BoardRepository, BoardService) + e2e board.spec
This commit is contained in:
Ken Yasue
2026-06-25 01:28:08 +02:00
parent ac598a50f2
commit 2017585f84
16 changed files with 1448 additions and 1 deletions

80
tests/e2e/board.spec.ts Normal file
View File

@ -0,0 +1,80 @@
import { test, expect } from '@playwright/test';
function unique(prefix: string): string {
return `${prefix}-${Date.now()}-${Math.floor(Math.random() * 1_000_000)}`;
}
async function setupOwner(page: import('@playwright/test').Page) {
const email = unique('owner') + '@example.com';
await page.goto('/login');
await page.getByRole('button', { name: '新規登録はこちら' }).click();
await page.getByLabel('表示名').fill('Owner');
await page.getByLabel('メールアドレス').fill(email);
await page.getByLabel('パスワード').fill('password123');
await page.getByRole('button', { name: '登録する' }).click();
await expect(page).toHaveURL(/\/dashboard/);
await page.getByLabel('プロジェクト名').fill(unique('Proj'));
await page.getByRole('button', { name: '新規プロジェクト' }).click();
await expect(page).toHaveURL(/\/projects\/\d+$/);
return Number(page.url().match(/\/projects\/(\d+)/)![1]);
}
test.describe('board', () => {
test('create thread, view detail, comment, and search', async ({ page }) => {
const projectId = await setupOwner(page);
const title = unique('Thread');
// スレッド作成(API)
const res = await page.request.post(
`/api/projects/${projectId}/board/threads`,
{
data: {
title,
bodyMd: '# Hello\n\nthis is **markdown**',
category: 'notice',
},
}
);
expect(res.ok()).toBeTruthy();
const { thread } = (await res.json()) as { thread: { id: number } };
// 一覧に表示される
await page.goto(`/projects/${projectId}/board`);
await expect(
page.getByRole('link', { name: new RegExp(title) })
).toBeVisible();
// 詳細: Markdown本文が表示される
await page.goto(`/projects/${projectId}/board/${thread.id}`);
await expect(
page.getByRole('heading', { name: 'Hello', level: 1 })
).toBeVisible();
await expect(page.getByText('markdown')).toBeVisible();
// コメント追加(API) → 詳細に表示される
const commentRes = await page.request.post(
`/api/projects/${projectId}/board/threads/${thread.id}/comments`,
{ data: { bodyMd: 'first comment' } }
);
expect(commentRes.ok()).toBeTruthy();
await page.reload();
await expect(page.getByText('first comment')).toBeVisible();
// 検索: 別スレッドを作成し、q で絞り込める
const otherTitle = unique('ZZZ');
await page.request.post(`/api/projects/${projectId}/board/threads`, {
data: { title: otherTitle, bodyMd: 'body', category: 'question' },
});
await page.goto(
`/projects/${projectId}/board?q=${encodeURIComponent(otherTitle)}`
);
await expect(
page.getByRole('link', { name: new RegExp(otherTitle) })
).toBeVisible();
// 前のスレッドは検索結果に含まれない
await expect(
page.getByRole('link', { name: new RegExp(title) })
).toHaveCount(0);
});
});

View File

@ -0,0 +1,127 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { createMigratedTestDb } from '@/tests/helpers/db';
import type { SqliteDatabase } from '@/lib/db/sqlite';
import { UserRepository } from '@/repositories/UserRepository';
import { ProjectRepository } from '@/repositories/ProjectRepository';
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
import { BoardRepository } from '@/repositories/BoardRepository';
describe('BoardRepository', () => {
let db: SqliteDatabase;
let repo: BoardRepository;
let projectId: number;
let authorId: number;
beforeEach(() => {
db = createMigratedTestDb();
repo = new BoardRepository(db);
authorId = new UserRepository(db).create({
name: 'A',
email: 'a@example.com',
passwordHash: 'h',
}).id;
projectId = new ProjectRepository(db).create({
name: 'P',
ownerId: authorId,
}).id;
new ProjectMemberRepository(db).add(projectId, authorId, 'admin');
});
afterEach(() => db.close());
function createThread(title: string, body = 'body') {
return repo.createThread({
projectId,
title,
bodyMd: body,
authorId,
category: 'notice',
});
}
it('creates and finds a thread', () => {
const t = createThread('T1');
expect(repo.findThreadById(t.id)?.title).toBe('T1');
});
it('lists threads pinned-first then newest', () => {
const t1 = createThread('old');
const t2 = createThread('new');
repo.updateThread(t1.id, { isPinned: 1 });
const { items } = repo.findThreads(projectId);
expect(items[0].id).toBe(t1.id);
expect(items[1].id).toBe(t2.id);
});
it('excludes soft-deleted threads', () => {
const t = createThread('T');
repo.deleteThread(t.id);
expect(repo.findThreadById(t.id)).toBeNull();
expect(repo.findThreads(projectId).total).toBe(0);
});
it('isolates threads by project', () => {
createThread('mine');
const otherProject = new ProjectRepository(db).create({
name: 'P2',
ownerId: authorId,
}).id;
repo.createThread({
projectId: otherProject,
title: 'theirs',
bodyMd: 'b',
authorId,
category: null,
});
expect(repo.findThreads(projectId).total).toBe(1);
expect(repo.findThreads(otherProject).total).toBe(1);
});
it('searches threads by title and body', () => {
createThread('Important notice', 'body');
createThread('Other', 'special keyword');
expect(repo.findThreads(projectId, { search: 'important' }).total).toBe(1);
expect(repo.findThreads(projectId, { search: 'keyword' }).total).toBe(1);
expect(repo.findThreads(projectId, { search: 'nomatch' }).total).toBe(0);
});
it('paginates threads', () => {
for (let i = 0; i < 5; i++) createThread(`t${i}`);
expect(
repo.findThreads(projectId, { page: 1, pageSize: 2 }).items
).toHaveLength(2);
expect(repo.findThreads(projectId, { page: 1, pageSize: 2 }).total).toBe(5);
});
it('creates and lists comments (oldest first)', () => {
const t = createThread('T');
const c1 = repo.createComment({
threadId: t.id,
authorId,
bodyMd: 'first',
});
repo.createComment({ threadId: t.id, authorId, bodyMd: 'second' });
const { items, total } = repo.findCommentsByThread(t.id);
expect(total).toBe(2);
expect(items[0].id).toBe(c1.id);
});
it('excludes soft-deleted comments', () => {
const t = createThread('T');
const c = repo.createComment({ threadId: t.id, authorId, bodyMd: 'x' });
repo.deleteComment(c.id);
expect(repo.findCommentById(c.id)).toBeNull();
expect(repo.findCommentsByThread(t.id).total).toBe(0);
});
it('updates a comment body', () => {
const t = createThread('T');
const c = repo.createComment({ threadId: t.id, authorId, bodyMd: 'x' });
const updated = repo.updateComment(c.id, 'updated');
expect(updated?.bodyMd).toBe('updated');
});
});

View File

@ -0,0 +1,170 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { createMigratedTestDb } from '@/tests/helpers/db';
import type { SqliteDatabase } from '@/lib/db/sqlite';
import { UserRepository } from '@/repositories/UserRepository';
import { ProjectRepository } from '@/repositories/ProjectRepository';
import { ProjectMemberRepository } from '@/repositories/ProjectMemberRepository';
import { BoardRepository } from '@/repositories/BoardRepository';
import { NotificationRepository } from '@/repositories/NotificationRepository';
import { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
import { NotificationService } from '@/services/NotificationService';
import { ActivityLogService } from '@/services/ActivityLogService';
import { BoardService } from '@/services/BoardService';
import { ForbiddenError, NotFoundError, ValidationError } from '@/lib/errors';
function makeService(db: SqliteDatabase) {
return new BoardService(
new BoardRepository(db),
new ProjectMemberRepository(db),
new NotificationService(new NotificationRepository(db)),
new ActivityLogService(new ActivityLogRepository(db))
);
}
describe('BoardService', () => {
let db: SqliteDatabase;
let service: BoardService;
let projectId: number;
let authorId: number;
let memberId: number;
let outsiderId: number;
beforeEach(() => {
db = createMigratedTestDb();
service = makeService(db);
const users = new UserRepository(db);
authorId = users.create({
name: 'Author',
email: 'a@example.com',
passwordHash: 'h',
}).id;
memberId = users.create({
name: 'Member',
email: 'm@example.com',
passwordHash: 'h',
}).id;
outsiderId = users.create({
name: 'Outsider',
email: 'o@example.com',
passwordHash: 'h',
}).id;
projectId = new ProjectRepository(db).create({
name: 'P',
ownerId: authorId,
}).id;
const members = new ProjectMemberRepository(db);
members.add(projectId, authorId, 'admin');
members.add(projectId, memberId, 'member');
});
afterEach(() => db.close());
it('allows a member to create a thread and records activity', () => {
const thread = service.createThread(memberId, projectId, {
title: 'T',
bodyMd: 'body',
category: 'notice',
});
expect(thread.title).toBe('T');
const logs = new ActivityLogRepository(db).findByProject(projectId);
expect(logs.items.some((l) => l.action === 'board_posted')).toBe(true);
});
it('forbids a non-member from creating a thread', () => {
expect(() =>
service.createThread(outsiderId, projectId, { title: 'T', bodyMd: 'b' })
).toThrow(ForbiddenError);
});
it('rejects an empty title', () => {
expect(() =>
service.createThread(memberId, projectId, { title: ' ', bodyMd: 'b' })
).toThrow(ValidationError);
});
it('only the author or admin can edit/delete a thread', () => {
const users = new UserRepository(db);
const member2 = users.create({
name: 'Member2',
email: 'm2@example.com',
passwordHash: 'h',
}).id;
new ProjectMemberRepository(db).add(projectId, member2, 'member');
const thread = service.createThread(memberId, projectId, {
title: 'T',
bodyMd: 'b',
});
// 非作者かつ非管理者は編集不可
expect(() =>
service.updateThread(member2, thread.id, { title: 'X' })
).toThrow(ForbiddenError);
// 管理者は編集可
expect(() =>
service.updateThread(authorId, thread.id, { title: 'X' })
).not.toThrow();
// 作者自身は編集可
expect(() =>
service.updateThread(memberId, thread.id, { title: 'Y' })
).not.toThrow();
// 外部者はアクセス不可
expect(() => service.getThread(outsiderId, thread.id)).toThrow(
ForbiddenError
);
});
it('creates a comment, notifies the thread author (not the commenter), and logs activity', () => {
const thread = service.createThread(authorId, projectId, {
title: 'T',
bodyMd: 'b',
});
const comment = service.createComment(memberId, thread.id, 'nice post');
expect(comment.bodyMd).toBe('nice post');
// 通知はスレッド投稿者(authorId)へ
const notifs = new NotificationRepository(db).findUnreadByUser(authorId);
expect(notifs.total).toBe(1);
// コメントした本人には通知しない
expect(new NotificationRepository(db).countUnreadByUser(memberId)).toBe(0);
const logs = new ActivityLogRepository(db).findByProject(projectId);
expect(logs.items.some((l) => l.action === 'comment_added')).toBe(true);
});
it('does not notify when the author comments on their own thread', () => {
const thread = service.createThread(authorId, projectId, {
title: 'T',
bodyMd: 'b',
});
service.createComment(authorId, thread.id, 'self comment');
expect(new NotificationRepository(db).countUnreadByUser(authorId)).toBe(0);
});
it('only the comment author can edit their comment', () => {
const thread = service.createThread(authorId, projectId, {
title: 'T',
bodyMd: 'b',
});
const comment = service.createComment(memberId, thread.id, 'c');
expect(() => service.updateComment(authorId, comment.id, 'hacked')).toThrow(
ForbiddenError
);
expect(service.updateComment(memberId, comment.id, 'edited').bodyMd).toBe(
'edited'
);
});
it('admin can delete another member comment', () => {
const thread = service.createThread(authorId, projectId, {
title: 'T',
bodyMd: 'b',
});
const comment = service.createComment(memberId, thread.id, 'c');
service.deleteComment(authorId, comment.id); // authorId is admin
expect(() => service.listComments(memberId, thread.id)).not.toThrow();
});
it('throws NotFoundError for a non-existent thread', () => {
expect(() => service.getThread(memberId, 99999)).toThrow(NotFoundError);
});
});