diff --git a/CONTEXT.md b/CONTEXT.md index ae43072..b79623a 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -34,10 +34,10 @@ Le brief original est [`prompt-pyquest.md`](prompt-pyquest.md) (résumé fidèle ## Reste à faire -1. **Tester dans un vrai navigateur** (`make dev`) : aucun test n'a encore tourné dans Chrome/Firefox/ - Safari. Le worker, le client et Pyodide sont validés ensemble sous Node - (`tests/js/pyodide-worker.integration.test.mjs`), mais pas l'UI réelle, le service worker, ni - IndexedDB. Prévoir un test sur iOS Safari (mémoire WASM, autoplay audio). +1. **Navigateurs** : parcours validé dans Chromium 155 (`make test-e2e` : niveaux 00 et 01, erreurs + traduites, interruption de boucle infinie en ~50 ms, persistance IndexedDB, hors-ligne via + service worker, thèmes clair/sombre, mobile 375 px sans débordement). **Reste** : Firefox, + Safari desktop et surtout iOS Safari (mémoire WASM, autoplay audio, `SharedArrayBuffer`). 2. Icônes PNG (iOS) en plus de `app/icons/icon.svg`. 3. Branchement du client de synchronisation (jeton Bearer + `ProgressStore.export/import`) : le backend (`backend/sync`) existe, pas encore le côté client ni l'UI. diff --git a/Makefile b/Makefile index 6deb38c..7c55715 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ VENV := .venv VENV_PY := $(VENV)/bin/python PORT ?= 8000 -.PHONY: help install vendor-pyodide dev validate-levels build test test-js test-python test-backend typecheck clean +.PHONY: help install vendor-pyodide dev validate-levels build test test-js test-python test-backend test-e2e typecheck clean help: ## Affiche cette aide @grep -E '^[a-z-]+:.*##' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf " %-16s %s\n", $$1, $$2}' @@ -41,6 +41,10 @@ test-backend: $(VENV)/.installed ## Tests du backend Django (nécessite Django d $(VENV)/bin/pip install --quiet -r backend/requirements.txt cd backend && ../$(VENV_PY) manage.py test +test-e2e: ## Tests bout en bout dans Chromium (CHROMIUM=/chemin/navigateur) + npm --prefix tests/e2e install --silent + node --test tests/e2e/e2e.test.mjs + typecheck: $(VENV)/.installed ## Vérifie les types Python avec pyright $(VENV)/bin/pyright diff --git a/app/css/app.css b/app/css/app.css index 0b1af36..240a6a2 100644 --- a/app/css/app.css +++ b/app/css/app.css @@ -256,3 +256,22 @@ body { grid-template-columns: 1fr; } } + +.explain > :first-child, +.cell > :first-child { + margin-top: 0; +} + +.level-button { + flex-wrap: wrap; + gap: 0.25rem 0.75rem; + text-align: left; +} + +.level-title { + flex: 1 1 8rem; +} + +.level-state { + white-space: nowrap; +} diff --git a/tests/e2e/e2e.test.mjs b/tests/e2e/e2e.test.mjs new file mode 100644 index 0000000..1d34850 --- /dev/null +++ b/tests/e2e/e2e.test.mjs @@ -0,0 +1,81 @@ +// Parcours de bout en bout dans Chromium : niveaux 00 et 01, erreurs, interruption, persistance. +// Prérequis : make vendor-pyodide ; un Chromium installé (variable CHROMIUM, défaut /usr/bin/chromium). +import { spawn } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { after, before, test } from 'node:test'; +import assert from 'node:assert'; +import { chromium } from 'playwright-core'; + +const root = new URL('../../', import.meta.url).pathname; +const executablePath = process.env.CHROMIUM ?? '/usr/bin/chromium'; +const port = 8125; +const base = `http://127.0.0.1:${port}/`; +const skip = !existsSync(`${root}vendor/pyodide/pyodide.mjs`) ? 'vendor/pyodide absent' : !existsSync(executablePath) ? `navigateur absent (${executablePath})` : false; + +let server; +let browser; + +before(async () => { + if (skip) return; + server = spawn('python3', [`${root}scripts/dev_server.py`, '--port', String(port)], { env: { ...process.env, PYQUEST_QUIET: '1' }, stdio: 'ignore' }); + await new Promise((resolve) => setTimeout(resolve, 1000)); + browser = await chromium.launch({ executablePath, args: ['--no-sandbox'] }); +}); + +after(async () => { + await browser?.close(); + server?.kill(); +}); + +test('parcours complet des niveaux 00 et 01', { skip, timeout: 180000 }, async () => { + const page = await (await browser.newContext()).newPage(); + const errors = []; + page.on('pageerror', (error) => errors.push(error.message)); + + await page.goto(base); + assert.equal(await page.evaluate(() => crossOriginIsolated), true, 'page isolée (COOP/COEP)'); + await page.waitForSelector('.explain', { timeout: 60000 }); + + const next = () => page.locator('.notebook-action button').last().click(); + const run = async (code) => { + const cell = page.locator('.cell').last(); + await cell.locator('textarea').fill(code); + await cell.locator('.btn-primary').click(); + await page.waitForFunction(() => document.querySelector('.cell:last-of-type')?.getAttribute('aria-busy') !== 'true'); + await page.waitForTimeout(150); + return (await cell.locator('.cell-feedback').textContent()).trim(); + }; + + // Niveau 00 : 4 explications puis « Je comprends ». + for (let i = 0; i < 4; i++) await next(); + await next(); + await page.waitForSelector('.toast'); + assert.match(await page.textContent('.progress-summary'), /10 XP/); + + // Niveau 01. + await page.locator('.level-button', { hasText: 'Ton premier programme' }).click(); + await page.waitForSelector('.explain'); + await next(); + assert.match(await run('print("Bonjour")'), /correct/); + await next(); + assert.match(await run('print("bonjour le monde")'), /majuscules/); + assert.match(await run('print(Bonjour)'), /Python ne connaît pas « Bonjour »/); + assert.match(await run('print("Bonjour le monde")'), /correct/); + + // Boucle infinie interrompue, puis cellule réutilisable. + const cell = page.locator('.cell').last(); + await cell.locator('textarea').fill('while True:\n pass'); + await cell.locator('.btn-primary').click(); + await page.waitForTimeout(500); + await cell.locator('.btn', { hasText: 'Arrêter' }).click(); + await page.waitForFunction(() => document.querySelector('.cell:last-of-type')?.getAttribute('aria-busy') !== 'true', null, { timeout: 15000 }); + assert.match(await cell.locator('.cell-feedback').textContent(), /Exécution arrêtée/); + assert.match(await run('print("Bonjour le monde")'), /correct/); + + // La progression survit au rechargement (IndexedDB). + await page.reload(); + await page.waitForSelector('.progress-map .level-button', { timeout: 60000 }); + assert.match(await page.textContent('.progress-map'), /10 XP/); + + assert.deepEqual(errors, []); +}); diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.json new file mode 100644 index 0000000..6503e14 --- /dev/null +++ b/tests/e2e/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "pyquest-e2e", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "pyquest-e2e", + "dependencies": { + "playwright-core": "1.49.1" + } + }, + "node_modules/playwright-core": { + "version": "1.49.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.49.1.tgz", + "integrity": "sha512-BzmpVcs4kE2CH15rWfzpjzVGhWERJfmnXmniSyKeRZUs9Ws65m+RGIi7mjJK/euCegfn3i7jvqWeWyHe9y3Vgg==", + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + } + } +} diff --git a/tests/e2e/package.json b/tests/e2e/package.json new file mode 100644 index 0000000..3a9cc26 --- /dev/null +++ b/tests/e2e/package.json @@ -0,0 +1,9 @@ +{ + "name": "pyquest-e2e", + "private": true, + "type": "module", + "description": "Tests de bout en bout dans un vrai navigateur (hors dépendances de l'application).", + "dependencies": { + "playwright-core": "1.49.1" + } +}