mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(roadtrip): add daily scheduling and configurable day endings
This commit is contained in:
@@ -83,6 +83,31 @@ function setupAssignmentFixtures(userId: number) {
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('Create assignment', () => {
|
||||
it('sets a day end for one visit, preserves its times, and clears it again', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { trip, day, place } = setupAssignmentFixtures(user.id);
|
||||
const created = await request(app).post(`/api/trips/${trip.id}/days/${day.id}/assignments`)
|
||||
.set('Cookie', authCookie(user.id)).send({ place_id: place.id });
|
||||
const id = created.body.assignment.id;
|
||||
const url = `/api/trips/${trip.id}/assignments/${id}/end-day`;
|
||||
testDb.prepare('UPDATE day_assignments SET assignment_time = ? WHERE id = ?').run('07:00', id);
|
||||
const changed = await request(app).put(url).set('Cookie', authCookie(user.id)).send({ end_day: true }).expect(200);
|
||||
expect(changed.body.assignment).toMatchObject({ end_day: true, assignment_time: '07:00' });
|
||||
const listed = await request(app).get(`/api/trips/${trip.id}/days`).set('Cookie', authCookie(user.id)).expect(200);
|
||||
expect(listed.body.days[0].assignments[0].end_day).toBe(true);
|
||||
const repeatedDay = createDay(testDb, trip.id, { day_number: 2 });
|
||||
const repeated = await request(app).post(`/api/trips/${trip.id}/days/${repeatedDay.id}/assignments`)
|
||||
.set('Cookie', authCookie(user.id)).send({ place_id: place.id }).expect(201);
|
||||
expect(repeated.body.assignment.end_day).toBe(false);
|
||||
await request(app).put(url).set('Cookie', authCookie(user.id)).send({ end_day: 'true' }).expect(400);
|
||||
const foreign = createTrip(testDb, user.id);
|
||||
await request(app).put(`/api/trips/${foreign.id}/assignments/${id}/end-day`).set('Cookie', authCookie(user.id))
|
||||
.send({ end_day: true }).expect(404);
|
||||
const cleared = await request(app).put(url).set('Cookie', authCookie(user.id)).send({ end_day: false }).expect(200);
|
||||
expect(cleared.body.assignment).toMatchObject({ end_day: false, assignment_time: '07:00' });
|
||||
await request(app).put(url).send({ end_day: true }).expect(401);
|
||||
});
|
||||
|
||||
it('ASSIGN-001 — POST creates assignment linking place to day', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const { trip, day, place } = setupAssignmentFixtures(user.id);
|
||||
|
||||
@@ -72,6 +72,23 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('Tool: assign_place_to_day', () => {
|
||||
it('sets and clears a visit day end through the shared service', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
const day = createDay(testDb, trip.id);
|
||||
const place = createPlace(testDb, trip.id);
|
||||
const visit = createDayAssignment(testDb, day.id, place.id);
|
||||
await withHarness(user.id, async h => {
|
||||
for (const end_day of [true, false]) {
|
||||
const changed = await h.client.callTool({ name: 'set_assignment_end_day', arguments: { tripId: trip.id, assignmentId: visit.id, end_day } });
|
||||
expect(parseToolResult(changed)).toMatchObject({ assignment: { end_day } });
|
||||
}
|
||||
const other = createTrip(testDb, user.id);
|
||||
const refused = await h.client.callTool({ name: 'set_assignment_end_day', arguments: { tripId: other.id, assignmentId: visit.id, end_day: true } });
|
||||
expect(refused.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('assigns a place to a day', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
const trip = createTrip(testDb, user.id);
|
||||
|
||||
@@ -67,6 +67,18 @@ describe('DayAssignmentsController (parity with the legacy day-assignments route
|
||||
});
|
||||
|
||||
describe('AssignmentOpsController (parity with the per-assignment op routes)', () => {
|
||||
it('scopes explicit day ends to the trip and broadcasts the saved visit', () => {
|
||||
const setEndDay = vi.fn().mockReturnValue({ id: 9, end_day: true });
|
||||
const s = svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ id: 9 }), setEndDay });
|
||||
const controller = new AssignmentOpsController(s);
|
||||
expect(controller.endDay('5', '9', { end_day: true }, 'sock')).toEqual({ assignment: { id: 9, end_day: true } });
|
||||
expect(s.getAssignmentForTrip).toHaveBeenCalledWith('9', '5');
|
||||
expect(setEndDay).toHaveBeenCalledWith('9', true);
|
||||
expect(s.broadcast).toHaveBeenCalledWith('5', 'assignment:updated', { assignment: { id: 9, end_day: true } }, 'sock');
|
||||
vi.mocked(s.getAssignmentForTrip).mockReturnValue(undefined);
|
||||
expect(thrown(() => controller.endDay('5', '9', { end_day: false }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
});
|
||||
|
||||
it('PUT /:id/move 404 assignment, 404 target day, else moves', () => {
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue(undefined) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Assignment not found' } });
|
||||
expect(thrown(() => new AssignmentOpsController(svc({ getAssignmentForTrip: vi.fn().mockReturnValue({ day_id: 3 }), dayExists: vi.fn().mockReturnValue(false) } as Partial<AssignmentsService>)).move(user, '5', '9', { new_day_id: 4 }))).toEqual({ status: 404, body: { error: 'Target day not found' } });
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { afterEach, expect, it, vi } from 'vitest';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
import { DayBoundariesService } from '../../../src/nest/roadtrip/day-boundaries.service';
|
||||
import { DayBoundariesController } from '../../../src/nest/roadtrip/day-boundaries.controller';
|
||||
import { DayBoundariesMcp } from '../../../src/nest/roadtrip/day-boundaries.mcp';
|
||||
import type { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import type { McpContext } from '../../../src/nest-mcp';
|
||||
|
||||
const open: Database.Database[] = [];
|
||||
afterEach(() => open.splice(0).forEach(db => db.close()));
|
||||
function setup() {
|
||||
const raw = new Database(':memory:');
|
||||
open.push(raw);
|
||||
raw.pragma('foreign_keys = ON');
|
||||
raw.exec(`CREATE TABLE days (id INTEGER PRIMARY KEY, trip_id INTEGER);
|
||||
CREATE TABLE day_assignments (id INTEGER PRIMARY KEY, day_id INTEGER REFERENCES days(id) ON DELETE CASCADE);
|
||||
CREATE TABLE roadtrip_day_boundaries (trip_id INTEGER, day_number INTEGER, from_assignment_id INTEGER REFERENCES day_assignments(id) ON DELETE CASCADE,
|
||||
to_assignment_id INTEGER REFERENCES day_assignments(id) ON DELETE CASCADE, fraction REAL, PRIMARY KEY(trip_id, day_number));
|
||||
INSERT INTO days VALUES (1, 10), (2, 20);
|
||||
INSERT INTO day_assignments VALUES (11, 1), (12, 1), (21, 2);`);
|
||||
const db = {
|
||||
get: (sql: string, ...params: unknown[]) => raw.prepare(sql).get(...params),
|
||||
all: (sql: string, ...params: unknown[]) => raw.prepare(sql).all(...params),
|
||||
run: (sql: string, ...params: unknown[]) => raw.prepare(sql).run(...params),
|
||||
canAccessTrip: vi.fn(() => true),
|
||||
};
|
||||
const service = new DayBoundariesService(db as unknown as DatabaseService);
|
||||
const realtime = { broadcast: vi.fn() };
|
||||
const auth = { isDemoUser: vi.fn(() => false) };
|
||||
const guards = { hasTripPermission: vi.fn(() => true) };
|
||||
const mcp = new DayBoundariesMcp(service, db as unknown as DatabaseService, auth as never, guards as never, realtime as never, {} as never);
|
||||
return { raw, service, controller: new DayBoundariesController(service, realtime as never), mcp, db, auth, guards, realtime };
|
||||
}
|
||||
const boundary = { day_number: 1, from_assignment_id: 11, to_assignment_id: 12, fraction: 0.4 };
|
||||
|
||||
it('replaces one day only, persists place snapping and cascades deleted visits', () => {
|
||||
const { service, raw } = setup();
|
||||
service.save(10, boundary);
|
||||
service.save(10, { ...boundary, day_number: 2, fraction: 0.8 });
|
||||
expect(service.save(10, { ...boundary, to_assignment_id: null })).toEqual([
|
||||
{ ...boundary, to_assignment_id: null, fraction: 1 }, { ...boundary, day_number: 2, fraction: 0.8 },
|
||||
]);
|
||||
expect(service.list(20)).toEqual([]);
|
||||
service.remove(20, 1);
|
||||
expect(service.list(10)).toHaveLength(2);
|
||||
raw.prepare('DELETE FROM day_assignments WHERE id = 12').run();
|
||||
expect(service.list(10)).toEqual([{ ...boundary, to_assignment_id: null, fraction: 1 }]);
|
||||
});
|
||||
|
||||
it('rejects either endpoint from a different trip without writing', () => {
|
||||
const { service } = setup();
|
||||
expect(() => service.save(10, { ...boundary, from_assignment_id: 21 })).toThrow(HttpException);
|
||||
expect(() => service.save(10, { ...boundary, to_assignment_id: 21 })).toThrow(HttpException);
|
||||
expect(service.list(10)).toEqual([]);
|
||||
});
|
||||
|
||||
it('REST changes broadcast the complete state and exclude the saving socket', () => {
|
||||
const { controller, realtime } = setup();
|
||||
controller.save('10', boundary, 'self');
|
||||
expect(realtime.broadcast).toHaveBeenCalledWith('10', 'roadtripBoundary:changed', { boundaries: [boundary] }, 'self');
|
||||
expect(controller.remove('10', 1)).toEqual({ boundaries: [] });
|
||||
});
|
||||
|
||||
it('MCP checks demo, trip access and edit permission before saving', async () => {
|
||||
const s = setup(), ctx = { userId: 1 } as McpContext;
|
||||
const request = { tripId: 10, dayNumber: 1, boundary };
|
||||
s.auth.isDemoUser.mockReturnValue(true);
|
||||
await s.mcp.save(request, ctx);
|
||||
s.auth.isDemoUser.mockReturnValue(false);
|
||||
s.db.canAccessTrip.mockReturnValue(false);
|
||||
await s.mcp.save(request, ctx);
|
||||
s.db.canAccessTrip.mockReturnValue(true);
|
||||
s.guards.hasTripPermission.mockReturnValue(false);
|
||||
await s.mcp.save(request, ctx);
|
||||
expect(s.service.list(10)).toEqual([]);
|
||||
s.guards.hasTripPermission.mockReturnValue(true);
|
||||
await s.mcp.save(request, ctx);
|
||||
expect(s.service.list(10)).toEqual([boundary]);
|
||||
await s.mcp.save({ ...request, boundary: null }, ctx);
|
||||
expect(s.service.list(10)).toEqual([]);
|
||||
});
|
||||
Reference in New Issue
Block a user