Download source/test/apps-script.test.js from FormaLau/microduck-reachy-simulator: direct link, hf CLI and curl.
- Browser
- Download file 8.56 kB
-
https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/apps-script.test.js
- Command line
-
hf download hf://spaces/FormaLau/microduck-reachy-simulator/source/test/apps-script.test.js
-
curl -L -o apps-script.test.js https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/test/apps-script.test.js
8.56 kB
| import test from 'node:test'; | |
| import assert from 'node:assert/strict'; | |
| import { randomUUID } from 'node:crypto'; | |
| import { activityReport, validateActivity } from '../shared/examActivity.js'; | |
| import { load } from './appsScriptHarness.js'; | |
| const journal = (revision, events) => ({ id: '0f0e0d0c-0b0a-4908-8706-050403020100', revision, dropped: 0, events }); | |
| test('Apps Script backend refuses access until the teacher sets a code, and limits guesses', () => { | |
| assert.equal(load({}).call({ action: 'access', code: 'x', student: 'Élise' }).error, 'exam_unavailable'); | |
| const { call } = load(); | |
| assert.equal(load().context.doPost({ postData: { contents: 'not json' } }).text, '{"error":"invalid_request"}'); | |
| for (let i = 0; i < 12; i++) assert.equal(call({ action: 'access', code: 'wrong', student: 'Élise Test' }).error, 'invalid_code'); | |
| assert.equal(call({ action: 'access', code: 'code-secret', student: 'Élise Test' }).error, 'too_many_attempts'); | |
| assert.equal(call({ action: 'access', code: ' code-secret ', student: 'Autre Élève' }).exam.exercises.length, 5); | |
| assert.equal(call({ action: 'access', code: 'code-secret', student: 'x' }).error, 'invalid_student'); | |
| }); | |
| test('Apps Script sessions are signed and the journal keeps its newest revision', () => { | |
| const { call, root, props } = load(); | |
| const auth = call({ action: 'access', code: 'code-secret', student: ' Élise Test ' }); | |
| assert.equal(auth.student, 'Élise Test'); | |
| assert.equal(auth.submissionAvailable, true); | |
| assert.ok(props.get('EXAM_SESSION_SECRET')); | |
| assert.equal(call({ action: 'session', token: auth.token }).studentKey, auth.studentKey); | |
| const [payload, signature] = auth.token.split('.'); | |
| const forged = Buffer.from(JSON.stringify({ ...JSON.parse(Buffer.from(payload, 'base64url')), student: 'Autre' })).toString('base64url'); | |
| assert.equal(call({ action: 'session', token: `${forged}.${signature}` }).error, 'session_expired'); | |
| assert.equal(call({ action: 'session' }).error, 'session_expired'); | |
| assert.equal(call({ action: 'activity', token: auth.token, activity: { id: 'bad' } }).error, 'invalid_activity'); | |
| assert.equal(call({ action: 'activity', token: auth.token, activity: journal(2, [{ at: 1000, state: 'active' }, { at: 2000, state: 'hidden' }]) }).saved, true); | |
| call({ action: 'activity', token: auth.token, activity: journal(1, [{ at: 1000, state: 'active' }]) }); | |
| const [file] = root().folders.find(item => item.name === 'suivi').files; | |
| assert.equal(JSON.parse(file.getBlob().getDataAsString()).activity.revision, 2); | |
| }); | |
| test('Apps Script submit all sends one email with every file, even when retried', () => { | |
| const { call, mails, root } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org', EXAM_ID: 'exam-c' }); | |
| const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' }); | |
| const copies = auth.exam.exercises.map((item, index) => ({ exerciseId: item.id, source: index === 1 ? '' : `// réponse ${index}` })); | |
| const body = { action: 'submit-all', token: auth.token, copies, requestId: randomUUID(), activity: journal(1, [{ at: 1000, state: 'active' }]) }; | |
| assert.equal(call({ ...body, copies: copies.slice(1) }).error, 'invalid_request'); | |
| assert.equal(call({ ...body, copies: copies.map(item => ({ ...item, source: ' ' })) }).error, 'empty_source'); | |
| const receipt = call(body); | |
| assert.match(receipt.id, /^exam-/); | |
| assert.deepEqual(call(body), receipt); | |
| assert.equal(mails.length, 1); | |
| const [mail] = mails; | |
| assert.equal(mail.to, 'prof@example.org'); | |
| assert.equal(mail.subject, '[Évaluation C] Élise Test — Tous les exercices'); | |
| assert.match(mail.body, /Exercice 2 : non répondu/); | |
| assert.deepEqual([...mail.attachments.map(file => file.getName())], ['Elise-Test-exercice-1.c', 'Elise-Test-exercice-2.c', 'Elise-Test-exercice-3.c', 'Elise-Test-exercice-4.c', 'Elise-Test-exercice-5.c', 'Elise-Test-suivi.txt']); | |
| assert.equal(mail.attachments[0].getDataAsString(), '// réponse 0'); | |
| assert.match(mail.attachments[5].getDataAsString(), /Élise Test — examen exam-c/); | |
| assert.equal(root().folders.find(item => item.name === 'copies').files.length, 6); | |
| const single = call({ action: 'submit', token: auth.token, exerciseId: 'exercice-3', source: 'int main(void) {}', requestId: randomUUID() }); | |
| assert.match(single.id, /^exam-/); | |
| assert.equal(mails[1].subject, '[Évaluation C] Élise Test — Exercice 3'); | |
| assert.equal(call({ action: 'submit', token: auth.token, exerciseId: 'nope', source: 'x', requestId: randomUUID() }).error, 'invalid_request'); | |
| }); | |
| test('Apps Script quiz sends the answers once, then stays closed until the teacher resets it', () => { | |
| const { call, mails, context } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org' }); | |
| const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' }); | |
| assert.equal(auth.quizDone, false); | |
| assert.equal(auth.exam.quiz.length, 5); | |
| const [first, second] = auth.exam.quiz.filter(item => item.choices); | |
| const free = auth.exam.quiz.find(item => !item.choices); | |
| assert.equal(call({ action: 'quiz', answers: {} }).error, 'session_expired'); | |
| assert.equal(call({ action: 'quiz', token: auth.token, answers: { [first.id]: 'Pas un choix' } }).error, 'invalid_answers'); | |
| assert.equal(call({ action: 'quiz', token: auth.token, answers: { inconnue: 'x' } }).error, 'invalid_answers'); | |
| const body = { action: 'quiz', token: auth.token, answers: { [first.id]: first.choices[0], [free.id]: 'Réponse libre' } }; | |
| const receipt = call(body); | |
| assert.match(receipt.id, /^quiz-/); | |
| assert.deepEqual(call({ ...body, answers: {} }), receipt); | |
| assert.equal(mails.length, 1); | |
| assert.equal(mails[0].subject, '[Évaluation C] Élise Test — Quiz'); | |
| assert.match(mails[0].body, new RegExp(`${auth.exam.quiz.indexOf(first) + 1}\\. ${first.question.replace(/[?]/g, '\\?')}\\n→ ${first.choices[0]}`)); | |
| assert.match(mails[0].body, new RegExp(`${auth.exam.quiz.indexOf(second) + 1}\\. .*\\n→ \\(sans réponse\\)`)); | |
| assert.match(mails[0].body, /→ Réponse libre/); | |
| assert.equal(mails[0].attachments[0].getName(), 'Elise-Test-quiz.txt'); | |
| const again = call({ action: 'access', code: 'code-secret', student: 'élise test' }); | |
| assert.equal(again.quizDone, true); | |
| assert.deepEqual([...again.exam.quiz], []); | |
| context.reinitialiserQuiz(); | |
| assert.equal(call({ action: 'session', token: auth.token }).quizDone, false); | |
| }); | |
| test('Apps Script still sends the quiz and the copies when Drive is not authorised', () => { | |
| const { call, mails, context } = load({ EXAM_ACCESS_CODE: 'code-secret', EXAM_RECIPIENT: 'prof@example.org' }); | |
| context.console = { ...console, error() {} }; | |
| const denied = () => { throw new Error('Exception: You do not have permission to call DriveApp.createFolder.'); }; | |
| context.DriveApp = { createFolder: denied, getFolderById: denied }; | |
| const auth = call({ action: 'access', code: 'code-secret', student: 'Élise Test' }); | |
| assert.match(call({ action: 'quiz', token: auth.token, answers: {} }).id, /^quiz-/); | |
| assert.equal(call({ action: 'session', token: auth.token }).quizDone, true); | |
| const copies = auth.exam.exercises.map(item => ({ exerciseId: item.id, source: '// code' })); | |
| assert.match(call({ action: 'submit-all', token: auth.token, copies, requestId: randomUUID(), activity: journal(1, [{ at: 1000, state: 'active' }]) }).id, /^exam-/); | |
| assert.equal(mails.length, 2); | |
| assert.match(mails[1].attachments.at(-1).getDataAsString(), /Drive n’est pas autorisé/); | |
| assert.equal(call({ action: 'activity', token: auth.token, activity: journal(2, [{ at: 1000, state: 'active' }]) }).error, 'server_error'); | |
| }); | |
| test('Apps Script journal helpers stay identical to shared/examActivity.js', () => { | |
| const { context } = load(); | |
| const log = journal(3, [{ at: 1000, state: 'active' }, { at: 4000, state: 'hidden' }, { at: 4500, state: 'unfocused' }, { at: 9000, state: 'active' }, { at: 12000, state: 'closed' }]); | |
| log.signals = [{ at: 4100, kind: 'extension', detail: '<sider-sidebar>' }, { at: 4200, kind: 'insertion', detail: 'insertText · 300 caractères' }]; | |
| const records = [{ student: 'Élise Test', examId: 'exam-c', receivedAt: 5000, activity: { ...log, dropped: 2 } }]; | |
| assert.equal(context.activityReport(records, 20000), activityReport(records, 20000)); | |
| assert.equal(JSON.stringify(context.validateActivity(log, 20000)), JSON.stringify(validateActivity(log, 20000))); | |
| assert.equal(context.activityReport([], 1), activityReport([], 1)); | |
| }); | |