Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions apps/journeys/tests/accordion.test.ts
Original file line number Diff line number Diff line change
@@ -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: `<p>${words}</p>` } } })

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([])
})
})
114 changes: 114 additions & 0 deletions apps/journeys/tests/column.test.ts
Original file line number Diff line number Diff line change
@@ -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: '<p>Two questions about rivers.</p>' } } }
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
Comment thread
alekswebnet marked this conversation as resolved.
Resolved
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 })
})
})
136 changes: 136 additions & 0 deletions apps/journeys/tests/dialog-cards.test.ts
Original file line number Diff line number Diff line change
@@ -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: `<p>${front}</p>`, answer: `<p>${back}</p>` })
const backs: Record<string, string> = { 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)
})
})
Loading