import test from 'node:test'; import assert from 'node:assert/strict'; import Fastify from 'fastify'; import cookie from '@fastify/cookie'; import rateLimit from '@fastify/rate-limit'; import api from '../src/api.js'; const cookies = { teach_session: 'x'.repeat(64) }; const teacher = { id: 3, username: 'juf', role: 'teacher', school_id: 2, class_id: null, data: {}, data_rev: 0, }; test('live inzicht berekent fout-, stiltesignalen en zevendaagse grafiekdata', async () => { const now = new Date(); const idleStart = new Date(now.getTime() - 4 * 60_000); const date = new Date(); date.setHours(12, 0, 0, 0); const day = date.toISOString().slice(0, 10); const query = async (sql) => { if (sql.includes('FROM sessions s JOIN users u')) return { rows: [teacher] }; if (sql.includes('FROM user_roles')) return { rows: [] }; if (sql.startsWith('SELECT * FROM classes')) return { rows: [{ id: 50, school_id: 2 }] }; if (sql.startsWith('SELECT 1 FROM class_teachers')) return { rows: [{ ok: 1 }] }; if (sql.includes('FROM live_learning_sessions ls')) return { rows: [ { id: 101, pupil_id: 9, display_name: 'Lena', widget_id: 'math-1', widget_type: 'madd', mode: 'werken', started_at: idleStart, last_seen_at: now, last_progress_at: now, attempts: 10, correct: 3, }, { id: 102, pupil_id: 10, display_name: 'Sam', widget_id: 'letters-1', widget_type: 'letters', mode: 'werken', started_at: idleStart, last_seen_at: now, last_progress_at: null, attempts: 0, correct: 0, }, ] }; if (sql.includes('FROM progress_events pe')) return { rows: [{ pupil_id: 9, day, attempts: 10, correct: 7, updates: 2, }] }; if (sql.includes('FROM assignment_step_progress asp')) return { rows: [{ pupil_id: 9, day, completed: 1, }] }; return { rows: [] }; }; const app = Fastify({ trustProxy: 2 }); await app.register(cookie); await app.register(rateLimit, { global: false }); app.decorate('pg', { query }); await app.register(api, { prefix: '/api' }); await app.ready(); const response = await app.inject({ method: 'GET', url: '/api/progress/live/class/50', cookies, }); assert.equal(response.statusCode, 200, response.body); const body = response.json(); assert.equal(body.helpCount, 2); assert.deepEqual(body.sessions.map((session) => session.helpSignal), ['errors', 'idle']); assert.equal(body.classTrend.at(-1).accuracy, 70); assert.equal(body.classTrend.at(-1).completed, 2); assert.equal(body.pupilTrends['9'].at(-1).updates, 2); await app.close(); });