Files
lunarfront-app/packages/backend/api-tests/suites/lessons.ts
Ryan Moon 5dbe837c08 Add lessons domain Phase 1: instructor and lesson type entities
Foundation tables for the lessons module with full CRUD, pagination,
search, and sorting. Includes migration, Drizzle schema, Zod validation,
services, routes, and 23 integration tests.
2026-03-30 09:17:32 -05:00

220 lines
9.5 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)
})
})