// Runnable check for the money/access rules: `pnpm test`. Test IDs from SPEC §13.2. import assert from 'node:assert/strict'; import type { Course, Student } from '@lms/data'; import { applyHeartbeat, DomainError, gradeAssessment, applyPaymentEvent, createOrder, dashboardMetrics, effectiveStatus, isStudentVisible, lessonAccess, publishProblems, validateLocation, validPassword } from './domain.ts'; const lesson = (id: string, extra = {}) => ({ id, sortOrder: Number(id.slice(-1)), isPreview: false, isPublished: true, durationSec: 100, videoStatus: 'READY' as const, translations: { en: { title: id } }, ...extra }); const course = (id: string, extra: Partial = {}): Course => ({ id, slug: id, categoryId: 'cat', instructorName: 'I', instructionLocale: 'am', status: 'PUBLISHED', isFeatured: false, isTest: false, sequentialLessons: false, createdAt: '2026-01-01T00:00:00Z', updatedAt: '2026-01-01T00:00:00Z', coverTone: 'rose', translations: { en: { title: id, description: 'd' } }, prices: { ETB: 120000 }, chapters: [{ id: id + 'ch', sortOrder: 1, translations: { en: { title: 'c' } }, lessons: [lesson(id + 'l1', { isPreview: true }), lesson(id + 'l2'), lesson(id + 'l3')] }], ...extra, }); const student = (): Student => ({ id: 's', name: 'S', country: 'ET', city: 'Adama', locale: 'am', status: 'ACTIVE', createdAt: '2026-03-01T00:00:00Z', updatedAt: '', enrollments: {}, orders: {}, progress: {}, issues: {} }); const merge = (s: Student, patch: Record | null): Student => { if (!patch) return s; const out: any = structuredClone(s); for (const [k, v] of Object.entries(patch)) for (const [id, body] of Object.entries(v as object)) out[k][id] = { ...(out[k][id] ?? {}), ...body }; return out; }; const A = course('a'), B = course('b', { prerequisiteCourseId: 'a', sequentialLessons: true }); let s = student(); const now = new Date('2026-05-10T10:00:00Z'); // SEC-07 / preview assert.deepEqual(lessonAccess(A, A.chapters[0].lessons[1], s, [A, B]), { ok: false, reason: 'COURSE_NOT_OWNED' }); assert.deepEqual(lessonAccess(A, A.chapters[0].lessons[0], s, [A, B]), { ok: true }); // PAY-01, PAY-09 idempotency const o = createOrder(s, A, 'TELEBIRR', 'k1', now, 'o1'); assert.equal(o.status, 'PENDING'); assert.equal(o.amountMinor, 120000); s = merge(s, { orders: { o1: o } }); assert.equal(createOrder(s, A, 'TELEBIRR', 'k1', now, 'o2').id, 'o1'); // PAY-05 amount mismatch → flagged, not unlocked const mismatch = merge(s, applyPaymentEvent(s, 'o1', { status: 'PAID', amountMinor: 10, currency: 'ETB' }, now)); assert.ok(mismatch.orders.o1.flaggedReason); assert.equal(mismatch.orders.o1.status, 'PENDING'); assert.ok(!mismatch.enrollments.a); // PAY-02 paid + enrollment together; PAY-03 duplicate is a no-op s = merge(s, applyPaymentEvent(s, 'o1', { status: 'PAID', amountMinor: 120000, currency: 'ETB' }, now)); assert.equal(s.orders.o1.status, 'PAID'); assert.equal(s.enrollments.a.orderId, 'o1'); assert.equal(applyPaymentEvent(s, 'o1', { status: 'PAID', amountMinor: 120000, currency: 'ETB' }, now), null); // PAY-08 owned course assert.throws(() => createOrder(s, A, 'TELEBIRR', 'k2', now, 'o3'), /ALREADY_OWNED/); // PAY-10 expiry assert.equal(effectiveStatus({ ...o, status: 'PENDING' }, now.getTime() + 31 * 60_000), 'EXPIRED'); // CRS-05 prerequisite, CRS-06 sequential s = merge(s, { enrollments: { b: { courseId: 'b', source: 'ADMIN_GRANT', createdAt: '' } } }); assert.deepEqual(lessonAccess(B, B.chapters[0].lessons[1], s, [A, B]), { ok: false, reason: 'PREREQUISITE_REQUIRED' }); for (const l of A.chapters[0].lessons) s = merge(s, { progress: { [l.id]: applyHeartbeat(undefined, 'a', 95, 100, now.toISOString()) } }); assert.deepEqual(lessonAccess(B, B.chapters[0].lessons[2], s, [A, B]), { ok: false, reason: 'LESSON_LOCKED' }); // PRG-01 stale heartbeat, PRG-02 90% const p1 = applyHeartbeat(undefined, 'a', 80, 100, 't'); assert.equal(p1.completedAt, null); assert.equal(applyHeartbeat(p1, 'a', 10, 100, 't').maxPositionSec, 80); assert.equal(applyHeartbeat(p1, 'a', 90, 100, 't2').completedAt, 't2'); // CRS-04 test courses hidden; CRS-01 checklist assert.equal(isStudentVisible(course('t', { isTest: true })), false); assert.deepEqual(publishProblems(course('x', { prices: {} })), ['ETB price']); // DSH-01..03: period filter, refunds excluded, currencies separate; test course excluded const T = course('t', { isTest: true }); s = merge(s, { orders: { o9: { ...o, id: 'o9', status: 'REFUNDED', paidAt: '2026-05-01T00:00:00Z' }, o8: { ...o, id: 'o8', courseId: 't', status: 'PAID', paidAt: '2026-05-01T00:00:00Z' }, o7: { ...o, id: 'o7', currency: 'USD', amountMinor: 2500, status: 'PAID', paidAt: '2025-05-01T00:00:00Z' } } }); const m = dashboardMetrics([s], [A, B, T], { kind: 'year', year: 2026 }); assert.deepEqual(m.revenue, { ETB: 120000, USD: 0 }); assert.deepEqual(dashboardMetrics([s], [A, B, T], { kind: 'all' }).revenue, { ETB: 120000, USD: 2500 }); // L4, L5 assert.ok(validPassword('abcdefgh')); assert.ok(!validPassword('short')); assert.equal(validateLocation('ET', 'Nairobi'), 'CITY_NOT_IN_LIST'); assert.equal(validateLocation('KE', 'Nairobi'), null); // assessments: grading, best score kept, a pass is never lost, incomplete attempts rejected const quiz = { id: 'q', passPct: 70, translations: { en: { title: 'Q' } }, questions: [0, 1, 2].map((answer, i) => ({ id: 'q' + i, answer, translations: { en: { prompt: 'p', options: ['a', 'b', 'c'] } } })) }; const g1 = gradeAssessment(A, quiz, [0, 1, 2], undefined, now); assert.deepEqual([g1.score, g1.total, g1.bestPct, g1.passed, g1.attempts], [3, 3, 100, true, 1]); const g2 = gradeAssessment(A, quiz, [0, 0, 0], g1, now); assert.deepEqual([g2.score, g2.bestPct, g2.passed, g2.attempts], [1, 100, true, 2]); assert.equal(gradeAssessment(A, quiz, [1, 1, 1], undefined, now).passed, false); assert.throws(() => gradeAssessment(A, quiz, [0, 1], undefined, now), DomainError); console.log('domain checks: all passed');