From 62d26570b14a48646dedaf3a99a5158f8d7f1a6c Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Thu, 3 Dec 2026 14:14:01 +0200 Subject: [PATCH 1/4] test(journeys): the Column case requires the page's id to be an address (#314) --- apps/journeys/tests/column.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/journeys/tests/column.test.ts b/apps/journeys/tests/column.test.ts index a608b702..2a8c23f8 100644 --- a/apps/journeys/tests/column.test.ts +++ b/apps/journeys/tests/column.test.ts @@ -64,7 +64,8 @@ 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 an empty one, would let the parents match vacuously. + expect(page).toMatch(/^https?:\/\/\S+$/) expect(answered.map((statement) => statement.context?.contextActivities?.parent?.[0].id)).toEqual([page, page]) }) From b24a5b371767981ae093bcbb822fc754e1998cbc Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Thu, 3 Dec 2026 14:52:32 +0200 Subject: [PATCH 2/4] fix(runtime): hear the statements content sends as it starts (#343) The boot script attached its xAPI listener after H5P.init(), so a statement sent as content attaches never reached the page: Accordion's consumed, and the attempted the core's setActivityStarted sends for H5P.Question types, Course Presentation and Interactive Video. It is now attached before init, as the core's own relay is. The journeys check Accordion's consumed and list attempted where they list every verb. --- apps/journeys/tests/accordion.test.ts | 7 ++++--- apps/journeys/tests/course-presentation.test.ts | 7 ++++--- apps/journeys/tests/drag-and-drop.test.ts | 4 ++-- apps/journeys/tests/interactive-video.test.ts | 7 +++---- design/frame.md | 4 ++++ packages/runtime/src/frame-boot.ts | 8 +++++--- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/apps/journeys/tests/accordion.test.ts b/apps/journeys/tests/accordion.test.ts index 2a07109d..5cafb545 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`, complete and unscored, from `attach`; opening 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/course-presentation.test.ts b/apps/journeys/tests/course-presentation.test.ts index e7b13718..1780295f 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) }) 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..2fc0fe6a 100644 --- a/design/frame.md +++ b/design/frame.md @@ -224,3 +224,7 @@ 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 heard + none of it. Those statements reach the element before `ready`; the journeys pin them. diff --git a/packages/runtime/src/frame-boot.ts b/packages/runtime/src/frame-boot.ts index ba3139f3..64d993df 100644 --- a/packages/runtime/src/frame-boot.ts +++ b/packages/runtime/src/frame-boot.ts @@ -640,9 +640,8 @@ 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 dispatcher = H5P.externalDispatcher; if (dispatcher) { dispatcher.on('xAPI', function (event) { @@ -655,6 +654,9 @@ askForUserData() } }); } + if (typeof H5P.init === 'function') H5P.init(); + H5P.preventInit = false; + // 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; From e1948199800dfc03cfd15f058748d864317fea9c Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Thu, 3 Dec 2026 15:03:44 +0200 Subject: [PATCH 3/4] fix(runtime): a result said while the content attaches is no finished (#343) With the listener ahead of H5P.init(), a Course Presentation resumed on its summary slide raised finished on load: it says completed again as the slide is drawn. H5P's core listens for a result only once the content is attached, so finished now waits for init to return, and the statement still goes out as xapi. A journey pins both. --- apps/journeys/tests/accordion.test.ts | 2 +- apps/journeys/tests/course-presentation.test.ts | 16 ++++++++++++++++ design/frame.md | 5 +++-- design/results.md | 11 +++++++---- docs/api.md | 2 +- packages/runtime/src/frame-boot.ts | 7 +++++-- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/apps/journeys/tests/accordion.test.ts b/apps/journeys/tests/accordion.test.ts index 5cafb545..fea10d5e 100644 --- a/apps/journeys/tests/accordion.test.ts +++ b/apps/journeys/tests/accordion.test.ts @@ -79,7 +79,7 @@ describe('Accordion', () => { await learner.page.waitForTimeout(2500) const { statements, finished, saved } = await learner.heard() - // `consumed`, complete and unscored, from `attach`; opening a panel says nothing. + // `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([]) diff --git a/apps/journeys/tests/course-presentation.test.ts b/apps/journeys/tests/course-presentation.test.ts index 1780295f..5c4f2657 100644 --- a/apps/journeys/tests/course-presentation.test.ts +++ b/apps/journeys/tests/course-presentation.test.ts @@ -231,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/design/frame.md b/design/frame.md index 2fc0fe6a..ee457edb 100644 --- a/design/frame.md +++ b/design/frame.md @@ -226,5 +226,6 @@ The document the H5P runtime runs in, and what it loads: the boot route and its 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 heard - none of it. Those statements reach the element before `ready`; the journeys pin them. + `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 64d993df..f1ef0aea 100644 --- a/packages/runtime/src/frame-boot.ts +++ b/packages/runtime/src/frame-boot.ts @@ -642,20 +642,23 @@ askForUserData() } // 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. From b6fdc49cd7c0ea84c85bb8e8bfe088d659a8897e Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Thu, 3 Dec 2026 15:12:37 +0200 Subject: [PATCH 4/4] test(journeys): the Column case parses the page's id, so an address with no host fails (#314) --- apps/journeys/tests/column.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/journeys/tests/column.test.ts b/apps/journeys/tests/column.test.ts index 2a8c23f8..0592e356 100644 --- a/apps/journeys/tests/column.test.ts +++ b/apps/journeys/tests/column.test.ts @@ -64,8 +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')) - // An xAPI activity id is an address: none, or an empty one, would let the parents match vacuously. - expect(page).toMatch(/^https?:\/\/\S+$/) + // 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]) })