Files
lunarfront-app/packages/backend/api-tests/suites/lessons.ts
Ryan Moon 73360cd478 Add lessons Phase 4: lesson sessions with hybrid calendar generation
Individual lesson occurrences generated from schedule slot patterns.
Idempotent session generation with configurable rolling window.
Post-lesson notes workflow with auto-set notesCompletedAt. Status
tracking (scheduled/attended/missed/makeup/cancelled) and date/time
filtering. 13 new tests (64 total lessons tests).
2026-03-30 09:29:03 -05:00

943 lines
45 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)
})
// ─── Enrollments: CRUD ───
t.test('creates an enrollment', { tags: ['enrollments', 'create'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Emma', lastName: 'Chen' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Enrollment Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Enrollment Type', durationMinutes: 30 })
const slot = 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.post('/v1/enrollments', {
memberId: member.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
monthlyRate: 120,
notes: 'Beginner piano student',
})
t.assert.status(res, 201)
t.assert.equal(res.data.status, 'active')
t.assert.equal(res.data.memberId, member.data.id)
t.assert.equal(res.data.monthlyRate, '120.00')
t.assert.equal(res.data.startDate, '2026-01-15')
t.assert.equal(res.data.makeupCredits, 0)
})
t.test('rejects enrollment without required fields', { tags: ['enrollments', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/enrollments', {})
t.assert.status(res, 400)
})
t.test('enforces slot capacity', { tags: ['enrollments', 'create', 'capacity'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Capacity Account', billingMode: 'consolidated' })
const m1 = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Student', lastName: 'One' })
const m2 = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Student', lastName: 'Two' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Capacity Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Capacity Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 3,
startTime: '14:00',
maxStudents: 1,
})
const first = await t.api.post('/v1/enrollments', {
memberId: m1.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/enrollments', {
memberId: m2.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
})
t.assert.status(second, 409)
})
t.test('prevents member from enrolling in conflicting time', { tags: ['enrollments', 'create', 'conflict'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Time Conflict Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Busy', lastName: 'Student' })
const i1 = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor A' })
const i2 = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor B' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Conflict LT', durationMinutes: 30 })
const slot1 = await t.api.post('/v1/schedule-slots', {
instructorId: i1.data.id, lessonTypeId: lt.data.id, dayOfWeek: 1, startTime: '10:00',
})
const slot2 = await t.api.post('/v1/schedule-slots', {
instructorId: i2.data.id, lessonTypeId: lt.data.id, dayOfWeek: 1, startTime: '10:00',
})
const first = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot1.data.id, instructorId: i1.data.id, startDate: '2026-01-15',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot2.data.id, instructorId: i2.data.id, startDate: '2026-01-15',
})
t.assert.status(second, 409)
})
t.test('gets enrollment by id', { tags: ['enrollments', 'read'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Get Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Get', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Get Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Get Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 4, startTime: '15:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-02-01',
})
const res = await t.api.get(`/v1/enrollments/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.id, created.data.id)
})
t.test('returns 404 for missing enrollment', { tags: ['enrollments', 'read'] }, async () => {
const res = await t.api.get('/v1/enrollments/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('updates an enrollment', { tags: ['enrollments', 'update'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Update Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Update', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Update Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 5, startTime: '17:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-02-01',
})
const res = await t.api.patch(`/v1/enrollments/${created.data.id}`, {
monthlyRate: 150,
notes: 'Updated rate',
endDate: '2026-06-30',
})
t.assert.status(res, 200)
t.assert.equal(res.data.monthlyRate, '150.00')
t.assert.equal(res.data.notes, 'Updated rate')
t.assert.equal(res.data.endDate, '2026-06-30')
})
// ─── Enrollments: Status Transitions ───
t.test('status lifecycle: active → paused → active → cancelled', { tags: ['enrollments', 'status'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Status Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Status', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Status Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Status Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 6, startTime: '09:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
t.assert.equal(created.data.status, 'active')
const paused = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'paused' })
t.assert.equal(paused.data.status, 'paused')
const resumed = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'active' })
t.assert.equal(resumed.data.status, 'active')
const cancelled = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'cancelled' })
t.assert.equal(cancelled.data.status, 'cancelled')
})
// ─── Enrollments: List, Filter ───
t.test('lists enrollments with pagination', { tags: ['enrollments', 'read', 'pagination'] }, async () => {
const res = await t.api.get('/v1/enrollments', { limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.pagination)
t.assert.ok(res.data.data.length >= 1)
})
t.test('filters enrollments by instructor', { tags: ['enrollments', 'filter'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Filter Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Filter', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Filter Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Filter Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 0, startTime: '11:00',
})
await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-03-01',
})
const res = await t.api.get('/v1/enrollments', { instructorId: instructor.data.id, limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((e: any) => e.instructorId === instructor.data.id))
})
t.test('filters enrollments by status', { tags: ['enrollments', 'filter'] }, async () => {
const res = await t.api.get('/v1/enrollments', { status: 'active', limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((e: any) => e.status === 'active'))
})
// ─── Lesson Sessions: Generation ───
t.test('generates sessions for an enrollment', { tags: ['sessions', 'generate'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Session Gen Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Session', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Session Gen Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Session Gen Type', durationMinutes: 30 })
// Use Tuesday (2) for predictable scheduling
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 2, startTime: '16:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const res = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=4`)
t.assert.status(res, 200)
t.assert.ok(res.data.generated >= 1, 'should generate at least 1 session')
t.assert.equal(res.data.sessions.length, res.data.generated)
// All sessions should be on Tuesday
for (const s of res.data.sessions) {
t.assert.equal(s.status, 'scheduled')
t.assert.equal(s.scheduledTime, '16:00:00')
const dayOfWeek = new Date(s.scheduledDate + 'T00:00:00').getDay()
t.assert.equal(dayOfWeek, 2, `session date ${s.scheduledDate} should be Tuesday`)
}
})
t.test('session generation is idempotent', { tags: ['sessions', 'generate'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Idempotent Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Idemp', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Idempotent Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Idempotent Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 3, startTime: '14:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const first = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=4`)
t.assert.ok(first.data.generated >= 1)
const second = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=4`)
t.assert.equal(second.data.generated, 0, 'second call should generate 0 new sessions')
})
// ─── Lesson Sessions: CRUD ───
t.test('gets lesson session by id', { tags: ['sessions', 'read'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Get Session Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Get', lastName: 'Session' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Get Session Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Get Session Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 4, startTime: '10:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
t.assert.ok(gen.data.sessions.length >= 1)
const res = await t.api.get(`/v1/lesson-sessions/${gen.data.sessions[0].id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.enrollmentId, enrollment.data.id)
t.assert.equal(res.data.status, 'scheduled')
})
t.test('returns 404 for missing lesson session', { tags: ['sessions', 'read'] }, async () => {
const res = await t.api.get('/v1/lesson-sessions/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('lists lesson sessions with filters', { tags: ['sessions', 'read', 'filter'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'List Session Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'List', lastName: 'Session' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'List Session Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'List Session Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 5, startTime: '15:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=4`)
const res = await t.api.get('/v1/lesson-sessions', { enrollmentId: enrollment.data.id, limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.length >= 1)
t.assert.ok(res.data.data.every((s: any) => s.enrollmentId === enrollment.data.id))
t.assert.ok(res.data.pagination)
})
// ─── Lesson Sessions: Status ───
t.test('marks a session as attended', { tags: ['sessions', 'status'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Attend Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Attend', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Attend Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Attend Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 1, startTime: '09:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const res = await t.api.post(`/v1/lesson-sessions/${gen.data.sessions[0].id}/status`, { status: 'attended' })
t.assert.status(res, 200)
t.assert.equal(res.data.status, 'attended')
})
t.test('marks a session as missed', { tags: ['sessions', 'status'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Missed Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Missed', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Missed Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Missed Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 0, startTime: '11:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const res = await t.api.post(`/v1/lesson-sessions/${gen.data.sessions[0].id}/status`, { status: 'missed' })
t.assert.status(res, 200)
t.assert.equal(res.data.status, 'missed')
})
t.test('cancels a session', { tags: ['sessions', 'status'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Cancel Session Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Cancel', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Cancel Session Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Cancel Session Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 6, startTime: '13:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const res = await t.api.post(`/v1/lesson-sessions/${gen.data.sessions[0].id}/status`, { status: 'cancelled' })
t.assert.status(res, 200)
t.assert.equal(res.data.status, 'cancelled')
})
// ─── Lesson Sessions: Notes ───
t.test('saves post-lesson notes and auto-sets notesCompletedAt', { tags: ['sessions', 'notes'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Notes Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Notes', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Notes Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Notes Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 3, startTime: '16:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const sessionId = gen.data.sessions[0].id
// Mark as attended first
await t.api.post(`/v1/lesson-sessions/${sessionId}/status`, { status: 'attended' })
const res = await t.api.post(`/v1/lesson-sessions/${sessionId}/notes`, {
instructorNotes: 'Emma was distracted today',
memberNotes: 'Great focus on scales. Left hand needs work.',
homeworkAssigned: 'Practice Fur Elise bars 1-8 hands together',
nextLessonGoals: 'Start bars 9-16',
topicsCovered: ['Fur Elise', 'C Major Scale'],
})
t.assert.status(res, 200)
t.assert.equal(res.data.instructorNotes, 'Emma was distracted today')
t.assert.equal(res.data.memberNotes, 'Great focus on scales. Left hand needs work.')
t.assert.equal(res.data.homeworkAssigned, 'Practice Fur Elise bars 1-8 hands together')
t.assert.equal(res.data.topicsCovered.length, 2)
t.assert.ok(res.data.notesCompletedAt, 'notesCompletedAt should be auto-set')
})
t.test('second notes save does not reset notesCompletedAt', { tags: ['sessions', 'notes'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Notes Idempotent Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Idemp', lastName: 'Notes' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Notes Idemp Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Notes Idemp Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 4, startTime: '11:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const sessionId = gen.data.sessions[0].id
const first = await t.api.post(`/v1/lesson-sessions/${sessionId}/notes`, { instructorNotes: 'First notes' })
t.assert.ok(first.data.notesCompletedAt)
const originalTimestamp = first.data.notesCompletedAt
const second = await t.api.post(`/v1/lesson-sessions/${sessionId}/notes`, { memberNotes: 'Updated member notes' })
t.assert.equal(second.data.notesCompletedAt, originalTimestamp, 'notesCompletedAt should not change')
t.assert.equal(second.data.memberNotes, 'Updated member notes')
})
// ─── Lesson Sessions: Update ───
t.test('updates actual start/end times', { tags: ['sessions', 'update'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Times Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Times', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Times Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Times Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 2, startTime: '15:00',
})
const enrollment = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
const gen = await t.api.post(`/v1/enrollments/${enrollment.data.id}/generate-sessions?weeks=2`)
const res = await t.api.patch(`/v1/lesson-sessions/${gen.data.sessions[0].id}`, {
actualStartTime: '15:05',
actualEndTime: '15:32',
})
t.assert.status(res, 200)
t.assert.equal(res.data.actualStartTime, '15:05:00')
t.assert.equal(res.data.actualEndTime, '15:32:00')
})
t.test('filters sessions by date range', { tags: ['sessions', 'filter'] }, async () => {
const res = await t.api.get('/v1/lesson-sessions', { dateFrom: '2026-01-01', dateTo: '2026-12-31', limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.pagination)
})
t.test('filters sessions by status', { tags: ['sessions', 'filter'] }, async () => {
const res = await t.api.get('/v1/lesson-sessions', { status: 'scheduled', limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((s: any) => s.status === 'scheduled'))
})
})