mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Add progress export/import (JSON file) with strict import validation and change events
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -417,6 +417,42 @@ test('carte de progression et badges : états en texte, badge obtenu à la fin d
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('sauvegarde : exporter la progression sur un appareil, l\'importer sur un autre', { skip, timeout: 120000 }, async () => {
|
||||
const errors = [];
|
||||
const open = async () => {
|
||||
const page = await (await browser.newContext({ acceptDownloads: true })).newPage();
|
||||
page.on('pageerror', (error) => errors.push(error.message));
|
||||
await page.goto(base);
|
||||
await page.waitForSelector('.explain', { timeout: 60000 });
|
||||
return page;
|
||||
};
|
||||
|
||||
// Appareil A : termine le niveau 00 puis exporte.
|
||||
const a = await open();
|
||||
for (let i = 0; i < 5; i++) await a.locator('.notebook-action button').last().click();
|
||||
await a.waitForSelector('.toast');
|
||||
await a.locator('#settings-toggle').click();
|
||||
const [download] = await Promise.all([a.waitForEvent('download'), a.getByRole('button', { name: 'Exporter ma progression' }).click()]);
|
||||
assert.match(download.suggestedFilename(), /^pyquest-progress-\d{4}-\d{2}-\d{2}\.json$/);
|
||||
const path = await download.path();
|
||||
const content = readFileSync(path, 'utf8');
|
||||
assert.ok('00-intro' in JSON.parse(content).completedLevels);
|
||||
|
||||
// Appareil B (profil vierge) : un fichier invalide est refusé, le bon est fusionné.
|
||||
const b = await open();
|
||||
await b.locator('#settings-toggle').click();
|
||||
const input = b.locator('.settings input[type=file]');
|
||||
await input.setInputFiles({ name: 'mauvais.json', mimeType: 'application/json', buffer: Buffer.from('{"version":1,"completedLevels":{"x":{"xp":"beaucoup"}}}') });
|
||||
await b.waitForFunction(() => /pas une progression PyQuest valide/.test(document.querySelector('.toast-region')?.textContent ?? ''));
|
||||
await input.setInputFiles({ name: 'progression.json', mimeType: 'application/json', buffer: Buffer.from(content) });
|
||||
await b.waitForFunction(() => /1 nouveau\(x\) niveau/.test(document.querySelector('.toast-region')?.textContent ?? ''));
|
||||
await b.keyboard.press('Escape');
|
||||
await b.locator('#levels-toggle').click();
|
||||
assert.match(await b.locator('.level-button.is-done').first().textContent(), /Terminé/);
|
||||
assert.match(await b.textContent('.badge-count'), /1 badge\(s\) obtenu\(s\)/);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('éditeur de niveaux (mode auteur) : écrire un niveau, le vérifier, l\'essayer, l\'exporter', { skip, timeout: 240000 }, async () => {
|
||||
const { spawnSync } = await import('node:child_process');
|
||||
const { mkdtempSync, writeFileSync } = await import('node:fs');
|
||||
|
||||
@@ -22,4 +22,29 @@ assert.equal(merged.xp, 140); assert.equal(Object.keys(merged.completedLevels).l
|
||||
assert.equal(merged.streak.lastDay, '2026-10-20');
|
||||
await assert.rejects(p2.import('{"version":99}'));
|
||||
await assert.rejects(new ProgressStore(kv).completeLevel('x', 1));
|
||||
assert.equal(merged.addedLevels, 1);
|
||||
// Fichier hostile ou corrompu : rejeté sans toucher à l'état
|
||||
const good = JSON.parse(other.export());
|
||||
const bad = [
|
||||
{ ...good, completedLevels: { x: { xp: 'beaucoup', completedAt: 'a' } } },
|
||||
{ ...good, completedLevels: { x: { xp: -5, completedAt: 'a' } } },
|
||||
{ ...good, completedLevels: { x: { xp: 1e12, completedAt: 'a' } } },
|
||||
{ ...good, completedLevels: JSON.parse('{"__proto__": {"xp": 1, "completedAt": "a"}}') },
|
||||
{ ...good, completedLevels: { '../etc': { xp: 1, completedAt: 'a' } } },
|
||||
{ ...good, badges: [] },
|
||||
{ ...good, streak: { current: 'x', best: 1, lastDay: null } },
|
||||
[],
|
||||
null,
|
||||
];
|
||||
const xpBefore = p2.snapshot().xp;
|
||||
for (const candidate of bad) {
|
||||
await assert.rejects(p2.import(JSON.stringify(candidate)));
|
||||
}
|
||||
assert.equal(p2.snapshot().xp, xpBefore);
|
||||
assert.equal(({}).xp, undefined);
|
||||
// Un changement persisté est signalé (utilisé par la synchronisation)
|
||||
let changes = 0;
|
||||
p2.addEventListener('change', () => (changes += 1));
|
||||
await p2.recordActivity(d('2026-10-21'));
|
||||
assert.equal(changes, 1);
|
||||
console.log('progress ok');
|
||||
|
||||
Reference in New Issue
Block a user