chore: move todo backend to proper DI

This commit is contained in:
jubnl
2026-07-26 14:09:54 +02:00
parent b5ebffa115
commit 79946cc711
21 changed files with 772 additions and 456 deletions
+3 -2
View File
@@ -1,8 +1,9 @@
/**
* Unit tests for MCP resources (resources.ts).
* Tests all 13 legacy-registrar resources via InMemoryTransport + Client
* Tests all 12 legacy-registrar resources via InMemoryTransport + Client
* (trek://categories moved to the DI-discovered CategoriesMcp — see
* tools-categories.test.ts).
* tools-categories.test.ts; trek://trips/{tripId}/todos moved to TodoMcp —
* see tools-todos.test.ts).
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll, afterEach } from 'vitest';
+107 -3
View File
@@ -1,7 +1,12 @@
/**
* Unit tests for MCP todo tools:
* Unit tests for the todo MCP surface (TodoMcp, DI-discovered):
* create_todo, update_todo, toggle_todo, delete_todo, reorder_todos,
* list_todos, get_todo_category_assignees, set_todo_category_assignees.
* list_todos, get_todo_category_assignees, set_todo_category_assignees,
* plus the trek://trips/{tripId}/todos resource.
*
* All of it attaches via the nest-mcp registry inside registerTools, so every
* harness here keeps withTools on (the resource is NOT registered by the
* legacy registerResources fan-out anymore).
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
@@ -38,7 +43,8 @@ import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import { createUser, createTrip, createTodoItem } from '../../helpers/factories';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness';
import { ADDON_IDS } from '../../../src/addons';
beforeAll(() => {
createTables(testDb);
@@ -436,3 +442,101 @@ describe('Tool: set_todo_category_assignees', () => {
});
});
});
// ---------------------------------------------------------------------------
// Scope gating (todos read/write, registration-time)
// ---------------------------------------------------------------------------
describe('Todo tools — scope gating', () => {
const READ_TOOLS = ['list_todos', 'get_todo_category_assignees'];
const WRITE_TOOLS = ['create_todo', 'update_todo', 'toggle_todo', 'delete_todo', 'reorder_todos', 'set_todo_category_assignees'];
async function listToolNames(userId: number, scopes: string[] | null): Promise<string[]> {
const h = await createMcpHarness({ userId, withResources: false, scopes });
try {
return (await h.client.listTools()).tools.map((t) => t.name);
} finally {
await h.cleanup();
}
}
it('registers all eight tools with null scopes (full access)', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, null);
for (const tool of [...READ_TOOLS, ...WRITE_TOOLS]) expect(names).toContain(tool);
});
it('registers only the read tools with todos:read', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, ['todos:read']);
for (const tool of READ_TOOLS) expect(names).toContain(tool);
for (const tool of WRITE_TOOLS) expect(names).not.toContain(tool);
});
it('registers no todo tools for an unrelated scope', async () => {
const { user } = createUser(testDb);
const names = await listToolNames(user.id, ['budget:read']);
for (const tool of [...READ_TOOLS, ...WRITE_TOOLS]) expect(names).not.toContain(tool);
});
});
// ---------------------------------------------------------------------------
// Addon gating (packing addon, the legacy whole-registrar early return —
// now the `when` predicate on every TodoMcp entry)
// ---------------------------------------------------------------------------
describe('Todo tools — packing addon gating', () => {
it('registers nothing (tools or resource) when the packing addon is disabled', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
testDb.prepare('UPDATE addons SET enabled = 0 WHERE id = ?').run(ADDON_IDS.PACKING);
try {
await withHarness(user.id, async (h) => {
const names = (await h.client.listTools()).tools.map((t) => t.name);
expect(names).not.toContain('list_todos');
expect(names).not.toContain('create_todo');
await expect(h.client.readResource({ uri: `trek://trips/${trip.id}/todos` })).rejects.toThrow();
});
} finally {
testDb.prepare('UPDATE addons SET enabled = 1 WHERE id = ?').run(ADDON_IDS.PACKING);
}
});
});
// ---------------------------------------------------------------------------
// trek://trips/{tripId}/todos resource (moved from the legacy registerResources)
// ---------------------------------------------------------------------------
describe('Resource: trek://trips/{tripId}/todos', () => {
it('returns the trip todos ordered by position', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
createTodoItem(testDb, trip.id, { name: 'First' });
createTodoItem(testDb, trip.id, { name: 'Second' });
await withHarness(user.id, async (h) => {
const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/todos` });
const items = parseResourceResult(result) as any[];
expect(items).toHaveLength(2);
expect(items[0].name).toBe('First');
expect(items[1].name).toBe('Second');
});
});
it('returns the access-denied payload for a non-member', async () => {
const { user } = createUser(testDb);
const { user: other } = createUser(testDb);
const trip = createTrip(testDb, other.id);
await withHarness(user.id, async (h) => {
const result = await h.client.readResource({ uri: `trek://trips/${trip.id}/todos` });
expect(parseResourceResult(result)).toEqual({ error: 'Trip not found or access denied' });
});
});
it('returns the access-denied payload for a malformed trip id', async () => {
const { user } = createUser(testDb);
await withHarness(user.id, async (h) => {
const result = await h.client.readResource({ uri: 'trek://trips/not-a-number/todos' });
expect(parseResourceResult(result)).toEqual({ error: 'Trip not found or access denied' });
});
});
});
@@ -1,6 +1,8 @@
/**
* Unit tests for todoService — TODO-SVC-001 through TODO-SVC-020.
* Uses a real in-memory SQLite DB so SQL logic is exercised faithfully.
* Unit tests for the DI-native TodoService — TODO-SVC-001 through TODO-SVC-024
* (001–020 moved 1:1 from the legacy tests/unit/services/todoService.test.ts;
* 021–024 pin the todo.bridge delegation). Uses a real in-memory SQLite DB so
* SQL logic is exercised faithfully.
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
@@ -35,21 +37,17 @@ vi.mock('../../../src/config', () => ({
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
updateJwtSecret: () => {},
}));
vi.mock('../../../src/websocket', () => ({ broadcast: vi.fn() }));
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import { createUser, createTrip, addTripMember } from '../../helpers/factories';
import {
verifyTripAccess,
listItems,
createItem,
updateItem,
deleteItem,
getCategoryAssignees,
updateCategoryAssignees,
reorderItems,
} from '../../../src/services/todoService';
import { DatabaseService } from '../../../src/nest/database/database.service';
import { TodoService } from '../../../src/nest/todo/todo.service';
import { listItems, createItem, updateItem, deleteItem } from '../../../src/nest/todo/todo.bridge';
const svc = new TodoService(new DatabaseService(testDb));
beforeAll(() => {
createTables(testDb);
@@ -70,16 +68,16 @@ describe('verifyTripAccess', () => {
it('TODO-SVC-001: returns trip for owner', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const result = verifyTripAccess(trip.id, user.id);
const result = svc.verifyTripAccess(trip.id, user.id);
expect(result).toBeDefined();
expect((result as any).id).toBe(trip.id);
expect(result?.id).toBe(trip.id);
});
it('TODO-SVC-002: returns null for non-member', () => {
const { user: owner } = createUser(testDb);
const { user: stranger } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
expect(verifyTripAccess(trip.id, stranger.id)).toBeFalsy();
expect(svc.verifyTripAccess(trip.id, stranger.id)).toBeFalsy();
});
it('TODO-SVC-003: returns trip for member', () => {
@@ -87,7 +85,7 @@ describe('verifyTripAccess', () => {
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const result = verifyTripAccess(trip.id, member.id);
const result = svc.verifyTripAccess(trip.id, member.id);
expect(result).toBeDefined();
});
});
@@ -98,13 +96,13 @@ describe('listItems and createItem', () => {
it('TODO-SVC-004: listItems returns empty array for new trip', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(listItems(trip.id)).toEqual([]);
expect(svc.listItems(trip.id)).toEqual([]);
});
it('TODO-SVC-005: createItem inserts a todo with name only', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Buy snacks' }) as any;
const item = svc.createItem(trip.id, { name: 'Buy snacks' }) as any;
expect(item).toBeDefined();
expect(item.name).toBe('Buy snacks');
expect(item.checked).toBe(0);
@@ -115,15 +113,15 @@ describe('listItems and createItem', () => {
it('TODO-SVC-006: createItem assigns incrementing sort_order', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const a = createItem(trip.id, { name: 'A' }) as any;
const b = createItem(trip.id, { name: 'B' }) as any;
const a = svc.createItem(trip.id, { name: 'A' }) as any;
const b = svc.createItem(trip.id, { name: 'B' }) as any;
expect(b.sort_order).toBe(a.sort_order + 1);
});
it('TODO-SVC-007: createItem stores optional fields', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, {
const item = svc.createItem(trip.id, {
name: 'Pack bag',
category: 'Prep',
description: 'All the gear',
@@ -137,10 +135,10 @@ describe('listItems and createItem', () => {
it('TODO-SVC-008: listItems returns items ordered by sort_order', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
createItem(trip.id, { name: 'First' });
createItem(trip.id, { name: 'Second' });
createItem(trip.id, { name: 'Third' });
const items = listItems(trip.id) as any[];
svc.createItem(trip.id, { name: 'First' });
svc.createItem(trip.id, { name: 'Second' });
svc.createItem(trip.id, { name: 'Third' });
const items = svc.listItems(trip.id) as any[];
expect(items).toHaveLength(3);
expect(items[0].sort_order).toBeLessThanOrEqual(items[1].sort_order);
expect(items[1].sort_order).toBeLessThanOrEqual(items[2].sort_order);
@@ -153,24 +151,24 @@ describe('updateItem', () => {
it('TODO-SVC-009: returns null for non-existent item', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(updateItem(trip.id, 99999, { name: 'Ghost' }, ['name'])).toBeNull();
expect(svc.updateItem(trip.id, 99999, { name: 'Ghost' }, ['name'])).toBeNull();
});
it('TODO-SVC-010: toggles checked status', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Visit museum' }) as any;
const updated = updateItem(trip.id, item.id, { checked: 1 }, ['checked']) as any;
const item = svc.createItem(trip.id, { name: 'Visit museum' }) as any;
const updated = svc.updateItem(trip.id, item.id, { checked: 1 }, ['checked']) as any;
expect(updated.checked).toBe(1);
const back = updateItem(trip.id, item.id, { checked: 0 }, ['checked']) as any;
const back = svc.updateItem(trip.id, item.id, { checked: 0 }, ['checked']) as any;
expect(back.checked).toBe(0);
});
it('TODO-SVC-011: updates name and category', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Old' }) as any;
const updated = updateItem(trip.id, item.id, { name: 'New', category: 'Misc' }, ['name', 'category']) as any;
const item = svc.createItem(trip.id, { name: 'Old' }) as any;
const updated = svc.updateItem(trip.id, item.id, { name: 'New', category: 'Misc' }, ['name', 'category']) as any;
expect(updated.name).toBe('New');
expect(updated.category).toBe('Misc');
});
@@ -178,8 +176,8 @@ describe('updateItem', () => {
it('TODO-SVC-012: clears due_date when key is present with null value', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Task', due_date: '2026-06-01' }) as any;
const updated = updateItem(trip.id, item.id, { due_date: null }, ['due_date']) as any;
const item = svc.createItem(trip.id, { name: 'Task', due_date: '2026-06-01' }) as any;
const updated = svc.updateItem(trip.id, item.id, { due_date: null }, ['due_date']) as any;
expect(updated.due_date).toBeNull();
});
});
@@ -190,15 +188,15 @@ describe('deleteItem', () => {
it('TODO-SVC-013: returns false for non-existent item', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(deleteItem(trip.id, 99999)).toBe(false);
expect(svc.deleteItem(trip.id, 99999)).toBe(false);
});
it('TODO-SVC-014: deletes item and returns true', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Gone' }) as any;
expect(deleteItem(trip.id, item.id)).toBe(true);
expect(listItems(trip.id)).toHaveLength(0);
const item = svc.createItem(trip.id, { name: 'Gone' }) as any;
expect(svc.deleteItem(trip.id, item.id)).toBe(true);
expect(svc.listItems(trip.id)).toHaveLength(0);
});
});
@@ -208,11 +206,11 @@ describe('reorderItems', () => {
it('TODO-SVC-015: assigns sort_order matching orderedIds array position', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const a = createItem(trip.id, { name: 'A' }) as any;
const b = createItem(trip.id, { name: 'B' }) as any;
const c = createItem(trip.id, { name: 'C' }) as any;
const a = svc.createItem(trip.id, { name: 'A' }) as any;
const b = svc.createItem(trip.id, { name: 'B' }) as any;
const c = svc.createItem(trip.id, { name: 'C' }) as any;
reorderItems(trip.id, [c.id, a.id, b.id]);
svc.reorderItems(trip.id, [c.id, a.id, b.id]);
const rows = testDb.prepare('SELECT id, sort_order FROM todo_items WHERE trip_id = ? ORDER BY sort_order').all(trip.id) as any[];
expect(rows[0].id).toBe(c.id);
@@ -227,7 +225,7 @@ describe('getCategoryAssignees / updateCategoryAssignees', () => {
it('TODO-SVC-016: returns empty object for new trip', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(getCategoryAssignees(trip.id)).toEqual({});
expect(svc.getCategoryAssignees(trip.id)).toEqual({});
});
it('TODO-SVC-017: updateCategoryAssignees sets assignees for a category', () => {
@@ -236,10 +234,10 @@ describe('getCategoryAssignees / updateCategoryAssignees', () => {
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const rows = updateCategoryAssignees(trip.id, 'Packing', [owner.id, member.id]) as any[];
const rows = svc.updateCategoryAssignees(trip.id, 'Packing', [owner.id, member.id]);
expect(rows).toHaveLength(2);
const assignees = getCategoryAssignees(trip.id) as any;
const assignees = svc.getCategoryAssignees(trip.id);
expect(assignees['Packing']).toHaveLength(2);
});
@@ -247,11 +245,11 @@ describe('getCategoryAssignees / updateCategoryAssignees', () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
updateCategoryAssignees(trip.id, 'Packing', [owner.id]);
const cleared = updateCategoryAssignees(trip.id, 'Packing', []) as any[];
svc.updateCategoryAssignees(trip.id, 'Packing', [owner.id]);
const cleared = svc.updateCategoryAssignees(trip.id, 'Packing', []);
expect(cleared).toHaveLength(0);
const assignees = getCategoryAssignees(trip.id) as any;
const assignees = svc.getCategoryAssignees(trip.id);
expect(assignees['Packing']).toBeUndefined();
});
@@ -261,10 +259,10 @@ describe('getCategoryAssignees / updateCategoryAssignees', () => {
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
updateCategoryAssignees(trip.id, 'Shopping', [owner.id]);
updateCategoryAssignees(trip.id, 'Logistics', [member.id]);
svc.updateCategoryAssignees(trip.id, 'Shopping', [owner.id]);
svc.updateCategoryAssignees(trip.id, 'Logistics', [member.id]);
const assignees = getCategoryAssignees(trip.id) as any;
const assignees = svc.getCategoryAssignees(trip.id);
expect(Object.keys(assignees)).toHaveLength(2);
expect(assignees['Shopping']).toHaveLength(1);
expect(assignees['Logistics']).toHaveLength(1);
@@ -276,12 +274,53 @@ describe('getCategoryAssignees / updateCategoryAssignees', () => {
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
updateCategoryAssignees(trip.id, 'Food', [owner.id, member.id]);
svc.updateCategoryAssignees(trip.id, 'Food', [owner.id, member.id]);
// Replace with just owner
updateCategoryAssignees(trip.id, 'Food', [owner.id]);
svc.updateCategoryAssignees(trip.id, 'Food', [owner.id]);
const assignees = getCategoryAssignees(trip.id) as any;
const assignees = svc.getCategoryAssignees(trip.id);
expect(assignees['Food']).toHaveLength(1);
expect(assignees['Food'][0].user_id).toBe(owner.id);
});
});
// ── todo.bridge (delegation to the service — inside the src/nest coverage gate)
describe('todo.bridge', () => {
it('TODO-SVC-021 — listItems delegates to the service', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
svc.createItem(trip.id, { name: 'Bridged' });
const items = listItems(trip.id) as any[];
expect(items).toHaveLength(1);
expect(items[0].name).toBe('Bridged');
});
it('TODO-SVC-022 — createItem delegates with the legacy defaults', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Via bridge' }) as any;
expect(item.checked).toBe(0);
expect(item.priority).toBe(0);
expect(item.sort_order).toBe(0);
});
it('TODO-SVC-023 — updateItem delegates and keeps the bodyKeys sentinel protocol', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Task', due_date: '2026-06-01' }) as any;
const untouched = updateItem(trip.id, item.id, { name: 'Renamed' }, []) as any;
expect(untouched.due_date).toBe('2026-06-01');
const cleared = updateItem(trip.id, item.id, { due_date: null }, ['due_date']) as any;
expect(cleared.due_date).toBeNull();
});
it('TODO-SVC-024 — deleteItem delegates to the service', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const item = createItem(trip.id, { name: 'Gone' }) as any;
expect(deleteItem(trip.id, item.id)).toBe(true);
expect(deleteItem(trip.id, item.id)).toBe(false);
expect(svc.listItems(trip.id)).toHaveLength(0);
});
});
+3 -2
View File
@@ -28,7 +28,6 @@ vi.mock('../../../src/services/tripService', () => tripSvc);
vi.mock('../../../src/services/dayService', () => ({ listDays: () => ({ days: [1] }), listAccommodations: () => [] }));
vi.mock('../../../src/services/placeService', () => ({ listPlaces: () => [] }));
vi.mock('../../../src/services/packingService', () => ({ listItems: () => [] }));
vi.mock('../../../src/services/todoService', () => ({ listItems: () => [] }));
vi.mock('../../../src/services/budgetService', () => ({
listBudgetItems: () => [],
rebaseTripCurrency: vi.fn(async () => {}),
@@ -37,9 +36,11 @@ vi.mock('../../../src/services/reservationService', () => ({ listReservations: (
vi.mock('../../../src/services/fileService', () => ({ listFiles: () => [] }));
import { TripsService } from '../../../src/nest/trips/trips.service';
import type { TodoService } from '../../../src/nest/todo/todo.service';
import { rebaseTripCurrency } from '../../../src/services/budgetService';
function svc() { return new TripsService(new DatabaseService(dbConn)); }
const todoStub = { listItems: () => [] } as unknown as TodoService;
function svc() { return new TripsService(new DatabaseService(dbConn), todoStub); }
beforeEach(() => vi.clearAllMocks());
describe('TripsService (wrapper delegation + bundle/copy/notify helpers)', () => {
@@ -144,7 +144,7 @@ vi.mock('../../../src/nest/tags/tags.bridge', () => ({
updateTag: vi.fn((tagId: number, name?: string) => ({ id: Number(tagId), name })),
deleteTag: vi.fn(),
}));
vi.mock('../../../src/services/todoService', () => ({
vi.mock('../../../src/nest/todo/todo.bridge', () => ({
listItems: vi.fn((tid: number) => [{ id: 1, trip_id: Number(tid), name: 'Pack' }]),
createItem: vi.fn((tid: number, data: { name: string }) => ({ id: 90, trip_id: Number(tid), name: data.name })),
updateItem: vi.fn((_tid: number, id: string) => (Number(id) === 404 ? null : { id: Number(id), name: 'Done' })),