feat(m9): todo/kanban with DnD, assignee notify, completion, tests, e2e

- TodoRepository (column CRUD, item CRUD, soft-delete, ordering, maxOrderIndex,
  project isolation) + TodoService (lazy standard-column init, member/admin
  permission, create/update/move/toggleComplete/delete, todo_assigned notify,
  todo_created/updated/completed activity logs, SSE todo.updated)
- APIs: columns + items (GET/POST/PATCH/DELETE), toggleComplete + move
- KanbanBoard client (HTML5 drag&drop move, new task per column, complete),
  todos page, ProjectNav ToDo link
- Unit tests (TodoRepository, TodoService) + e2e todo-kanban
This commit is contained in:
Ken Yasue
2026-06-25 01:54:24 +02:00
parent d2a4d56543
commit 385b251cc7
13 changed files with 1399 additions and 0 deletions

View File

@ -0,0 +1,144 @@
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 { TodoRepository } from '@/repositories/TodoRepository';
describe('TodoRepository', () => {
let db: SqliteDatabase;
let repo: TodoRepository;
let projectId: number;
let userId: number;
let columnId: number;
beforeEach(() => {
db = createMigratedTestDb();
repo = new TodoRepository(db);
userId = new UserRepository(db).create({
name: 'U',
email: 'u@example.com',
passwordHash: 'h',
}).id;
projectId = new ProjectRepository(db).create({
name: 'P',
ownerId: userId,
}).id;
new ProjectMemberRepository(db).add(projectId, userId, 'admin');
columnId = repo.createColumn({ projectId, name: 'Todo', orderIndex: 0 }).id;
});
afterEach(() => db.close());
it('creates and lists columns ordered by order_index', () => {
repo.createColumn({ projectId, name: 'Done', orderIndex: 2 });
repo.createColumn({ projectId, name: 'Doing', orderIndex: 1 });
const cols = repo.findColumns(projectId);
expect(cols.map((c) => c.name)).toEqual(['Todo', 'Doing', 'Done']);
});
it('updates and deletes a column', () => {
expect(repo.updateColumn(columnId, { name: 'Renamed' })?.name).toBe(
'Renamed'
);
repo.deleteColumn(columnId);
expect(repo.findColumnById(columnId)).toBeNull();
});
it('creates an item and lists by column order then item order', () => {
repo.createItem({
projectId,
columnId,
title: 'second',
creatorId: userId,
orderIndex: 1,
});
repo.createItem({
projectId,
columnId,
title: 'first',
creatorId: userId,
orderIndex: 0,
});
const items = repo.findItemsByProject(projectId);
expect(items.map((i) => i.title)).toEqual(['first', 'second']);
});
it('excludes soft-deleted items', () => {
const item = repo.createItem({
projectId,
columnId,
title: 'x',
creatorId: userId,
orderIndex: 0,
});
repo.deleteItem(item.id);
expect(repo.findItemById(item.id)).toBeNull();
expect(repo.findItemsByProject(projectId)).toHaveLength(0);
});
it('maxItemOrderIndex returns -1 for empty column, else max', () => {
expect(repo.maxItemOrderIndex(columnId)).toBe(-1);
repo.createItem({
projectId,
columnId,
title: 'a',
creatorId: userId,
orderIndex: 5,
});
expect(repo.maxItemOrderIndex(columnId)).toBe(5);
});
it('updates item fields including column move', () => {
const col2 = repo.createColumn({
projectId,
name: 'Done',
orderIndex: 1,
}).id;
const item = repo.createItem({
projectId,
columnId,
title: 'x',
creatorId: userId,
orderIndex: 0,
});
const updated = repo.updateItem(item.id, {
title: 'renamed',
columnId: col2,
orderIndex: 0,
completedAt: '2026-01-01T00:00:00.000Z',
});
expect(updated?.columnId).toBe(col2);
expect(updated?.title).toBe('renamed');
expect(updated?.completedAt).toBeTruthy();
});
it('isolates items by project', () => {
const p2 = new ProjectRepository(db).create({
name: 'P2',
ownerId: userId,
}).id;
const c2 = repo.createColumn({
projectId: p2,
name: 'X',
orderIndex: 0,
}).id;
repo.createItem({
projectId,
columnId,
title: 'mine',
creatorId: userId,
orderIndex: 0,
});
repo.createItem({
projectId: p2,
columnId: c2,
title: 'theirs',
creatorId: userId,
orderIndex: 0,
});
expect(repo.findItemsByProject(projectId)).toHaveLength(1);
expect(repo.findItemsByProject(p2)).toHaveLength(1);
});
});

