diff --git a/apps/journeys/tests/course-presentation.test.ts b/apps/journeys/tests/course-presentation.test.ts new file mode 100644 index 00000000..e7b13718 --- /dev/null +++ b/apps/journeys/tests/course-presentation.test.ts @@ -0,0 +1,233 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, patient, source, type Learner } from './harness' + +/** + * Course Presentation 1.27, as `src/scripts/cp.js` and `summary-slide.js` run it: the sum of its + * slides' tasks, reported each time the summary slide is drawn, and resumed at the slide reached. + */ + +const { build, play } = journeys() +const text = (html: string) => ({ action: { library: 'H5P.AdvancedText', params: { text: html } } }) +const slides = [ + { elements: [text('

Rivers of Europe

Three questions follow.

')], keywords: [{ main: 'Rivers' }] }, + { elements: [{ action: { library: 'H5P.MultiChoice', params: { question: 'Which river is the longest in Europe?', answers: [{ text: 'Volga', correct: true }, { text: 'Danube', correct: false }] } } }], keywords: [{ main: 'The longest' }] }, + { elements: [{ action: { library: 'H5P.TrueFalse', params: { question: 'The Rhine flows into the North Sea.', correct: 'true' } } }], keywords: [{ main: 'The Rhine' }] }, + { elements: [{ action: { library: 'H5P.Blanks', params: { text: 'Fill in the names of the rivers.', questions: ['Paris lies on the *Seine*, London on the *Thames*.'] } } }], keywords: [{ main: 'Two capitals' }] } +] + +beforeAll(async () => { + await build('deck', source('H5P.CoursePresentation', { presentation: { slides } })) + await build('hidden', source('H5P.CoursePresentation', { presentation: { slides }, override: { hideSummarySlide: true } })) + await build('plain', source('H5P.CoursePresentation', { + presentation: { slides }, + override: { showSolutionButton: 'off', retryButton: 'off', summarySlideSolutionButton: false, summarySlideRetryButton: false } + })) + const reading = [slides[0], { elements: [text('

The Volga is the longest of them.

')], keywords: [{ main: 'The Volga' }] }] + await build('reading', source('H5P.CoursePresentation', { presentation: { slides: reading } })) +}) + +const check = /^Check/ +const retry = /^Retry/ +const solution = /^Show the solution/ +const ENDING_POINT = 'http://id.tincanapi.com/extension/ending-point' + +/** The slide on screen: the deck keeps the slides beside it in the page too. */ +const here = ({ frame }: Learner) => frame.locator('.h5p-slide.h5p-current') +const summary = ({ frame }: Learner) => frame.locator('.h5p-summary-slide') +/** Which slide is on screen, from 0. */ +const slideAt = ({ frame }: Learner) => frame.locator('.h5p-slide').evaluateAll((all) => all.findIndex((slide) => slide.classList.contains('h5p-current'))) + +/** Waits for the slide `index` to be on screen and still: the deck ignores a press while a slide moves. */ +async function settled(learner: Learner, index: number) { + await expect.poll(() => slideAt(learner)).toBe(index) + await learner.frame.locator('.h5p-slide.h5p-animate').first().waitFor({ state: 'detached' }) +} + +/** Moves on a slide at a time, as a learner presses Next. */ +async function next(learner: Learner, times = 1) { + for (let i = 0; i < times; i++) { + const from = await slideAt(learner) + await learner.frame.getByRole('button', { name: 'Next slide' }).click() + await settled(learner, from + 1) + } +} + +/** + * Answers the deck's three questions from its second slide, each checked; `blanks` is how many + * blanks are right, and `each` runs on a question's slide once it is checked. + */ +async function answer(learner: Learner, [choice, statement, blanks]: [boolean, boolean, 0 | 1 | 2], each = async () => {}) { + await next(learner) + await here(learner).getByRole('radio', { name: choice ? 'Volga' : 'Danube' }).click() + await here(learner).getByRole('button', { name: check }).click() + await each() + await next(learner) + await here(learner).getByRole('radio', { name: statement ? 'True' : 'False' }).click() + await here(learner).getByRole('button', { name: check }).click() + await each() + await next(learner) + await here(learner).getByRole('textbox').nth(0).fill(blanks > 0 ? 'Seine' : 'Rhine') + await here(learner).getByRole('textbox').nth(1).fill(blanks > 1 ? 'Thames' : 'Rhine') + await here(learner).getByRole('button', { name: check }).click() + await each() +} + +/** The summary slide's rows: each slide with a task, and its score. */ +const rows = (learner: Learner) => + summary(learner).locator('.h5p-theme-results-list-item').evaluateAll((items) => + items.map((item) => [item.querySelector('.slide-number-summary-page')?.textContent, item.querySelector('.h5p-theme-results-points')?.textContent?.trim()])) + +describe('Course Presentation', () => { + it('reports the sum of every slide’s tasks on the summary slide, which lists each slide’s score', async () => { + const learner = await play('deck') + await answer(learner, [true, false, 1]) + await next(learner) + + expect(await learner.result()).toEqual({ raw: 2, max: 4, success: undefined }) + expect(await summary(learner).getByText('2 / 4 correct').count()).toBe(1) + expect(await rows(learner)).toEqual([['Slide 2', '1 / 1'], ['Slide 3', '0 / 1'], ['Slide 4', '1 / 2']]) + // Each question reports its own answer as a part of the deck, with the slide it is on. + const { statements } = await learner.heard() + const parts = statements.filter((statement) => statement.verb.id.endsWith('/answered')) + expect(parts.map((part) => [part.result?.score?.raw, part.context?.extensions?.[ENDING_POINT]])).toEqual([[1, 2], [0, 3], [1, 4]]) + expect(parts.every((part) => part.context?.contextActivities?.parent?.length === 1)).toBe(true) + // And the progress bar marks each slide whose task has an answer. + expect(await learner.frame.locator('.h5p-progressbar-part-has-task.h5p-answered').count()).toBe(3) + }) + + it('reports again each time the summary slide is shown, with the score as it then stands', async () => { + const learner = await play('deck') + await answer(learner, [false, false, 0]) + await next(learner) + expect(await learner.result()).toEqual({ raw: 0, max: 4, success: undefined }) + + // Back by the summary's own link to the slide, where the question offers another go. + await summary(learner).getByRole('link', { name: /Slide 2/ }).click() + await settled(learner, 1) + await here(learner).getByRole('button', { name: retry }).click() + await here(learner).getByRole('radio', { name: 'Volga' }).click() + await here(learner).getByRole('button', { name: check }).click() + await next(learner, 3) + + expect(await learner.result(2)).toEqual({ raw: 1, max: 4, success: undefined }) + }) + + it('shows every solution from the summary slide, and reports nothing more while it does', async () => { + const learner = await play('deck') + await answer(learner, [false, true, 2]) + await next(learner) + await learner.result() + + await summary(learner).getByRole('button', { name: 'Show solutions' }).click() + // Back on the first slide with a task, the right answer marked. + await settled(learner, 1) + await here(learner).locator('.h5p-answer.h5p-should', { hasText: 'Volga' }).waitFor() + await learner.frame.getByRole('button', { name: 'Exit solution mode' }).waitFor() + + // The summary drawn again, and then a later statement heard: a result sent with it would have come first. + await summary(learner).locator('.h5p-summary-footer').evaluate((footer) => footer.setAttribute('data-seen', '')) + await next(learner, 3) + await summary(learner).locator('.h5p-summary-footer[data-seen]').waitFor({ state: 'detached' }) + const progressed = (await learner.verbs()).filter((verb) => verb === 'progressed').length + await learner.frame.getByRole('button', { name: 'Previous slide' }).click() + await expect.poll(async () => (await learner.verbs()).filter((verb) => verb === 'progressed').length).toBe(progressed + 1) + expect((await learner.heard()).finished).toHaveLength(1) + }) + + it('starts over on Retry: every answer cleared, back on the first slide, and the next summary reported anew', async () => { + const learner = await play('deck') + await answer(learner, [true, true, 2]) + await next(learner) + expect(await learner.result()).toEqual({ raw: 4, max: 4, success: undefined }) + + await summary(learner).getByRole('button', { name: 'Retry' }).click() + await settled(learner, 0) + await next(learner) + expect(await here(learner).getByRole('radio', { checked: true }).count()).toBe(0) + await here(learner).getByRole('radio', { name: 'Volga' }).click() + await here(learner).getByRole('button', { name: check }).click() + await next(learner, 3) + + expect(await learner.result(2)).toEqual({ raw: 1, max: 4, success: undefined }) + }) + + it('asks before the summary slide where reporting is on, and reports once the learner confirms', async () => { + const learner = await play('deck', { reporting: '' }) + await answer(learner, [true, false, 0]) + const ask = learner.frame.getByRole('alertdialog').filter({ hasText: 'Submit your answers' }) + + await learner.frame.getByRole('button', { name: 'Next slide' }).click() + await ask.getByRole('button', { name: 'Cancel' }).click() + await ask.waitFor({ state: 'detached' }) + // Still on the last slide, with nothing reported: only the summary sends a result. + expect(await slideAt(learner)).toBe(3) + expect((await learner.heard()).finished).toHaveLength(0) + + await learner.frame.getByRole('button', { name: 'Next slide' }).click() + await ask.getByRole('button', { name: 'Confirm' }).click() + await settled(learner, 4) + expect(await learner.result()).toEqual({ raw: 1, max: 4, success: undefined }) + }) + + it('reports no total where the author hid the summary slide: each question reports only its own answer', async () => { + const learner = await play('hidden') + expect(await learner.frame.locator('.h5p-progressbar-part').count()).toBe(4) + await answer(learner, [true, true, 2]) + + // The last answer heard, nothing else to send one: no summary slide, and no result of the deck's. + await expect.poll(async () => (await learner.verbs()).filter((verb) => verb === 'answered').length).toBe(3) + expect(await summary(learner).count()).toBe(0) + expect((await learner.heard()).finished).toHaveLength(0) + }) + + it('reports no result for a deck with no task: it has no summary slide, and says only which slide was reached', async () => { + const learner = await play('reading') + expect(await learner.frame.locator('.h5p-progressbar-part').count()).toBe(2) + await next(learner) + + // On the last slide and still: a result would have come with the move. + await expect.poll(() => learner.verbs()).toEqual(['progressed']) + const { statements, finished } = await learner.heard() + expect(statements[0].object?.definition?.extensions?.[ENDING_POINT]).toBe(2) + expect(await summary(learner).count()).toBe(0) + expect(finished).toHaveLength(0) + }) + + it('takes the author’s overrides: no Retry or Show solution for any question, nor on the summary slide', async () => { + const learner = await play('plain') + await answer(learner, [false, false, 0], async () => { + await here(learner).getByRole('button', { name: check }).waitFor({ state: 'detached' }) + expect(await here(learner).getByRole('button', { name: retry }).count()).toBe(0) + expect(await here(learner).getByRole('button', { name: solution }).count()).toBe(0) + }) + + await next(learner) + await learner.result() + expect(await summary(learner).getByRole('button').count()).toBe(0) + }) + + it('comes back at the slide a learner had reached, with each slide’s answer', async () => { + const learner = await play('deck', { resume: 'device', 'save-interval': '1' }) + await next(learner) + await here(learner).getByRole('radio', { name: 'Volga' }).click() + await here(learner).getByRole('button', { name: check }).click() + await next(learner) + await here(learner).getByRole('radio', { name: 'True' }).click() + // The save that holds the last click, not only the slide: one may be taken between the two. + type Saved = { answers: ({ answer?: boolean }[] | null)[] } + await expect.poll(async () => ((await learner.saved()) as Saved).answers[2]?.[0]?.answer, patient).toBe(true) + + await learner.comeBack() + + await settled(learner, 2) + expect(await here(learner).getByRole('radio', { checked: true }).innerText()).toContain('True') + await here(learner).getByRole('button', { name: check }).click() + await next(learner) + await here(learner).getByRole('textbox').nth(0).fill('Seine') + await here(learner).getByRole('textbox').nth(1).fill('Thames') + await here(learner).getByRole('button', { name: check }).click() + await next(learner) + // The first question counts as it was answered before the learner left. + expect(await learner.result()).toEqual({ raw: 4, max: 4, success: undefined }) + }) +}) diff --git a/apps/journeys/tests/harness.ts b/apps/journeys/tests/harness.ts index 58a15131..a5ad3c8d 100644 --- a/apps/journeys/tests/harness.ts +++ b/apps/journeys/tests/harness.ts @@ -50,7 +50,7 @@ export const patient = { timeout: 30_000, interval: 200 } export interface Statement { verb: { id: string } object?: { id?: string; definition?: { extensions?: Record } } - context?: { contextActivities?: { parent?: { id: string }[] } } + context?: { contextActivities?: { parent?: { id: string }[] }; extensions?: Record } result?: { score?: { raw: number; max: number; scaled?: number }; success?: boolean; completion?: boolean; response?: string } } diff --git a/design/testing.md b/design/testing.md index bc1e862b..53ff8d2e 100644 --- a/design/testing.md +++ b/design/testing.md @@ -156,10 +156,11 @@ below says that each of the pack's types starts; a journey says that one of them - **A journey is written from the content type's own script, read first.** What a type scores, what a wrong answer costs, when it offers Retry and what it saves are in its script, not in the labels of its fields, and each case names one of them: "takes a point off for each wrong answer, - and never goes below nothing". Three things only the scripts said: Question Set offers Finish + and never goes below nothing". Four things only the scripts said: Question Set offers Finish only once every question has an answer; True/False unchecks an answer in a timeout of its own; - and an Interactive Video reports a result only with a submit screen, which H5P's editor gives a - new video (#317). + an Interactive Video reports a result only with a submit screen, which H5P's editor gives a new + video (#317); and Course Presentation reports each time its summary slide is drawn, and never + without one: where the author hid it, or in a deck with no task. - **The package is built in the test by h5p-author, from a short source**, so the test knows the right answers. It plays against the repository's copy of the pack, on a page the verifier's server serves (`tests/harness.ts`), with no network. Each play has a browser profile of its @@ -180,8 +181,8 @@ below says that each of the pack's types starts; a journey says that one of them doing: reported as [h5p/h5p-drag-text#231](https://github.com/h5p/h5p-drag-text/issues/231). Question Set's fourth question is typed there instead. -Covered: Multiple Choice 1.16, True/False 1.8, Fill in the Blanks 1.14, Drag the Words 1.10 and -Question Set 1.21. +Covered: Multiple Choice 1.16, True/False 1.8, Fill in the Blanks 1.14, Drag the Words 1.10, +Question Set 1.21 and Course Presentation 1.27. ## The content-type sweep