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:
127
tests/unit/repositories/BoardRepository.test.ts
Normal file
127
tests/unit/repositories/BoardRepository.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
170
tests/unit/services/BoardService.test.ts
Normal file
170
tests/unit/services/BoardService.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user