feat(m5): notifications & activity log foundation
- Expand NotificationRepository (unread list paginated, count, markRead ownership-scoped); new ActivityLogRepository (create, findByProject paginated) with deterministic created_at+id ordering - NotificationService (notifyOnEvent, resolveTargets for 8 event types, injectable SseBroadcaster for M8) + ActivityLogService (logActivity, listByProject) - NotificationEvent/SseEvent/SseBroadcaster types in lib/types - APIs: GET /api/notifications, POST /api/notifications/:id/read, GET /api/projects/:projectId/activity - Screens: notifications list + MarkReadButton, NotificationBadge in Header, project activity page + ProjectNav link - Unit tests for both repositories and services (28 new)
This commit is contained in:
114
tests/unit/repositories/ActivityLogRepository.test.ts
Normal file
114
tests/unit/repositories/ActivityLogRepository.test.ts
Normal file
@ -0,0 +1,114 @@
|
||||
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 { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
|
||||
|
||||
describe('ActivityLogRepository', () => {
|
||||
let db: SqliteDatabase;
|
||||
let repo: ActivityLogRepository;
|
||||
let userId: number;
|
||||
let projectId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
repo = new ActivityLogRepository(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;
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
it('creates an activity log entry', () => {
|
||||
const log = repo.create({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'todo_created',
|
||||
targetType: 'todo',
|
||||
targetId: 1,
|
||||
metadataJson: JSON.stringify({ title: 'T' }),
|
||||
});
|
||||
|
||||
expect(log.id).toBeGreaterThan(0);
|
||||
expect(log.action).toBe('todo_created');
|
||||
expect(log.projectId).toBe(projectId);
|
||||
});
|
||||
|
||||
it('lists logs for a project (newest first) with total', () => {
|
||||
repo.create({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'a',
|
||||
targetType: 't',
|
||||
targetId: 1,
|
||||
metadataJson: null,
|
||||
});
|
||||
repo.create({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'b',
|
||||
targetType: 't',
|
||||
targetId: 2,
|
||||
metadataJson: null,
|
||||
});
|
||||
|
||||
const result = repo.findByProject(projectId, 1, 20);
|
||||
|
||||
expect(result.total).toBe(2);
|
||||
expect(result.items[0].action).toBe('b');
|
||||
});
|
||||
|
||||
it('isolates logs per project', () => {
|
||||
const p2 = new ProjectRepository(db).create({
|
||||
name: 'P2',
|
||||
ownerId: userId,
|
||||
}).id;
|
||||
repo.create({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'a',
|
||||
targetType: 't',
|
||||
targetId: 1,
|
||||
metadataJson: null,
|
||||
});
|
||||
repo.create({
|
||||
projectId: p2,
|
||||
actorId: userId,
|
||||
action: 'b',
|
||||
targetType: 't',
|
||||
targetId: 2,
|
||||
metadataJson: null,
|
||||
});
|
||||
|
||||
expect(repo.findByProject(projectId).total).toBe(1);
|
||||
expect(repo.findByProject(p2).total).toBe(1);
|
||||
});
|
||||
|
||||
it('paginates results', () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
repo.create({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'a',
|
||||
targetType: 't',
|
||||
targetId: i,
|
||||
metadataJson: null,
|
||||
});
|
||||
}
|
||||
|
||||
const page1 = repo.findByProject(projectId, 1, 2);
|
||||
const page2 = repo.findByProject(projectId, 2, 2);
|
||||
|
||||
expect(page1.items).toHaveLength(2);
|
||||
expect(page2.items).toHaveLength(2);
|
||||
expect(page1.total).toBe(5);
|
||||
});
|
||||
});
|
||||
165
tests/unit/repositories/NotificationRepository.test.ts
Normal file
165
tests/unit/repositories/NotificationRepository.test.ts
Normal file
@ -0,0 +1,165 @@
|
||||
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 { NotificationRepository } from '@/repositories/NotificationRepository';
|
||||
|
||||
describe('NotificationRepository', () => {
|
||||
let db: SqliteDatabase;
|
||||
let repo: NotificationRepository;
|
||||
let userId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
repo = new NotificationRepository(db);
|
||||
userId = new UserRepository(db).create({
|
||||
name: 'U',
|
||||
email: 'u@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
it('creates a notification as unread', () => {
|
||||
const n = repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 't',
|
||||
body: 'b',
|
||||
});
|
||||
expect(n.id).toBeGreaterThan(0);
|
||||
expect(n.readAt).toBeNull();
|
||||
expect(n.userId).toBe(userId);
|
||||
});
|
||||
|
||||
it('lists unread notifications for the user (newest first) with total', () => {
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: '1',
|
||||
body: null,
|
||||
});
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: '2',
|
||||
body: null,
|
||||
});
|
||||
|
||||
const result = repo.findUnreadByUser(userId, 1, 20);
|
||||
|
||||
expect(result.total).toBe(2);
|
||||
expect(result.items).toHaveLength(2);
|
||||
expect(result.items[0].title).toBe('2');
|
||||
});
|
||||
|
||||
it('excludes already-read notifications', () => {
|
||||
const n = repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'x',
|
||||
body: null,
|
||||
});
|
||||
repo.markRead(n.id, userId);
|
||||
|
||||
expect(repo.findUnreadByUser(userId).total).toBe(0);
|
||||
});
|
||||
|
||||
it('isolates notifications per user', () => {
|
||||
const other = new UserRepository(db).create({
|
||||
name: 'O',
|
||||
email: 'o@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'mine',
|
||||
body: null,
|
||||
});
|
||||
repo.create({
|
||||
userId: other,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'theirs',
|
||||
body: null,
|
||||
});
|
||||
|
||||
expect(repo.findUnreadByUser(userId).total).toBe(1);
|
||||
expect(repo.findUnreadByUser(other).total).toBe(1);
|
||||
});
|
||||
|
||||
it('paginates results', () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: `t${i}`,
|
||||
body: null,
|
||||
});
|
||||
}
|
||||
|
||||
const page1 = repo.findUnreadByUser(userId, 1, 2);
|
||||
const page2 = repo.findUnreadByUser(userId, 2, 2);
|
||||
|
||||
expect(page1.items).toHaveLength(2);
|
||||
expect(page2.items).toHaveLength(2);
|
||||
expect(page1.total).toBe(5);
|
||||
});
|
||||
|
||||
it('countUnreadByUser returns the unread count', () => {
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'a',
|
||||
body: null,
|
||||
});
|
||||
repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'b',
|
||||
body: null,
|
||||
});
|
||||
expect(repo.countUnreadByUser(userId)).toBe(2);
|
||||
});
|
||||
|
||||
it('markRead only affects the owner unread notification and returns true', () => {
|
||||
const n = repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'a',
|
||||
body: null,
|
||||
});
|
||||
expect(repo.markRead(n.id, userId)).toBe(true);
|
||||
// 二回目は既読済みなので false
|
||||
expect(repo.markRead(n.id, userId)).toBe(false);
|
||||
});
|
||||
|
||||
it('markRead rejects other users (ownership scope)', () => {
|
||||
const other = new UserRepository(db).create({
|
||||
name: 'O',
|
||||
email: 'o@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
const n = repo.create({
|
||||
userId,
|
||||
projectId: null,
|
||||
type: 'mention',
|
||||
title: 'a',
|
||||
body: null,
|
||||
});
|
||||
|
||||
expect(repo.markRead(n.id, other)).toBe(false);
|
||||
expect(repo.countUnreadByUser(userId)).toBe(1);
|
||||
});
|
||||
});
|
||||
104
tests/unit/services/ActivityLogService.test.ts
Normal file
104
tests/unit/services/ActivityLogService.test.ts
Normal file
@ -0,0 +1,104 @@
|
||||
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 { ActivityLogRepository } from '@/repositories/ActivityLogRepository';
|
||||
import { ActivityLogService } from '@/services/ActivityLogService';
|
||||
|
||||
describe('ActivityLogService', () => {
|
||||
let db: SqliteDatabase;
|
||||
let repo: ActivityLogRepository;
|
||||
let service: ActivityLogService;
|
||||
let userId: number;
|
||||
let projectId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
repo = new ActivityLogRepository(db);
|
||||
service = new ActivityLogService(repo);
|
||||
userId = new UserRepository(db).create({
|
||||
name: 'U',
|
||||
email: 'u@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
projectId = new ProjectRepository(db).create({
|
||||
name: 'P',
|
||||
ownerId: userId,
|
||||
}).id;
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
it('logActivity creates a log entry with serialized metadata', () => {
|
||||
const log = service.logActivity({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'todo_created',
|
||||
targetType: 'todo',
|
||||
targetId: 1,
|
||||
metadata: { title: 'T' },
|
||||
});
|
||||
|
||||
expect(log.action).toBe('todo_created');
|
||||
expect(log.metadataJson).toBe(JSON.stringify({ title: 'T' }));
|
||||
});
|
||||
|
||||
it('logActivity stores null metadata when not provided', () => {
|
||||
const log = service.logActivity({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'member_added',
|
||||
targetType: 'member',
|
||||
targetId: 2,
|
||||
});
|
||||
|
||||
expect(log.metadataJson).toBeNull();
|
||||
});
|
||||
|
||||
it('listByProject returns paginated logs for the project', () => {
|
||||
service.logActivity({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'a',
|
||||
targetType: 't',
|
||||
targetId: 1,
|
||||
});
|
||||
service.logActivity({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'b',
|
||||
targetType: 't',
|
||||
targetId: 2,
|
||||
});
|
||||
|
||||
const result = service.listByProject(projectId, 1);
|
||||
|
||||
expect(result.total).toBe(2);
|
||||
expect(result.items[0].action).toBe('b');
|
||||
});
|
||||
|
||||
it('listByProject isolates by project', () => {
|
||||
const p2 = new ProjectRepository(db).create({
|
||||
name: 'P2',
|
||||
ownerId: userId,
|
||||
}).id;
|
||||
service.logActivity({
|
||||
projectId,
|
||||
actorId: userId,
|
||||
action: 'a',
|
||||
targetType: 't',
|
||||
targetId: 1,
|
||||
});
|
||||
service.logActivity({
|
||||
projectId: p2,
|
||||
actorId: userId,
|
||||
action: 'b',
|
||||
targetType: 't',
|
||||
targetId: 2,
|
||||
});
|
||||
|
||||
expect(service.listByProject(projectId).total).toBe(1);
|
||||
expect(service.listByProject(p2).total).toBe(1);
|
||||
});
|
||||
});
|
||||
155
tests/unit/services/NotificationService.test.ts
Normal file
155
tests/unit/services/NotificationService.test.ts
Normal file
@ -0,0 +1,155 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } 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 { NotificationRepository } from '@/repositories/NotificationRepository';
|
||||
import { NotificationService } from '@/services/NotificationService';
|
||||
import type { NotificationEvent, SseBroadcaster } from '@/lib/types';
|
||||
|
||||
describe('NotificationService', () => {
|
||||
let db: SqliteDatabase;
|
||||
let repo: NotificationRepository;
|
||||
let service: NotificationService;
|
||||
let userId: number;
|
||||
let projectId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createMigratedTestDb();
|
||||
repo = new NotificationRepository(db);
|
||||
service = new NotificationService(repo);
|
||||
userId = new UserRepository(db).create({
|
||||
name: 'U',
|
||||
email: 'u@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
projectId = new ProjectRepository(db).create({
|
||||
name: 'P',
|
||||
ownerId: userId,
|
||||
}).id;
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
function makeEvent(
|
||||
type: NotificationEvent['type'],
|
||||
extra: Record<string, unknown>
|
||||
): NotificationEvent {
|
||||
return {
|
||||
type,
|
||||
projectId,
|
||||
title: 'title',
|
||||
body: 'body',
|
||||
...extra,
|
||||
} as NotificationEvent;
|
||||
}
|
||||
|
||||
describe('resolveTargets', () => {
|
||||
it('mention → mentionedUserId', () => {
|
||||
expect(
|
||||
service.resolveTargets(makeEvent('mention', { mentionedUserId: 7 }))
|
||||
).toEqual([7]);
|
||||
});
|
||||
it('todo_assigned → assigneeId', () => {
|
||||
expect(
|
||||
service.resolveTargets(makeEvent('todo_assigned', { assigneeId: 7 }))
|
||||
).toEqual([7]);
|
||||
});
|
||||
it('todo_due_soon → assigneeId', () => {
|
||||
expect(
|
||||
service.resolveTargets(makeEvent('todo_due_soon', { assigneeId: 7 }))
|
||||
).toEqual([7]);
|
||||
});
|
||||
it('meeting_invited → memberIds', () => {
|
||||
expect(
|
||||
service.resolveTargets(
|
||||
makeEvent('meeting_invited', { memberIds: [1, 2, 3] })
|
||||
)
|
||||
).toEqual([1, 2, 3]);
|
||||
});
|
||||
it('board_commented → threadAuthorId', () => {
|
||||
expect(
|
||||
service.resolveTargets(
|
||||
makeEvent('board_commented', { threadAuthorId: 9 })
|
||||
)
|
||||
).toEqual([9]);
|
||||
});
|
||||
it('project_added → addedUserId', () => {
|
||||
expect(
|
||||
service.resolveTargets(makeEvent('project_added', { addedUserId: 5 }))
|
||||
).toEqual([5]);
|
||||
});
|
||||
it('file_shared → projectMemberIds', () => {
|
||||
expect(
|
||||
service.resolveTargets(
|
||||
makeEvent('file_shared', { projectMemberIds: [1, 2] })
|
||||
)
|
||||
).toEqual([1, 2]);
|
||||
});
|
||||
it('note_updated → projectMemberIds', () => {
|
||||
expect(
|
||||
service.resolveTargets(
|
||||
makeEvent('note_updated', { projectMemberIds: [3, 4] })
|
||||
)
|
||||
).toEqual([3, 4]);
|
||||
});
|
||||
});
|
||||
|
||||
it('notifyOnEvent creates a notification per target user', () => {
|
||||
const other = new UserRepository(db).create({
|
||||
name: 'O',
|
||||
email: 'o@example.com',
|
||||
passwordHash: 'h',
|
||||
}).id;
|
||||
const created = service.notifyOnEvent(
|
||||
makeEvent('meeting_invited', { memberIds: [userId, other] })
|
||||
);
|
||||
|
||||
expect(created).toHaveLength(2);
|
||||
expect(repo.countUnreadByUser(userId)).toBe(1);
|
||||
expect(repo.countUnreadByUser(other)).toBe(1);
|
||||
});
|
||||
|
||||
it('notifyOnEvent broadcasts notification.created when a broadcaster is injected', () => {
|
||||
const broadcast = vi.fn();
|
||||
const broadcaster: SseBroadcaster = { broadcast };
|
||||
const withBroadcaster = new NotificationService(repo, broadcaster);
|
||||
|
||||
withBroadcaster.notifyOnEvent(
|
||||
makeEvent('project_added', { addedUserId: userId })
|
||||
);
|
||||
|
||||
expect(broadcast).toHaveBeenCalledTimes(1);
|
||||
expect(broadcast).toHaveBeenCalledWith(
|
||||
1,
|
||||
expect.objectContaining({ type: 'notification.created' })
|
||||
);
|
||||
});
|
||||
|
||||
it('notifyOnEvent does not broadcast when projectId is null', () => {
|
||||
const broadcast = vi.fn();
|
||||
const withBroadcaster = new NotificationService(repo, { broadcast });
|
||||
const event: NotificationEvent = {
|
||||
type: 'mention',
|
||||
projectId: null,
|
||||
title: 't',
|
||||
body: null,
|
||||
mentionedUserId: userId,
|
||||
};
|
||||
|
||||
withBroadcaster.notifyOnEvent(event);
|
||||
|
||||
expect(broadcast).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('listUnread / countUnread / markRead delegate to the repository', () => {
|
||||
const n = service.notifyOnEvent(
|
||||
makeEvent('mention', { mentionedUserId: userId })
|
||||
)[0];
|
||||
|
||||
expect(service.countUnread(userId)).toBe(1);
|
||||
expect(service.listUnread(userId).total).toBe(1);
|
||||
expect(service.markRead(n.id, userId)).toBe(true);
|
||||
expect(service.countUnread(userId)).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user