View File

@ -0,0 +1,164 @@
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 { TodoRepository } from '@/repositories/TodoRepository';
import { NotificationRepository } from '@/repositories/NotificationRepository';
import { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
import { NotificationService } from '@/services/NotificationService';
import { ActivityLogService } from '@/services/ActivityLogService';
import { TodoService } from '@/services/TodoService';
import { SseHub } from '@/lib/sse/hub';
import { ForbiddenError, NotFoundError } from '@/lib/errors';
function makeService(db: SqliteDatabase) {
const hub = new SseHub();
const members = new ProjectMemberRepository(db);
const users = new UserRepository(db);
return {
hub,
service: new TodoService(
new TodoRepository(db),
members,
new NotificationService(new NotificationRepository(db)),
new ActivityLogService(new ActivityLogRepository(db)),
hub
),
members,
users,
};
}
describe('TodoService', () => {
let db: SqliteDatabase;
let service: TodoService;
let projectId: number;
let authorId: number;
let memberId: number;
let outsiderId: number;
beforeEach(() => {
db = createMigratedTestDb();
const ctx = makeService(db);
service = ctx.service;
const users = ctx.users;
authorId = users.create({
name: 'A',
email: 'a@example.com',
passwordHash: 'h',
}).id;
memberId = users.create({
name: 'M',
email: 'm@example.com',
passwordHash: 'h',
}).id;
outsiderId = users.create({
name: 'O',
email: 'o@example.com',
passwordHash: 'h',
}).id;
projectId = new ProjectRepository(db).create({
name: 'P',
ownerId: authorId,
}).id;
ctx.members.add(projectId, authorId, 'admin');
ctx.members.add(projectId, memberId, 'member');
});
afterEach(() => db.close());
it('auto-creates the 5 standard columns on first getColumns', () => {
const cols = service.getColumns(authorId, projectId);
expect(cols.map((c) => c.name)).toEqual([
'Backlog',
'To Do',
'In Progress',
'Review',
'Done',
]);
});
it('forbids a non-member from accessing columns', () => {
expect(() => service.getColumns(outsiderId, projectId)).toThrow(
ForbiddenError
);
});
it('creates an item with assignee → todo_assigned notification + todo_created activity', () => {
const col = service.getColumns(authorId, projectId)[0];
const item = service.createItem(authorId, projectId, {
title: 'task',
columnId: col.id,
assigneeId: memberId,
priority: 'high',
});
expect(item.assigneeId).toBe(memberId);
expect(new NotificationRepository(db).countUnreadByUser(memberId)).toBe(1);
expect(
new ActivityLogRepository(db)
.findByProject(projectId)
.items.some((l) => l.action === 'todo_created')
).toBe(true);
});
it('does not notify when assigning to self', () => {
const col = service.getColumns(authorId, projectId)[0];
service.createItem(authorId, projectId, {
title: 'self',
columnId: col.id,
assigneeId: authorId,
});
expect(new NotificationRepository(db).countUnreadByUser(authorId)).toBe(0);
});
it('toggling complete sets completedAt and logs todo_completed', () => {
const col = service.getColumns(authorId, projectId)[0];
const item = service.createItem(authorId, projectId, {
title: 't',
columnId: col.id,
});
const completed = service.toggleComplete(authorId, item.id);
expect(completed.completedAt).not.toBeNull();
expect(
new ActivityLogRepository(db)
.findByProject(projectId)
.items.some((l) => l.action === 'todo_completed')
).toBe(true);
// もう一度トグルで未完了に戻る
const reopened = service.toggleComplete(authorId, item.id);
expect(reopened.completedAt).toBeNull();
});
it('moves an item to another column', () => {
const cols = service.getColumns(authorId, projectId);
const item = service.createItem(authorId, projectId, {
title: 't',
columnId: cols[0].id,
});
const moved = service.moveItem(authorId, item.id, cols[4].id, 0);
expect(moved.columnId).toBe(cols[4].id);
});
it('only creator or admin can delete an item', () => {
const col = service.getColumns(authorId, projectId)[0];
const item = service.createItem(memberId, projectId, {
title: 't',
columnId: col.id,
});
expect(() => service.deleteItem(outsiderId, item.id)).toThrow(
ForbiddenError
);
service.deleteItem(authorId, item.id); // admin
expect(() => service.toggleComplete(memberId, item.id)).toThrow(
NotFoundError
);
});
it('throws NotFoundError for a non-existent item', () => {
expect(() => service.toggleComplete(authorId, 99999)).toThrow(
NotFoundError
);
});
});