| 1 | // applyPatches — el aplicador de parches sobre el HTML que genera el modelo. |
| 2 | // Cada caso viene de lo que Gemma E4B emitió DE VERDAD al pedirle que arreglara |
| 3 | // un juego suyo. |
| 4 | import { createRequire } from 'module'; |
| 5 | const { chromium } = createRequire(import.meta.url)('playwright'); |
| 6 | |
| 7 | const HTML = [ |
| 8 | '<!doctype html><html><body>', |
| 9 | '<canvas id="c" width="400" height="300"></canvas>', |
| 10 | '<script>', |
| 11 | ' const ctx = document.getElementById("c").getContext("2d");', |
| 12 | ' const gameSpeed = 150;', |
| 13 | ' function bucle() {', |
| 14 | ' ctx.fillStyle = "#4CAF50";', |
| 15 | ' requestAnimationFrame(bucle);', |
| 16 | ' }', |
| 17 | ' bucle();', |
| 18 | '<\/script></body></html>', |
| 19 | ].join('\n'); |
| 20 | |
| 21 | const CASOS = [ |
| 22 | { id: 'una-linea', desc: 'formato de una línea, el de siempre', |
| 23 | resp: 'BUSCAR: const gameSpeed = 150;\nCAMBIAR: const gameSpeed = 90;\nMOTIVO: más rápido', |
| 24 | aplica: 1, trae: 'const gameSpeed = 90;' }, |
| 25 | |
| 26 | { id: 'no-op', desc: 'BUSCAR igual a CAMBIAR → se descarta (el modelo los emite)', |
| 27 | resp: 'BUSCAR: ctx.fillStyle = "#4CAF50";\nCAMBIAR: ctx.fillStyle = "#4CAF50";\nMOTIVO: unificar color', |
| 28 | aplica: 0, sinCambio: true }, |
| 29 | |
| 30 | { id: 'multilinea', desc: 'bloque de varias líneas: lo que hace falta para AÑADIR código', |
| 31 | resp: 'BUSCAR\n bucle();\nCAMBIAR\n bucle();\n document.addEventListener("keydown", e => { dir = e.key; });\nFIN', |
| 32 | aplica: 1, trae: 'addEventListener("keydown"' }, |
| 33 | |
| 34 | { id: 'sangria-mal', desc: 'el modelo cita con otra sangría', |
| 35 | resp: 'BUSCAR\nconst gameSpeed = 150;\nCAMBIAR\nconst gameSpeed = 60;\nFIN', |
| 36 | aplica: 1, trae: 'const gameSpeed = 60;' }, |
| 37 | |
| 38 | { id: 'inventado', desc: 'cita algo que no existe → se descarta, no se inventa', |
| 39 | resp: 'BUSCAR\nconst noExiste = 1;\nCAMBIAR\nconst noExiste = 2;\nFIN', |
| 40 | aplica: 0, sinCambio: true }, |
| 41 | |
| 42 | { id: 'ambiguo', desc: 'cita algo que aparece dos veces → se descarta', |
| 43 | resp: 'BUSCAR: }\nCAMBIAR: } // fin', |
| 44 | aplica: 0, sinCambio: true }, |
| 45 | |
| 46 | { id: 'varios', desc: 'varios parches en la misma respuesta', |
| 47 | resp: 'BUSCAR\n const gameSpeed = 150;\nCAMBIAR\n const gameSpeed = 80;\nFIN\n\nBUSCAR\n bucle();\nCAMBIAR\n bucle();\n window.addEventListener("keyup", () => {});\nFIN', |
| 48 | aplica: 2, trae: 'keyup' }, |
| 49 | ]; |
| 50 | |
| 51 | const b = await chromium.launch({ channel: 'chrome', headless: true }); |
| 52 | const p = await (await b.newContext()).newPage(); |
| 53 | await p.goto('https://claw.elffuss.com/', { waitUntil: 'domcontentloaded' }); |
| 54 | await p.waitForTimeout(1200); |
| 55 | |
| 56 | const res = await p.evaluate(async ({ CASOS, HTML }) => { |
| 57 | const rlm = await import('/js/rlm.js?v=' + Date.now()); |
| 58 | return CASOS.map(c => { |
| 59 | const r = rlm.applyPatches(HTML, c.resp); |
| 60 | return { id: c.id, aplicados: r.aplicados.length, fallidos: r.fallidos, cambio: r.html !== HTML, html: r.html }; |
| 61 | }); |
| 62 | }, { CASOS, HTML }); |
| 63 | |
| 64 | console.log('applyPatches — parches sobre el HTML generado\n'); |
| 65 | let ok = 0; |
| 66 | for (const c of CASOS) { |
| 67 | const r = res.find(x => x.id === c.id); |
| 68 | let bien = r.aplicados === c.aplica, nota = ''; |
| 69 | if (bien && c.trae && !r.html.includes(c.trae)) { bien = false; nota = `no metió «${c.trae}»`; } |
| 70 | if (bien && c.sinCambio && r.cambio) { bien = false; nota = 'cambió el HTML y no debía'; } |
| 71 | if (!bien && !nota) nota = `aplicó ${r.aplicados}, esperaba ${c.aplica}` + |
| 72 | (r.fallidos.length ? ` · descartes: ${r.fallidos.map(f => f.motivo).join(', ')}` : ''); |
| 73 | if (bien) ok++; |
| 74 | console.log(` ${bien ? '✓' : '✗'} ${c.id.padEnd(13)} ${c.desc}`); |
| 75 | if (!bien) console.log(` └─ ${nota}`); |
| 76 | } |
| 77 | console.log(`\n ${ok}/${CASOS.length} correctos`); |
| 78 | await b.close(); |
| 79 | process.exit(ok === CASOS.length ? 0 : 1); |