Files
lunarfront-app/packages/backend/api-tests/suites/lessons.ts
Ryan Moon f777ce5184 Add lessons Phase 2: schedule slots with conflict detection
Recurring weekly time slots linking instructors to lesson types.
Includes day/time overlap detection, instructor and day-of-week
filtering, and 17 new integration tests (40 total lessons tests).
2026-03-30 09:20:03 -05:00

488 lines
20 KiB
TypeScript

import { suite } from '../lib/context.js'
suite('Lessons', { tags: ['lessons'] }, (t) => {
// ─── Instructors: CRUD ───
t.test('creates an instructor', { tags: ['instructors', 'create'] }, async () => {
const res = await t.api.post('/v1/instructors', {
displayName: 'Sarah Mitchell',
bio: 'Piano and voice instructor with 10 years experience',
instruments: ['Piano', 'Voice'],
})
t.assert.status(res, 201)
t.assert.equal(res.data.displayName, 'Sarah Mitchell')
t.assert.ok(res.data.id)
t.assert.equal(res.data.isActive, true)
t.assert.equal(res.data.instruments.length, 2)
t.assert.equal(res.data.instruments[0], 'Piano')
})
t.test('creates an instructor with minimal fields', { tags: ['instructors', 'create'] }, async () => {
const res = await t.api.post('/v1/instructors', {
displayName: 'John Doe',
})
t.assert.status(res, 201)
t.assert.equal(res.data.displayName, 'John Doe')
t.assert.equal(res.data.bio, null)
t.assert.equal(res.data.instruments, null)
})
t.test('rejects instructor creation without display name', { tags: ['instructors', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/instructors', {})
t.assert.status(res, 400)
})
t.test('gets instructor by id', { tags: ['instructors', 'read'] }, async () => {
const created = await t.api.post('/v1/instructors', { displayName: 'Get By ID Instructor' })
const res = await t.api.get(`/v1/instructors/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.displayName, 'Get By ID Instructor')
})
t.test('returns 404 for missing instructor', { tags: ['instructors', 'read'] }, async () => {
const res = await t.api.get('/v1/instructors/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('updates an instructor', { tags: ['instructors', 'update'] }, async () => {
const created = await t.api.post('/v1/instructors', { displayName: 'Before Update', bio: 'Old bio' })
const res = await t.api.patch(`/v1/instructors/${created.data.id}`, {
displayName: 'After Update',
bio: 'New bio',
instruments: ['Guitar', 'Bass'],
})
t.assert.status(res, 200)
t.assert.equal(res.data.displayName, 'After Update')
t.assert.equal(res.data.bio, 'New bio')
t.assert.equal(res.data.instruments.length, 2)
})
t.test('soft-deletes an instructor', { tags: ['instructors', 'delete'] }, async () => {
const created = await t.api.post('/v1/instructors', { displayName: 'To Delete' })
const res = await t.api.del(`/v1/instructors/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.isActive, false)
})
// ─── Instructors: List, Search, Sort ───
t.test('lists instructors with pagination', { tags: ['instructors', 'read', 'pagination'] }, async () => {
await t.api.post('/v1/instructors', { displayName: 'List Test A' })
await t.api.post('/v1/instructors', { displayName: 'List Test B' })
const res = await t.api.get('/v1/instructors', { limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.length >= 2)
t.assert.ok(res.data.pagination.total >= 2)
})
t.test('searches instructors by display name', { tags: ['instructors', 'search'] }, async () => {
await t.api.post('/v1/instructors', { displayName: 'Searchable Piano Teacher' })
const res = await t.api.get('/v1/instructors', { q: 'Piano Teacher' })
t.assert.status(res, 200)
t.assert.ok(res.data.data.some((i: any) => i.displayName === 'Searchable Piano Teacher'))
})
t.test('sorts instructors by display name descending', { tags: ['instructors', 'sort'] }, async () => {
await t.api.post('/v1/instructors', { displayName: 'AAA First Instructor' })
await t.api.post('/v1/instructors', { displayName: 'ZZZ Last Instructor' })
const res = await t.api.get('/v1/instructors', { sort: 'display_name', order: 'desc', limit: 100 })
t.assert.status(res, 200)
const names = res.data.data.map((i: any) => i.displayName)
const zIdx = names.findIndex((n: string) => n.includes('ZZZ'))
const aIdx = names.findIndex((n: string) => n.includes('AAA'))
t.assert.ok(zIdx < aIdx, 'ZZZ should come before AAA in desc order')
})
t.test('deleted instructor does not appear in list', { tags: ['instructors', 'delete', 'list'] }, async () => {
const created = await t.api.post('/v1/instructors', { displayName: 'Ghost Instructor XYZ' })
await t.api.del(`/v1/instructors/${created.data.id}`)
const res = await t.api.get('/v1/instructors', { q: 'Ghost Instructor XYZ', limit: 100 })
t.assert.equal(res.data.data.length, 0)
})
// ─── Lesson Types: CRUD ───
t.test('creates a lesson type', { tags: ['lesson-types', 'create'] }, async () => {
const res = await t.api.post('/v1/lesson-types', {
name: '30-min Private Piano',
instrument: 'Piano',
durationMinutes: 30,
lessonFormat: 'private',
baseRateMonthly: 120,
})
t.assert.status(res, 201)
t.assert.equal(res.data.name, '30-min Private Piano')
t.assert.equal(res.data.instrument, 'Piano')
t.assert.equal(res.data.durationMinutes, 30)
t.assert.equal(res.data.lessonFormat, 'private')
t.assert.equal(res.data.baseRateMonthly, '120')
t.assert.ok(res.data.id)
})
t.test('creates a group lesson type', { tags: ['lesson-types', 'create'] }, async () => {
const res = await t.api.post('/v1/lesson-types', {
name: '60-min Group Guitar',
instrument: 'Guitar',
durationMinutes: 60,
lessonFormat: 'group',
baseRateMonthly: 80,
})
t.assert.status(res, 201)
t.assert.equal(res.data.lessonFormat, 'group')
})
t.test('rejects lesson type without required fields', { tags: ['lesson-types', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/lesson-types', {})
t.assert.status(res, 400)
})
t.test('rejects lesson type without duration', { tags: ['lesson-types', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/lesson-types', { name: 'No Duration' })
t.assert.status(res, 400)
})
t.test('gets lesson type by id', { tags: ['lesson-types', 'read'] }, async () => {
const created = await t.api.post('/v1/lesson-types', { name: 'Get By ID Type', durationMinutes: 45 })
const res = await t.api.get(`/v1/lesson-types/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.name, 'Get By ID Type')
})
t.test('returns 404 for missing lesson type', { tags: ['lesson-types', 'read'] }, async () => {
const res = await t.api.get('/v1/lesson-types/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('updates a lesson type', { tags: ['lesson-types', 'update'] }, async () => {
const created = await t.api.post('/v1/lesson-types', { name: 'Before Update Type', durationMinutes: 30 })
const res = await t.api.patch(`/v1/lesson-types/${created.data.id}`, {
name: 'After Update Type',
durationMinutes: 45,
baseRateMonthly: 150,
})
t.assert.status(res, 200)
t.assert.equal(res.data.name, 'After Update Type')
t.assert.equal(res.data.durationMinutes, 45)
t.assert.equal(res.data.baseRateMonthly, '150')
})
t.test('soft-deletes a lesson type', { tags: ['lesson-types', 'delete'] }, async () => {
const created = await t.api.post('/v1/lesson-types', { name: 'To Delete Type', durationMinutes: 30 })
const res = await t.api.del(`/v1/lesson-types/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.isActive, false)
})
// ─── Lesson Types: List, Search, Sort ───
t.test('lists lesson types with pagination', { tags: ['lesson-types', 'read', 'pagination'] }, async () => {
await t.api.post('/v1/lesson-types', { name: 'List Type A', durationMinutes: 30 })
await t.api.post('/v1/lesson-types', { name: 'List Type B', durationMinutes: 60 })
const res = await t.api.get('/v1/lesson-types', { limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.length >= 2)
t.assert.ok(res.data.pagination.total >= 2)
})
t.test('searches lesson types by name', { tags: ['lesson-types', 'search'] }, async () => {
await t.api.post('/v1/lesson-types', { name: 'Searchable Violin Lesson', instrument: 'Violin', durationMinutes: 30 })
const res = await t.api.get('/v1/lesson-types', { q: 'Violin' })
t.assert.status(res, 200)
t.assert.ok(res.data.data.some((lt: any) => lt.name.includes('Violin')))
})
t.test('sorts lesson types by name descending', { tags: ['lesson-types', 'sort'] }, async () => {
await t.api.post('/v1/lesson-types', { name: 'AAA First Type', durationMinutes: 30 })
await t.api.post('/v1/lesson-types', { name: 'ZZZ Last Type', durationMinutes: 30 })
const res = await t.api.get('/v1/lesson-types', { sort: 'name', order: 'desc', limit: 100 })
t.assert.status(res, 200)
const names = res.data.data.map((lt: any) => lt.name)
const zIdx = names.findIndex((n: string) => n.includes('ZZZ'))
const aIdx = names.findIndex((n: string) => n.includes('AAA'))
t.assert.ok(zIdx < aIdx, 'ZZZ should come before AAA in desc order')
})
t.test('deleted lesson type does not appear in list', { tags: ['lesson-types', 'delete', 'list'] }, async () => {
const created = await t.api.post('/v1/lesson-types', { name: 'Ghost Type XYZ', durationMinutes: 30 })
await t.api.del(`/v1/lesson-types/${created.data.id}`)
const res = await t.api.get('/v1/lesson-types', { q: 'Ghost Type XYZ', limit: 100 })
t.assert.equal(res.data.data.length, 0)
})
// ─── Schedule Slots: CRUD ───
t.test('creates a schedule slot', { tags: ['schedule-slots', 'create'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Slot Lesson', durationMinutes: 30 })
const res = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 2, // Tuesday
startTime: '16:00',
room: 'Room A',
maxStudents: 1,
})
t.assert.status(res, 201)
t.assert.equal(res.data.dayOfWeek, 2)
t.assert.equal(res.data.startTime, '16:00:00')
t.assert.equal(res.data.room, 'Room A')
t.assert.equal(res.data.maxStudents, 1)
t.assert.equal(res.data.isActive, true)
})
t.test('creates a group slot with higher max students', { tags: ['schedule-slots', 'create'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Group Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Group Lesson', durationMinutes: 60, lessonFormat: 'group' })
const res = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 4, // Thursday
startTime: '17:00',
room: 'Room B',
maxStudents: 6,
})
t.assert.status(res, 201)
t.assert.equal(res.data.maxStudents, 6)
})
t.test('rejects schedule slot without required fields', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/schedule-slots', {})
t.assert.status(res, 400)
})
t.test('rejects invalid day of week', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Day Validation Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Day Validation Type', durationMinutes: 30 })
const res = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 7,
startTime: '10:00',
})
t.assert.status(res, 400)
})
t.test('rejects invalid time format', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Time Validation Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Time Validation Type', durationMinutes: 30 })
const res = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 1,
startTime: '3pm',
})
t.assert.status(res, 400)
})
t.test('detects overlapping slot for same instructor, day, and time', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor' })
const type1 = await t.api.post('/v1/lesson-types', { name: 'Conflict Type 1', durationMinutes: 30 })
const type2 = await t.api.post('/v1/lesson-types', { name: 'Conflict Type 2', durationMinutes: 30 })
const first = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: type1.data.id,
dayOfWeek: 3,
startTime: '14:00',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: type2.data.id,
dayOfWeek: 3,
startTime: '14:00',
})
t.assert.status(second, 409)
})
t.test('allows same time for different instructors', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
const instructor1 = await t.api.post('/v1/instructors', { displayName: 'No Conflict Instructor A' })
const instructor2 = await t.api.post('/v1/instructors', { displayName: 'No Conflict Instructor B' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'No Conflict Type', durationMinutes: 30 })
const first = await t.api.post('/v1/schedule-slots', {
instructorId: instructor1.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 1,
startTime: '10:00',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/schedule-slots', {
instructorId: instructor2.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 1,
startTime: '10:00',
})
t.assert.status(second, 201)
})
t.test('gets schedule slot by id', { tags: ['schedule-slots', 'read'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Get Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Get Slot Type', durationMinutes: 30 })
const created = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 5,
startTime: '09:00',
})
const res = await t.api.get(`/v1/schedule-slots/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.dayOfWeek, 5)
})
t.test('returns 404 for missing schedule slot', { tags: ['schedule-slots', 'read'] }, async () => {
const res = await t.api.get('/v1/schedule-slots/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('updates a schedule slot', { tags: ['schedule-slots', 'update'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Update Slot Type', durationMinutes: 30 })
const created = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 0,
startTime: '11:00',
room: 'Old Room',
})
const res = await t.api.patch(`/v1/schedule-slots/${created.data.id}`, {
room: 'New Room',
maxStudents: 3,
})
t.assert.status(res, 200)
t.assert.equal(res.data.room, 'New Room')
t.assert.equal(res.data.maxStudents, 3)
})
t.test('update detects conflict when changing time', { tags: ['schedule-slots', 'update', 'conflict'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Conflict Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Update Conflict Type', durationMinutes: 30 })
await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 2,
startTime: '15:00',
})
const second = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 2,
startTime: '16:00',
})
const res = await t.api.patch(`/v1/schedule-slots/${second.data.id}`, {
startTime: '15:00',
})
t.assert.status(res, 409)
})
t.test('soft-deletes a schedule slot', { tags: ['schedule-slots', 'delete'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Delete Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Delete Slot Type', durationMinutes: 30 })
const created = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 6,
startTime: '12:00',
})
const res = await t.api.del(`/v1/schedule-slots/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.isActive, false)
})
// ─── Schedule Slots: List, Filter ───
t.test('lists schedule slots with pagination', { tags: ['schedule-slots', 'read', 'pagination'] }, async () => {
const res = await t.api.get('/v1/schedule-slots', { limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.pagination)
t.assert.ok(res.data.data.length >= 1)
})
t.test('filters schedule slots by instructor', { tags: ['schedule-slots', 'filter'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Filter Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Filter Slot Type', durationMinutes: 30 })
await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 1,
startTime: '08:00',
})
const res = await t.api.get('/v1/schedule-slots', { instructorId: instructor.data.id, limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((s: any) => s.instructorId === instructor.data.id))
})
t.test('filters schedule slots by day of week', { tags: ['schedule-slots', 'filter'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Day Filter Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Day Filter Type', durationMinutes: 30 })
await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 0,
startTime: '13:00',
})
const res = await t.api.get('/v1/schedule-slots', { dayOfWeek: '0', limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((s: any) => s.dayOfWeek === 0))
})
t.test('deleted slot does not appear in list', { tags: ['schedule-slots', 'delete', 'list'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Ghost Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Ghost Slot Type', durationMinutes: 30 })
const created = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 4,
startTime: '19:00',
room: 'Ghost Room XYZ',
})
await t.api.del(`/v1/schedule-slots/${created.data.id}`)
const res = await t.api.get('/v1/schedule-slots', { q: 'Ghost Room XYZ', limit: 100 })
t.assert.equal(res.data.data.length, 0)
})
t.test('deactivated slot frees the time for new slot', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
const instructor = await t.api.post('/v1/instructors', { displayName: 'Reuse Slot Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Reuse Slot Type', durationMinutes: 30 })
const first = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 5,
startTime: '18:00',
})
t.assert.status(first, 201)
await t.api.del(`/v1/schedule-slots/${first.data.id}`)
const second = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 5,
startTime: '18:00',
})
t.assert.status(second, 201)
})
})