Files
lunarfront-app/packages/backend/api-tests/suites/lessons.ts
Ryan Moon 93405af3b2 Add lessons Phase 3: enrollments with capacity and time conflict checks
Links members to schedule slots via enrollments. Enforces max_students
capacity on slots and prevents members from double-booking the same
day/time. Supports status transitions and filtering. 11 new tests
(51 total lessons tests).
2026-03-30 09:23:43 -05:00

690 lines
30 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'))
})
})