|
| 1 | +import { deflateRawSync } from 'node:zlib' |
| 2 | +import { expect, test, type Page } from '@playwright/test' |
| 3 | + |
| 4 | +/* |
| 5 | + * The build page, /h5p/build/, in a real browser against the built site: a source pasted, opened |
| 6 | + * from a link or naming a file of its own becomes a package that plays on the page and downloads |
| 7 | + * from it, and a source with problems gets them listed, all at once, for the chat that wrote it. |
| 8 | + * Every library comes from this origin, and any Content-Security-Policy violation or page error |
| 9 | + * fails the test. |
| 10 | + */ |
| 11 | + |
| 12 | +const RIVERS = { |
| 13 | + type: 'H5P.Blanks', |
| 14 | + title: 'Rivers', |
| 15 | + language: 'en', |
| 16 | + license: 'CC BY', |
| 17 | + authors: [{ name: 'Ann Author', role: 'Author' }], |
| 18 | + params: { questions: ['The *Danube* flows into the Black Sea.'] } |
| 19 | +} |
| 20 | + |
| 21 | +/** A 2×2 PNG, red. */ |
| 22 | +const PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAEElEQVR42mP4z8AARAwQCgAf7gP9Y167WwAAAABJRU5ErkJggg==', 'base64') |
| 23 | + |
| 24 | +/** Collects console errors, CSP reports, page errors and requests that leave the origin. */ |
| 25 | +function watch(page: Page, baseURL: string | undefined): { problems: string[], elsewhere: string[] } { |
| 26 | + const problems: string[] = [] |
| 27 | + const elsewhere: string[] = [] |
| 28 | + page.on('console', (message) => { |
| 29 | + const text = message.text() |
| 30 | + if (message.type() === 'error' || /Content.Security.Policy/i.test(text)) problems.push(text) |
| 31 | + }) |
| 32 | + page.on('pageerror', (error) => problems.push(error.message)) |
| 33 | + page.on('request', (request) => { |
| 34 | + const url = request.url() |
| 35 | + if (/^https?:/.test(url) && !url.startsWith(baseURL ?? '')) elsewhere.push(url) |
| 36 | + }) |
| 37 | + return { problems, elsewhere } |
| 38 | +} |
| 39 | + |
| 40 | +const playerState = (page: Page) => page.locator('h5p-player').evaluate((element: HTMLElement & { state?: string }) => element.state) |
| 41 | + |
| 42 | +async function expectPlays(page: Page): Promise<void> { |
| 43 | + await expect(page.locator('#status')).toHaveAttribute('data-state', 'ready', { timeout: 40_000 }) |
| 44 | + await expect.poll(() => playerState(page)).toBe('ready') |
| 45 | + await expect(page.frameLocator('h5p-player iframe').first().locator('.h5p-content, .h5p-container').first()).toBeVisible() |
| 46 | +} |
| 47 | + |
| 48 | +test('a pasted source builds, plays and downloads', async ({ page, baseURL }) => { |
| 49 | + const { problems, elsewhere } = watch(page, baseURL) |
| 50 | + await page.goto('/h5p/build/') |
| 51 | + await expect(page.locator('meta[http-equiv="Content-Security-Policy"]')).toHaveCount(1) |
| 52 | + // The site's own layout: its theme switch, which site.js shows once it runs under the page's policy. |
| 53 | + await expect(page.locator('[data-theme-toggle]')).toBeVisible() |
| 54 | + await expect(page.locator('#files-step')).toBeHidden() |
| 55 | + // A chat hands the source in a code fence; the page takes what is inside it. |
| 56 | + await page.locator('#source').fill('```json\n' + JSON.stringify(RIVERS, null, 2) + '\n```') |
| 57 | + await page.locator('#build').click() |
| 58 | + await expectPlays(page) |
| 59 | + await expect(page.locator('#status')).toContainText('Built “Rivers”: H5P.Blanks, ') |
| 60 | + await expect(page.locator('#problems-box')).toBeHidden() |
| 61 | + await expect(page.frameLocator('h5p-player iframe').first().locator('.h5p-blanks')).toContainText('flows into the Black Sea') |
| 62 | + // The player's worker is scoped to its own folder of assets: no other page of the site is under it. |
| 63 | + const scopes = await page.evaluate(async () => (await navigator.serviceWorker.getRegistrations()).map(({ scope }) => new URL(scope).pathname)) |
| 64 | + expect(scopes).toEqual(['/h5p/assets/h5p/']) |
| 65 | + |
| 66 | + const [download] = await Promise.all([page.waitForEvent('download'), page.locator('#download').click()]) |
| 67 | + expect(download.suggestedFilename()).toBe('rivers.h5p') |
| 68 | + const stream = await download.createReadStream() |
| 69 | + const chunks: Buffer[] = [] |
| 70 | + for await (const chunk of stream) chunks.push(chunk as Buffer) |
| 71 | + const zip = Buffer.concat(chunks) |
| 72 | + expect(zip.subarray(0, 4).toString('latin1'), 'a zip').toBe('PK\x03\x04') |
| 73 | + // The libraries an H5P site's upload check asks for are in it, as the command's build writes them. |
| 74 | + expect(zip.includes(Buffer.from('H5PEditor.'))).toBe(true) |
| 75 | + |
| 76 | + expect(elsewhere, 'nothing was fetched from another site').toEqual([]) |
| 77 | + expect(problems, problems.join('\n')).toEqual([]) |
| 78 | +}) |
| 79 | + |
| 80 | +test('a source with problems gets them all listed, ready for the chat', async ({ page, context, baseURL }) => { |
| 81 | + const { problems } = watch(page, baseURL) |
| 82 | + await context.grantPermissions(['clipboard-read', 'clipboard-write']) |
| 83 | + await page.goto('/h5p/build/') |
| 84 | + const { title: _title, ...untitled } = RIVERS |
| 85 | + await page.locator('#source').fill(JSON.stringify({ ...untitled, colour: 'blue' })) |
| 86 | + await page.locator('#build').click() |
| 87 | + await expect(page.locator('#status')).toHaveAttribute('data-state', 'error') |
| 88 | + const listed = page.locator('#problems li') |
| 89 | + // Each problem by its path, every one at once. |
| 90 | + await expect(listed.locator('code', { hasText: /^colour$/ })).toHaveCount(1) |
| 91 | + await expect(listed.locator('code', { hasText: /^title$/ })).toHaveCount(1) |
| 92 | + await expect(page.locator('#play-step')).toBeHidden() |
| 93 | + |
| 94 | + await page.locator('#copy-problems').click() |
| 95 | + await expect(page.locator('#copy-result')).toHaveText('Copied.') |
| 96 | + const copied = await page.evaluate(() => navigator.clipboard.readText()) |
| 97 | + expect(copied).toMatch(/^The build page found \d+ problems in the source\. Fix them all and write the whole source again:\n- /) |
| 98 | + expect(copied).toContain('- colour: ') |
| 99 | + |
| 100 | + // Text that is not JSON is a problem of its own, said where it is. |
| 101 | + await page.locator('#source').fill('{ "type": "H5P.Blanks", ') |
| 102 | + await page.locator('#build').click() |
| 103 | + await expect(listed).toHaveCount(1) |
| 104 | + await expect(listed.first()).toContainText('is not valid JSON') |
| 105 | + expect(problems, problems.join('\n')).toEqual([]) |
| 106 | +}) |
| 107 | + |
| 108 | +test('a link carrying the source builds it at once, deflated or not', async ({ page, baseURL }) => { |
| 109 | + const { problems, elsewhere } = watch(page, baseURL) |
| 110 | + const deflated = deflateRawSync(Buffer.from(JSON.stringify(RIVERS))).toString('base64url') |
| 111 | + await page.goto(`/h5p/build/#source=${deflated}`) |
| 112 | + await expectPlays(page) |
| 113 | + await expect(page.locator('#source')).toHaveValue(JSON.stringify(RIVERS)) |
| 114 | + |
| 115 | + // Plain base64url, with umlauts and ß: the bytes are UTF-8, and the file name keeps the letters. |
| 116 | + const plain = Buffer.from(JSON.stringify({ ...RIVERS, language: 'de', title: 'Größte Flüsse' })).toString('base64url') |
| 117 | + await page.goto(`/h5p/build/#source=${plain}`) |
| 118 | + await expect(page.locator('#status')).toContainText('Built “Größte Flüsse”') |
| 119 | + await expectPlays(page) |
| 120 | + await expect(page.locator('#download')).toHaveAttribute('download', 'größte-flüsse.h5p') |
| 121 | + |
| 122 | + // A title in another script keeps its letters too, rather than becoming activity.h5p. |
| 123 | + await page.goto(`/h5p/build/#source=${Buffer.from(JSON.stringify({ ...RIVERS, language: 'uk', title: 'Річки України' })).toString('base64url')}`) |
| 124 | + await expectPlays(page) |
| 125 | + await expect(page.locator('#download')).toHaveAttribute('download', 'річки-україни.h5p') |
| 126 | + |
| 127 | + expect(elsewhere, 'nothing was fetched from another site').toEqual([]) |
| 128 | + expect(problems, problems.join('\n')).toEqual([]) |
| 129 | +}) |
| 130 | + |
| 131 | +test('a Question Set shows its cover title as written, tags and entities nowhere', async ({ page, baseURL }) => { |
| 132 | + const { problems } = watch(page, baseURL) |
| 133 | + // h5p-author 0.5.0 wrapped this plain field in <div>, which the set draws as text (h5p-offline-player#164). |
| 134 | + const title = "Germany's Surrender: die größten Ereignisse" |
| 135 | + const quiz = { |
| 136 | + type: 'H5P.QuestionSet', title: 'Surrender', language: 'en', license: 'CC BY', authors: [{ name: 'Ann Author', role: 'Author' }], |
| 137 | + params: { |
| 138 | + introPage: { showIntroPage: true, title, introduction: 'Five questions.' }, |
| 139 | + questions: [{ library: 'H5P.TrueFalse', params: { question: 'The war in Europe ended in 1945.', correct: 'true' } }] |
| 140 | + } |
| 141 | + } |
| 142 | + await page.goto(`/h5p/build/#source=${Buffer.from(JSON.stringify(quiz)).toString('base64url')}`) |
| 143 | + await expectPlays(page) |
| 144 | + await expect(page.frameLocator('h5p-player iframe').first().locator('h2').first()).toHaveText(title) |
| 145 | + expect(problems, problems.join('\n')).toEqual([]) |
| 146 | +}) |
| 147 | + |
| 148 | +test('a source that names a file waits for it, then builds with it', async ({ page, baseURL }) => { |
| 149 | + const { problems } = watch(page, baseURL) |
| 150 | + const delta = { |
| 151 | + type: 'H5P.ImageHotspots', |
| 152 | + title: 'Delta', |
| 153 | + language: 'en', |
| 154 | + license: 'CC BY', |
| 155 | + authors: [{ name: 'Ann Author', role: 'Author' }], |
| 156 | + params: { |
| 157 | + image: { path: 'pictures/delta.png' }, |
| 158 | + backgroundImageAltText: 'A river delta seen from above', |
| 159 | + hotspots: [{ position: { x: 30, y: 40 }, header: 'Main channel', content: [{ library: 'H5P.Text', params: { text: 'Most of the water flows here.' } }] }] |
| 160 | + } |
| 161 | + } |
| 162 | + await page.goto(`/h5p/build/#source=${Buffer.from(JSON.stringify(delta)).toString('base64url')}`) |
| 163 | + await expect(page.locator('#files-step')).toBeVisible() |
| 164 | + await expect(page.locator('#build-number')).toHaveText('3') |
| 165 | + await expect(page.locator('#file-list li')).toHaveAttribute('data-chosen', 'false') |
| 166 | + await expect(page.locator('#status')).toHaveText('The source names files: choose them, then Build.') |
| 167 | + |
| 168 | + await page.locator('#files').setInputFiles({ name: 'delta.png', mimeType: 'image/png', buffer: PNG }) |
| 169 | + await expect(page.locator('#file-list li')).toHaveAttribute('data-chosen', 'true') |
| 170 | + await page.locator('#build').click() |
| 171 | + await expectPlays(page) |
| 172 | + await expect(page.locator('#status')).toContainText('1 media file') |
| 173 | + // The picture reaches the content and decodes: 2×2, as chosen. |
| 174 | + await expect.poll(() => page.frameLocator('h5p-player iframe').first().locator('img').first().evaluate((img: HTMLImageElement) => img.naturalWidth)).toBe(2) |
| 175 | + expect(problems, problems.join('\n')).toEqual([]) |
| 176 | +}) |
0 commit comments