mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
test(server): seed the service, MCP and route specs through the ORM factories
Service, MCP, integration and e2e suites with up to a dozen raw statements now seed and read through the factories. A buildApp() suite uses the app's own ORM, a partial-module e2e or a unit suite a createTestOrm() or sharedTestOrm() handle on its database. Reads that used to come back undefined now come back null, and the assertions say so; where a projected SELECT was compared whole, the same columns are picked off the row. Two reads inside the scheduler's delivery callback stay synchronous raw statements, marked, so they keep seeing the row at the moment of delivery. Two more database mocks lose a canAccessTrip/isOwner stand-in the module no longer exports.
This commit is contained in:
@@ -22,6 +22,10 @@ vi.mock('../../../src/websocket', () => ({ broadcast: broadcastMock }));
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createUser, createTrip, createDay, createPlace, createDayAssignment } from '../../helpers/factories';
|
||||
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { findRow, updateRows } from '../../helpers/factories/rows';
|
||||
import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity';
|
||||
import { Days } from '../../../src/db/entities/Days.entity';
|
||||
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
@@ -29,7 +33,14 @@ beforeEach(() => {
|
||||
delete process.env.DEMO_MODE;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
let orm: TestOrm;
|
||||
|
||||
beforeAll(async () => {
|
||||
orm = await createTestOrm(testDb);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await orm.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
@@ -52,7 +63,7 @@ describe('Tool: set_assignment_route_excluded (#2532)', () => {
|
||||
await withHarness(user.id, async (h) => {
|
||||
const out = parseToolResult(await h.client.callTool({ name: 'set_assignment_route_excluded', arguments: { tripId: trip.id, assignmentId: assignment.id, excluded: true } })) as any;
|
||||
expect(out.assignment.route_excluded).toBe(true);
|
||||
expect(testDb.prepare('SELECT route_excluded FROM day_assignments WHERE id = ?').get(assignment.id)).toEqual({ route_excluded: 1 });
|
||||
expect((await findRow(orm, DayAssignments, { id: assignment.id }))?.route_excluded).toBe(1);
|
||||
const back = parseToolResult(await h.client.callTool({ name: 'set_assignment_route_excluded', arguments: { tripId: trip.id, assignmentId: assignment.id, excluded: false } })) as any;
|
||||
expect(back.assignment.route_excluded).toBe(false);
|
||||
expect(broadcastMock).toHaveBeenCalledWith(trip.id, 'assignment:updated', expect.any(Object));
|
||||
@@ -82,7 +93,7 @@ describe('Tool: set_leg_transport_mode', () => {
|
||||
const assignment = createDayAssignment(testDb, day.id, place.id);
|
||||
// Seed the incoming column so the assertion below proves it survives; the factory
|
||||
// leaves it NULL, so without this it would pass whether or not the tool touched it.
|
||||
testDb.prepare('UPDATE day_assignments SET incoming_leg_transport_mode = ? WHERE id = ?').run('walking', assignment.id);
|
||||
await updateRows(orm, DayAssignments, { id: assignment.id }, { incoming_leg_transport_mode: 'walking' });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
@@ -103,7 +114,7 @@ describe('Tool: set_leg_transport_mode', () => {
|
||||
const place = createPlace(testDb, trip.id);
|
||||
const assignment = createDayAssignment(testDb, day.id, place.id);
|
||||
// Seed the outgoing column so we prove incoming does NOT touch it (panel item 4).
|
||||
testDb.prepare('UPDATE day_assignments SET leg_transport_mode = ? WHERE id = ?').run('driving', assignment.id);
|
||||
await updateRows(orm, DayAssignments, { id: assignment.id }, { leg_transport_mode: 'driving' });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
@@ -139,7 +150,7 @@ describe('Tool: set_leg_transport_mode', () => {
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id);
|
||||
const assignment = createDayAssignment(testDb, day.id, place.id);
|
||||
testDb.prepare('UPDATE day_assignments SET leg_transport_mode = ? WHERE id = ?').run('cycling', assignment.id);
|
||||
await updateRows(orm, DayAssignments, { id: assignment.id }, { leg_transport_mode: 'cycling' });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
@@ -221,7 +232,7 @@ describe('Tool: set_day_default_transport_mode', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
testDb.prepare('UPDATE days SET default_transport_mode = ? WHERE id = ?').run('driving', day.id);
|
||||
await updateRows(orm, Days, { id: day.id }, { default_transport_mode: 'driving' });
|
||||
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
|
||||
@@ -29,6 +29,12 @@ import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createUser, createTrip, createTodoItem } from '../../helpers/factories';
|
||||
import { createMcpHarness, parseToolResult, parseResourceResult, type McpHarness } from '../../helpers/mcp-harness';
|
||||
import { ADDON_IDS } from '../../../src/addons';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { findRow, insertRow } from '../../helpers/factories/rows';
|
||||
import { makeTodoItem } from '../../helpers/factories/todos';
|
||||
import { setAddonEnabled } from '../../helpers/factories/settings';
|
||||
import { TodoItems } from '../../../src/db/entities/TodoItems.entity';
|
||||
import { TodoCategoryAssignees } from '../../../src/db/entities/TodoCategoryAssignees.entity';
|
||||
|
||||
beforeEach(() => {
|
||||
resetTestDb(testDb);
|
||||
@@ -36,7 +42,14 @@ beforeEach(() => {
|
||||
delete process.env.DEMO_MODE;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
let orm: TestOrm;
|
||||
|
||||
beforeAll(async () => {
|
||||
orm = await createTestOrm(testDb);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await orm.close();
|
||||
testDb.close();
|
||||
});
|
||||
|
||||
@@ -180,8 +193,7 @@ describe('Tool: update_todo', () => {
|
||||
it('clears due_date when passed null', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
testDb.prepare("INSERT INTO todo_items (trip_id, name, checked, sort_order, due_date) VALUES (?, 'Task', 0, 0, '2025-01-01')").run(trip.id);
|
||||
const item = testDb.prepare('SELECT * FROM todo_items WHERE trip_id = ? ORDER BY id DESC LIMIT 1').get(trip.id) as any;
|
||||
const item = await makeTodoItem(orm, trip.id, { name: 'Task', checked: 0, sort_order: 0, due_date: '2025-01-01' });
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'update_todo',
|
||||
@@ -289,7 +301,7 @@ describe('Tool: delete_todo', () => {
|
||||
const result = await h.client.callTool({ name: 'delete_todo', arguments: { tripId: trip.id, itemId: item.id } });
|
||||
const data = parseToolResult(result) as any;
|
||||
expect(data.success).toBe(true);
|
||||
expect(testDb.prepare('SELECT id FROM todo_items WHERE id = ?').get(item.id)).toBeUndefined();
|
||||
expect(await findRow(orm, TodoItems, { id: item.id })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -342,8 +354,8 @@ describe('Tool: reorder_todos', () => {
|
||||
const data = parseToolResult(result) as any;
|
||||
expect(data.success).toBe(true);
|
||||
// item2 should now have sort_order 0
|
||||
const updated = testDb.prepare('SELECT sort_order FROM todo_items WHERE id = ?').get(item2.id) as any;
|
||||
expect(updated.sort_order).toBe(0);
|
||||
const updated = await findRow(orm, TodoItems, { id: item2.id });
|
||||
expect(updated?.sort_order).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -397,7 +409,7 @@ describe('Tool: set_todo_category_assignees', () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
// Set then clear
|
||||
testDb.prepare('INSERT INTO todo_category_assignees (trip_id, category_name, user_id) VALUES (?, ?, ?)').run(trip.id, 'Booking', user.id);
|
||||
await insertRow(orm, TodoCategoryAssignees, { trip: trip.id, category_name: 'Booking', user: user.id });
|
||||
await withHarness(user.id, async (h) => {
|
||||
const result = await h.client.callTool({
|
||||
name: 'set_todo_category_assignees',
|
||||
@@ -468,7 +480,7 @@ 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);
|
||||
await setAddonEnabled(orm, ADDON_IDS.PACKING, false);
|
||||
try {
|
||||
await withHarness(user.id, async (h) => {
|
||||
const names = (await h.client.listTools()).tools.map((t) => t.name);
|
||||
@@ -477,7 +489,7 @@ describe('Todo tools — packing addon gating', () => {
|
||||
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);
|
||||
await setAddonEnabled(orm, ADDON_IDS.PACKING, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user