mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
chore: move todo backend to proper DI
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
/**
|
||||
* To-do module e2e — exercises the migrated /api/trips/:tripId/todo endpoints
|
||||
* through the real JwtAuthGuard against a temp SQLite db. todoService, the
|
||||
* permission check and the WebSocket broadcast are mocked.
|
||||
* through the real JwtAuthGuard against a temp SQLite db. TodoService runs its
|
||||
* real SQL via DatabaseModule (the DATABASE_CONNECTION factory picks up the
|
||||
* mocked db singleton); trip access resolves through a real-SQL canAccessTrip
|
||||
* over the temp db. Only the permission check and the WebSocket broadcast stay
|
||||
* mocked.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
|
||||
import request from 'supertest';
|
||||
@@ -16,33 +19,71 @@ const { db } = vi.hoisted(() => {
|
||||
const tmp = new Database(':memory:');
|
||||
tmp.exec('PRAGMA journal_mode = WAL');
|
||||
tmp.exec(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,
|
||||
email TEXT NOT NULL UNIQUE, role TEXT NOT NULL DEFAULT 'user', password_version INTEGER NOT NULL DEFAULT 0);`);
|
||||
email TEXT NOT NULL UNIQUE, role TEXT NOT NULL DEFAULT 'user', password_version INTEGER NOT NULL DEFAULT 0,
|
||||
avatar TEXT);`);
|
||||
tmp.exec('CREATE TABLE trips (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, title TEXT);');
|
||||
tmp.exec('CREATE TABLE trip_members (trip_id INTEGER NOT NULL, user_id INTEGER NOT NULL);');
|
||||
tmp.exec(`CREATE TABLE todo_items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
trip_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
checked INTEGER NOT NULL DEFAULT 0,
|
||||
category TEXT,
|
||||
sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
due_date TEXT,
|
||||
description TEXT,
|
||||
assigned_user_id INTEGER,
|
||||
priority INTEGER NOT NULL DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);`);
|
||||
tmp.exec(`CREATE TABLE todo_category_assignees (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
trip_id INTEGER NOT NULL,
|
||||
category_name TEXT NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
UNIQUE(trip_id, category_name, user_id)
|
||||
);`);
|
||||
return { db: tmp };
|
||||
});
|
||||
|
||||
vi.mock('../../src/db/database', () => ({ db, closeDb: () => {}, reinitialize: () => {} }));
|
||||
vi.mock('../../src/db/database', () => ({
|
||||
db,
|
||||
// Real-SQL trip access over the temp db — TodoService.verifyTripAccess and
|
||||
// DatabaseModule both read the mocked singleton.
|
||||
canAccessTrip: (tripId: number | string, userId: number) =>
|
||||
db.prepare(`
|
||||
SELECT t.id, t.user_id FROM trips t
|
||||
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ?
|
||||
WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)
|
||||
`).get(userId, tripId, userId),
|
||||
isOwner: () => false,
|
||||
getPlaceWithTags: () => null,
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
}));
|
||||
vi.mock('../../src/websocket', () => ({ broadcast: vi.fn() }));
|
||||
|
||||
const { checkPermission } = vi.hoisted(() => ({ checkPermission: vi.fn() }));
|
||||
vi.mock('../../src/services/permissions', () => ({ checkPermission }));
|
||||
|
||||
const { svc } = vi.hoisted(() => ({
|
||||
svc: {
|
||||
verifyTripAccess: vi.fn(), listItems: vi.fn(), createItem: vi.fn(), updateItem: vi.fn(),
|
||||
deleteItem: vi.fn(), reorderItems: vi.fn(), getCategoryAssignees: vi.fn(), updateCategoryAssignees: vi.fn(),
|
||||
},
|
||||
}));
|
||||
vi.mock('../../src/services/todoService', () => svc);
|
||||
|
||||
import { TodoModule } from '../../src/nest/todo/todo.module';
|
||||
import { DatabaseModule } from '../../src/nest/database/database.module';
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
|
||||
describe('To-do e2e (real auth guard + temp SQLite)', () => {
|
||||
function insertItem(tripId: number, name: string, extra: Partial<{ sort_order: number; due_date: string }> = {}): number {
|
||||
const res = db
|
||||
.prepare('INSERT INTO todo_items (trip_id, name, sort_order, due_date) VALUES (?, ?, ?, ?)')
|
||||
.run(tripId, name, extra.sort_order ?? 0, extra.due_date ?? null);
|
||||
return Number(res.lastInsertRowid);
|
||||
}
|
||||
|
||||
describe('To-do e2e (real auth guard + real SQL over temp SQLite)', () => {
|
||||
let server: Server;
|
||||
let app: Awaited<ReturnType<typeof build>>;
|
||||
let tripId: number;
|
||||
|
||||
async function build() {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [TodoModule] }).compile();
|
||||
const moduleRef = await Test.createTestingModule({ imports: [DatabaseModule, TodoModule] }).compile();
|
||||
const nest = moduleRef.createNestApplication();
|
||||
nest.use(cookieParser());
|
||||
nest.useGlobalFilters(new TrekExceptionFilter());
|
||||
@@ -52,14 +93,17 @@ describe('To-do e2e (real auth guard + temp SQLite)', () => {
|
||||
|
||||
beforeAll(async () => {
|
||||
seedUser(db as never, { id: 1 });
|
||||
seedUser(db as never, { id: 2, email: 'stranger@example.test' });
|
||||
app = await build();
|
||||
server = app.getHttpServer();
|
||||
svc.listItems.mockReturnValue([{ id: 1, name: 'Book hotel' }]);
|
||||
svc.createItem.mockReturnValue({ id: 9, name: 'Book hotel' });
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
svc.verifyTripAccess.mockReturnValue({ id: 5, user_id: 1 });
|
||||
db.exec('DELETE FROM todo_category_assignees');
|
||||
db.exec('DELETE FROM todo_items');
|
||||
db.exec('DELETE FROM trip_members');
|
||||
db.exec('DELETE FROM trips');
|
||||
tripId = Number(db.prepare('INSERT INTO trips (user_id, title) VALUES (1, ?)').run('Trip').lastInsertRowid);
|
||||
checkPermission.mockReturnValue(true);
|
||||
});
|
||||
|
||||
@@ -68,33 +112,98 @@ describe('To-do e2e (real auth guard + temp SQLite)', () => {
|
||||
});
|
||||
|
||||
it('401 without a session cookie', async () => {
|
||||
const res = await request(server).get('/api/trips/5/todo');
|
||||
const res = await request(server).get(`/api/trips/${tripId}/todo`);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('200 list for an accessible trip', async () => {
|
||||
const res = await request(server).get('/api/trips/5/todo').set('Cookie', sessionCookie(1));
|
||||
it('200 list ordered by sort_order', async () => {
|
||||
insertItem(tripId, 'Second', { sort_order: 1 });
|
||||
insertItem(tripId, 'First', { sort_order: 0 });
|
||||
const res = await request(server).get(`/api/trips/${tripId}/todo`).set('Cookie', sessionCookie(1));
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual({ items: [{ id: 1, name: 'Book hotel' }] });
|
||||
expect(res.body.items.map((i: { name: string }) => i.name)).toEqual(['First', 'Second']);
|
||||
});
|
||||
|
||||
it('404 when the trip is not accessible', async () => {
|
||||
svc.verifyTripAccess.mockReturnValue(undefined);
|
||||
const res = await request(server).get('/api/trips/5/todo').set('Cookie', sessionCookie(1));
|
||||
const res = await request(server).get(`/api/trips/${tripId}/todo`).set('Cookie', sessionCookie(2));
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Trip not found' });
|
||||
});
|
||||
|
||||
it('201 on create with permission', async () => {
|
||||
const res = await request(server).post('/api/trips/5/todo').set('Cookie', sessionCookie(1)).send({ name: 'Book hotel' });
|
||||
it('201 on create, inserting the row with the legacy defaults and incrementing sort_order', async () => {
|
||||
insertItem(tripId, 'Existing', { sort_order: 4 });
|
||||
const res = await request(server).post(`/api/trips/${tripId}/todo`).set('Cookie', sessionCookie(1)).send({ name: 'Book hotel' });
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body).toEqual({ item: { id: 9, name: 'Book hotel' } });
|
||||
expect(res.body.item).toMatchObject({ name: 'Book hotel', checked: 0, priority: 0, sort_order: 5 });
|
||||
expect(db.prepare('SELECT name FROM todo_items WHERE id = ?').get(res.body.item.id)).toEqual({ name: 'Book hotel' });
|
||||
});
|
||||
|
||||
it('403 on create without permission', async () => {
|
||||
it('400 on create without a name', async () => {
|
||||
const res = await request(server).post(`/api/trips/${tripId}/todo`).set('Cookie', sessionCookie(1)).send({});
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body).toEqual({ error: 'Item name is required' });
|
||||
});
|
||||
|
||||
it('403 on create without permission, writing nothing', async () => {
|
||||
checkPermission.mockReturnValue(false);
|
||||
const res = await request(server).post('/api/trips/5/todo').set('Cookie', sessionCookie(1)).send({ name: 'Book hotel' });
|
||||
const res = await request(server).post(`/api/trips/${tripId}/todo`).set('Cookie', sessionCookie(1)).send({ name: 'X' });
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body).toEqual({ error: 'No permission' });
|
||||
expect((db.prepare('SELECT COUNT(*) AS n FROM todo_items').get() as { n: number }).n).toBe(0);
|
||||
});
|
||||
|
||||
it('200 on update; a body key with null clears the field, omitted keys stay', async () => {
|
||||
const id = insertItem(tripId, 'Task', { due_date: '2026-06-01' });
|
||||
const renamed = await request(server).put(`/api/trips/${tripId}/todo/${id}`).set('Cookie', sessionCookie(1)).send({ name: 'Renamed' });
|
||||
expect(renamed.status).toBe(200);
|
||||
expect(renamed.body.item).toMatchObject({ id, name: 'Renamed', due_date: '2026-06-01' });
|
||||
const cleared = await request(server).put(`/api/trips/${tripId}/todo/${id}`).set('Cookie', sessionCookie(1)).send({ due_date: null });
|
||||
expect(cleared.status).toBe(200);
|
||||
expect(cleared.body.item.due_date).toBeNull();
|
||||
});
|
||||
|
||||
it('404 on update of a missing item', async () => {
|
||||
const res = await request(server).put(`/api/trips/${tripId}/todo/9999`).set('Cookie', sessionCookie(1)).send({ name: 'X' });
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body).toEqual({ error: 'Item not found' });
|
||||
});
|
||||
|
||||
it('200 on reorder, persisting the new sort_order', async () => {
|
||||
const a = insertItem(tripId, 'A', { sort_order: 0 });
|
||||
const b = insertItem(tripId, 'B', { sort_order: 1 });
|
||||
const res = await request(server).put(`/api/trips/${tripId}/todo/reorder`).set('Cookie', sessionCookie(1)).send({ orderedIds: [b, a] });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toEqual({ success: true });
|
||||
const rows = db.prepare('SELECT id FROM todo_items WHERE trip_id = ? ORDER BY sort_order').all(tripId) as { id: number }[];
|
||||
expect(rows.map((r) => r.id)).toEqual([b, a]);
|
||||
});
|
||||
|
||||
it('200 on delete, removing the row; 404 when already gone', async () => {
|
||||
const id = insertItem(tripId, 'Gone');
|
||||
const ok = await request(server).delete(`/api/trips/${tripId}/todo/${id}`).set('Cookie', sessionCookie(1));
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.body).toEqual({ success: true });
|
||||
expect(db.prepare('SELECT id FROM todo_items WHERE id = ?').get(id)).toBeUndefined();
|
||||
const missing = await request(server).delete(`/api/trips/${tripId}/todo/${id}`).set('Cookie', sessionCookie(1));
|
||||
expect(missing.status).toBe(404);
|
||||
expect(missing.body).toEqual({ error: 'Item not found' });
|
||||
});
|
||||
|
||||
it('category assignees round-trip: PUT replaces, GET groups by category', async () => {
|
||||
db.prepare('INSERT INTO trip_members (trip_id, user_id) VALUES (?, ?)').run(tripId, 2);
|
||||
const put = await request(server)
|
||||
.put(`/api/trips/${tripId}/todo/category-assignees/Booking`)
|
||||
.set('Cookie', sessionCookie(1))
|
||||
.send({ user_ids: [1, 2] });
|
||||
expect(put.status).toBe(200);
|
||||
expect(put.body.assignees).toHaveLength(2);
|
||||
const get = await request(server).get(`/api/trips/${tripId}/todo/category-assignees`).set('Cookie', sessionCookie(1));
|
||||
expect(get.status).toBe(200);
|
||||
expect(get.body.assignees.Booking).toHaveLength(2);
|
||||
const replaced = await request(server)
|
||||
.put(`/api/trips/${tripId}/todo/category-assignees/Booking`)
|
||||
.set('Cookie', sessionCookie(1))
|
||||
.send({ user_ids: [2] });
|
||||
expect(replaced.body.assignees).toEqual([{ user_id: 2, username: 'e2e-user', avatar: null }]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,6 +20,11 @@ const { db } = vi.hoisted(() => {
|
||||
tmp.exec(`CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL,
|
||||
email TEXT NOT NULL UNIQUE, role TEXT NOT NULL DEFAULT 'user', password_version INTEGER NOT NULL DEFAULT 0);`);
|
||||
tmp.exec('CREATE TABLE trips (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT);');
|
||||
// bundle()'s todoItems now runs TodoService's real SQL (DI-injected, no mock).
|
||||
tmp.exec(`CREATE TABLE todo_items (id INTEGER PRIMARY KEY AUTOINCREMENT, trip_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL, checked INTEGER NOT NULL DEFAULT 0, category TEXT, sort_order INTEGER NOT NULL DEFAULT 0,
|
||||
due_date TEXT, description TEXT, assigned_user_id INTEGER, priority INTEGER NOT NULL DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP);`);
|
||||
return { db: tmp };
|
||||
});
|
||||
|
||||
@@ -47,7 +52,6 @@ vi.mock('../../src/services/tripService', () => tripSvc);
|
||||
vi.mock('../../src/services/dayService', () => ({ listDays: () => ({ days: [] }), 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: () => [] }));
|
||||
vi.mock('../../src/services/reservationService', () => ({ listReservations: () => [] }));
|
||||
vi.mock('../../src/services/fileService', () => ({ listFiles: () => [] }));
|
||||
|
||||
@@ -6,6 +6,8 @@ import { CategoriesService } from '../../src/nest/categories/categories.service'
|
||||
import { DatabaseService } from '../../src/nest/database/database.service';
|
||||
import { TagsMcp } from '../../src/nest/tags/tags.mcp';
|
||||
import { TagsService } from '../../src/nest/tags/tags.service';
|
||||
import { TodoMcp } from '../../src/nest/todo/todo.mcp';
|
||||
import { TodoService } from '../../src/nest/todo/todo.service';
|
||||
|
||||
/**
|
||||
* Hand-wired counterpart of the boot-time discovery in McpRegistryService,
|
||||
@@ -20,6 +22,7 @@ export function createMcpTestRegistry(): McpRegistry {
|
||||
[
|
||||
new TagsMcp(new TagsService(dbService)),
|
||||
new CategoriesMcp(new CategoriesService(dbService)),
|
||||
new TodoMcp(new TodoService(dbService)),
|
||||
],
|
||||
{ accessPolicy: trekMcpAccessPolicy },
|
||||
);
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+92
-53
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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' })),
|
||||
|
||||
Reference in New Issue
Block a user