| 1 | // Verifica la Ciudad 3D (motor VibeCodeViewer vendorizado): siembra un proyecto |
| 2 | // anidado, abre la vista, comprueba que el WebGL pinta la metrópolis y captura. |
| 3 | import { chromium } from 'playwright'; |
| 4 | import { CHROME_ARGS } from './chrome-args.mjs'; |
| 5 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; |
| 8 | const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 9 | |
| 10 | const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS] }); |
| 11 | const ctx = await b.newContext(); |
| 12 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 13 | const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } }); |
| 14 | p.on('pageerror', e => { if (!/allow-same-origin/.test(e.message)) console.log(' pageerror:', e.message.slice(0, 160)); }); |
| 15 | p.on('console', m => { if (m.type() === 'error' && !/allow-same-origin/.test(m.text())) console.log(' console.error:', m.text().slice(0, 160)); }); |
| 16 | |
| 17 | // proyecto anidado (varios distritos, edificios con plantas y ficheros) |
| 18 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 19 | await p.evaluate(async () => { |
| 20 | const o = await navigator.storage.getDirectory(); |
| 21 | const w = async (path, txt) => { const parts = path.split('/'); const name = parts.pop(); let d = o; for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); const f = await d.getFileHandle(name, { create: true }); const s = await f.createWritable(); await s.write(txt); await s.close(); }; |
| 22 | const body = n => 'x'.repeat(200 + n * 137); |
| 23 | await w('README.md', '# demo city\n' + body(3)); |
| 24 | await w('package.json', '{ "name": "demo" }'); |
| 25 | for (const [i, f] of ['index.js', 'app.js', 'router.js', 'store.js'].entries()) await w('src/' + f, body(i + 2)); |
| 26 | for (const [i, f] of ['Button.jsx', 'Modal.jsx', 'Nav.jsx'].entries()) await w('src/components/' + f, body(i + 1)); |
| 27 | for (const [i, f] of ['api.js', 'auth.js'].entries()) await w('src/services/' + f, body(i + 4)); |
| 28 | for (const [i, f] of ['helpers.js', 'format.js', 'dates.js'].entries()) await w('lib/' + f, body(i)); |
| 29 | for (const [i, f] of ['unit.test.js', 'e2e.test.js'].entries()) await w('tests/' + f, body(i + 1)); |
| 30 | await w('docs/guide.md', body(6)); |
| 31 | }); |
| 32 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 33 | await p.waitForTimeout(2500); |
| 34 | |
| 35 | // abrir la Ciudad 3D |
| 36 | await p.click('#act-city'); |
| 37 | await p.waitForTimeout(4500); // construir modelo + montar three + primeros frames |
| 38 | |
| 39 | const head = await p.locator('#view-body .view-head').textContent().catch(() => ''); |
| 40 | ok('cabecera de la ciudad presente', /distritos/.test(head), head.slice(0, 60)); |
| 41 | const hasCanvas = await p.locator('#city-canvas').count(); |
| 42 | ok('canvas de la ciudad montado', hasCanvas > 0); |
| 43 | |
| 44 | // el motor real montó la escena (canvas con tamaño real + contexto WebGL vivo). |
| 45 | // El brillo de píxeles se valida por la CAPTURA (readPixels fuera del loop da |
| 46 | // negro por preserveDrawingBuffer:false, no es fiable aquí). |
| 47 | const glState = await p.evaluate(() => { |
| 48 | const c = document.getElementById('city-canvas'); if (!c) return { ok: false }; |
| 49 | const gl = c.getContext('webgl2') || c.getContext('webgl'); |
| 50 | return { ok: !!gl, w: c.width, h: c.height, lost: gl ? gl.isContextLost() : true }; |
| 51 | }); |
| 52 | ok('canvas con tamaño real y contexto WebGL vivo', glState.ok && glState.w > 100 && glState.h > 100 && !glState.lost, JSON.stringify(glState)); |
| 53 | |
| 54 | // clic en el centro → debería abrir un fichero (raycast contra la ciudad) |
| 55 | await p.mouse.click(560, 400); |
| 56 | await p.waitForTimeout(600); |
| 57 | |
| 58 | // captura recortada del lienzo: prueba visual de la metrópolis |
| 59 | const cnv = await p.locator('#city-canvas'); |
| 60 | await cnv.screenshot({ path: OUT + '/city_vcc_canvas.png' }).catch(() => {}); |
| 61 | await p.screenshot({ path: OUT + '/city_vcc.png' }); |
| 62 | console.log('capturas → city_vcc.png, city_vcc_canvas.png'); |
| 63 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ CIUDAD 3D (VibeCodeViewer) OK'); |
| 64 | await b.close(); |
| 65 | process.exit(fails ? 1 : 0); |