diff --git a/apps/journeys/tests/accordion.test.ts b/apps/journeys/tests/accordion.test.ts new file mode 100644 index 00000000..2a07109d --- /dev/null +++ b/apps/journeys/tests/accordion.test.ts @@ -0,0 +1,91 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, source, type Learner } from './harness' + +/** + * Accordion 1.0, as `h5p-accordion.js` runs it: every panel shut at first, one open at a time, + * moved through by the arrow keys. It scores nothing, has no result and keeps nothing. + */ + +const { build, play } = journeys() + +const panel = (title: string, words: string) => ({ title, content: { library: 'H5P.AdvancedText', params: { text: `

${words}

` } } }) + +beforeAll(async () => { + await build( + 'river', + source('H5P.Accordion', { + panels: [panel('Source', 'Where a river begins.'), panel('Course', 'The way it takes.'), panel('Mouth', 'Where it ends, in a sea or a lake.')] + }) + ) +}) + +const title = ({ frame }: Learner, name: string) => frame.getByRole('button', { name }) +/** The titles of the panels that are open, as a screen reader is told. */ +const open = ({ frame }: Learner) => frame.locator('.h5p-panel-button[aria-expanded="true"]').allInnerTexts() +/** Waits for a panel's words to be there, or gone: a panel slides for 200 ms. */ +const shows = ({ frame }: Learner, words: string, state: 'visible' | 'hidden' = 'visible') => frame.getByText(words).waitFor({ state }) + +describe('Accordion', () => { + it('starts with every panel shut, and opens one on a press of its title', async () => { + const learner = await play('river') + expect(await open(learner)).toEqual([]) + expect(await learner.frame.getByText('Where a river begins.').isVisible()).toBe(false) + + await title(learner, 'Source').click() + await shows(learner, 'Where a river begins.') + expect(await open(learner)).toEqual(['Source']) + }) + + it('shuts the open panel when another is opened, and an open one on a second press of its title', async () => { + const learner = await play('river') + await title(learner, 'Source').click() + await shows(learner, 'Where a river begins.') + + await title(learner, 'Mouth').click() + await shows(learner, 'Where it ends, in a sea or a lake.') + await shows(learner, 'Where a river begins.', 'hidden') + expect(await open(learner)).toEqual(['Mouth']) + + await title(learner, 'Mouth').click() + await shows(learner, 'Where it ends, in a sea or a lake.', 'hidden') + expect(await open(learner)).toEqual([]) + }) + + it('moves between the titles with the arrow keys, stops at the ends, and opens with Enter and Space', async () => { + const learner = await play('river') + const focused = () => learner.frame.locator('.h5p-panel-button:focus').allInnerTexts() + await title(learner, 'Source').focus() + + await learner.page.keyboard.press('ArrowUp') + expect(await focused()).toEqual(['Source']) + await learner.page.keyboard.press('ArrowDown') + await learner.page.keyboard.press('ArrowRight') + await learner.page.keyboard.press('ArrowDown') + expect(await focused()).toEqual(['Mouth']) + await learner.page.keyboard.press('ArrowLeft') + expect(await focused()).toEqual(['Course']) + + await learner.page.keyboard.press('Enter') + await shows(learner, 'The way it takes.') + await learner.page.keyboard.press('Space') + await shows(learner, 'The way it takes.', 'hidden') + }) + + it('has no result to report and keeps nothing: a learner who comes back finds every panel shut', async () => { + const learner = await play('river', { resume: 'device', 'save-interval': '1' }) + await title(learner, 'Course').click() + await shows(learner, 'The way it takes.') + // Long enough for a save, were there a state to save. + await learner.page.waitForTimeout(2500) + + const { statements, finished, saved } = await learner.heard() + // Opening a panel says nothing, and nothing the type says has a score. + expect(statements.filter((statement) => statement.result?.score)).toEqual([]) + expect(finished).toEqual([]) + expect(saved.filter((save) => save.data !== null)).toEqual([]) + + await learner.comeBack() + await title(learner, 'Course').waitFor() + expect(await open(learner)).toEqual([]) + }) +}) diff --git a/apps/journeys/tests/column.test.ts b/apps/journeys/tests/column.test.ts new file mode 100644 index 00000000..a608b702 --- /dev/null +++ b/apps/journeys/tests/column.test.ts @@ -0,0 +1,114 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, patient, source, type Learner, type Statement } from './harness' + +/** + * Column 1.22, as `scripts/h5p-column.js` runs it: it scores nothing itself, counts the content + * it knows as tasks, and reports the sum of their last results once each of them has one. + */ + +const { build, play } = journeys() + +const text = { content: { library: 'H5P.AdvancedText', params: { text: '

Two questions about rivers.

' } } } +const rhine = { content: { library: 'H5P.TrueFalse', metadata: { title: 'The Rhine' }, params: { question: 'The Rhine flows into the North Sea.', correct: 'true' } } } +const longest = { + content: { + library: 'H5P.MultiChoice', + metadata: { title: 'The longest river' }, + params: { + question: 'Which river is the longest in Europe?', + answers: [ + { text: 'Volga', correct: true }, + { text: 'Danube', correct: false } + ] + } + } +} +/** A row of one column, as the editor writes one: its pieces bare, with no separator beside each. */ +const row = (...pieces: { content: unknown }[]) => ({ + content: { library: 'H5P.Row', params: { columns: [{ content: { library: 'H5P.RowColumn', params: { content: pieces.map((piece) => piece.content) } } }] } } +}) + +beforeAll(async () => { + await build('page', source('H5P.Column', { content: [text, rhine, longest] })) + await build('reading', source('H5P.Column', { content: [text] })) + await build('rows', source('H5P.Column', { content: [rhine, row(text, longest)] })) +}) + +const check = /^Check/ +const retry = /^Retry/ +/** A question of the page, by its words. */ +const task = ({ frame }: Learner, words: string) => frame.locator('.h5p-column-content', { hasText: words }) +const answer = async (learner: Learner, words: string, choice: string) => { + await task(learner, words).getByRole('radio', { name: choice }).click() + await task(learner, words).getByRole('button', { name: check }).click() +} +/** How far the page says the learner is: the place of the task each `progressed` names. */ +const progress = (statements: Statement[]) => + statements + .filter((statement) => statement.verb.id.endsWith('/progressed')) + .map((statement) => statement.object?.definition?.extensions?.['http://id.tincanapi.com/extension/ending-point']) + +describe('Column', () => { + it('reports once every task has a result: the sum of them, with the text counted as no task', async () => { + const learner = await play('page') + await answer(learner, 'The Rhine', 'True') + await task(learner, 'The Rhine').getByRole('button', { name: check }).waitFor({ state: 'detached' }) + // One task of two: the page says how far, and nothing of a result. + await expect.poll(async () => progress((await learner.heard()).statements)).toEqual([1]) + expect((await learner.heard()).finished).toEqual([]) + + await answer(learner, 'longest', 'Volga') + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: undefined }) + const { statements } = await learner.heard() + expect(progress(statements)).toEqual([1, 2]) + // Each question's own answer names the page as what it is a part of. + const page = statements.find((statement) => statement.verb.id.endsWith('/completed'))?.object?.id + const answered = statements.filter((statement) => statement.verb.id.endsWith('/answered')) + expect(page).toEqual(expect.any(String)) + expect(answered.map((statement) => statement.context?.contextActivities?.parent?.[0].id)).toEqual([page, page]) + }) + + it('reports again each time a task is checked after that, with the sum as it then stands', async () => { + const learner = await play('page') + await answer(learner, 'The Rhine', 'False') + await answer(learner, 'longest', 'Volga') + expect(await learner.result()).toEqual({ raw: 1, max: 2, success: undefined }) + + await task(learner, 'The Rhine').getByRole('button', { name: retry }).click() + await answer(learner, 'The Rhine', 'True') + expect(await learner.result(2)).toEqual({ raw: 2, max: 2, success: undefined }) + }) + + it('reports nothing for a page with no task on it', async () => { + const learner = await play('reading') + await learner.frame.getByText('Two questions about rivers.').waitFor() + // Nothing to wait for: a page of text has no result to send, at the start or later. + await learner.page.waitForTimeout(1500) + const { statements, finished } = await learner.heard() + expect(finished).toEqual([]) + expect(statements.filter((statement) => /\/(completed|progressed)$/.test(statement.verb.id))).toEqual([]) + }) + + it('counts a task in a row beside the tasks of the page', async () => { + const learner = await play('rows') + await answer(learner, 'longest', 'Volga') + expect((await learner.heard()).finished).toEqual([]) + + await answer(learner, 'The Rhine', 'True') + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: undefined }) + }) + + it('comes back with each task as a learner had left it', async () => { + const learner = await play('page', { resume: 'device', 'save-interval': '1' }) + await task(learner, 'longest').getByRole('radio', { name: 'Volga' }).click() + // One state for the page, with each piece's own by its place: none for the text. + await expect.poll(async () => ((await learner.saved()) as { instances: unknown[] }).instances.length, patient).toBe(3) + + await learner.comeBack() + + await expect.poll(() => task(learner, 'longest').getByRole('radio', { name: 'Volga' }).isChecked()).toBe(true) + await task(learner, 'longest').getByRole('button', { name: check }).click() + await answer(learner, 'The Rhine', 'True') + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: undefined }) + }) +}) diff --git a/apps/journeys/tests/dialog-cards.test.ts b/apps/journeys/tests/dialog-cards.test.ts new file mode 100644 index 00000000..9de66645 --- /dev/null +++ b/apps/journeys/tests/dialog-cards.test.ts @@ -0,0 +1,136 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, patient, source, type Learner } from './harness' + +/** + * Dialog Cards 1.9, as `src/scripts/h5p-dialogcards.js` and its card manager run it: cards to + * turn and leaf through, or, in repetition, rounds that bring a card back until it is known. + */ + +const { build, play } = journeys() + +const card = (front: string, back: string) => ({ text: `

${front}

`, answer: `

${back}

` }) +const backs: Record = { source: 'where a river begins', mouth: 'where a river ends', delta: 'where it fans out', bank: 'its edge' } +const cards = (...fronts: string[]) => fronts.map((front) => card(front, backs[front])) + +beforeAll(async () => { + await build('cards', source('H5P.Dialogcards', { dialogs: cards('source', 'mouth') })) + await build('drill', source('H5P.Dialogcards', { dialogs: cards('source', 'mouth', 'delta', 'bank'), mode: 'repetition' })) + await build('one', source('H5P.Dialogcards', { dialogs: cards('source'), mode: 'repetition', behaviour: { maxProficiency: 3 } })) +}) + +/** A button on the card in front: every card has its own Turn, and the one before is still sliding away. */ +const onCard = ({ frame }: Learner, name: string) => frame.locator('.h5p-dialogcards-current').getByRole('button', { name }) +/** Presses a button under the cards, or on the summary. */ +const press = ({ frame }: Learner, name: string | RegExp) => frame.getByRole('button', { name }).filter({ visible: true }).first().click() +const offered = ({ frame }: Learner, name: string | RegExp) => frame.getByRole('button', { name }).filter({ visible: true }).count() +/** The words on the card in front: its front, or its back once turned. */ +const shown = async ({ frame }: Learner) => (await frame.locator('.h5p-dialogcards-current .h5p-dialogcards-card-text').first().innerText()).trim() +/** The line under the cards: which card of how many, or the round and how many cards it still holds. */ +const footer = async ({ frame }: Learner) => (await frame.locator('.h5p-dialogcards-round:visible, .h5p-theme-progress:visible').allInnerTexts()).join(', ') +/** Turns the card in front and says whether the learner knew it, in a round. */ +const answer = async (learner: Learner, knew: (front: string) => boolean) => { + const front = await shown(learner) + await onCard(learner, 'Turn').click() + await expect.poll(() => shown(learner)).toBe(backs[front]) + await onCard(learner, knew(front) ? 'I got it right!' : 'I got it wrong').click() + return front +} + +describe('Dialog Cards', () => { + it('shows a card’s back on Turn, and its front again on the next', async () => { + const learner = await play('cards') + expect(await shown(learner)).toBe('source') + + await onCard(learner, 'Turn').click() + await expect.poll(() => shown(learner)).toBe('where a river begins') + await onCard(learner, 'Turn').click() + await expect.poll(() => shown(learner)).toBe('source') + }) + + it('leafs through the cards with Next and Previous, counting them, and offers Retry once the last is turned', async () => { + const learner = await play('cards') + expect(await footer(learner)).toBe('Card 1 of 2') + expect(await offered(learner, 'Previous')).toBe(0) + + await press(learner, 'Next') + await expect.poll(() => footer(learner)).toBe('Card 2 of 2') + await expect.poll(() => shown(learner)).toBe('mouth') + expect(await offered(learner, 'Next')).toBe(0) + // On the last card, and Retry not there until it has been turned. + expect(await offered(learner, 'Retry')).toBe(0) + + await onCard(learner, 'Turn').click() + await press(learner, 'Retry') + await expect.poll(() => footer(learner)).toBe('Card 1 of 2') + await expect.poll(() => shown(learner)).toBe('source') + }) + + it('asks whether the learner knew a card only once it is turned, in repetition', async () => { + const learner = await play('drill') + expect(await footer(learner)).toBe('Round 1, Cards left: 4') + expect(await onCard(learner, 'I got it right!').isDisabled()).toBe(true) + expect(await onCard(learner, 'I got it wrong').isDisabled()).toBe(true) + + await onCard(learner, 'Turn').click() + await expect.poll(() => onCard(learner, 'I got it right!').isEnabled()).toBe(true) + await onCard(learner, 'I got it right!').click() + await expect.poll(() => footer(learner)).toBe('Round 1, Cards left: 3') + }) + + it('ends a round on a summary, and brings into the next every card got wrong and half of those got right', async () => { + const learner = await play('drill') + const known = new Set(['source', 'mouth']) + for (let turn = 0; turn < 4; turn++) await answer(learner, (front) => known.has(front)) + // After the last card its Turn gives way to a Next, which shows the round's summary. + await onCard(learner, 'Next').click() + const summary = () => learner.frame.locator('.h5p-dialogcards-summary-screen:visible').innerText() + await expect.poll(async () => (await summary()).replace(/\s+/g, ' ')).toMatch(/Cards you got right: 2 \/ 4 .*Cards you got wrong: 2 \/ 4 .*Cards you have completed learning: 0 \/ 4/) + + await press(learner, 'Proceed to round 2') + // Two wrong, and one of the two got right once: a card known is asked half as often. + await expect.poll(() => footer(learner)).toBe('Round 2, Cards left: 3') + const asked: string[] = [] + for (let turn = 0; turn < 3; turn++) asked.push(await answer(learner, () => true)) + expect(asked).toEqual(expect.arrayContaining(['delta', 'bank'])) + expect(asked.filter((front) => known.has(front))).toHaveLength(1) + }) + + it('counts a card completed once it was known as many rounds running as the author set, and then offers only Retry', async () => { + const learner = await play('one') + const summary = async () => (await learner.frame.locator('.h5p-dialogcards-summary-screen:visible').innerText()).replace(/\s+/g, ' ') + for (const round of [1, 2]) { + await answer(learner, () => true) + await onCard(learner, 'Next').click() + await expect.poll(summary).toMatch(/Cards you have completed learning: 0 \/ 1/) + await press(learner, `Proceed to round ${round + 1}`) + await expect.poll(() => footer(learner)).toBe(`Round ${round + 1}, Cards left: 1`) + } + await answer(learner, () => true) + await onCard(learner, 'Next').click() + + await expect.poll(summary).toMatch(/Well done! You have mastered all 1 cards by getting them correct 3 times!/) + // No further round and no Start over: Retry, which begins again at round 1. + expect(await offered(learner, /^Proceed to round/)).toBe(0) + expect(await offered(learner, 'Start over')).toBe(0) + await press(learner, 'Retry') + await expect.poll(() => footer(learner)).toBe('Round 1, Cards left: 1') + }) + + it('comes back in the round and on the card a learner had reached, and has no result to report', async () => { + const learner = await play('drill', { resume: 'device', 'save-interval': '1' }) + const first = await answer(learner, () => true) + await expect.poll(() => footer(learner)).toBe('Round 1, Cards left: 3') + const second = await shown(learner) + type Saved = { round: number; currentCardId: number; results: { result: boolean }[] } + await expect.poll(async () => { const state = (await learner.saved()) as Saved; return [state.round, state.currentCardId, state.results.map((each) => each.result)] }, patient).toEqual([1, 1, [true]]) + const { statements, finished } = await learner.heard() + expect(statements.filter((statement) => statement.result)).toEqual([]) + expect(finished).toEqual([]) + + await learner.comeBack() + + await expect.poll(() => footer(learner)).toBe('Round 1, Cards left: 3') + expect(await shown(learner)).toBe(second) + expect(second).not.toBe(first) + }) +}) diff --git a/apps/journeys/tests/drag-and-drop.test.ts b/apps/journeys/tests/drag-and-drop.test.ts new file mode 100644 index 00000000..7df440e4 --- /dev/null +++ b/apps/journeys/tests/drag-and-drop.test.ts @@ -0,0 +1,197 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, patient, source, type Learner } from './harness' + +/** + * Drag and Drop 1.15, as `src/drag-question.js` and `src/draggable.js` score it: a point for each + * element in a zone it belongs in, one off for each in any other, and never below nothing. + */ + +const { build, play } = journeys() + +const words = (text: string) => ({ library: 'H5P.AdvancedText', params: { text: `

${text}

` } }) +const anywhere = ['0', '1'] +/** Two rivers, each right in one city, and a third that is right in neither. */ +const rivers = (behaviour: Record = {}, zone: Record = {}) => + source('H5P.DragQuestion', { + question: { + task: { + elements: [ + { type: words('Danube'), dropZones: anywhere }, + { type: words('Tiber'), dropZones: anywhere }, + { type: words('Volga'), dropZones: anywhere } + ], + dropZones: [ + { label: 'Vienna', correctElements: ['0'], tipsAndFeedback: { feedbackOnCorrect: 'It runs through Vienna.', feedbackOnIncorrect: 'Not through Vienna.' }, ...zone }, + { label: 'Rome', correctElements: ['1'], ...zone } + ] + } + }, + overallFeedback: [ + { from: 0, to: 50, feedback: '@score of @total: look at a map.' }, + { from: 51, to: 100, feedback: 'All @total.' } + ], + behaviour + }) + +beforeAll(async () => { + await build('rivers', rivers()) + await build('no-penalty', rivers({ applyPenalties: false, enableRetry: false })) + await build('one-point', rivers({ singlePoint: true })) + await build('one-each', rivers({}, { single: true })) + await build( + 'copies', + source('H5P.DragQuestion', { + question: { + task: { + elements: [{ type: words('a capital'), dropZones: anywhere, multiple: true }], + dropZones: [ + { label: 'Vienna', correctElements: ['0'] }, + { label: 'Rome', correctElements: ['0'] } + ] + } + } + }) + ) +}) + +const check = /^Check/ +const retry = /^Retry/ +/** An element still where it began, and a zone by its label. */ +const element = ({ frame }: Learner, text: string) => frame.locator('.h5p-draggable:not(.h5p-dropped)', { hasText: text }).first() +const zone = ({ frame }: Learner, label: string) => frame.locator('.h5p-dropzone', { hasText: label }) +const drop = (learner: Learner, text: string, label: string) => learner.drag(element(learner, text), zone(learner, label)) +/** What the last statement says the learner did: each placed element as `zone[.]element`. */ +const response = async (learner: Learner) => (await learner.heard()).finished.at(-1)?.result?.response + +describe('Drag and Drop', () => { + it('gives a point for each element in a zone it belongs in, and then leaves nothing to try', async () => { + const learner = await play('rivers') + await drop(learner, 'Danube', 'Vienna') + await drop(learner, 'Tiber', 'Rome') + await learner.frame.getByRole('button', { name: check }).click() + + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: true }) + expect(await response(learner)).toBe('0[.]0[,]1[.]1') + await learner.says('All 2.') + await learner.checked() + expect(await learner.frame.getByRole('button', { name: retry }).count()).toBe(0) + // Each drop is said as it happens, and the answer once. + expect(await learner.verbs()).toEqual(['interacted', 'interacted', 'answered']) + }) + + it('takes a point off for each element in a wrong zone, and never goes below nothing', async () => { + const learner = await play('rivers') + await drop(learner, 'Danube', 'Vienna') + await drop(learner, 'Tiber', 'Vienna') + await learner.frame.getByRole('button', { name: check }).click() + // One right and one wrong: nothing. + expect(await learner.result()).toEqual({ raw: 0, max: 2, success: false }) + await learner.says('0 of 2: look at a map.') + + await learner.frame.getByRole('button', { name: retry }).click() + await drop(learner, 'Danube', 'Rome') + await drop(learner, 'Tiber', 'Vienna') + await learner.frame.getByRole('button', { name: check }).click() + // Two wrong would be two below. + expect(await learner.result(2)).toEqual({ raw: 0, max: 2, success: false }) + }) + + it('takes a point off for an element that belongs nowhere, wherever it is dropped', async () => { + const learner = await play('rivers') + await drop(learner, 'Danube', 'Vienna') + await drop(learner, 'Tiber', 'Rome') + await drop(learner, 'Volga', 'Rome') + await learner.frame.getByRole('button', { name: check }).click() + + expect(await learner.result()).toEqual({ raw: 1, max: 2, success: false }) + }) + + it('puts every element back on Retry, and scores what is placed after it alone', async () => { + const learner = await play('rivers') + await drop(learner, 'Tiber', 'Vienna') + await drop(learner, 'Volga', 'Rome') + await learner.frame.getByRole('button', { name: check }).click() + expect(await learner.result()).toEqual({ raw: 0, max: 2, success: false }) + + await learner.frame.getByRole('button', { name: retry }).click() + await expect.poll(() => learner.frame.locator('.h5p-draggable:not(.h5p-dropped)').count()).toBe(3) + await drop(learner, 'Danube', 'Vienna') + await learner.frame.getByRole('button', { name: check }).click() + expect(await learner.result(2)).toEqual({ raw: 1, max: 2, success: false }) + expect(await response(learner)).toBe('0[.]0') + }) + + it('takes nothing off where the author turned penalties off, and offers no Retry where they turned that off', async () => { + const learner = await play('no-penalty') + await drop(learner, 'Danube', 'Vienna') + await drop(learner, 'Tiber', 'Vienna') + await drop(learner, 'Volga', 'Rome') + await learner.frame.getByRole('button', { name: check }).click() + + expect(await learner.result()).toEqual({ raw: 1, max: 2, success: false }) + await learner.checked() + expect(await learner.frame.getByRole('button', { name: retry }).count()).toBe(0) + }) + + it('gives one point for the whole task where the author asked, and only for all of it', async () => { + const all = await play('one-point') + await drop(all, 'Danube', 'Vienna') + await drop(all, 'Tiber', 'Rome') + await all.frame.getByRole('button', { name: check }).click() + expect(await all.result()).toEqual({ raw: 1, max: 1, success: true }) + + const most = await play('one-point') + await drop(most, 'Danube', 'Vienna') + await most.frame.getByRole('button', { name: check }).click() + expect(await most.result()).toEqual({ raw: 0, max: 1, success: false }) + }) + + it('hands out another copy of an element with endless copies, and counts each zone it belongs in', async () => { + const learner = await play('copies') + await drop(learner, 'a capital', 'Vienna') + // The one dragged left another where it began. + await drop(learner, 'a capital', 'Rome') + await learner.frame.getByRole('button', { name: check }).click() + + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: true }) + expect(await response(learner)).toBe('0[.]0[,]1[.]0') + }) + + it('sends a second element back from a zone made for one', async () => { + const learner = await play('one-each') + await drop(learner, 'Danube', 'Vienna') + await drop(learner, 'Tiber', 'Vienna') + await learner.frame.getByRole('button', { name: check }).click() + + // The Tiber, sent back, is neither right nor wrong. + expect(await learner.result()).toEqual({ raw: 1, max: 2, success: false }) + expect(await response(learner)).toBe('0[.]0') + }) + + it('says the zone’s own words for a right match and for a wrong one, below the task', async () => { + const right = await play('rivers') + await drop(right, 'Danube', 'Vienna') + await right.frame.getByRole('button', { name: check }).click() + await right.frame.getByText('It runs through Vienna.').first().waitFor() + + const wrong = await play('rivers') + await drop(wrong, 'Tiber', 'Vienna') + await wrong.frame.getByRole('button', { name: check }).click() + await wrong.frame.getByText('Not through Vienna.').first().waitFor() + expect(await wrong.frame.getByText('It runs through Vienna.').count()).toBe(0) + }) + + it('comes back with each element in the zone a learner had left it in', async () => { + const learner = await play('rivers', { resume: 'device', 'save-interval': '1' }) + await drop(learner, 'Tiber', 'Rome') + // Kept by the element's place in the task, with the zone's: nothing for the Danube before it. + await expect.poll(() => learner.saved(), patient).toEqual({ answers: [null, [{ x: expect.any(Number), y: expect.any(Number), dz: 1 }]] }) + + await learner.comeBack() + + await expect.poll(() => learner.frame.locator('.h5p-draggable.h5p-dropped').allInnerTexts()).toEqual([expect.stringContaining('Tiber')]) + await drop(learner, 'Danube', 'Vienna') + await learner.frame.getByRole('button', { name: check }).click() + expect(await learner.result()).toEqual({ raw: 2, max: 2, success: true }) + }) +}) diff --git a/apps/journeys/tests/image-hotspots.test.ts b/apps/journeys/tests/image-hotspots.test.ts new file mode 100644 index 00000000..911d9df9 --- /dev/null +++ b/apps/journeys/tests/image-hotspots.test.ts @@ -0,0 +1,130 @@ +import { resolve } from 'node:path' +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, source, type Learner } from './harness' + +/** + * Image Hotspots 1.11, as `scripts/hotspot.js` and `popup.js` run it: a spot opens its words over + * the picture, one at a time, and the type scores, reports and keeps nothing. + */ + +const { build, play } = journeys() +/** The demo's own picture: the journeys carry no file of their own. */ +const media = resolve(import.meta.dirname, '../../demo/demo') + +const spot = (x: number, y: number, header: string, words: string, more: Record = {}) => ({ + position: { x, y }, + header, + content: [{ library: 'H5P.Text', params: { text: `

${words}

` } }], + ...more +}) +const delta = (more: Record = {}) => + source('H5P.ImageHotspots', { + image: { path: 'og-image.png' }, + backgroundImageAltText: 'A river delta seen from above', + hotspots: [spot(30, 40, 'Main channel', 'Most of the water flows here.', more), spot(70, 65, 'Mouth', 'Here the river meets the sea.')] + }) + +beforeAll(async () => { + await build('delta', delta(), media) + await build('wide', delta({ alwaysFullscreen: true }), media) +}) + +/** A spot by its place among the spots: while a popup is open, the spots are hidden from a screen reader. */ +const spotAt = ({ frame }: Learner, index: number) => frame.locator('.h5p-image-hotspot').nth(index) +const popup = ({ frame }: Learner) => frame.getByRole('dialog') +/** Waits for a popup with `words` to have arrived: it slides in for 300 ms, takes the focus, and its spot ignores a press till then. */ +const opened = async (learner: Learner, words: string) => { + await popup(learner).getByText(words).waitFor() + await expect.poll(() => popup(learner).evaluate((dialog) => dialog === document.activeElement)).toBe(true) +} +const closed = (learner: Learner) => popup(learner).waitFor({ state: 'detached' }) + +describe('Image Hotspots', () => { + it('opens a spot’s words under its header on a press, and moves the focus into them', async () => { + const learner = await play('delta') + expect(await popup(learner).count()).toBe(0) + + await learner.frame.getByRole('button', { name: 'Main channel' }).click() + await opened(learner, 'Most of the water flows here.') + expect(await popup(learner).innerText()).toMatch(/^Main channel\s+Most of the water flows here\.$/) + }) + + it('closes from its own Close, and leaves the spots within reach again', async () => { + const learner = await play('delta') + await learner.frame.getByRole('button', { name: 'Main channel' }).click() + await opened(learner, 'Most of the water flows here.') + // While one is open the spots are out of the tab order. + expect(await learner.frame.locator('.h5p-image-hotspot[tabindex="0"]').count()).toBe(0) + + await learner.frame.getByRole('button', { name: 'Close' }).click() + await closed(learner) + expect(await learner.frame.locator('.h5p-image-hotspot[tabindex="0"]').count()).toBe(2) + }) + + it('lays a spot’s words beside it, over the other spot, and closes on a press on the picture', async () => { + const learner = await play('delta') + await spotAt(learner, 0).click() + await opened(learner, 'Most of the water flows here.') + // One at a time: the words lie over the spot to the right, which cannot be pressed until they go. + const words = await popup(learner).boundingBox() + const other = await spotAt(learner, 1).boundingBox() + if (!words || !other) throw new Error('The popup or the other spot is not on the page') + expect(other.x).toBeGreaterThan(words.x) + expect(other.x + other.width).toBeLessThan(words.x + words.width) + + const picture = await learner.frame.locator('.h5p-image-hotspots-container').boundingBox() + if (!picture) throw new Error('No picture on the page') + await learner.page.mouse.click(picture.x + picture.width * 0.1, picture.y + picture.height * 0.85) + await closed(learner) + + await spotAt(learner, 1).click() + await opened(learner, 'Here the river meets the sea.') + }) + + it('opens on Enter and on Space, and Close by key gives the spot the focus back', async () => { + const learner = await play('delta') + await spotAt(learner, 1).focus() + await learner.page.keyboard.press('Enter') + await opened(learner, 'Here the river meets the sea.') + + await learner.frame.getByRole('button', { name: 'Close' }).focus() + await learner.page.keyboard.press('Enter') + await closed(learner) + await expect.poll(() => spotAt(learner, 1).evaluate((button) => button === document.activeElement)).toBe(true) + + await learner.page.keyboard.press('Space') + await opened(learner, 'Here the river meets the sea.') + }) + + it('lays a spot’s words over the whole picture where the author asked for that', async () => { + const beside = await play('delta') + await beside.frame.getByRole('button', { name: 'Main channel' }).click() + await opened(beside, 'Most of the water flows here.') + const wide = await play('wide') + await wide.frame.getByRole('button', { name: 'Main channel' }).click() + await opened(wide, 'Most of the water flows here.') + + const picture = (learner: Learner) => learner.frame.locator('.h5p-image-hotspots-container').boundingBox() + const widths = async (learner: Learner) => [Math.round((await popup(learner).boundingBox())?.width ?? 0), Math.round((await picture(learner))?.width ?? 0)] + // Beside its spot a popup takes the room to the spot's right; over the picture it takes all of it. + await expect.poll(async () => { const [words, whole] = await widths(wide); return words === whole }).toBe(true) + const [words, whole] = await widths(beside) + expect(words).toBeLessThan(whole * 0.75) + }) + + it('reports nothing and keeps nothing: a learner who comes back finds every spot closed', async () => { + const learner = await play('delta', { resume: 'device', 'save-interval': '1' }) + await learner.frame.getByRole('button', { name: 'Main channel' }).click() + await opened(learner, 'Most of the water flows here.') + // Long enough for a save, were there a state to save. + await learner.page.waitForTimeout(2500) + const { statements, finished, saved } = await learner.heard() + expect(statements.filter((statement) => statement.result)).toEqual([]) + expect(finished).toEqual([]) + expect(saved.filter((save) => save.data !== null)).toEqual([]) + + await learner.comeBack() + await spotAt(learner, 0).waitFor() + expect(await popup(learner).count()).toBe(0) + }) +}) diff --git a/apps/journeys/tests/interactive-book.test.ts b/apps/journeys/tests/interactive-book.test.ts new file mode 100644 index 00000000..b2cf29a7 --- /dev/null +++ b/apps/journeys/tests/interactive-book.test.ts @@ -0,0 +1,138 @@ +import { beforeAll, describe, expect, it } from 'vitest' +import { journeys, patient, source, type Learner, type Statement } from './harness' + +/** + * Interactive Book 1.15, as `src/scripts/app.js` and `summary.js` run it: each chapter says when + * it is done, and the book reports its own result only from Submit Report on its summary. + */ + +const { build, play } = journeys() + +const page = (title: string, ...pieces: unknown[]) => ({ library: 'H5P.Column', metadata: { title }, params: { content: pieces.map((content) => ({ content })) } }) +const text = { library: 'H5P.AdvancedText', params: { text: '

A river begins at its source.

' } } +const rhine = { library: 'H5P.TrueFalse', metadata: { title: 'The Rhine' }, params: { question: 'The Rhine flows into the North Sea.', correct: 'true' } } +const chapters = [page('Sources', text), page('Check yourself', rhine)] + +beforeAll(async () => { + await build('book', source('H5P.InteractiveBook', { chapters })) + await build('by-hand', source('H5P.InteractiveBook', { chapters, behaviour: { progressAuto: false } })) + await build('covered', source('H5P.InteractiveBook', { chapters, showCoverPage: true, bookCover: { coverDescription: '

Rivers, from source to sea.

' } })) +}) + +const check = /^Check/ +/** The bar's own button of that name: the bar is drawn above the page and again below it. */ +const bar = ({ frame }: Learner, name: string) => frame.getByRole('button', { name }).first() +/** Waits for the page to stop moving: a chapter slides in, and the book ignores a press while one does. */ +const settled = ({ frame }: Learner) => expect.poll(() => frame.locator('.h5p-interactive-book-animate').count()).toBe(0) +const next = async (learner: Learner) => { + await settled(learner) + await bar(learner, 'Next page').click() + await settled(learner) +} +const summary = async (learner: Learner) => { + await settled(learner) + await bar(learner, 'Summary & submit').click() +} +/** Each `completed` a chapter or the book has sent, as its score. */ +const completed = (statements: Statement[]) => + statements.filter((statement) => statement.verb.id.endsWith('/completed')).map((statement) => `${statement.result?.score?.raw}/${statement.result?.score?.max}`) +const chapterShown = ({ frame }: Learner) => frame.locator('.h5p-interactive-book-navigation-current').innerText() + +describe('Interactive Book', () => { + it('counts a chapter done once each of its tasks has an answer, checked or not, and one with no task on moving on', async () => { + const learner = await play('book') + expect(completed((await learner.heard()).statements)).toEqual([]) + + await next(learner) + await expect.poll(async () => completed((await learner.heard()).statements)).toEqual(['0/0']) + + // An answer chosen, and Check not pressed: the chapter is done, with the score it would get. + await learner.frame.getByRole('radio', { name: 'True' }).click() + await expect.poll(async () => completed((await learner.heard()).statements)).toEqual(['0/0', '1/1']) + // Nothing of that is the book's own result. + expect((await learner.heard()).finished).toEqual([]) + }) + + it('reports itself only from Submit Report on its summary: the sum of its chapters, once', async () => { + const learner = await play('book', { reporting: '' }) + await next(learner) + await learner.frame.getByRole('radio', { name: 'True' }).click() + await learner.frame.getByRole('button', { name: check }).click() + await summary(learner) + // The summary lists each task with its score. + await expect.poll(() => learner.frame.locator('.h5p-interactive-book-summary-section-score:visible').allInnerTexts()).toEqual(['1 / 1']) + expect((await learner.heard()).finished).toEqual([]) + + await learner.frame.getByRole('button', { name: 'Submit Report' }).click() + expect(await learner.result()).toEqual({ raw: 1, max: 1, success: undefined }) + await learner.frame.getByText('Your answers are submitted for review!').waitFor() + // Sent once: the button goes with it. + await learner.frame.getByRole('button', { name: 'Submit Report' }).waitFor({ state: 'hidden' }) + }) + + it('offers no Submit Report where the host takes no reports, so the book has no result of its own', async () => { + const learner = await play('book') + await next(learner) + await learner.frame.getByRole('radio', { name: 'True' }).click() + await learner.frame.getByRole('button', { name: check }).click() + await summary(learner) + + await learner.frame.getByRole('button', { name: 'Restart' }).waitFor() + expect(await learner.frame.getByRole('button', { name: 'Submit Report' }).count()).toBe(0) + expect((await learner.heard()).finished).toEqual([]) + }) + + it('starts every chapter over on Restart, from the first', async () => { + const learner = await play('book', { reporting: '' }) + await next(learner) + await learner.frame.getByRole('radio', { name: 'False' }).click() + await learner.frame.getByRole('button', { name: check }).click() + await summary(learner) + await learner.frame.getByRole('button', { name: 'Restart' }).click() + await expect.poll(() => chapterShown(learner)).toContain('Sources') + + await next(learner) + await expect.poll(() => learner.frame.getByRole('radio', { name: 'False' }).isChecked()).toBe(false) + await learner.frame.getByRole('radio', { name: 'True' }).click() + await learner.frame.getByRole('button', { name: check }).click() + await summary(learner) + await learner.frame.getByRole('button', { name: 'Submit Report' }).click() + expect(await learner.result()).toEqual({ raw: 1, max: 1, success: undefined }) + }) + + it('leaves it to the learner to say a page is finished where the author turned automatic progress off', async () => { + const learner = await play('by-hand') + await next(learner) + await learner.frame.getByRole('radio', { name: 'True' }).click() + // An answer chosen, which is a chapter done where progress is automatic: here it is not. The + // book hears the choice before the page does, so a chapter done would have been said by now. + await expect.poll(() => learner.verbs()).toContain('interacted') + expect(completed((await learner.heard()).statements)).toEqual([]) + + await learner.frame.getByRole('checkbox', { name: 'I have finished this page' }).check() + await expect.poll(async () => completed((await learner.heard()).statements)).toEqual(['1/1']) + }) + + it('opens on its cover where the author asked for one, and on the first chapter from Read', async () => { + const learner = await play('covered') + await learner.frame.getByText('Rivers, from source to sea.').waitFor() + expect(await learner.frame.getByText('A river begins at its source.').isVisible()).toBe(false) + + await learner.frame.getByRole('button', { name: 'Read' }).click() + await learner.frame.getByText('A river begins at its source.').waitFor() + }) + + it('comes back on the chapter a learner was reading, with what they had answered', async () => { + const learner = await play('book', { resume: 'device', 'save-interval': '1' }) + await next(learner) + await learner.frame.getByRole('radio', { name: 'True' }).click() + type Saved = { chapters: { completed: boolean | null }[]; score?: number } + // The place in the book, each chapter's own state, and the score so far once past the first. + await expect.poll(async () => { const state = (await learner.saved()) as Saved; return [state.chapters.map((chapter) => chapter.completed), state.score] }, patient).toEqual([[true, true], 1]) + + await learner.comeBack() + + await expect.poll(() => chapterShown(learner)).toContain('Check yourself') + await expect.poll(() => learner.frame.getByRole('radio', { name: 'True' }).isChecked()).toBe(true) + }) +}) diff --git a/design/testing.md b/design/testing.md index a7ad26f2..94acb695 100644 --- a/design/testing.md +++ b/design/testing.md @@ -163,13 +163,22 @@ 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". Four things only the scripts said: Question Set offers Finish + and never goes below nothing". Nine 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; an Interactive Video reports a result only on Submit, on a submit screen that H5P's editor gives a new video (#317) and a video with no question never shows, and counts an answer chosen but - never checked; 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. + never checked; 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; Drag and Drop takes a point + off for an element that belongs in no zone wherever it is dropped, and counts an element with + endless copies once for each zone it belongs in; Column reports only once every task on it has + a result, then again each time one is checked, and never for a page with no task; + Interactive Book counts a chapter done once each of its tasks has an answer, checked or not, and + reports its own result only from Submit Report on its summary, which it offers only where the + host takes reports (`reporting`); Image Hotspots lays a spot's words over the other spots, so + one is open at a time; and Dialog Cards, in repetition, brings into the next round every card + got wrong and half of those got right once. What slides is waited for: a book's chapter, a + spot's words (300 ms, its spot deaf till then) and the card before the one in front, whose own + Turn is still on the page. - **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 @@ -200,7 +209,8 @@ below says that each of the pack's types starts; a journey says that one of them 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, -Question Set 1.21, Course Presentation 1.27 and Interactive Video 1.28. +Question Set 1.21, Course Presentation 1.27, Interactive Video 1.28, Drag and Drop 1.15, +Column 1.22, Interactive Book 1.15, Image Hotspots 1.11, Accordion 1.0 and Dialog Cards 1.9. ## The content-type sweep