diff --git a/apps/journeys/tests/accordion.test.ts b/apps/journeys/tests/accordion.test.ts index 2a07109d..fea10d5e 100644 --- a/apps/journeys/tests/accordion.test.ts +++ b/apps/journeys/tests/accordion.test.ts @@ -71,7 +71,7 @@ describe('Accordion', () => { 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 () => { + it('says it was consumed as it starts, and nothing more: no result, and 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.') @@ -79,8 +79,9 @@ describe('Accordion', () => { 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([]) + // `consumed` (its id ends in `consume`), complete and unscored, from `attach`; a panel says nothing. + expect(await learner.verbs()).toEqual(['consume']) + expect(statements[0].result).toEqual({ completion: true }) expect(finished).toEqual([]) expect(saved.filter((save) => save.data !== null)).toEqual([]) diff --git a/apps/journeys/tests/column.test.ts b/apps/journeys/tests/column.test.ts index a608b702..0592e356 100644 --- a/apps/journeys/tests/column.test.ts +++ b/apps/journeys/tests/column.test.ts @@ -64,7 +64,9 @@ describe('Column', () => { // 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)) + // An xAPI activity id is an address: none, or one with no host, would let the parents match vacuously. + const address = page && URL.canParse(page) ? new URL(page) : null + expect(address?.protocol).toMatch(/^https?:$/) expect(answered.map((statement) => statement.context?.contextActivities?.parent?.[0].id)).toEqual([page, page]) }) diff --git a/apps/journeys/tests/course-presentation.test.ts b/apps/journeys/tests/course-presentation.test.ts index e7b13718..5c4f2657 100644 --- a/apps/journeys/tests/course-presentation.test.ts +++ b/apps/journeys/tests/course-presentation.test.ts @@ -180,15 +180,16 @@ describe('Course Presentation', () => { 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 () => { + it('reports no result for a deck with no task: it has no summary slide, and says only that it started and 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']) + await expect.poll(() => learner.verbs()).toEqual(['attempted', 'progressed']) const { statements, finished } = await learner.heard() - expect(statements[0].object?.definition?.extensions?.[ENDING_POINT]).toBe(2) + const progressed = statements.find((statement) => statement.verb.id.endsWith('/progressed')) + expect(progressed?.object?.definition?.extensions?.[ENDING_POINT]).toBe(2) expect(await summary(learner).count()).toBe(0) expect(finished).toHaveLength(0) }) @@ -230,4 +231,20 @@ describe('Course Presentation', () => { // The first question counts as it was answered before the learner left. expect(await learner.result()).toEqual({ raw: 4, max: 4, success: undefined }) }) + + it('says its result again to a learner who comes back to the summary slide, as a statement and not a result', async () => { + const learner = await play('deck', { resume: 'device', 'save-interval': '1' }) + await answer(learner, [true, true, 2]) + await next(learner) + expect(await learner.result()).toEqual({ raw: 4, max: 4, success: undefined }) + await expect.poll(async () => ((await learner.saved()) as { progress?: number }).progress, patient).toBe(4) + + await learner.comeBack() + + await settled(learner, 4) + // Drawn again as the deck attaches, which is before H5P listens for a result, and so before `finished`. + expect(await learner.verbs()).toEqual(['attempted', 'progressed', 'completed']) + expect(await summary(learner).count()).toBe(1) + expect((await learner.heard()).finished).toEqual([]) + }) }) diff --git a/apps/journeys/tests/drag-and-drop.test.ts b/apps/journeys/tests/drag-and-drop.test.ts index 7df440e4..0c48e9cb 100644 --- a/apps/journeys/tests/drag-and-drop.test.ts +++ b/apps/journeys/tests/drag-and-drop.test.ts @@ -75,8 +75,8 @@ describe('Drag and Drop', () => { 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']) + // The start as it opens (the core's `setActivityStarted`), each drop as it happens, the answer once. + expect(await learner.verbs()).toEqual(['attempted', 'interacted', 'interacted', 'answered']) }) it('takes a point off for each element in a wrong zone, and never goes below nothing', async () => { diff --git a/apps/journeys/tests/interactive-video.test.ts b/apps/journeys/tests/interactive-video.test.ts index bbda9777..5eed365b 100644 --- a/apps/journeys/tests/interactive-video.test.ts +++ b/apps/journeys/tests/interactive-video.test.ts @@ -303,7 +303,7 @@ describe('Interactive Video', () => { expect(await learner.result()).toEqual({ raw: 1, max: 1, success: undefined }) }) - it('reports nothing for a video with no question: no star, no submit screen, and no statement', async () => { + it('reports no result for a video with no question: no star, no submit screen, and no statement but its start', async () => { const learner = await play('watch') await press(learner) await expect.poll(async () => (await clip(learner)).ended, patient).toBe(true) @@ -311,9 +311,8 @@ describe('Interactive Video', () => { expect(await learner.frame.getByRole('button', { name: 'Open summary dialog' }).count()).toBe(0) expect(await screen(learner).count()).toBe(0) - const { statements, finished } = await learner.heard() - expect(statements).toEqual([]) - expect(finished).toEqual([]) + expect(await learner.verbs()).toEqual(['attempted']) + expect((await learner.heard()).finished).toEqual([]) }) it('counts the summary task at the video’s end as a question', async () => { diff --git a/design/frame.md b/design/frame.md index 5c00024d..ee457edb 100644 --- a/design/frame.md +++ b/design/frame.md @@ -224,3 +224,8 @@ The document the H5P runtime runs in, and what it loads: the boot route and its because a resource failure fires on the element and never bubbles, and it reports only tags marked `data-h5p`, the ones the boot script added, the core's and the libraries'. A content image that 404s, or an optional script a content type reaches for, is the content's business. +- **The frame hears the content from its start.** The boot script's xAPI listener is attached + before `H5P.init()`, as the core's own relay is: content speaks as it attaches (Accordion's + `consumed`, the core's `attempted` from `setActivityStarted`), and a listener added after hears + none of it. Those statements reach the element before `ready`, and a result among them is no + `finished` (*`finished` is the content's result*); the journeys pin both. diff --git a/design/results.md b/design/results.md index fca76233..7c0d8572 100644 --- a/design/results.md +++ b/design/results.md @@ -58,10 +58,13 @@ What reaches the host from the content: statements and the build they name, `fin finishes, and a Question Set inside a Column finishes twice, for the inner set and then for the Column; cmi5 records the first `finished` of a registration, so it would complete on the wrong one. A question checked again says `answered` again, so `finished` can come more than once in a - load; the last is the result, as with `setFinished`. `finished.test.ts` plays `finished.h5p`, the - fixture content type with an Answer button, a child of itself as a part, and another child run - without a parent, and fails on the first rule for the part and for the answer, and on H5P's rule - for the part without a parent. + load; the last is the result, as with `setFinished`. A result said while the content attaches is + a statement and no `finished`, as the core adds its listener after `attach`: a Course + Presentation resumed on its summary slide says `completed` again as it is drawn, and reopening a + finished deck would otherwise record it again (the journeys pin it). `finished.test.ts` plays + `finished.h5p`, the fixture content type with an Answer button, a child of itself as a part, and + another child run without a parent, and fails on the first rule for the part and for the answer, + and on H5P's rule for the part without a parent. Some content types create their parts without a parent, so a part's result passes H5P's rule and the core's `setFinished` records it as the content's: Interactive Book 1.15 creates each chapter diff --git a/docs/api.md b/docs/api.md index 68d4e5fd..5f050315 100644 --- a/docs/api.md +++ b/docs/api.md @@ -55,7 +55,7 @@ Events are `CustomEvent`s: |---|---|---| | `ready` | `{ pkgId, source, metadata, revision, libraryBundle, downgradedLibraries }` | The content is visible | | `xapi` | `{ statement, verb }` | Any xAPI statement; nothing is stored | -| `finished` | `{ statement }` | The content reported its result: a `completed` or `answered` statement from the content itself, not from a part inside it. This is H5P's own `setFinished` rule, except that a part created without a parent still counts as a part (H5P's rule misses Interactive Book's chapters and Game Map's exercises). It can come more than once, for a question checked again; the last is the result. Content that reports no result of its own never raises it, and Interactive Book reports one only with `reporting` | +| `finished` | `{ statement }` | The content reported its result: a `completed` or `answered` statement from the content itself, not from a part inside it. This is H5P's own `setFinished` rule, except that a part created without a parent still counts as a part (H5P's rule misses Interactive Book's chapters and Game Map's exercises). It can come more than once, for a question checked again; the last is the result. A result the content repeats as it starts, as a presentation reopened on its summary slide does, is an `xapi` statement only, as on an H5P site. Content that reports no result of its own never raises it, and Interactive Book reports one only with `reporting` | | `userdata` | `{ pkgId, dataType, subContentId, data, revision }` | With `resume`: the content saved its state; `data: null` means delete it | | `progress` | `{ phase, fraction, loaded, total, entry? }` | `download`, `libraries`, `warm` or `extract`; `fraction` is 0–1, or `null` while the total is unknown | | `resize` | `{ height }` | The content's height changed | diff --git a/packages/runtime/src/frame-boot.ts b/packages/runtime/src/frame-boot.ts index ba3139f3..f1ef0aea 100644 --- a/packages/runtime/src/frame-boot.ts +++ b/packages/runtime/src/frame-boot.ts @@ -640,21 +640,26 @@ askForUserData() return content && content.contentUrl ? content.contentUrl : getContentPath!.call(H5P, contentId); }; } - if (typeof H5P.init === 'function') H5P.init(); - H5P.preventInit = false; - + // Before `init`, as the core's own relay is: content sends statements as it starts (Accordion's + // `consumed`, the core's `attempted`), and a listener added after hears none of them. + var attached = false; var dispatcher = H5P.externalDispatcher; if (dispatcher) { dispatcher.on('xAPI', function (event) { var statement = event && event.data ? event.data.statement : undefined; var verb = statement && statement.verb ? statement.verb.id : undefined; post({ type: 'xapi', statement: statement, verb: verb }); - // H5P's own rule for a result: `completed` or `answered`, from the content itself. - if (isResult(statement)) { + // H5P's own rule for a result: `completed` or `answered`, from the content itself, once it + // is attached, when the core starts listening for one: a deck resumed on its summary repeats it. + if (attached && isResult(statement)) { post({ type: 'finished', statement: statement }); } }); } + if (typeof H5P.init === 'function') H5P.init(); + H5P.preventInit = false; + attached = true; + // Sizing is H5P's own, from content resize events; a font arriving raises none, so text // measured in the fallback face kept its height (47 px lost): resize all once faces load. var fonts = document.fonts;